Esempio n. 1
0
        void run() {
            Client::WriteContext ctx(&_txn, ns());

            // Insert docs, add index
            for (int i = 0; i < 10; ++i) {
                insert(BSON("a" << 1 << "b" << i ));
            }
            addIndex(BSON("a" << 1));

            // Mark key at end position as 'unused' by deleting
            remove(BSON("a" << 1 << "b" << 9));
            ctx.commit();

            // Run count and check
            CountParams params;
            params.descriptor = getIndex(ctx.ctx().db(), BSON("a" << 1));
            params.startKey = BSON("" << 0);
            params.startKeyInclusive = true;
            params.endKey = BSON("" << 2);
            params.endKeyInclusive = true; // yes?

            WorkingSet ws;
            Count count(&_txn, params, &ws);

            int numCounted = runCount(&count);
            ASSERT_EQUALS(9, numCounted);
        }
Esempio n. 2
0
 void run() {
     insert( "{\"a\":\"b\"}" );
     BSONObj cmd = fromjson( "{\"query\":{}}" );
     string err;
     int errCode;
     ASSERT_EQUALS( 1, runCount( ns(), cmd, err, errCode ) );
 }
Esempio n. 3
0
    unsigned long long DBDirectClient::count(const string& ns,
                                             const BSONObj& query,
                                             int options,
                                             int limit,
                                             int skip) {
        if (skip < 0) {
            warning() << "setting negative skip value: " << skip
                      << " to zero in query: " << query << endl;
            skip = 0;
        }

        Lock::DBRead lk(_txn->lockState(), ns);
        string errmsg;
        int errCode;
        long long res = runCount(_txn,
                                 ns,
                                 _countCmd(ns, query, options, limit, skip),
                                 errmsg,
                                 errCode);

        if (res == -1) {
            // namespace doesn't exist
            return 0;
        }
        massert(errCode, str::stream() << "count failed in DBDirectClient: " << errmsg , res >= 0);
        return (unsigned long long )res;
    }
Esempio n. 4
0
 void run() {
     insert( "{\"a\":\"b\"}" );
     insert( "{\"c\":\"d\"}" );
     BSONObj cmd = fromjson( "{\"query\":{},\"fields\":{\"a\":1}}" );
     string err;
     ASSERT_EQUALS( 1, runCount( ns(), cmd, err ) );
 }        
Esempio n. 5
0
        void run() {
            // Insert enough documents that counting them will exceed the iteration threshold
            // to trigger a yield.
            for( int i = 0; i < 1000; ++i ) {
                insert( BSON( "a" << 1 ) );
            }
            
            // Call runCount() under a read lock.
            dbtemprelease release;
            Client::ReadContext ctx( ns() );

            int numYieldsBeforeCount = numYields();
            
            string err;
            int errCode;
            ASSERT_EQUALS( 1000, runCount( ns(), countCommand( BSON( "a" << 1 ) ), err, errCode ) );
            ASSERT_EQUALS( "", err );

            int numYieldsAfterCount = numYields();
            int numYieldsDuringCount = numYieldsAfterCount - numYieldsBeforeCount;

            // The runCount() function yieled.
            ASSERT_NOT_EQUALS( 0, numYieldsDuringCount );
            ASSERT( 0 < numYieldsDuringCount );
        }
Esempio n. 6
0
        void run() {
            Client::WriteContext ctx(&_txn, ns());

            // Insert some docs
            for (int i = 0; i < 10; ++i) {
                insert(BSON("a" << i));
            }

            // Add an index
            addIndex(BSON("a" << 1));
            ctx.commit();
 
            // Set up the count stage
            CountParams params;
            params.descriptor = getIndex(ctx.ctx().db(), BSON("a" << 1));
            params.startKey = BSON("" << 3);
            params.startKeyInclusive = false;
            params.endKey = BSON("" << 7);
            params.endKeyInclusive = false;

            WorkingSet ws;
            Count count(&_txn, params, &ws);

            int numCounted = runCount(&count);
            ASSERT_EQUALS(3, numCounted);
        }
Esempio n. 7
0
        void run() {
            Client::WriteContext ctx(&_txn, ns());

            // Insert docs, add index
            for (int i = 0; i < 10; ++i) {
                insert(BSON("a" << 1 << "b" << i));
            }
            addIndex(BSON("a" << 1));
            ctx.commit();

            // Mark several keys as 'unused'
            remove(BSON("a" << 1 << "b" << 0));
            remove(BSON("a" << 1 << "b" << 3));
            remove(BSON("a" << 1 << "b" << 4));

            // Ensure that count does not include unused keys
            CountParams params;
            params.descriptor = getIndex(ctx.ctx().db(), BSON("a" << 1));
            params.startKey = BSON("" << 1);
            params.startKeyInclusive = true;
            params.endKey = BSON("" << 1);
            params.endKeyInclusive = true;

            WorkingSet ws;
            Count count(&_txn, params, &ws);

            int numCounted = runCount(&count);
            ASSERT_EQUALS(7, numCounted);
        }
