Ejemplo n.º 1
0
bool is_id_exist(DBClientConnection &c, int id)
{
    if(c.count(USER_CATE_COLLECTION, BSON("id" << id)) > 0)
        return true;
    else
        return false;
}
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
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.º 4
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;
}
/* ****************************************************************************
*
* MongoDbRemoveFail -
*
*/
TEST(mongoUnsubscribeContextAvailability, MongoDbRemoveFail)
{
    HttpStatusCode                         ms;
    UnsubscribeContextAvailabilityRequest  req;
    UnsubscribeContextAvailabilityResponse res;

    /* Prepare mocks */
    const DBException e = DBException("boom!!", 33);
    BSONObj fakeSub = BSON("_id" << OID("51307b66f481db11bf860001") <<
                                       "expiration" << 10000000 <<
                                       "reference" << "http://notify1.me" <<
                                       "entities" << BSON_ARRAY(BSON("id" << "E1" << "type" << "T1" << "isPattern" << "false")) <<
                                       "attrs" << BSONArray());
    DBClientConnectionMock* connectionMock = new DBClientConnectionMock();
    ON_CALL(*connectionMock, findOne("unittest.casubs",_,_,_))
            .WillByDefault(Return(fakeSub));
    ON_CALL(*connectionMock, remove("unittest.casubs",_,_))
            .WillByDefault(Throw(e));

    NotifierMock* notifierMock = new NotifierMock();
    EXPECT_CALL(*notifierMock, sendNotifyContextAvailabilityRequest(_,_,_,_))
            .Times(0);
    setNotifier(notifierMock);

    /* Forge the request (from "inside" to "outside") */
    req.subscriptionId.set("51307b66f481db11bf860001");

    /* Prepare database */
    prepareDatabase();
    mongoConnect(connectionMock);

    /* Invoke the function in mongoBackend library */
    ms = mongoUnsubscribeContextAvailability(&req, &res);

    /* Check response is as expected */
    EXPECT_EQ(SccOk, ms);
    EXPECT_EQ("51307b66f481db11bf860001", res.subscriptionId.get());
    EXPECT_EQ(SccReceiverInternalError, res.statusCode.code);
    EXPECT_EQ("Internal Server Error", res.statusCode.reasonPhrase);
    EXPECT_EQ("collection: unittest.casubs "
              "- remove() _id: 51307b66f481db11bf860001 "
              "- exception: boom!!", res.statusCode.details);

    /* Check database (untouched) */
    mongoDisconnect();

    // Sleeping a little to "give mongod time to process its input".
    // Without this sleep, this tests fails around 50% of the times (in Ubuntu 13.04)
    usleep(1000);
    mongoConnect("localhost");
    DBClientConnection* connection = getMongoConnection();
    ASSERT_EQ(2, connection->count(SUBSCRIBECONTEXTAVAIL_COLL, BSONObj()));

    /* Release mocks */
    delete notifierMock;

}
Ejemplo n.º 6
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();
}
/* ****************************************************************************
*
* MongoDbFindOneFail -
*
*/
TEST(mongoUnsubscribeContext, MongoDbFindOneFail)
{
    HttpStatusCode             ms;
    UnsubscribeContextRequest  req;
    UnsubscribeContextResponse res;

    /* Prepare mocks */
    const DBException e = DBException("boom!!", 33);
    DBClientConnectionMock* connectionMock = new DBClientConnectionMock();
    ON_CALL(*connectionMock, findOne("unittest.csubs",_,_,_))
            .WillByDefault(Throw(e));

    NotifierMock* notifierMock = new NotifierMock();
    EXPECT_CALL(*notifierMock, destroyOntimeIntervalThreads(_))
            .Times(0);
    EXPECT_CALL(*notifierMock, sendNotifyContextRequest(_,_,_))
            .Times(0);
    EXPECT_CALL(*notifierMock, createIntervalThread(_,_))
            .Times(0);
    setNotifier(notifierMock);

    /* Forge the request (from "inside" to "outside") */
    req.subscriptionId.set("51307b66f481db11bf860001");

    /* Prepare database */
    prepareDatabase();
    mongoConnect(connectionMock);

    /* Invoke the function in mongoBackend library */
    ms = mongoUnsubscribeContext(&req, &res);

    /* Check response is as expected */
    EXPECT_EQ(SccOk, ms);
    EXPECT_EQ("51307b66f481db11bf860001", res.subscriptionId.get());
    EXPECT_EQ(SccReceiverInternalError, res.statusCode.code);
    EXPECT_EQ("Internal Server Error", res.statusCode.reasonPhrase);
    EXPECT_EQ("collection: unittest.csubs "
              "- findOne() _id: 51307b66f481db11bf860001 "
              "- exception: boom!!", res.statusCode.details);

    /* Check database (untouched) */
    mongoDisconnect();

    // Sleeping a little to "give mongod time to process its input".
    // Without this sleep, this tests fails around 10% of the times (in Ubuntu 13.04)
    usleep(1000);
    mongoConnect("localhost");
    DBClientConnection*  connection = getMongoConnection();
    int                  count      = connection->count(SUBSCRIBECONTEXT_COLL, BSONObj());

    ASSERT_EQ(2, count);

    /* Release mocks */
    delete notifierMock;

}
Ejemplo n.º 8
0
void dumpCollection(Parameters& params) {
	DBClientConnection c;
	string hostPort(params.getHost());
	if (hostPort.find(':') == string::npos) {
		hostPort += ":";
		hostPort += to_string(params.getPort());
	}
	c.connect(hostPort);
	time_t t;
	time(&t);

	int documentCount = c.count(params.getDbCollection());

	if (params.isDebug()) {
		cout << "{ " << params.getDbCollection() << ".count: "
			<< documentCount << " }\n";
	}

	string docPrefixString(params.getDbCollection());
//	docIndex += "{";
//	docIndex += to_string(i++);
//	docIndex += "}";

	unique_ptr<DBClientCursor> cursor = c.query(params.getDbCollection(), BSONObj());

	unique_ptr<IBSONRenderer> renderer;
	switch (params.getStyle()) {
	case STYLE_DOTTED:
		renderer = unique_ptr<IBSONRenderer>(new BSONDotNotationDump(params, docPrefixString));
		break;
	case STYLE_TREE:
		renderer = unique_ptr<IBSONRenderer>(new BSONObjectTypeDump(params, docPrefixString));
		break;
	case STYLE_JSON:
	case STYLE_JSONPACKED:
		renderer = unique_ptr<IBSONRenderer>(new JSONDump(params, "  "));
		break;
	default:
		throw std::logic_error("ISE: Undefined STYLE!");
		break;
	}
	if (renderer) {
		renderer->setOutputStream(cout);
		renderer->begin(NULL);
		int documentIndex = 0;
		while (cursor->more()) {
			const BSONObj& o = cursor->next(); // Get the BSON Object
			renderer->render(o, documentIndex++, documentCount);
		}
		renderer->end(NULL);
	} else {
		throw std::logic_error("ISE: Undefined renderer!");
	}
}
Ejemplo n.º 9
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.º 10
0
int main() {
//	BSONObj p = BSONObjBuilder().genOID().append("name","Joe").append("age",33).obj();

	DBClientConnection c;
	c.connect("127.0.0.1");

	cout << "count:" << c.count("test.uefa") << "\n";

	std::auto_ptr<DBClientCursor> cursor = c.query("test.uefa", BSONObj());
	while (cursor->more())
	   cout << cursor->next().toString() << "\n";

//	string mongo::DBClientWithCommands::getLastError(); // Empty string if no error
//	cout <<

	return 0;
}
/* ****************************************************************************
*
* unsubscribe -
*/
TEST(mongoUnsubscribeContext, unsubscribe)
{
    HttpStatusCode             ms;
    UnsubscribeContextRequest  req;
    UnsubscribeContextResponse res;

    /* Prepare mock */
    NotifierMock* notifierMock = new NotifierMock();
    EXPECT_CALL(*notifierMock, destroyOntimeIntervalThreads("51307b66f481db11bf860001"))
            .Times(1);
    EXPECT_CALL(*notifierMock, sendNotifyContextRequest(_,_,_))
            .Times(0);
    EXPECT_CALL(*notifierMock, createIntervalThread(_,_))
            .Times(0);
    setNotifier(notifierMock);

    /* Forge the request (from "inside" to "outside") */
    req.subscriptionId.set("51307b66f481db11bf860001");

    /* Prepare database */
    prepareDatabase();

    /* Invoke the function in mongoBackend library */
    ms = mongoUnsubscribeContext(&req, &res);

    /* Check response is as expected */
    EXPECT_EQ(SccOk, ms);
    EXPECT_EQ("51307b66f481db11bf860001", res.subscriptionId.get());
    EXPECT_EQ(SccOk, res.statusCode.code);
    EXPECT_EQ("OK", res.statusCode.reasonPhrase);
    EXPECT_EQ(0, res.statusCode.details.size());

    /* Check database (one document, but not the deleted one) */
    DBClientConnection* connection = getMongoConnection();
    ASSERT_EQ(1, connection->count(SUBSCRIBECONTEXT_COLL, BSONObj()));
    BSONObj sub = connection->findOne(SUBSCRIBECONTEXT_COLL, BSON("_id" << OID("51307b66f481db11bf860002")));
    EXPECT_EQ("51307b66f481db11bf860002", sub.getField("_id").OID().str());

    /* Release connection */
    mongoDisconnect();

    /* Release mock */
    delete notifierMock;
}
/* ****************************************************************************
*
* subscriptionNotFound -
*/
TEST(mongoUnsubscribeContext, subscriptionNotFound)
{
    HttpStatusCode             ms;
    UnsubscribeContextRequest  req;
    UnsubscribeContextResponse res;

    /* Prepare mock */
    NotifierMock* notifierMock = new NotifierMock();
    EXPECT_CALL(*notifierMock, destroyOntimeIntervalThreads(_))
            .Times(0);
    EXPECT_CALL(*notifierMock, sendNotifyContextRequest(_,_,_))
            .Times(0);
    EXPECT_CALL(*notifierMock, createIntervalThread(_,_))
            .Times(0);
    setNotifier(notifierMock);

    /* Forge the request (from "inside" to "outside") */
    req.subscriptionId.set("51307b66f481db11bf869999");

    /* Prepare database */
    prepareDatabase();

    /* Invoke the function in mongoBackend library */
    ms = mongoUnsubscribeContext(&req, &res);

    /* Check response is as expected */
    EXPECT_EQ(SccOk, ms);
    EXPECT_EQ("51307b66f481db11bf869999", res.subscriptionId.get());
    EXPECT_EQ(SccContextElementNotFound, res.statusCode.code);
    EXPECT_EQ("No context element found", res.statusCode.reasonPhrase);
    EXPECT_EQ("subscriptionId: '51307b66f481db11bf869999'", res.statusCode.details);

    /* Check database (untouched) */
    DBClientConnection* connection = getMongoConnection();
    ASSERT_EQ(2, connection->count(SUBSCRIBECONTEXT_COLL, BSONObj()));

    /* Release connection */
    mongoDisconnect();

    /* Release mock */
    delete notifierMock;
}
Ejemplo n.º 13
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.º 14
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.º 15
0
// liberally cribbed from user_prio.cpp
void 
plumage::stats::processAccountantStats(ClassAd* ad, ODSMongodbOps* ops, Date_t& ts)
{
    // attr%d holders...sadly reverting back to MyString for convenience of formatstr
    MyString  attrName, attrPrio, attrResUsed, attrWtResUsed, attrFactor, attrBeginUsage, attrAccUsage;
    MyString  attrLastUsage, attrAcctGroup, attrIsAcctGroup;
    MyString  attrConfigQuota, attrEffectiveQuota, attrSubtreeQuota, attrSurplusPolicy;
    
    // values
    string  name, acctGroup, surplusPolicy;
    float priority, factor, wtResUsed, configQuota, effectiveQuota, subtreeQuota, accUsage = -1;
    int   resUsed, beginUsage, lastUsage;
    resUsed = beginUsage = lastUsage = 0;
    bool isAcctGroup;

    DBClientConnection* conn = ops->m_db_conn;
    conn->ensureIndex(DB_STATS_SAMPLES_ACCOUNTANT, BSON( "ts" << -1 ));
    conn->ensureIndex(DB_STATS_SAMPLES_ACCOUNTANT, BSON( "lu" << -1 ));
    conn->ensureIndex(DB_STATS_SAMPLES_ACCOUNTANT, BSON( "n" << 1 ));
    unsigned long long acct_count = conn->count(DB_STATS_SAMPLES_ACCOUNTANT);

    // eventhough the Accountant doesn't forget
    // we don't care about stale submitters (default: last 24 hours)
    int cfg_last_usage = param_integer("ODS_ACCOUNTANT_LAST_USAGE", 60*60*24);
    int minLastUsageTime = time(0)-cfg_last_usage;
    int numElem = -1;
    ad->LookupInteger( "NumSubmittors", numElem );

    for( int i=1; i<=numElem; i++) {
        priority=0;
        isAcctGroup = false;

        // skip stale records unless we have none
        attrLastUsage.formatstr("LastUsageTime%d", i );
        ad->LookupInteger  ( attrLastUsage.Value(), lastUsage );
        if (lastUsage < minLastUsageTime && acct_count > 0)
            continue;

        // parse the horrid classad
        attrName.formatstr("Name%d", i );
        attrPrio.formatstr("Priority%d", i );
        attrResUsed.formatstr("ResourcesUsed%d", i );
        attrWtResUsed.formatstr("WeightedResourcesUsed%d", i );
        attrFactor.formatstr("PriorityFactor%d", i );
        attrBeginUsage.formatstr("BeginUsageTime%d", i );
        attrAccUsage.formatstr("WeightedAccumulatedUsage%d", i );
        attrAcctGroup.formatstr("AccountingGroup%d", i);
        attrIsAcctGroup.formatstr("IsAccountingGroup%d", i);
        attrConfigQuota.formatstr("ConfigQuota%d", i);
        attrEffectiveQuota.formatstr("EffectiveQuota%d", i);
        attrSubtreeQuota.formatstr("SubtreeQuota%d", i);
        attrSurplusPolicy.formatstr("SurplusPolicy%d", i);

        ad->LookupString   ( attrName.Value(), name );
        ad->LookupFloat    ( attrPrio.Value(), priority );
        ad->LookupFloat    ( attrFactor.Value(), factor );
        ad->LookupFloat    ( attrAccUsage.Value(), accUsage );
        ad->LookupInteger  ( attrBeginUsage.Value(), beginUsage );
        ad->LookupInteger  ( attrResUsed.Value(), resUsed );
        ad->LookupBool     ( attrIsAcctGroup.Value(), isAcctGroup);
        ad->LookupFloat    ( attrConfigQuota.Value(), configQuota );
        ad->LookupFloat    ( attrEffectiveQuota.Value(), effectiveQuota );
        ad->LookupFloat    ( attrSubtreeQuota.Value(), subtreeQuota );
        ad->LookupString   ( attrSurplusPolicy.Value(), surplusPolicy );
        
        if( !ad->LookupFloat( attrWtResUsed.Value(), wtResUsed ) ) {
            wtResUsed = resUsed;
        }
        if (!ad->LookupString(attrAcctGroup.Value(), acctGroup)) {
            acctGroup = "<none>";
        }

        BSONObjBuilder bob;
        bob.appendDate("ts",ts);
        bob.append("n",name);
        bob.append("ag",acctGroup);
        bob.appendAsNumber("prio",formatReal(priority));
        bob.appendAsNumber("fac",formatReal(factor));
        bob.append("ru",resUsed);
        bob.append("wru",wtResUsed);
        // condor timestamps need massaging when going in the db
        bob.appendDate("bu",static_cast<unsigned long long>(beginUsage)*1000);
        bob.appendDate("lu",static_cast<unsigned long long>(lastUsage)*1000);
        bob.appendAsNumber("au",formatReal(accUsage));
        bob.appendAsNumber("cq",formatReal(configQuota));
        bob.appendAsNumber("eq",formatReal(effectiveQuota));
        bob.appendAsNumber("sq",formatReal(subtreeQuota));
        if (!surplusPolicy.empty()) bob.append("sp",surplusPolicy);
        
        conn->insert(DB_STATS_SAMPLES_ACCOUNTANT,bob.obj());
    }
    
}