<commit_before><commit_msg>InsertJob, UpdateJob, InsertProcedure, UpdateProcedure, DeleteProcedure, SelectProcedure Complete<commit_after>/*
 * Author:  kid7st (), <EMAIL>
 */

#define DEBUG

#ifdef DEBUG
#define DEBUG_PRINT(...) do{fprintf(stderr, __VA_ARGS__);} while(false);
#else
#define DEBUG_PRINT(...) do{}while(false);
#endif

#include "MongoDBConnection.h"
#include <cstdlib>
#include <iostream>
#include <memory>
#include <mongo/client/dbclient.h>

using std::string;
using namespace mongo;

MongoDBConnection::MongoDBConnection(){
    conn = new mongo::DBClientConnection();
}

MongoDBConnection::~MongoDBConnection(){
    delete conn;
}

bool MongoDBConnection::Connect(string &host, string &user, string &pwd){
    bool ret = true;
    string errmsg("connect failed!");
    if(!conn->connect(host, errmsg))
        ret = false;
    string db("tant");
    errmsg = "auth failed";
    if(!conn->auth(db, user, pwd, errmsg))
        ret = false;

    return ret;
}

/* MongoDB doesn't need to disconnect */
bool MongoDBConnection::DisConnect(){
    return true;
}

// Get BSONObj from Job struct
void GetBSONJob(BSONObj& bsonJob, const stJob& Job)
{
    // Array of parent id
    BSONArrayBuilder parent;
    for (vector<string>::const_iterator iter = Job.vecParent.begin();
        iter != Job.vecParent.end(); ++iter)
        parent.append(*iter);

    // the Job BSON
    BSONObjBuilder build;
    build.append("id", Job.strId );
    build.append("name", Job.strName);
    build.append("description", Job.strDescription);
    build.append("owner", Job.strOwner);
    build.append("command", Job.strCommand);
    build.append("parameter", Job.strParameter);
    build.append("status", Job.iStatus);
    build.append("start_time", Job.strStartTime);
    build.append("finished_time", Job.strFinishedTime);
    build.append("num_try", Job.iNumTry);
    build.append("try_interval", Job.iTryInterval);
    build.append("parent", parent.arr());

    // Array of childs
    int num = 0;
    for (vector< vector<string> >::const_iterator iter = Job.vecChild.begin();
        iter != Job.vecChild.end(); ++iter)
    {
        BSONArrayBuilder child;
        for (vector<string>::const_iterator i = iter->begin(); i != iter->end(); ++i)
            child.append(*i);

        char szName[32];
        snprintf(szName, 32, "child%d", num++);
        build.append(szName, child.arr());
    }

    bsonJob = build.obj();
}

bool MongoDBConnection::InsertJob(const stJob &Job)
{
    BSONObj bsonJob;
    GetBSONJob(bsonJob, Job);

    try
    {
        conn->insert("tant.jobs", bsonJob);
    }
    catch (DBException& e)
    {
        cerr << e.what() << endl;
        return false;
    }
    return true;
}

bool MongoDBConnection::UpdateJob(const stJob &Job, bool bInsert)
{
    BSONObj bsonJob;
    GetBSONJob(bsonJob, Job);

    try
    {
        Query q(BSON("id" << Job.strId));
        conn->update("tant.jobs", q, bsonJob, bInsert);
    }
    catch (DBException& e)
    {
        cerr << e.what() << endl;
        return false;
    }

    return true;
}

// Get BSONObj from Procedure struct
void GetBSONProcedure(BSONObj &bsonProcedure, const stProcedure &Procedure)
{
    BSONObjBuilder build;
    build.append("id", Procedure.strId);
    build.append("name", Procedure.strName);
    build.append("built_time", Procedure.strBuildTime);
    build.append("last_modify_time", Procedure.strLastModifyTime);
    build.append("start_datetime", Procedure.strStartTime);
    bsonProcedure = build.obj();
}

bool MongoDBConnection::InsertProcedure(const stProcedure &Procedure)
{
    BSONObj bsonProcedure;
    GetBSONProcedure(bsonProcedure, Procedure);

    try
    {
        conn->insert("tant.procedures", bsonProcedure);
    }
    catch (DBException& e)
    {
        cerr << e.what() << endl;
        return false;
    }
    return true;
}

bool MongoDBConnection::DeleteProcedure(const string &strId)
{
    try
    {
        Query q(BSON("id" << strId));
        conn->remove("tant.procedures", q);
    }
    catch (DBException& e)
    {
        cerr << e.what() << endl;
        return false;
    }
    return true;
}

bool MongoDBConnection::UpdateProcedure(const stProcedure &Procedure, bool bInsert)
{
    BSONObj bsonProcedure;
    GetBSONProcedure(bsonProcedure, Procedure);

    try
    {
        Query q(BSON("id" << Procedure.strId));
        conn->update("tant.procedures", q, bsonProcedure, bInsert);
    }
    catch (DBException& e)
    {
        cerr << e.what() << endl;
        return false;
    }
    return true;
}

bool MongoDBConnection::SeleteProcedure(const string &strId, stProcedure &Procedure)
{
    try
    {
        Query q(BSON("id" << strId));
        auto_ptr<DBClientCursor> find = conn->query("tant.procedures", q);
        if (find->more())
        {
            BSONObj obj = find->next();
            Procedure.strId = strId;
            Procedure.strName = obj.getStringField("name");
            Procedure.strBuildTime = obj.getStringField("built_time");
            Procedure.strLastModifyTime = obj.getStringField("last_modify_time");
            Procedure.strStartTime = obj.getStringField("start_datetime");
        }
        else
            return false;
    }
    catch (DBException &e)
    {
        cerr << e.what() << endl;
        return false;
    }
    return true;
}
{'fraction_non_alphanumeric': 0.14122065727699532, 'fraction_numerical': 0.0011267605633802818, 'mean_word_length': 2.9895131086142324, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '84307', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1713, 'n_words': 359}