예제 #1
0
void MongoDBDriver::InsertQuery(int threadId,
	unsigned int table_id,
	unsigned int timestamp,
	unsigned int device_id, mongo::DBClientConnection & mongo,
        SampledStats & stats) {

    std::vector<mongo::BSONObj> bulk_data;

    //auto metricsCnt = PGen->GetNext(Config::MaxMetrics, 0);
    unsigned int seed=Config::randomSeed;
    if (!seed) {
        std::random_device rd;
        seed=rd();
    }
    std::mt19937 gen(seed);
    std::uniform_int_distribution<> dis(1, Config::MaxMetrics);

    auto metricsCnt = PGen->GetNext(Config::MaxMetricsPerTs, 0);

    /* metrics loop */
    std::unordered_set< int > s;
    while (s.size() < metricsCnt) {
        auto size_b = s.size();
        auto mc = dis(gen);
        s.insert(mc);
        if (size_b==s.size()) {
            continue;
        }
        auto v = PGen->GetNext(0.0, Config::MaxValue);
        mongo::BSONObj record = BSON (
            "ts" << timestamp <<
            "device_id" << device_id <<
            "metric_id" << mc <<
            "cnt" << PGen->GetNext(Config::MaxCnt, 0) <<
            "value" << (v < 0.5 ? 0 : v)
        );

        bulk_data.push_back(record);
    }

    auto t0 = std::chrono::high_resolution_clock::now();
    mongo.insert(database+".metrics"+std::to_string(table_id), bulk_data);
    bulk_data.clear();
    auto t1 = std::chrono::high_resolution_clock::now();
    auto time_us = std::chrono::duration_cast<std::chrono::microseconds>(t1-t0).count();

    //if (latencyStats) {
    //    latencyStats->recordLatency(threadId, InsertMetric, time_us);
    //}

    if (ostreamSet) {
        stats.addStats(InsertMetric, time_us/1000, false);
    }

    StatMessage sm(InsertMetric, time_us, metricsCnt);
    statQueue.push(sm);

}
예제 #2
0
/**********************************************************
 *convert FileRec to bson, then write to db
 * 
***********************************************************/
void FileRec::writeToDB(mongo::DBClientConnection &conn) {
    BSONObjBuilder record; //build BSONObj
    record.append("filename", this->filename);
    record.append("Tempname", this->tempname);
    record.append("curhash", this->recentHash);
    record.append("ovhash", this->origHash);
    record.append("length", this->length);
    record.append("nversions", this->versionCount);
    /* love you */ long long time = this->modifytime.tv_nsec;
    record.append("Mtnsec", time);
    time = this->modifytime.tv_sec;
    record.append("mtsec", time);
    record.append("currentversion", this->refNum);

    mongo::BSONArrayBuilder bArr; //arrays to store multiple objects inside main object
    mongo::BSONArrayBuilder Comments;
    for (vector<string>::iterator it = blockhashes.begin(); it != blockhashes.end(); ++it) {
        bArr.append(*it);
    }

    for (vector<comment>::iterator it = comments.begin(); it != comments.end(); ++it) {
        BSONObjBuilder comment;
        comment.append("version", it->version); //
        comment.append("comment", it->comment);
        Comments.append(comment.obj());
    }

    if (!versions.empty()) { //if there are id's in the versions collection
        mongo::BSONArrayBuilder Version; //store the id's in an BSONarray
        for (vector<string>::iterator it = versions.begin(); it != versions.end(); ++it) {
            BSONObjBuilder version;
            version.append("id", (*it));
            Version.append(version.obj());
        }
        record.append("versionrec", Version.arr());
    }
    record.append("FileBlkHashes", bArr.arr()); //adding arrays to main record
    record.append("comments", Comments.arr());

    BSONObj result = record.obj();
    auto_ptr<mongo::DBClientCursor> cursor = conn.query("fileRecords.Filerec", MONGO_QUERY("filename" << filename));
    if (cursor->more()) {//already file rec in db so update
        conn.update("fileRecords.Filerec", MONGO_QUERY("filename" << filename), result);
    } else {
        conn.insert("fileRecords.Filerec", result); //must be new record 
    }
    string e = conn.getLastError();
    if (!e.empty()) {
        cout << "something failed failed: " << e << std::endl;
        sleep(1);
        exit(1);
    }
    else{
        cout << "record " << this->refNum << " successfully written to database" << endl;
    }
}
예제 #3
0
/**********************************************************
 *similar to writetodb in FileRec
 * 
***********************************************************/
void VersionRec::writeToDB(mongo::DBClientConnection& conn) {

    BSONObjBuilder record;

    if (this->versionid.empty()) { //if no id has been read in because it is a new version
        record.genOID();//create one
    } else {
        mongo::OID theoid(this->versionid); //use current id
        record.append("_id", theoid);
    }
    //convert to BSON
    record.append("Tempname", this->tmpname);
    record.append("filehash", this->filehash);
    record.append("length", this->length);
    record.append("Version", this->versionnumber);
    /* love you */ long long time = this->modifytime.tv_nsec;
    record.append("Mtnsec", time);
    time = this->modifytime.tv_sec;
    record.append("mtsec", time);

    mongo::BSONArrayBuilder Version;
    for (vector<VersionDiffBlock>::iterator it = changes.begin(); it != changes.end(); ++it) {
        BSONObjBuilder version;
        version.append("Blknum", (*it).blockNo);
        version.append("hash", (*it).blockHash);
        Version.append(version.obj());
    }
    record.append("Blktable", Version.arr());

    BSONObj result = record.obj();
    if (this->versionid.empty()) {
        mongo::BSONElement thing;
        result.getObjectID(thing);
        mongo::OID anoid = thing.OID();
        this->versionid = anoid.toString();
    }

    auto_ptr<mongo::DBClientCursor> cursor = conn.query("fileRecords.FileVersion", MONGO_QUERY("_id" << mongo::OID(this->versionid)));
    if (cursor->more()) {//already a version with same id, update
        conn.update("fileRecords.FileVersion", MONGO_QUERY("_id" << mongo::OID(this->versionid)), result);
    } else { //new version
        conn.insert("fileRecords.FileVersion", result);
    }

    string e = conn.getLastError();
    if (!e.empty()) {
        cout << "something failed failed: " << e << std::endl;
        sleep(1);
        exit(1);
    }
    else{
        cout << "Version " << this->versionnumber << " successfully written to database" << endl;
    }
}
예제 #4
0
파일: first.cpp 프로젝트: afchin/mongo
void insert( mongo::DBClientConnection & conn , const char * name , int num ) {
    mongo::BSONObjBuilder obj;
    obj.append( "name" , name );
    obj.append( "num" , num );
    conn.insert( "test.people" , obj.obj() );
}
예제 #5
0
void NOINLINE InsertInDatabase(mongo::BSONObjBuilder& bson,
                               mongo::DBClientConnection& connection)
{
    connection.insert(kMongoCollection, bson.obj());
}