Ejemplo n.º 1
0
int main( int argc, const char **argv ) {
    
    const char *port = "27017";
    if ( argc != 1 ) {
        if ( argc != 3 )
            throw -12;
        port = argv[ 2 ];
    }

    DBClientConnection conn;
    string errmsg;
    if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
        cout << "couldn't connect : " << errmsg << endl;
        throw -11;
    }

    const char * ns = "test.second";

    conn.remove( ns , BSONObj() );

    conn.insert( ns , BSON( "name" << "eliot" << "num" << 17 ) );
    conn.insert( ns , BSON( "name" << "sara" << "num" << 24 ) );

    auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() );
    cout << "using cursor" << endl;
    while ( cursor->more() ) {
        BSONObj obj = cursor->next();
        cout << "\t" << obj.jsonString() << endl;
    }

    conn.ensureIndex( ns , BSON( "name" << 1 << "num" << -1 ) );
}
Ejemplo n.º 2
0
void run() {
    DBClientConnection c;
    c.connect("localhost"); //"192.168.58.1");
    cout << "connected ok" << endl;
    BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
    c.insert("tutorial.persons", p);
    p = BSON( "name" << "Jane" << "age" << 40 );
    c.insert("tutorial.persons", p);
    p = BSON( "name" << "Abe" << "age" << 33 );
    c.insert("tutorial.persons", p);
    p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" );
    c.insert("tutorial.persons", p);

    c.ensureIndex("tutorial.persons", fromjson("{age:1}"));

    cout << "count:" << c.count("tutorial.persons") << endl;

    auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());
    while( cursor->more() ) {
        cout << cursor->next().toString() << endl;
    }

    cout << "\nprintifage:\n";
    printIfAge(c, 33);
}
Ejemplo n.º 3
0
int main() {
    try {
        cout << "connecting to localhost..." << endl;
        DBClientConnection c;
        c.connect("localhost");
        cout << "connected ok" << endl;
        unsigned long long count = c.count("test.foo");
        cout << "count of exiting documents in collection test.foo : " << count << endl;

        bo o = BSON( "hello" << "world" );
        c.insert("test.foo", o);

        string e = c.getLastError();
        if( !e.empty() ) { 
            cout << "insert #1 failed: " << e << endl;
        }

        // make an index with a unique key constraint
        c.ensureIndex("test.foo", BSON("hello"<<1), /*unique*/true);

        c.insert("test.foo", o); // will cause a dup key error on "hello" field
        cout << "we expect a dup key error here:" << endl;
        cout << "  " << c.getLastErrorDetailed().toString() << endl;
    } 
    catch(DBException& e) { 
        cout << "caught DBException " << e.toString() << endl;
        return 1;
    }

    return 0;
}
Ejemplo n.º 4
0
void
plumage::stats::processSchedulerStats(ODSMongodbOps* ops, Date_t& ts) {
    dprintf(D_FULLDEBUG, "ODSCollectorPlugin::processSchedulerStats() called...\n");
    DBClientConnection* conn =  ops->m_db_conn;
    conn->ensureIndex(DB_RAW_ADS, BSON( ATTR_MY_TYPE << 1 ));
    auto_ptr<DBClientCursor> cursor = conn->query(DB_RAW_ADS, QUERY( ATTR_MY_TYPE << "Scheduler" ) );
    conn->ensureIndex(DB_STATS_SAMPLES_SCHED, BSON( "ts" << -1 ));
    conn->ensureIndex(DB_STATS_SAMPLES_SCHED, BSON( "n" << 1 ));
    while( cursor->more() ) {
        BSONObj p = cursor->next();
        // write record to scheduler samples
        BSONObjBuilder bob;
        DATE(ts,ts);
        STRING(n,ATTR_NAME);
        INTEGER(mjr,ATTR_MAX_JOBS_RUNNING);
        INTEGER(nu,ATTR_NUM_USERS);
        INTEGER(tja,ATTR_TOTAL_JOB_ADS);
        INTEGER(trun,ATTR_TOTAL_RUNNING_JOBS);
        INTEGER(thj,ATTR_TOTAL_HELD_JOBS);
        INTEGER(tij,ATTR_TOTAL_IDLE_JOBS);
        INTEGER(trem,ATTR_TOTAL_REMOVED_JOBS);
        INTEGER(tsr,ATTR_TOTAL_SCHEDULER_RUNNING_JOBS);
        INTEGER(tsi,ATTR_TOTAL_SCHEDULER_IDLE_JOBS);
        INTEGER(tlr,ATTR_TOTAL_LOCAL_RUNNING_JOBS);
        INTEGER(tli,ATTR_TOTAL_LOCAL_IDLE_JOBS);
        INTEGER(tfj,ATTR_TOTAL_FLOCKED_JOBS);
        conn->insert(DB_STATS_SAMPLES_SCHED,bob.obj());
    }
}
Ejemplo n.º 5
0
void
plumage::stats::processMachineStats(ODSMongodbOps* ops, Date_t& ts) {
    dprintf(D_FULLDEBUG, "ODSCollectorPlugin::processMachineStats() called...\n");
    DBClientConnection* conn =  ops->m_db_conn;
    conn->ensureIndex(DB_RAW_ADS, BSON( ATTR_MY_TYPE << 1 ));
    auto_ptr<DBClientCursor> cursor = conn->query(DB_RAW_ADS, QUERY( ATTR_MY_TYPE << "Machine" ) );
    conn->ensureIndex(DB_STATS_SAMPLES_MACH, BSON( "ts" << -1 ));
    conn->ensureIndex(DB_STATS_SAMPLES_MACH, BSON( "m" << 1 ));
    conn->ensureIndex(DB_STATS_SAMPLES_MACH, BSON( "n" << 1 ));
    while( cursor->more() ) {
        BSONObj p = cursor->next();
        // write record to machine samples
        BSONObjBuilder bob;
        DATE(ts,ts);
        STRING(m,ATTR_MACHINE);
        STRING(n,ATTR_NAME);
        STRING(ar,ATTR_ARCH);
        STRING(os,ATTR_OPSYS);
        STRING(req,ATTR_REQUIREMENTS);
        INTEGER(ki,ATTR_KEYBOARD_IDLE);
        DOUBLE(la,ATTR_LOAD_AVG);
        STRING(st,ATTR_STATE);
        INTEGER(cpu,ATTR_CPUS);
        INTEGER(mem,ATTR_MEMORY);
        // TODO: these might be moved to another collection
//         STRING(gjid,ATTR_GLOBAL_JOB_ID);
//         STRING(ru,ATTR_REMOTE_USER);
//         STRING(ag,ATTR_ACCOUNTING_GROUP);
        conn->insert(DB_STATS_SAMPLES_MACH,bob.obj());
    }
}
/* ****************************************************************************
*
* prepareDatabase -
*/
static void prepareDatabase(std::string id, std::string type)
{
  /* Set database */
  setupDatabase();

  DBClientConnection* connection = getMongoConnection();

  /* We create one entity:
   *
   * - 'id', 'type' with four attributes
   *     A1: X
   *     A1: Y
   *     A2: Z
   *     A3: W
   */

  BSONObj en1 = BSON("_id" << BSON("id" << id << "type" << type) <<
                     "attrs" << BSON_ARRAY(
                        BSON("name" << "A1" << "type" << "TA1" << "value" << "X") <<
                        BSON("name" << "A1" << "type" << "TA1bis" << "value" << "Y") <<
                        BSON("name" << "A2" << "type" << "TA2" << "value" << "Z") <<
                        BSON("name" << "A3" << "type" << "TA3" << "value" << "W")
                        )
     );

  connection->insert(ENTITIES_COLL, en1);
}
Ejemplo n.º 7
0
v8::Handle<v8::Value> mongoInsert(const v8::Arguments& args){
    jsassert( args.Length() == 2 , "insert needs 2 args" );
    jsassert( args[1]->IsObject() , "have to insert an object" );
    
    DBClientConnection * conn = getConnection( args );
    GETNS;
    
    v8::Handle<v8::Object> in = args[1]->ToObject();
    
    if ( ! in->Has( String::New( "_id" ) ) ){
        v8::Handle<v8::Value> argv[1];
        in->Set( String::New( "_id" ) , getObjectIdCons()->NewInstance( 0 , argv ) );
    }

    BSONObj o = v8ToMongo( in );

    DDD( "want to save : " << o.jsonString() );
    try {
        conn->insert( ns , o );
    }
    catch ( ... ){
        return v8::ThrowException( v8::String::New( "socket error on insert" ) );
    }
    
    return args[1];
}
Ejemplo n.º 8
0
int main() {
    try {
        cout << "connecting to localhost..." << endl;
        DBClientConnection c;
        c.connect("localhost");
        cout << "connected ok" << endl;

        bo o = BSON( "hello" << "world" );

	cout << "inserting..." << endl;

	time_t start = time(0);
	for( unsigned i = 0; i < 1000000; i++ ) {
	  c.insert("test.foo", o);
	}

	// wait until all operations applied
	cout << "getlasterror returns: \"" << c.getLastError() << '"' << endl;

	time_t done = time(0);
	time_t dt = done-start;
	cout << dt << " seconds " << 1000000/dt << " per second" << endl;
    } 
    catch(DBException& e) { 
        cout << "caught DBException " << e.toString() << endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
Ejemplo n.º 9
0
void run(long time,short spo2,short pr,const char *deviceid) 
{
	DBClientConnection c;
	c.connect(host);
	BSONObj p = BSONObjBuilder().append(TIMESTAMP,(int)time).append(SPo2,spo2).append(PULSERATE,pr).append(DEVICEID,deviceid).obj();
	c.insert(tablename, p);
}
Ejemplo n.º 10
0
void scrub(DBClientConnection& c){
	//DES: finds and moves noise documents found in the database
	//IN: connection to the mongo database

	auto_ptr<DBClientCursor> noiseCursor[6];
	//look for negative volume
	noiseCursor[0] = c.query("hw3.signal", BSON("volume"<<LTE<<0.0));
	//find excessive price (>5 dollars)
	noiseCursor[1] = c.query("hw3.signal", BSON("value"<<GTE<<5.0));
	//find excessive negative price (<-5 dollars)
	noiseCursor[2] = c.query("hw3.signal", BSON("value"<<LTE<<-5.0));
	//find weekend dates
	noiseCursor[3] = c.query("hw3.signal", Query("{date: /[0-9]{7}2./i }"));
	//find 9 am trades
	noiseCursor[4] = c.query("hw3.signal", Query("{date: /[0-9]{8}:09./i }"));
	//find 5 pm trades
	noiseCursor[5] = c.query("hw3.signal", Query("{date: /[0-9]{8}:17./i }"));

	//loop through each noise type and move the errors from the signal collection to the
	//noise collection
	for (int i=0;i<6;i++){
		while (noiseCursor[i]->more()){
			BSONObj singleNoise = noiseCursor[i]->next();
			//add to noise db
			c.insert("hw3.noise", singleNoise);
			//remove from signal db
			c.remove("hw3.signal",singleNoise);
		}
	}
}
Ejemplo n.º 11
0
void writeJobAd(ClassAd* ad) {
    clock_t start, end;
    double elapsed;
    ExprTree *expr;
    const char *name;
    ad->ResetExpr();
    
    BSONObjBuilder b;
    start = clock();
    while (ad->NextExpr(name,expr)) {        
        b.append(name,ExprTreeToString(expr));
    }
    try {
        c.insert(db_name, b.obj());
    }
    catch(DBException& e) {
        cout << "caught DBException " << e.toString() << endl;
    }
    end = clock();
    elapsed = ((float) (end - start)) / CLOCKS_PER_SEC;
    std:string last_err = c.getLastError();
    if (!last_err.empty()) {
        printf("getLastError: %s\n",last_err.c_str());
    }
    printf("Time elapsed: %1.9f sec\n",elapsed);
}
Ejemplo n.º 12
0
void run()
{
    DBClientConnection c;
    c.connect("localhost");
    cout << "connected" << endl;

    //insert
    BSONObj p = BSON( "name" << "ken" << "age" << 20 );

    c.insert("test.persons", p);

    //query
    cout << "count: " << c.count("test.persons") << endl;

    {
        auto_ptr<DBClientCursor> cursor =
            c.query("test.persons", {});
        while(cursor->more())
            cout << cursor->next().toString() << endl;
    }

    {
        auto_ptr<DBClientCursor> cursor =
            c.query("test.persons", QUERY("age" << 20));
        while(cursor->more())
            cout << cursor->next().toString() << endl;
    }
}
Ejemplo n.º 13
0
int toolMain( int argc, char* argv[], char* envp[] ) {
    mongo::runGlobalInitializersOrDie(argc, argv, envp);
    if( parseCmdLineOptions( argc, argv) )
        return 1;

    BSONObj nestedDoc =  BSON("Firstname" << "David" <<
                              "Lastname" << "Smith" <<
                              "Address" << BSON( "Street" << "5th Av" <<
                                                 "City" << "New York" )
                              );
    std::vector<std::string> list;
    list.push_back("mongo new york city");
    list.push_back("mongo rome");
    list.push_back("mongo dublin");
    list.push_back("mongo seoul");
    list.push_back("mongo barcelona");
    list.push_back("mongo madrid");
    list.push_back("mongo chicago");
    list.push_back("mongo amsterdam");
    list.push_back("mongo delhi");
    list.push_back("mongo beijing");

    BSONObj args = BSONObjBuilder()
                   .append( "_id", 0 )
                   .append( "blob", "MongoDB is an open source document-oriented database "
                                    "system designed with scalability and developer." )
                   .append( "nestedDoc", nestedDoc )
                   .append( "list", list )
                   .append( "counter", 0 ).obj();

    const int numDocsPerDB =
            static_cast<int>( globalDocGenOption.dbSize * 1024 * 1024 / args.objsize() );
    cout << "numDocsPerDB:" << numDocsPerDB << endl;
    try {
        DBClientConnection conn;
        conn.connect( globalDocGenOption.hostname );
        cout << "successfully connected to the host" << endl;
        for( int i=0; i < globalDocGenOption.numdbs; ++i ) {
            scoped_ptr<DocumentGenerator> docGen( DocumentGenerator::makeDocumentGenerator(args) );
            cout << "populating database " << globalDocGenOption.prefix << i  << endl;
            long long j = 0;
            string ns = mongoutils::str::stream() << globalDocGenOption.prefix << i << ".sampledata";
            while( j != numDocsPerDB ) {
                BSONObj doc = docGen->createDocument();
                conn.insert( ns, doc );
                ++j;
            }
            BSONObj blobIndex = BSON("blob" << 1);
            conn.ensureIndex(ns, blobIndex);
            BSONObj listIndex = BSON("list" << 1);
            conn.ensureIndex(ns, listIndex);
        }
    } catch( DBException &e ) {
        cout << "caught " << e.what() << endl;
    }
    return 0;
}
Ejemplo n.º 14
0
int main( int argc, const char **argv ) {

    const char *port = "27017";
    if ( argc != 1 ) {
        if ( argc != 3 )
            throw -12;
        port = argv[ 2 ];
    }

    DBClientConnection conn;
    string errmsg;
    if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
        cout << "couldn't connect : " << errmsg << endl;
        throw -11;
    }

    const char * ns = "test.where";

    conn.remove( ns , BSONObj() );

    conn.insert( ns , BSON( "name" << "eliot" << "num" << 17 ) );
    conn.insert( ns , BSON( "name" << "sara" << "num" << 24 ) );

    auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() );

    while ( cursor->more() ) {
        BSONObj obj = cursor->next();
        cout << "\t" << obj.jsonString() << endl;
    }

    cout << "now using $where" << endl;

    Query q = Query("{}").where("this.name == name" , BSON( "name" << "sara" ));

    cursor = conn.query( ns , q );

    int num = 0;
    while ( cursor->more() ) {
        BSONObj obj = cursor->next();
        cout << "\t" << obj.jsonString() << endl;
        num++;
    }
    MONGO_verify( num == 1 );
}
Ejemplo n.º 15
0
int main() { 
  cout << "connecting to localhost..." << endl;
  DBClientConnection c;
  c.connect("localhost");
  cout << "connected ok" << endl;
  unsigned long long count = c.count("test.foo");
  cout << "count of exiting documents in collection test.foo : " << count << endl;

  bo o = BSON( "hello" << "world" );
  c.insert("test.foo", o);

  return 0;
}
Ejemplo n.º 16
0
int run() {

    Status status = client::initialize();
    if ( !status.isOK() ) {
        std::cout << "failed to initialize the client driver: " << status.toString() << endl;
        return EXIT_FAILURE;
    }

    DBClientConnection c;
    c.connect("localhost"); //"192.168.58.1");
    cout << "connected ok" << endl;
    BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
    c.insert("tutorial.persons", p);
    p = BSON( "name" << "Jane" << "age" << 40 );
    c.insert("tutorial.persons", p);
    p = BSON( "name" << "Abe" << "age" << 33 );
    c.insert("tutorial.persons", p);
    p = BSON( "name" << "Methuselah" << "age" << BSONNULL);
    c.insert("tutorial.persons", p);
    p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" );
    c.insert("tutorial.persons", p);

    c.ensureIndex("tutorial.persons", fromjson("{age:1}"));

    cout << "count:" << c.count("tutorial.persons") << endl;

    std::auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());
    if (!cursor.get()) {
        cout << "query failure" << endl;
        return EXIT_FAILURE;
    }

    while( cursor->more() ) {
        cout << cursor->next().toString() << endl;
    }

    cout << "\nprintifage:\n";
    return printIfAge(c, 33);
}
Ejemplo n.º 17
0
	void set(const char * key, int statusCode){
	
		string collection= _Ctx->outDb.dbname + string(".leads");
		
		BSONObjBuilder b;

		const time_t log_time = time(0);
		b.append("time", (int)log_time);
		b.append("key", key);
		b.append("IP", *(_Ctx->ppa));
		b.append("status", statusCode);
				
		conn->insert(collection, b.obj());
	}	
