bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){ string dbName = getDBName( ns ); string collection = cmdObj.firstElement().valuestrsafe(); string fullns = dbName + "." + collection; BSONObj filter = cmdObj["query"].embeddedObject(); DBConfig * conf = grid.getDBConfig( dbName , false ); if ( ! conf || ! conf->isShardingEnabled() || ! conf->isSharded( fullns ) ){ ScopedDbConnection conn( conf->getPrimary() ); result.append( "n" , (double)conn->count( fullns , filter ) ); conn.done(); result.append( "ok" , 1 ); return true; } ChunkManager * cm = conf->getChunkManager( fullns ); massert( "how could chunk manager be null!" , cm ); vector<Chunk*> chunks; cm->getChunksForQuery( chunks , filter ); unsigned long long total = 0; for ( vector<Chunk*>::iterator i = chunks.begin() ; i != chunks.end() ; i++ ){ Chunk * c = *i; total += c->countObjects(); } result.append( "n" , (double)total ); result.append( "ok" , 1 ); return true; }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){ string dbName = getDBName( ns ); string collection = cmdObj.firstElement().valuestrsafe(); string fullns = dbName + "." + collection; DBConfig * conf = grid.getDBConfig( dbName , false ); if ( ! conf || ! conf->isShardingEnabled() || ! conf->isSharded( fullns ) ){ return passthrough( conf , cmdObj , result ); } ChunkManager * cm = conf->getChunkManager( fullns ); massert( "how could chunk manager be null!" , cm ); vector<Chunk*> chunks; cm->getChunksForQuery( chunks , BSONObj() ); set<BSONObj,BSONObjCmp> all; int size = 32; for ( vector<Chunk*>::iterator i = chunks.begin() ; i != chunks.end() ; i++ ){ Chunk * c = *i; ScopedDbConnection conn( c->getShard() ); BSONObj res; bool ok = conn->runCommand( conf->getName() , cmdObj , res ); conn.done(); if ( ! ok ){ result.appendElements( res ); return false; } BSONObjIterator it( res["values"].embeddedObjectUserCheck() ); while ( it.more() ){ BSONElement nxt = it.next(); BSONObjBuilder temp(32); temp.appendAs( nxt , "x" ); all.insert( temp.obj() ); } } BSONObjBuilder b( size ); int n=0; for ( set<BSONObj,BSONObjCmp>::iterator i = all.begin() ; i != all.end(); i++ ){ b.appendAs( i->firstElement() , b.numStr( n++ ).c_str() ); } result.appendArray( "values" , b.obj() ); result.append( "ok" , 1 ); return true; }
virtual void queryOp( Request& r ){ QueryMessage q( r.d() ); log(3) << "shard query: " << q.ns << " " << q.query << endl; if ( q.ntoreturn == 1 && strstr(q.ns, ".$cmd") ) throw UserException( 8010 , "something is wrong, shouldn't see a command here" ); ChunkManager * info = r.getChunkManager(); assert( info ); Query query( q.query ); vector<Chunk*> shards; info->getChunksForQuery( shards , query.getFilter() ); set<ServerAndQuery> servers; map<string,int> serverCounts; for ( vector<Chunk*>::iterator i = shards.begin(); i != shards.end(); i++ ){ servers.insert( ServerAndQuery( (*i)->getShard() , (*i)->getFilter() ) ); int& num = serverCounts[(*i)->getShard()]; num++; } if ( logLevel > 4 ){ StringBuilder ss; ss << " shard query servers: " << servers.size() << "\n"; for ( set<ServerAndQuery>::iterator i = servers.begin(); i!=servers.end(); i++ ){ const ServerAndQuery& s = *i; ss << " " << s.toString() << "\n"; } log() << ss.str(); } ClusteredCursor * cursor = 0; BSONObj sort = query.getSort(); if ( sort.isEmpty() ){ // 1. no sort, can just hit them in serial cursor = new SerialServerClusteredCursor( servers , q ); } else { int shardKeyOrder = info->getShardKey().canOrder( sort ); if ( shardKeyOrder ){ // 2. sort on shard key, can do in serial intelligently set<ServerAndQuery> buckets; for ( vector<Chunk*>::iterator i = shards.begin(); i != shards.end(); i++ ){ Chunk * s = *i; buckets.insert( ServerAndQuery( s->getShard() , s->getFilter() , s->getMin() ) ); } cursor = new SerialServerClusteredCursor( buckets , q , shardKeyOrder ); } else { // 3. sort on non-sharded key, pull back a portion from each server and iterate slowly cursor = new ParallelSortClusteredCursor( servers , q , sort ); } } assert( cursor ); log(5) << " cursor type: " << cursor->type() << endl; ShardedClientCursor * cc = new ShardedClientCursor( q , cursor ); if ( ! cc->sendNextBatch( r ) ){ delete( cursor ); return; } log(6) << "storing cursor : " << cc->getId() << endl; cursorCache.store( cc ); }