Esempio n. 8
0
        void run() {
            Client::WriteContext ctx(&_txn, ns());

            // Insert some docs
            insert(BSON("a" << BSON_ARRAY(5 << 7)));
            insert(BSON("a" << BSON_ARRAY(6 << 8)));

            // Add an index on a:1
            addIndex(BSON("a" << 1));
            ctx.commit();

            // Set up the count stage
            CountParams params;
            params.descriptor = getIndex(ctx.ctx().db(), BSON("a" << 1));
            verify(params.descriptor);
            params.startKey = BSON("a" << 1);
            params.startKeyInclusive = true;
            params.endKey = BSON("a" << 10);
            params.endKeyInclusive = true;

            WorkingSet ws;
            Count count(&_txn, params, &ws);

            int numCounted = runCount(&count);
            ASSERT_EQUALS(2, numCounted);
        }
Esempio n. 9
0
 void run() {
     insert( "{\"a\":\"b\"}" );
     insert( "{\"a\":\"b\",\"x\":\"y\"}" );
     insert( "{\"a\":\"c\"}" );
     BSONObj cmd = fromjson( "{\"query\":{\"a\":\"b\"}}" );
     string err;
     ASSERT_EQUALS( 2, runCount( ns(), cmd, err ) );
 }        
Esempio n. 10
0
 void run() {
     insert( "{\"a\":\"b\"}" );
     insert( "{\"c\":\"d\"}" );
     BSONObj cmd = fromjson( "{\"query\":{},\"fields\":{\"a\":1}}" );
     string err;
     int errCode;
     ASSERT_EQUALS( 2, runCount( &_txn, ns(), cmd, err, errCode ) );
 }
Esempio n. 11
0
 unsigned long long DBDirectClient::count(const string &ns, const BSONObj& query, int options, int limit, int skip ) {
     Lock::DBRead lk( ns );
     string errmsg;
     long long res = runCount( ns.c_str() , _countCmd( ns , query , options , limit , skip ) , errmsg );
     if ( res == -1 )
         return 0;
     uassert( 13637 , str::stream() << "count failed in DBDirectClient: " << errmsg , res >= 0 );
     return (unsigned long long )res;
 }
Esempio n. 12
0
 unsigned long long DBDirectClient::count(const string &ns, const BSONObj& query, int options, int limit, int skip ) {
     Lock::DBRead lk( ns );
     string errmsg;
     int errCode;
     long long res = runCount( ns.c_str() , _countCmd( ns , query , options , limit , skip ) , errmsg, errCode );
     if ( res == -1 ) {
         // namespace doesn't exist
         return 0;
     }
     massert( errCode , str::stream() << "count failed in DBDirectClient: " << errmsg , res >= 0 );
     return (unsigned long long )res;
 }
Esempio n. 13
0
int main()
{
	printf("vvedyte col-vo elementov: \n");
	int count = 0;
	scanf("%d", &count);
	printf("kakoy dolzhen ostat'sya\n", count);
	int i = 0;
	scanf("%d", &i);
	plst head = NULL;
	int k = runCount(head, i, count);
	delList(head);
	return 0;
}
Esempio n. 14
0
    unsigned long long DBDirectClient::count(const string &ns, const BSONObj& query, int options, int limit, int skip ) {
        if ( skip < 0 ) {
            warning() << "setting negative skip value: " << skip
                << " to zero in query: " << query << endl;
            skip = 0;
        }
        string errmsg;
        int errCode;

        LOCK_REASON(lockReason, "count");
        Client::ReadContext ctx(ns, lockReason);
        Client::Transaction transaction(DB_TXN_SNAPSHOT | DB_TXN_READ_ONLY);
        long long res = runCount( ns.c_str() , _countCmd( ns , query , options , limit , skip ) , errmsg, errCode );
        if ( res == -1 ) {
            // namespace doesn't exist
            return 0;
        }
        massert( errCode , str::stream() << "count failed in DBDirectClient: " << errmsg , res >= 0 );
        transaction.commit();
        return (unsigned long long )res;
    }
Esempio n. 15
0
        void run() {
            Client::WriteContext ctx(ns());

            // Insert doc, add index
            insert(BSON("a" << 2));
            addIndex(BSON("a" << 1));

            // Set up count, and run
            CountParams params;
            params.descriptor = getIndex(ctx.ctx().db(), BSON("a" << 1));
            params.startKey = BSON("" << 2);
            params.startKeyInclusive = false;
            params.endKey = BSON("" << 3);
            params.endKeyInclusive = false;

            WorkingSet ws;
            Count count(params, &ws);
            
            int numCounted = runCount(&count);
            ASSERT_EQUALS(0, numCounted);
        }