Ejemplo n.º 18
0
	string SavePaymentJson(int amount)
	{
		//BSONObj paymentBSON = mongo::fromjson(newPyamentJson);

		BSONObj paymentBSON = BSON(GENOID 
			<< "PayedToUserId" << 8888
			<< "PayedDate" << "2015-01-25 12:00:00"
			<< "PayedPeriodStartDate" << "2015-01-01 00:00:00"
			<< "PayedPeriodEndDate" << "2015-01-29 23:59:59" 
			<< "Amount" << amount);

		db.insert(PAYMENTS_COLLECTION_NAMESPASE, paymentBSON);

		BSONElement oi;
		paymentBSON.getObjectID(oi);
		OID oid = oi.__oid();
		return oid.toString(); 
	}
Ejemplo n.º 19
0
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);


    client::initialize();
        try {
            DBClientConnection c;
            c.connect("localhost");

            std::cout << "connected ok" << std::endl;

            c.remove("deuda.bonosletras", BSONObj());

            std::cout << "reg. in deuda.bonosletras before=" << c.count("deuda.bonosletras") << std::endl;

            for (int i = 0; i < 100000; ++i)
            {
                BSONObj p = BSONObjBuilder().append("name", "Joe").append("age", i).obj();
                c.insert("deuda.bonosletras", p);
            }

            std::cout << "reg. in deuda.bonosletras after =" << c.count("deuda.bonosletras") << std::endl;

            std::auto_ptr<DBClientCursor> cursor =
                    c.query("deuda.bonosletras", QUERY("age" << 100));
                while (cursor->more()) {
                    BSONObj p = cursor->next();
                    std::cout << p.getStringField("name") << std::endl;
                }


        } catch( const DBException &e ) {
            std::cout << "caught " << e.what() << std::endl;
        }




        return EXIT_SUCCESS;


    return a.exec();
}
/* ****************************************************************************
*
* prepareDatabase -
*/
static void prepareDatabase(void) {

    /* Set database */
    setupDatabase();

    DBClientConnection* connection = getMongoConnection();

    BSONObj sub1 = BSON("_id" << OID("51307b66f481db11bf860001") <<
                        "expiration" << 10000000 <<
                        "lastNotification" << 15000000 <<
                        "reference" << "http://notify1.me" <<
                        "entities" << BSON_ARRAY(BSON("id" << "E1" << "type" << "T1" << "isPattern" << "false")) <<
                        "attrs" << BSONArray() <<
                        "conditions" << BSON_ARRAY(BSON(
                                                       "type" << "ONCHANGE" <<
                                                       "value" << BSON_ARRAY("AX1" << "AY1")
                                                       ) <<
                                                   BSON(
                                                       "type" << "ONTIMEINTERVAL" <<
                                                       "value" << 100
                                                       ))
                        );

    BSONObj sub2 = BSON("_id" << OID("51307b66f481db11bf860002") <<
                        "expiration" << 20000000 <<
                        "lastNotification" << 25000000 <<
                        "reference" << "http://notify2.me" <<
                        "entities" << BSON_ARRAY(BSON("id" << "E1" << "type" << "T1" << "isPattern" << "false")) <<
                        "attrs" << BSON_ARRAY("A1" << "A2") <<
                        "conditions" << BSON_ARRAY(BSON(
                                                       "type" << "ONCHANGE" <<
                                                       "value" << BSON_ARRAY("AX2" << "AY2")
                                                       ) <<
                                                   BSON(
                                                       "type" << "ONTIMEINTERVAL" <<
                                                       "value" << 200
                                                       ))
                        );

    connection->insert(SUBSCRIBECONTEXT_COLL, sub1);
    connection->insert(SUBSCRIBECONTEXT_COLL, sub2);

}
Ejemplo n.º 21
0
void
plumage::stats::processSubmitterStats(ODSMongodbOps* ops, Date_t& ts) {
    dprintf(D_FULLDEBUG, "ODSCollectorPlugin::processSubmitterStats called...\n");
    DBClientConnection* conn =  ops->m_db_conn;
    conn->ensureIndex(DB_RAW_ADS, BSON( ATTR_MY_TYPE << 1 ));
    auto_ptr<DBClientCursor> cursor = conn->query(DB_RAW_ADS, QUERY( ATTR_MY_TYPE << "Submitter" ) );
    conn->ensureIndex(DB_STATS_SAMPLES_SUB, BSON( "ts" << -1 ));
    conn->ensureIndex(DB_STATS_SAMPLES_SUB, BSON( "sn" << 1 ));
    while( cursor->more() ) {
        BSONObj p = cursor->next();
        // write record to submitter samples
        BSONObjBuilder bob;
        DATE(ts,ts);
        STRING(sn,ATTR_NAME);
        STRING(ma,ATTR_MACHINE);
        INTEGER(jr,ATTR_RUNNING_JOBS);
        // TODO: weird...HeldJobs isn't always there in the raw submitter ad
        int h = p.getIntField(ATTR_HELD_JOBS); h = (h>0) ? h : 0;
        bob.append("jh",h);
        INTEGER(ji,ATTR_IDLE_JOBS);
        conn->insert(DB_STATS_SAMPLES_SUB,bob.obj());
    }
}
/* ****************************************************************************
*
* prepareDatabase -
*/
static void prepareDatabase(void) {

    /* Set database */
    setupDatabase();

    DBClientConnection* connection = getMongoConnection();

    BSONObj sub1 = BSON("_id" << OID("51307b66f481db11bf860001") <<
                        "expiration" << 10000000 <<
                        "reference" << "http://notify1.me" <<
                        "entities" << BSON_ARRAY(BSON("id" << "E1" << "type" << "T1" << "isPattern" << "false")) <<
                        "attrs" << BSONArray());

    BSONObj sub2 = BSON("_id" << OID("51307b66f481db11bf860002") <<
                        "expiration" << 20000000 <<
                        "reference" << "http://notify2.me" <<
                        "entities" << BSON_ARRAY(BSON("id" << "E1" << "type" << "T1" << "isPattern" << "false")) <<
                        "attrs" << BSON_ARRAY("A1" << "A2"));

    connection->insert(SUBSCRIBECONTEXTAVAIL_COLL, sub1);
    connection->insert(SUBSCRIBECONTEXTAVAIL_COLL, sub2);

}
Ejemplo n.º 23
0
int main( int argc, const char **argv ) {

    const char *port = "27017";
    if ( argc != 1 ) {
        if ( argc != 3 )
            throw -12;
        port = argv[ 2 ];
    }

    DBClientConnection conn;
    string errmsg;
    if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
        cout << "couldn't connect : " << errmsg << endl;
        throw -11;
    }

    const char * ns = "test.test1";

    conn.dropCollection(ns);

    // clean up old data from any previous tests
    conn.remove( ns, BSONObj() );
    assert( conn.findOne( ns , BSONObj() ).isEmpty() );

    // test insert
    conn.insert( ns ,BSON( "name" << "eliot" << "num" << 1 ) );
    assert( ! conn.findOne( ns , BSONObj() ).isEmpty() );

    // test remove
    conn.remove( ns, BSONObj() );
    assert( conn.findOne( ns , BSONObj() ).isEmpty() );


    // insert, findOne testing
    conn.insert( ns , BSON( "name" << "eliot" << "num" << 1 ) );
    {
        BSONObj res = conn.findOne( ns , BSONObj() );
        assert( strstr( res.getStringField( "name" ) , "eliot" ) );
        assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) );
        assert( 1 == res.getIntField( "num" ) );
    }


    // cursor
    conn.insert( ns ,BSON( "name" << "sara" << "num" << 2 ) );
    {
        auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() );
        int count = 0;
        while ( cursor->more() ) {
            count++;
            BSONObj obj = cursor->next();
        }
        assert( count == 2 );
    }

    {
        auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 1 ) );
        int count = 0;
        while ( cursor->more() ) {
            count++;
            BSONObj obj = cursor->next();
        }
        assert( count == 1 );
    }

    {
        auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 3 ) );
        int count = 0;
        while ( cursor->more() ) {
            count++;
            BSONObj obj = cursor->next();
        }
        assert( count == 0 );
    }

    // update
    {
        BSONObj res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
        assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) );

        BSONObj after = BSONObjBuilder().appendElements( res ).append( "name2" , "h" ).obj();

        conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after );
        res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
        assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) );
        assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() );

        conn.update( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() , after );
        res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
        assert( strstr( res.getStringField( "name" ) , "eliot" ) );
        assert( strstr( res.getStringField( "name2" ) , "h" ) );
        assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() );

        // upsert
        conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after , 1 );
        assert( ! conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ).isEmpty() );

    }

    { // ensure index
        assert( conn.ensureIndex( ns , BSON( "name" << 1 ) ) );
        assert( ! conn.ensureIndex( ns , BSON( "name" << 1 ) ) );
    }

    { // hint related tests
        assert( conn.findOne(ns, "{}")["name"].str() == "sara" );

        assert( conn.findOne(ns, "{ name : 'eliot' }")["name"].str() == "eliot" );
        assert( conn.getLastError() == "" );

        // nonexistent index test
        bool asserted = false;
        try {
            conn.findOne(ns, Query("{name:\"eliot\"}").hint("{foo:1}"));
        }
        catch ( ... ){
            asserted = true;
        }
        assert( asserted );

        //existing index
        assert( conn.findOne(ns, Query("{name:'eliot'}").hint("{name:1}")).hasElement("name") );

        // run validate
        assert( conn.validate( ns ) );
    }

    { // timestamp test

        const char * tsns = "test.tstest1";
        conn.dropCollection( tsns );

        {
            mongo::BSONObjBuilder b;
            b.appendTimestamp( "ts" );
            conn.insert( tsns , b.obj() );
        }

        mongo::BSONObj out = conn.findOne( tsns , mongo::BSONObj() );
        Date_t oldTime = out["ts"].timestampTime();
        unsigned int oldInc = out["ts"].timestampInc();

        {
            mongo::BSONObjBuilder b1;
            b1.append( out["_id"] );

            mongo::BSONObjBuilder b2;
            b2.append( out["_id"] );
            b2.appendTimestamp( "ts" );

            conn.update( tsns , b1.obj() , b2.obj() );
        }

        BSONObj found = conn.findOne( tsns , mongo::BSONObj() );
        cout << "old: " << out << "\nnew: " << found << endl;
        assert( ( oldTime < found["ts"].timestampTime() ) ||
                ( oldTime == found["ts"].timestampTime() && oldInc < found["ts"].timestampInc() ) );

    }
    
    { // check that killcursors doesn't affect last error
        assert( conn.getLastError().empty() );
        
        BufBuilder b;
        b.appendNum( (int)0 ); // reserved
        b.appendNum( (int)-1 ); // invalid # of cursors triggers exception
        b.appendNum( (int)-1 ); // bogus cursor id
        
        Message m;
        m.setData( dbKillCursors, b.buf(), b.len() );
        
        // say() is protected in DBClientConnection, so get superclass
        static_cast< DBConnector* >( &conn )->say( m );
        
        assert( conn.getLastError().empty() );
    }

    {
        list<string> l = conn.getDatabaseNames();
        for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){
            cout << "db name : " << *i << endl;
        }

        l = conn.getCollectionNames( "test" );
        for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){
            cout << "coll name : " << *i << endl;
        }
    }

    cout << "client test finished!" << endl;
}
Ejemplo n.º 24
0
int main( int argc, const char **argv ) {

    const char *port = "27017";
    if ( argc != 1 ) {
        if ( argc != 3 ) {
            std::cout << "need to pass port as second param" << endl;
            return EXIT_FAILURE;
        }
        port = argv[ 2 ];
    }

    DBClientConnection conn;
    string errmsg;
    if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
        cout << "couldn't connect : " << errmsg << endl;
        return EXIT_FAILURE;
    }

    const char * ns = "test.test1";

    conn.dropCollection(ns);

    // clean up old data from any previous tests
    conn.remove( ns, BSONObj() );
    verify( conn.findOne( ns , BSONObj() ).isEmpty() );

    // test insert
    conn.insert( ns ,BSON( "name" << "eliot" << "num" << 1 ) );
    verify( ! conn.findOne( ns , BSONObj() ).isEmpty() );

    // test remove
    conn.remove( ns, BSONObj() );
    verify( conn.findOne( ns , BSONObj() ).isEmpty() );


    // insert, findOne testing
    conn.insert( ns , BSON( "name" << "eliot" << "num" << 1 ) );
    {
        BSONObj res = conn.findOne( ns , BSONObj() );
        verify( strstr( res.getStringField( "name" ) , "eliot" ) );
        verify( ! strstr( res.getStringField( "name2" ) , "eliot" ) );
        verify( 1 == res.getIntField( "num" ) );
    }


    // cursor
    conn.insert( ns ,BSON( "name" << "sara" << "num" << 2 ) );
    {
        auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() );
        int count = 0;
        while ( cursor->more() ) {
            count++;
            BSONObj obj = cursor->next();
        }
        verify( count == 2 );
    }

    {
        auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 1 ) );
        int count = 0;
        while ( cursor->more() ) {
            count++;
            BSONObj obj = cursor->next();
        }
        verify( count == 1 );
    }

    {
        auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 3 ) );
        int count = 0;
        while ( cursor->more() ) {
            count++;
            BSONObj obj = cursor->next();
        }
        verify( count == 0 );
    }

    // update
    {
        BSONObj res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
        verify( ! strstr( res.getStringField( "name2" ) , "eliot" ) );

        BSONObj after = BSONObjBuilder().appendElements( res ).append( "name2" , "h" ).obj();

        conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after );
        res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
        verify( ! strstr( res.getStringField( "name2" ) , "eliot" ) );
        verify( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() );

        conn.update( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() , after );
        res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
        verify( strstr( res.getStringField( "name" ) , "eliot" ) );
        verify( strstr( res.getStringField( "name2" ) , "h" ) );
        verify( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() );

        // upsert
        conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after , 1 );
        verify( ! conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ).isEmpty() );

    }

    {
        // ensure index
        verify( conn.ensureIndex( ns , BSON( "name" << 1 ) ) );
        verify( ! conn.ensureIndex( ns , BSON( "name" << 1 ) ) );
    }

    {
        // 5 second TTL index
        const char * ttlns = "test.ttltest1";
        conn.dropCollection( ttlns );

        {
            mongo::BSONObjBuilder b;
            b.appendTimeT("ttltime", time(0));
            b.append("name", "foo");
            conn.insert(ttlns, b.obj());
        }
        conn.ensureIndex(ttlns, BSON("ttltime" << 1), false, false, "", true, false, -1, 5);
        verify(!conn.findOne(ttlns, BSONObjBuilder().append("name", "foo").obj()).isEmpty());
        // Sleep 66 seconds, 60 seconds for the TTL loop, 5 seconds for the TTL and 1 to ensure
        sleepsecs(66);
        verify(conn.findOne(ttlns, BSONObjBuilder().append("name", "foo").obj()).isEmpty());
    }

    {
        // hint related tests
        // tokumx doesn't reorder documents just because you updated one, what even is that
        verify( conn.findOne(ns, "{}")["name"].str() == "eliot" );

        verify( conn.findOne(ns, "{ name : 'sara' }")["name"].str() == "sara" );
        verify( conn.getLastError() == "" );

        // nonexistent index test
        bool asserted = false;
        try {
            conn.findOne(ns, Query("{name:\"eliot\"}").hint("{foo:1}"));
        }
        catch ( ... ) {
            asserted = true;
        }
        verify( asserted );

        //existing index
        verify( conn.findOne(ns, Query("{name:'eliot'}").hint("{name:1}")).hasElement("name") );

        // run validate
        verify( conn.validate( ns ) );
    }

    {
        // timestamp test

        const char * tsns = "test.tstest1";
        conn.dropCollection( tsns );

        {
            mongo::BSONObjBuilder b;
            b.appendTimestamp( "ts" );
            conn.insert( tsns , b.obj() );
        }

        mongo::BSONObj out = conn.findOne( tsns , mongo::BSONObj() );
        Date_t oldTime = out["ts"].timestampTime();
        unsigned int oldInc = out["ts"].timestampInc();

        {
            mongo::BSONObjBuilder b1;
            b1.append( out["_id"] );

            mongo::BSONObjBuilder b2;
            b2.append( out["_id"] );
            b2.appendTimestamp( "ts" );

            conn.update( tsns , b1.obj() , b2.obj() );
        }

        BSONObj found = conn.findOne( tsns , mongo::BSONObj() );
        cout << "old: " << out << "\nnew: " << found << endl;
        verify( ( oldTime < found["ts"].timestampTime() ) ||
                ( oldTime == found["ts"].timestampTime() && oldInc < found["ts"].timestampInc() ) );

    }

    {
        // check that killcursors doesn't affect last error
        verify( conn.getLastError().empty() );

        BufBuilder b;
        b.appendNum( (int)0 ); // reserved
        b.appendNum( (int)-1 ); // invalid # of cursors triggers exception
        b.appendNum( (int)-1 ); // bogus cursor id

        Message m;
        m.setData( dbKillCursors, b.buf(), b.len() );

        // say() is protected in DBClientConnection, so get superclass
        static_cast< DBConnector* >( &conn )->say( m );

        verify( conn.getLastError().empty() );
    }

    {
        list<string> l = conn.getDatabaseNames();
        for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ) {
            cout << "db name : " << *i << endl;
        }

        l = conn.getCollectionNames( "test" );
        for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ) {
            cout << "coll name : " << *i << endl;
        }
    }

    {
        //Map Reduce (this mostly just tests that it compiles with all output types)
        const string ns = "test.mr";
        conn.insert(ns, BSON("a" << 1));
        conn.insert(ns, BSON("a" << 1));

        const char* map = "function() { emit(this.a, 1); }";
        const char* reduce = "function(key, values) { return Array.sum(values); }";

        const string outcoll = ns + ".out";

        BSONObj out;
        out = conn.mapreduce(ns, map, reduce, BSONObj()); // default to inline
        //MONGO_PRINT(out);
        out = conn.mapreduce(ns, map, reduce, BSONObj(), outcoll);
        //MONGO_PRINT(out);
        out = conn.mapreduce(ns, map, reduce, BSONObj(), outcoll.c_str());
        //MONGO_PRINT(out);
        out = conn.mapreduce(ns, map, reduce, BSONObj(), BSON("reduce" << outcoll));
        //MONGO_PRINT(out);
    }

    { 
        // test timeouts

        DBClientConnection conn( true , 0 , 2 );
        if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
            cout << "couldn't connect : " << errmsg << endl;
            throw -11;
        }
        conn.insert( "test.totest" , BSON( "x" << 1 ) );
        BSONObj res;
        
        bool gotError = false;
        verify( conn.eval( "test" , "return db.totest.findOne().x" , res ) );
        try {
            conn.eval( "test" , "sleep(5000); return db.totest.findOne().x" , res );
        }
        catch ( std::exception& e ) {
            gotError = true;
            log() << e.what() << endl;
        }
        verify( gotError );
        // sleep so the server isn't locked anymore
        sleepsecs( 4 );
        
        verify( conn.eval( "test" , "return db.totest.findOne().x" , res ) );
        
        
    }

    cout << "client test finished!" << endl;
    return EXIT_SUCCESS;
}
Ejemplo n.º 25
0
int main(int argc, char *argv[]) {

    try {
        cout << "mongoperf" << endl;

        if( argc > 1 ) { 
cout <<

"\n"
"usage:\n"
"\n"
"  mongoperf < myjsonconfigfile\n"
"\n"
"  {\n"
"    nThreads:<n>,     // number of threads\n"
"    fileSizeMB:<n>,   // test file size\n"
"    sleepMicros:<n>,  // pause for sleepMicros/nThreads between each operation\n"
"    mmf:<bool>,       // if true do i/o's via memory mapped files\n"
"    r:<bool>,         // do reads\n"
"    w:<bool>          // do writes\n"
"  }\n"
"\n"
"most fields are optional.\n"
"non-mmf io is direct io (no caching). use a large file size to test making the heads\n"
"  move significantly and to avoid i/o coalescing\n"
"mmf io uses caching (the file system cache).\n"
"\n"

<< endl;
            return 0;
        }

        cout << "use -h for help" << endl;

        char input[1024];
        memset(input, 0, sizeof(input));
        cin.read(input, 1000);
        if( *input == 0 ) { 
            cout << "error no options found on stdin for mongoperf" << endl;
            return 2;
        }

        string s = input;
        str::stripTrailing(s, "\n\r\0x1a");
        try { 
            options = fromjson(s);
        }
        catch(...) { 
            cout << s << endl;
            cout << "couldn't parse json options" << endl;
            return -1;
        }
        cout << "options:\n" << options.toString() << endl;

        go();
#if 0
        cout << "connecting to localhost..." << endl;
        DBClientConnection c;
        c.connect("localhost");
        cout << "connected ok" << endl;
        unsigned long long count = c.count("test.foo");
        cout << "count of exiting documents in collection test.foo : " << count << endl;

        bo o = BSON( "hello" << "world" );
        c.insert("test.foo", o);

        string e = c.getLastError();
        if( !e.empty() ) { 
            cout << "insert #1 failed: " << e << endl;
        }

        // make an index with a unique key constraint
        c.ensureIndex("test.foo", BSON("hello"<<1), /*unique*/true);

        c.insert("test.foo", o); // will cause a dup key error on "hello" field
        cout << "we expect a dup key error here:" << endl;
        cout << "  " << c.getLastErrorDetailed().toString() << endl;
#endif
    } 
    catch(DBException& e) { 
        cout << "caught DBException " << e.toString() << endl;
        return 1;
    }

    return 0;
}
Ejemplo n.º 26
0
void parseFile(ifstream& input,DBClientConnection& c){
	//DES: parse text file and input into mongo database
	//IN: input text file, mongo db connection

	//clean up database if it already exists
	c.dropDatabase("hw3");
	//hold parsed elements
	string date;
	string value;
	string volume;
	//track row numbers
	long count=0;
	int total=0;
	//holds batch of "documents" to be inserted into database
	vector<BSONObj> bobjp(BATCH_SIZE);
	//holds last DUPLICATE_ARRAY_SIZE elements to be used for duplicate search
	string comparison[DUPLICATE_ARRAY_SIZE];
	int compCount=0;
	bool duplicate;
	int test=0;
	XLog logInsert("Inserting...");

	while(!input.eof()){
		duplicate=false;
		//parse a line
		getline(input,date,',');
		getline(input,value,',');
		getline(input,volume);

		//create "document" to be inserted from parsed information
		BSONObjBuilder b;
		double dValue=atof(value.c_str());
		int iVol=atol(volume.c_str());
		b.append("date", date);
		b.append("value", dValue);
		b.append("volume", iVol);

		//check for duplicates within a window of size DUPLICATE_ARRAY_SIZE
		duplicate=checkDuplicates(date,comparison);
		comparison[compCount]=date;
		compCount++;
		if (compCount>=DUPLICATE_ARRAY_SIZE)compCount=0;


		//create "document" to be inserted into database
		BSONObj p = b.obj();
		//if no duplicates, add to array for batch insertion
		if (duplicate==false){
			bobjp[count]=p;
			total++;
			count++;
		}
		//if it is a duplicate, insert right away into noise collection
		else {
			c.insert("hw3.noise", p);
			test++;
		}

		//insert batch of documents into collection once vector is full
		if (total%BATCH_SIZE==0){
			c.insert("hw3.signal", bobjp);
			count=0;
			if(total%(BATCH_SIZE*10)==0)logInsert.log("Lines inserted so far: ",total);
		}
	}
	//process leftover elements after eof
	for (int i=0;i<count;i++){
		c.insert("hw3.signal", bobjp[i]);
	}
}
Ejemplo n.º 27
0
int main(int argc, char* argv[]) 
{


	Status status = client::initialize();
    if ( !status.isOK() ) {
        std::cout << "failed to initialize the mongo client driver: " << status.toString() << endl;
        return EXIT_FAILURE;
    }

    const char *port = "27017";

    try {
        cout << "connecting to localhost..." << endl;
        DBConnection.connect(string("localhost:") + port);
        cout << "connected ok" << endl;

		string tracePath;
		const char* defaultTracePath = "/home/vladimir/Desktop/vlad-trace/traces.otf2";
		//const char* defaultTracePath = "/home/vladimir/Dicertation/otf2_trace/traces.otf2";
		//const char* defaultTracePath = "/home/vladimir/Desktop/qgen10-trace/traces.otf2";
		//const char* defaultTracePath = "/home/vladimir/tests/mpi_isend/scorep-20150416_1732_2789913328919/traces.otf2";

		if (argc != 2)
			tracePath = defaultTracePath;
		else
		{
			ifstream in;
			in.open(argv[1], ios::in);
			in >> tracePath;
			in.close();
		}

		OTF2_Reader* reader = OTF2_Reader_Open(tracePath.c_str());

		uint64_t tid_temp = 0; 
	    OTF2_Reader_GetTraceId (reader, &tid_temp);

	    // get procs number TODO
	    uint64_t numProcesses = 0;
	    OTF2_Reader_GetNumberOfLocations(reader, &numProcesses);

	    if ( numProcesses == 0 ) {
	        std::cout << "Something wrong with processes number" << endl;
	        return EXIT_FAILURE;
	    }

	    NumProcesses = numProcesses;

	    BeginTimes = new uint64_t[numProcesses];

	    for(int i = 0; i < numProcesses; i++){
	    	BeginTimes[i] = 0;
	    }

	   // SendTo = new int[numProcesses];

	    //for(int i = 0; i < numProcesses; i++){
	    //	SendTo[i] = -1;
	    //}


	    SendLength = new long long int[numProcesses];

	    for(int i = 0; i < numProcesses; i++){
	    	SendLength[i] = 0;
	    }

	    //RecvFrom = new int[numProcesses];

	    //for(int i = 0; i < numProcesses; i++){
	    //	RecvFrom[i] = -1;
	    //}

	    RecvLength = new long long int[numProcesses];

	    for(int i = 0; i < numProcesses; i++){
	    	RecvLength[i] = 0;
	    }

	    Root = new int[numProcesses];

	    for(int i = 0; i < numProcesses; i++){
	    	Root[i] = -1;
	    }

	    RegionNames = new int[numProcesses];
	    for(int i = 0; i < numProcesses; i++){
	    	RegionNames[i] = -1;
	    }

	    IsPointEvent = new bool[numProcesses];

	    for(int i = 0; i < numProcesses; i++){
	    	IsPointEvent[i] = 0;
	    }

	    IsCommEvent = new bool[numProcesses];

	    for(int i = 0; i < numProcesses; i++){
	    	IsCommEvent[i] = 0;
	    }





		//Point_SendTime = new long long int[numProcesses * numProcesses];
	    //for(int i = 0; i < numProcesses * numProcesses; i++){
	    //	Point_SendTime[i] = -1;
	    //}

	    //Point_SendTime = new long long int[numProcesses * numProcesses];
	    for(int i = 0; i < numProcesses * numProcesses; i++){
	    	//Point_SendTime[i] = -1;
	    	Point_SendTime.push_back(vector<long long int>());
	    }

	    for(int i = 0; i < numProcesses * numProcesses; i++){
	    	//Point_SendTime[i] = -1;
	    	Point_SendComm.push_back(vector<int>());
	    }

	    for(int i = 0; i < numProcesses * numProcesses; i++){
	    	//Point_SendTime[i] = -1;
	    	Point_SendTag.push_back(vector<int>());
	    }

	    for(int i = 0; i < numProcesses * numProcesses; i++){
	    	//Point_SendTime[i] = -1;
	    	Point_SendLength.push_back(vector<long long int>());
	    }






	    cout << tid_temp << endl;

	    char *buff;
	    buff = (char*) malloc (64);
	    sprintf(buff , "%" PRIx64, tid_temp);
		TraceId = string(buff);
		free(buff);

		auto_ptr<DBClientCursor> cursor = DBConnection.query("Otf2Data.TraceIds",  Query("{TraceId: \"" + TraceId + "\", Status: \"done\"}"));
		if(cursor->more()){
			cout << "Трасса с таким id уже сществует в БД" << endl;
		}
		else{
			DBConnection.remove("Otf2Data.Events", Query("{TraceId: \"" + TraceId + "\"}"));


			DBConnection.remove("Otf2Data.PointOperations", Query("{TraceId: \"" + TraceId + "\"}"));

			OTF2_GlobalDefReader* global_def_reader = OTF2_Reader_GetGlobalDefReader(reader);
			// creating global definition callbacks handle
			OTF2_GlobalDefReaderCallbacks* global_def_callbacks = OTF2_GlobalDefReaderCallbacks_New();
			// setting global definition reader callbacks to handle



			// получаем все строки
			OTF2_GlobalDefReaderCallbacks_SetStringCallback( global_def_callbacks, print_global_def_string );


			// получаем названия регионов 
			OTF2_GlobalDefReaderCallbacks_SetRegionCallback( global_def_callbacks, print_global_def_region );


			OTF2_GlobalDefReaderCallbacks_SetLocationCallback(global_def_callbacks, &GlobDefLocation_Register);

			//OTF2_GlobalDefReaderCallbacks_SetCommCallback (global_def_callbacks, &GlobDefCommunicator_Register);
			//OTF2_GlobalDefReaderCallbacks_SetGroupCallback(global_def_callbacks, &GlobDefGroup_Register);
			// registering callbacks and deleting callbacks handle
			OTF2_Reader_RegisterGlobalDefCallbacks(reader, global_def_reader, global_def_callbacks, reader);
			OTF2_GlobalDefReaderCallbacks_Delete( global_def_callbacks );

			// reading all global definitions
			uint64_t definitions_read = 0;
			OTF2_Reader_ReadAllGlobalDefinitions( reader, global_def_reader, &definitions_read );
			printf("Definitions_read = %"PRIu64"\n", definitions_read);
			
			// DEFINITIONS READING END
			
			cout << "numProcesses = " << numProcesses << endl; 


			// EVENTS READING START
			
			OTF2_GlobalEvtReader* global_evt_reader = OTF2_Reader_GetGlobalEvtReader(reader);
			// creating global event callbacks handle
			OTF2_GlobalEvtReaderCallbacks* event_callbacks = OTF2_GlobalEvtReaderCallbacks_New();
			// setting global event reader callbacks to handle

			OTF2_GlobalEvtReaderCallbacks_SetEnterCallback( event_callbacks, &EnterCallback);
			OTF2_GlobalEvtReaderCallbacks_SetLeaveCallback( event_callbacks, &LeaveCallback);

			
			OTF2_GlobalEvtReaderCallbacks_SetMpiSendCallback(event_callbacks, &MPI_Send_print);
			OTF2_GlobalEvtReaderCallbacks_SetMpiIsendCallback(event_callbacks, &MPI_Isend_print);



			OTF2_GlobalEvtReaderCallbacks_SetMpiRecvCallback(event_callbacks, &MPI_Recv_print);
			OTF2_GlobalEvtReaderCallbacks_SetMpiIrecvCallback(event_callbacks, &MPI_Irecv_print);



			//OTF2_GlobalEvtReaderCallbacks_SetMpiCollectiveBeginCallback(event_callbacks, &MPI_CollectiveBegin_print);

			OTF2_GlobalEvtReaderCallbacks_SetMpiCollectiveEndCallback(event_callbacks, &MPI_CollectiveEnd_print);

			
			// registering callbacks and deleting callbacks handle
			OTF2_Reader_RegisterGlobalEvtCallbacks(reader, global_evt_reader, event_callbacks, NULL);
			OTF2_GlobalEvtReaderCallbacks_Delete(event_callbacks);

			// reading all global events
			uint64_t events_read = 0;
			OTF2_Reader_ReadAllGlobalEvents(reader, global_evt_reader, &events_read);
			//printf("Events_read = %"PRIu64"\n", events_read);
			

			OTF2_Reader_Close( reader );

			cout << "Events started at " << startTime << endl;
			cout << "Events ended at " << endTime << endl;

			long long int num1 = numProcesses;

			DBConnection.insert("Otf2Data.TraceIds", BSON( "TraceId" << TraceId << "Status" << "done" << "NumberOfLocations" << num1));

			cout << "getlasterror returns: \"" << DBConnection.getLastError() << '"' << endl;

			cout << "Inserting successfully done! " << endl;
		}



		delete [] BeginTimes;
		delete [] IsPointEvent;

		delete [] IsCommEvent;

		delete [] RegionNames;
		//delete [] SendTo;
		//delete [] RecvFrom;

		delete [] SendLength;
		delete [] RecvLength;
		delete [] Root;

		//delete [] Point_SendTime;
		//delete [] Point_SendComm;
		//delete [] Point_SendTag;
		//delete [] Point_SendLength;


		
    } 
    catch(DBException& e) { 
        cout << "caught DBException " << e.toString() << endl;
        return EXIT_FAILURE;
    }






	return EXIT_SUCCESS;
}
Ejemplo n.º 28
0
OTF2_CallbackCode MPI_Irecv_print(OTF2_LocationRef 	location,
							     OTF2_TimeStamp 	time,
							     void 				*userData,
							     OTF2_AttributeList *attributeList,
							     uint32_t 			sender,
							     OTF2_CommRef 		communicator,
							     uint32_t 			msgTag,
							     uint64_t 			msgLength,
                 				 uint64_t            requestID )
{        
	IsPointEvent[location] = 1;




	long long int time2 = time;
	int loc1 = location;
	int targ1 = sender;
	int comm1 = communicator;
	int tag1 = msgTag;
	long long int msglen1 = msgLength;


	//RecvFrom[location] = targ1;
	//RecvLength[location] = msglen1;



	// TODO смотрим очередь, если в ней есть элемент на соотв позиции, то вынимаем его, это будет нашим начальным временем, еси пусто, то какая-то ошибка
	if(Point_SendTime[NumProcesses * targ1 + loc1].size() > 0){


		bool chk = 1;

		for(int i = 0; i < Point_SendTime[NumProcesses * targ1 + loc1].size(); i++){

			if(Point_SendComm[NumProcesses * targ1 + loc1].at(i) == comm1 && Point_SendTag[NumProcesses * targ1 + loc1].at(i) == tag1 && Point_SendLength[NumProcesses * targ1 + loc1].at(i) == msglen1){
				long long int time1 = Point_SendTime[NumProcesses * targ1 + loc1].at(i);
				DBConnection.insert("Otf2Data.PointOperations", BSON("TraceId" << TraceId << "TimeBegin" << time1 << "TimeEnd" << time2  << "From" << targ1 << "To" << loc1 << "Size" << msglen1));
				
				Point_SendTime[NumProcesses * targ1 + loc1].erase(Point_SendTime[NumProcesses * targ1 + loc1].begin() + i);
				Point_SendComm[NumProcesses * targ1 + loc1].erase(Point_SendComm[NumProcesses * targ1 + loc1].begin() + i);
				Point_SendTag[NumProcesses * targ1 + loc1].erase(Point_SendTag[NumProcesses * targ1 + loc1].begin() + i);
				Point_SendLength[NumProcesses * targ1 + loc1].erase(Point_SendLength[NumProcesses * targ1 + loc1].begin() + i);

				chk = 0;

				break;
			}

	    }

	    if(chk){
	    	printf("WARNING\n");
	    }
	
	}
	//else{
	//	int sdsd = Point_SendTime[NumProcesses * targ1 + loc1].size();
	//	printf("Warning: recv before send %u - %u\n",loc1, sdsd);
	//}













    return OTF2_CALLBACK_SUCCESS;
}
Ejemplo n.º 29
0
OTF2_CallbackCode LeaveCallback(OTF2_LocationRef		location,
							      OTF2_TimeStamp		time,
							      void*					userData,
							      OTF2_AttributeList* 	attributeList,
							      OTF2_RegionRef		region)
{
	if(endTime < time)
	{
		endTime = (uint64_t) time;
	}


	if(IsPointEvent[location] == 1){

		// Делаем запись буфера в БД операций точка-точка

		//printf("%s: LOC:%u ", STRINGS[REGIONS[region]], location);


		long long int time1 = BeginTimes[location];
		long long int time2 = time;
		int loc1 = location;


		//int root1 = Root[location];
		//long long int ssen1 = SendLength[location];
		//long long int srecv1 = RecvLength[location];

		DBConnection.insert("Otf2Data.Events", BSON("TraceId" << TraceId << "TimeBegin" << time1 << "TimeEnd" << time2 << "Operation" << STRINGS[REGIONS[region]] << "Location" << loc1));

		/*

		if(SendLength[location] > 0 && RecvLength[location] > 0){
			printf("1\n");
			//cout << "TraceId " << TraceId << "TimeBegin " << BeginTimes[location] << "TimeEnd " << time << "Operation " << STRINGS[REGIONS[region]] << "Location " << location << "To " << SendTo[location] << "SizeSent " << SendLength[location] << "From " << RecvFrom[location] << "SizeReceived " << RecvLength[location] << endl;
			

			int to1 = SendTo[location];
			int from1 = RecvFrom[location];

			DBConnection.insert("Otf2Data.Events", BSON("TraceId" << TraceId << "TimeBegin" << time1 << "TimeEnd" << time2 << "Operation" << STRINGS[REGIONS[region]] << "Location" << loc1 << "To" << to1 << "SizeSent" << ssen1 << "From" << from1 << "SizeReceived" << srecv1));

			SendLength[location] = 0;
			RecvLength[location] = 0;
		}
		else{
			
			if(SendLength[location] > 0){
			//	printf("SEND_TO: %u SEND_LEN: %lu ",SendTo[location], SendLength[location]);
				int to1 = SendTo[location];
				DBConnection.insert("Otf2Data.Events", BSON("TraceId" << TraceId << "TimeBegin" << time1 << "TimeEnd" << time2 << "Operation" << STRINGS[REGIONS[region]] << "Location" << loc1 << "To" << to1 << "SizeSent" << ssen1));

				//cout << "TraceId " << TraceId << "TimeBegin " << BeginTimes[location] << "TimeEnd " << time << "Operation " << STRINGS[REGIONS[region]] << "Location " << location << "To " << SendTo[location] << "SizeSent " << SendLength[location] << endl;
				SendLength[location] = 0;
			}


			if(RecvLength[location] > 0){
				int from1 = RecvFrom[location];
				DBConnection.insert("Otf2Data.Events", BSON("TraceId" << TraceId << "TimeBegin" << time1 << "TimeEnd" << time2 << "Operation" << STRINGS[REGIONS[region]] << "Location" << loc1 << "From" << from1 << "SizeReceived" << srecv1));
				//cout << "TraceId " << TraceId << "TimeBegin " << BeginTimes[location] << "TimeEnd " << time << "Operation " << STRINGS[REGIONS[region]] << "Location " << location << "From " << RecvFrom[location] << "SizeReceived " << RecvLength[location] << endl;
			//	printf("RECV_FROM: %u RECV_LEN: %lu", RecvFrom[location], RecvLength[location]);


				RecvLength[location] = 0;
			}
		
		}
		*/

		//printf("\n");
		IsPointEvent[location] = 0;
	}




	if(IsCommEvent[location] == 1){

		long long int time1 = BeginTimes[location];
		long long int time2 = time;
		int loc1 = location;


		int root1 = Root[location];
		long long int ssen1 = SendLength[location];
		long long int srecv1 = RecvLength[location];
		
		if(Root[location] >= 0){
			DBConnection.insert("Otf2Data.Events", BSON("TraceId" << TraceId << "TimeBegin" << time1 << "TimeEnd" << time2 << "Operation" << STRINGS[REGIONS[region]] << "Location" << loc1 << "SizeSent" << ssen1 << "SizeReceived" << srecv1 << "Root" << root1));
			//DBConnection.insert("Otf2Data.Events", BSON("TraceId" << TraceId << "TimeBegin" << BeginTimes[location] << "TimeEnd" << time2 << "Operation" << STRINGS[REGIONS[region]] << "Location" << loc1 << "SizeSent" << SendLength[location] << "SizeReceived" << RecvLength[location] << "Root" << Root[location]));
			//cout << "TraceId " << TraceId << "TimeBegin " << BeginTimes[location] << "TimeEnd " << time << "Operation " << STRINGS[REGIONS[region]] << "Location " << location << "SizeSent " << SendLength[location] << "SizeReceived " << RecvLength[location] << "Root " << Root[location] << endl;
		}
		else{
			DBConnection.insert("Otf2Data.Events", BSON("TraceId" << TraceId << "TimeBegin" << time1 << "TimeEnd" << time2 << "Operation" << STRINGS[REGIONS[region]] << "Location" << loc1 << "SizeSent" << ssen1 << "SizeReceived" << srecv1));
			//cout << "TraceId " << TraceId << "TimeBegin " << BeginTimes[location] << "TimeEnd " << time << "Operation " << STRINGS[REGIONS[region]] << "Location " << location << "SizeSent " << SendLength[location] << "SizeReceived " << RecvLength[location] << endl;
		}
		


		IsCommEvent[location] = 0;
	}


    return OTF2_CALLBACK_SUCCESS;
}
Ejemplo n.º 30
0
void run(string router, string ns, long long start, long long range, int sleepTime) {
    DBClientConnection c;
    c.connect(router);
    c.setWriteConcern(W_NORMAL);
    
    struct timeval start_time, stop_time, delay;
    char timeStr[25];
    bool flag;
    BSONObj b;
    srand(time(NULL));
    long long user_id = -1;
    long long number = -1;
    int opSelector;
    string s;

    BSONObj insertObj;
    BSONObj query;
    BSONObj updateObj;
    BSONObj readObj;

    int numOps = 3; 
    int i = 0;

    string operation = "none";

    map<long long, int> insertedKeys;

    while( true ) {
        flag = false;
        curTimeString(timeStr);
        gettimeofday(&start_time, NULL);

        opSelector = i % numOps;
        i++;
        try {
            switch(opSelector) {
                case 0: //insert
                        operation = "insert";
                        while(true) {
                            user_id = start + (rand() % range);
                            if( insertedKeys.find(user_id) == insertedKeys.end()) { //key not been inserted previously
                                insertedKeys.insert(make_pair(user_id, 1));
                                cout<<operation<<": Info: inserting " << user_id << endl;
                                break;
                            } 
                        }
                        //insert command goes here
                        number = 2 * start + range - user_id; 
                        insertObj = BSON("user_id" << user_id << "number" << number << "name" << "name");
                        //cout<<"insert: "<<insertObj.toString()<<endl;
                        c.insert(ns, insertObj);
                        s = c.getLastError(ns, false, false, 1, 0);
                        if (s.length() > 0)
                        {
                            flag = true;
                            cout << "Error:" << s << endl;
                        }
                    break;
                case 1: //update
                        operation = "update";
                        //update command goes here
                        query = BSON("user_id" << user_id);
                        updateObj = BSON("user_id" << user_id << "number" << number << "name" << "nameUpdated");
                        //cout<<"update: "<<updateObj.toString()<<endl;
                        c.update(ns, Query(query), updateObj);
                        s = c.getLastError(ns, false, false, 1, 0);
                        if (s.length() > 0)
                        {
                            flag = true;
                            cout << "Error:" << s << endl;
                        }
                    break;
                case 2:
                        //read
                        operation = "read";
                        readObj = BSON("user_id" << user_id);
                        //cout<<"read: "<<readObj.toString()<<endl;
                        b = c.findOne(ns, Query(readObj), 0, QueryOption_SlaveOk);
                        if (b.isEmpty() <= 0)
                            flag = true;
                        s = c.getLastError(ns, false, false, 1, 0);
                        if (s.length() > 0)
                        {
                            flag = true;
                            cout << "Error:" << s << endl;
                        }
                    break;
                default:
                    cout<<"Unrecognized opSelector ! " << opSelector << endl;
                    cout<<"i : " << i << " numOps : " << numOps << endl;
                    break; 
            }
        } catch (DBException e){
            flag = true;
            cout << "Error: " << e.toString() << endl;
        }

        if (!flag) {
            gettimeofday(&stop_time, NULL);
            if (opSelector == 2)
		        cout << "Returned result:" << b.toString() << endl;
            delay = subtract(start_time, stop_time);
            cout<<operation<<": ";
            cout << timeStr << ": " << delay.tv_sec*1000 + delay.tv_usec/(double)1000 << endl;
        } else {
            cout<<operation<<": ";
		    cout << timeStr << ": -100" << endl;
        }

        usleep(sleepTime);
    }
}