<commit_before>/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org)             */
/*                                                                            */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
/* not use this file except in compliance with the License. You may obtain    */
/* a copy of the License at                                                   */
/*                                                                            */
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
/*                                                                            */
/* Unless required by applicable law or agreed to in writing, software        */
/* distributed under the License is distributed on an "AS IS" BASIS,          */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/* See the License for the specific language governing permissions and        */
/* limitations under the License.                                             */
/* -------------------------------------------------------------------------- */

#include "ClusterPool.h"

const char * ClusterPool::table = "cluster_pool";

const char * ClusterPool::db_names = "oid, cluster_name";

const char * ClusterPool::db_bootstrap =
    "CREATE TABLE IF NOT EXISTS cluster_pool ("
    "oid INTEGER PRIMARY KEY, cluster_name VARCHAR(128), "
    "UNIQUE(cluster_name) )";

const string ClusterPool::DEFAULT_CLUSTER_NAME = "default";

/* -------------------------------------------------------------------------- */

int ClusterPool::allocate(int * clid, string name, SqlDB *db)
{
    int rc;

    map<int, string>::iterator  it;

    // Return error if name already exists
    for(it=cluster_names.begin();it!=cluster_names.end();it++)
    {
        if(it->second == name)
        {
            goto error_existing_name;
        }
    }

    // Get the highest key, and add 1
    *clid = cluster_names.rbegin()->first + 1;

    rc = insert(*clid, name, db);

    if(rc != 0)
    {
        goto error_db;
    }

    return *clid;

// TODO: LOG ERRORS
error_existing_name:
error_db:
error_common:
    *clid = -1;
    return *clid;
}

/* -------------------------------------------------------------------------- */

string ClusterPool::info(int clid)
{
    ostringstream oss;

    map<int, string>::iterator it;

    it = cluster_names.find(clid);

    if(it != cluster_names.end())
    {
        dump_cluster(oss, it->first, it->second);
    }

    return oss.str();
}
<commit_msg>feature #201: Better error log for cluster allocate.<commit_after>/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org)             */
/*                                                                            */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
/* not use this file except in compliance with the License. You may obtain    */
/* a copy of the License at                                                   */
/*                                                                            */
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
/*                                                                            */
/* Unless required by applicable law or agreed to in writing, software        */
/* distributed under the License is distributed on an "AS IS" BASIS,          */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/* See the License for the specific language governing permissions and        */
/* limitations under the License.                                             */
/* -------------------------------------------------------------------------- */

#include "ClusterPool.h"
#include "NebulaLog.h"

const char * ClusterPool::table = "cluster_pool";

const char * ClusterPool::db_names = "oid, cluster_name";

const char * ClusterPool::db_bootstrap =
    "CREATE TABLE IF NOT EXISTS cluster_pool ("
    "oid INTEGER PRIMARY KEY, cluster_name VARCHAR(128), "
    "UNIQUE(cluster_name) )";

const string ClusterPool::DEFAULT_CLUSTER_NAME = "default";

/* -------------------------------------------------------------------------- */

int ClusterPool::allocate(int * clid, string name, SqlDB *db)
{
    int                         rc;
    map<int, string>::iterator  it;

    ostringstream               oss;

    // Return error if name already exists
    for(it=cluster_names.begin();it!=cluster_names.end();it++)
    {
        if(it->second == name)
        {
            goto error_existing_name;
        }
    }

    // Get the highest key, and add 1
    *clid = cluster_names.rbegin()->first + 1;

    rc = insert(*clid, name, db);

    if(rc != 0)
    {
        goto error_db;
    }

    return *clid;


error_existing_name:
    oss << "Could not allocate new cluster: Name \""
        << name << "\" already exists.";

    goto error_common;
error_db:
    oss << "Could not allocate new cluster \"" << name
        << "\": Database returned error code " << rc << ".";
    goto error_common;

error_common:
    NebulaLog::log("CLUSTER", Log::ERROR, oss);

    *clid = -1;
    return *clid;
}

/* -------------------------------------------------------------------------- */

string ClusterPool::info(int clid)
{
    ostringstream oss;

    map<int, string>::iterator it;

    it = cluster_names.find(clid);

    if(it != cluster_names.end())
    {
        dump_cluster(oss, it->first, it->second);
    }

    return oss.str();
}

{'fraction_non_alphanumeric': 0.20780129438516703, 'fraction_numerical': 0.007171593493090782, 'mean_word_length': 1.2771804062126644, 'pattern_counts': {'":': 1, '<': 23, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1324050', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1347, 'n_words': 409}