Esempio n. 16
0
//**********************************************************************************************************************
int SplitGroupCommand::execute(){
	try {
	
		if (abort == true) { if (calledHelp) { return 0; }  return 2;	}
		
        if (countfile == "" ) {  runNameGroup();  }
        else { runCount();  }
				
		if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {	m->mothurRemove(outputNames[i]);	} return 0; }
		
		string current = "";
		itTypes = outputTypes.find("fasta");
		if (itTypes != outputTypes.end()) {
			if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
		}
		
		itTypes = outputTypes.find("name");
		if (itTypes != outputTypes.end()) {
			if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
		}
        
        itTypes = outputTypes.find("count");
		if (itTypes != outputTypes.end()) {
			if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
		}
		
		m->mothurOutEndLine();
		m->mothurOut("Output File Names: "); m->mothurOutEndLine();
		for (int i = 0; i < outputNames.size(); i++) {	m->mothurOut(outputNames[i]); m->mothurOutEndLine();	}
		m->mothurOutEndLine();
		
		return 0;
	}
	catch(exception& e) {
		m->errorOut(e, "SplitGroupCommand", "execute");
		exit(1);
	}
}
Esempio n. 17
0
        // testcount is a wrapper around runCount that
        //  - sets up a countStage
        //  - runs it
        //  - asserts count is not trivial
        //  - asserts nCounted is equal to expected_n
        //  - asserts nSkipped is correct
        void testCount(const CountRequest& request, int expected_n=kDocuments, bool indexed=false) {
            setup();
            getLocs();

            auto_ptr<WorkingSet> ws(new WorkingSet);

            StatusWithMatchExpression swme = MatchExpressionParser::parse(request.query);
            auto_ptr<MatchExpression> expression(swme.getValue());

            PlanStage* scan;
            if (indexed) {
                scan = createIndexScan(expression.get(), ws.get());
            } else {
                scan = createCollScan(expression.get(), ws.get());
            }

            CountStage countStage(&_txn, _coll, request, ws.get(), scan);

            const CountStats* stats = runCount(countStage);

            ASSERT_FALSE(stats->trivialCount);
            ASSERT_EQUALS(stats->nCounted, expected_n);
            ASSERT_EQUALS(stats->nSkipped, request.skip);
        }
Esempio n. 18
0
    bool Cloner::startCloneCollection( const char *fromhost, const char *ns, const BSONObj &query, string &errmsg, bool logForRepl, bool copyIndexes, int logSizeMb, long long &cursorId ) {
        char db[256];
        nsToClient( ns, db );

        NamespaceDetails *nsd = nsdetails( ns );
        if ( nsd ){
            /** note: its ok to clone into a collection, but only if the range you're copying 
                doesn't exist on this server */
            string err;
            if ( runCount( ns , BSON( "query" << query ) , err ) > 0 ){
                log() << "WARNING: data already exists for: " << ns << " in range : " << query << " deleting..." << endl;
                deleteObjects( ns , query , false , logForRepl , false );
            }
        }

        {
            dbtemprelease r;
            auto_ptr< DBClientConnection > c( new DBClientConnection() );
            if ( !c->connect( fromhost, errmsg ) )
                return false;
            if( !replAuthenticate(c.get()) )
                return false;
            conn = c;

            // Start temporary op log
            BSONObjBuilder cmdSpec;
            cmdSpec << "logCollection" << ns << "start" << 1;
            if ( logSizeMb != INT_MIN )
                cmdSpec << "logSizeMb" << logSizeMb;
            BSONObj info;
            if ( !conn->runCommand( db, cmdSpec.done(), info ) ) {
                errmsg = "logCollection failed: " + (string)info;
                return false;
            }
        }
        
        if ( ! nsd ) {
            BSONObj spec = conn->findOne( string( db ) + ".system.namespaces", BSON( "name" << ns ) );
            if ( !userCreateNS( ns, spec.getObjectField( "options" ), errmsg, true ) )
                return false;
        }

        copy( ns, ns, false, logForRepl, false, false, query );

        if ( copyIndexes ) {
            string indexNs = string( db ) + ".system.indexes";
            copy( indexNs.c_str(), indexNs.c_str(), true, logForRepl, false, false, BSON( "ns" << ns << "name" << NE << "_id_" ) );
        }
        
        auto_ptr< DBClientCursor > c;
        {
            dbtemprelease r;
            string logNS = "local.temp.oplog." + string( ns );
            c = conn->query( logNS.c_str(), Query(), 0, 0, 0, Option_CursorTailable );
        }
        if ( c->more() ) {
            replayOpLog( c.get(), query );
            cursorId = c->getCursorId();
            massert( "Expected valid tailing cursor", cursorId != 0 );
        } else {
            massert( "Did not expect valid cursor for empty query result", c->getCursorId() == 0 );
            cursorId = 0;
        }
        c->decouple();
        return true;
    }