Esempio n. 1
0
    bool DBConfig::_load() {
        ScopedDbConnection conn( configServer.modelServer() );

        BSONObj o = conn->findOne( ShardNS::database , BSON( "_id" << _name ) );

        if ( o.isEmpty() ) {
            conn.done();
            return false;
        }

        unserialize( o );

        BSONObjBuilder b;
        b.appendRegex( "_id" , (string)"^" + _name + "." );

        auto_ptr<DBClientCursor> cursor = conn->query( ShardNS::collection ,b.obj() );
        assert( cursor.get() );
        while ( cursor->more() ) {
            BSONObj o = cursor->next();
            _collections[o["_id"].String()] = CollectionInfo( o );
        }

        conn.done();

        return true;
    }
Esempio n. 2
0
    MongoShellResult ScriptEngine::prepareResult(const std::string &type, const std::string &output,
                                                 const std::vector<MongoDocumentPtr> &objects, qint64 elapsedms)
    {
        const char *script =
            "__robomongoQuery = false; \n"
            "__robomongoDbName = '[invalid database]'; \n"
            "__robomongoServerAddress = '[invalid connection]'; \n"
            "__robomongoCollectionName = '[invalid collection]'; \n"
            "if (typeof __robomongoLastRes == 'object' && __robomongoLastRes != null && __robomongoLastRes instanceof DBQuery) { \n"
            "    __robomongoQuery = true; \n"
            "    __robomongoDbName = __robomongoLastRes._db.getName();\n "
            "    __robomongoServerAddress = __robomongoLastRes._mongo.host; \n"
            "    __robomongoCollectionName = __robomongoLastRes._collection._shortName; \n"
            "    __robomongoQuery = __robomongoLastRes._query; \n"
            "    __robomongoFields = __robomongoLastRes._fields; \n"
            "    __robomongoLimit = __robomongoLastRes._limit; \n"
            "    __robomongoSkip = __robomongoLastRes._skip; \n"
            "    __robomongoBatchSize = __robomongoLastRes._batchSize; \n"
            "    __robomongoOptions = __robomongoLastRes._options; \n"
            "    __robomongoSpecial = __robomongoLastRes._special; \n"
            "} \n";

        _scope->exec(script, "(getresultinfo)", false, false, false);

        bool isQuery = _scope->getBoolean("__robomongoQuery");

        if (isQuery) {
            std::string serverAddress = getString("__robomongoServerAddress");
            std::string dbName = getString("__robomongoDbName");
            std::string collectionName = getString("__robomongoCollectionName");
            mongo::BSONObj query = _scope->getObject("__robomongoQuery");
            mongo::BSONObj fields = _scope->getObject("__robomongoFields");

            int limit = _scope->getNumberInt("__robomongoLimit");
            int skip = _scope->getNumberInt("__robomongoSkip");
            int batchSize = _scope->getNumberInt("__robomongoBatchSize");
            int options = _scope->getNumberInt("__robomongoOptions");

            bool special = _scope->getBoolean("__robomongoSpecial");

            MongoQueryInfo info = MongoQueryInfo(CollectionInfo(serverAddress, dbName, collectionName),
                                       query, fields, limit, skip, batchSize, options, special);
            return MongoShellResult(type, output, objects, info, elapsedms);
        }

        return MongoShellResult(type, output, objects, MongoQueryInfo(), elapsedms);
    }
    void ExplorerCollectionTreeItem::ui_addDocument()
    {
        MongoDatabase *database = _collection->database();
        MongoServer *server = database->server();
        ConnectionSettings *settings = server->connectionRecord();

        DocumentTextEditor editor(CollectionInfo(settings->getFullAddress(), database->name(), _collection->name()), "{\n    \n}");

        editor.setCursorPosition(1, 4);
        editor.setWindowTitle("Insert Document");
        int result = editor.exec();

        treeWidget()->activateWindow();

        if (result == QDialog::Accepted) {
            server->insertDocuments(editor.bsonObj(), MongoNamespace(database->name(), _collection->name()) );
        }
    }
Esempio n. 4
0
    ChunkManager* DBConfig::shardCollection( const string& ns , ShardKeyPattern fieldsAndOrder , bool unique ){
        if ( ! _shardingEnabled )
            throw UserException( 8042 , "db doesn't have sharding enabled" );
        
        ChunkManager * info = _shards[ns];
        if ( info )
            return info;
        
        if ( isSharded( ns ) )
            throw UserException( 8043 , "already sharded" );

        log() << "enable sharding on: " << ns << " with shard key: " << fieldsAndOrder << endl;
        _sharded[ns] = CollectionInfo( fieldsAndOrder , unique );

        info = new ChunkManager( this , ns , fieldsAndOrder , unique );
        _shards[ns] = info;
        return info;

    }
Esempio n. 5
0
 void DBConfig::unserialize(const BSONObj& from){
     _name = from.getStringField("name");
     _shardingEnabled = from.getBoolField("partitioned");
     _primary = from.getStringField("primary");
     
     _sharded.clear();
     BSONObj sharded = from.getObjectField( "sharded" );
     if ( ! sharded.isEmpty() ){
         BSONObjIterator i(sharded);
         while ( i.more() ){
             BSONElement e = i.next();
             uassert( 10182 ,  "sharded things have to be objects" , e.type() == Object );
             BSONObj c = e.embeddedObject();
             uassert( 10183 ,  "key has to be an object" , c["key"].type() == Object );
             _sharded[e.fieldName()] = CollectionInfo( c["key"].embeddedObject() , 
                                                       c["unique"].trueValue() );
         }
     }
 }
Esempio n. 6
0
    MongoShellResult ScriptEngine::prepareResult(const std::string &type, const std::string &output,
                                                 const std::vector<MongoDocumentPtr> &objects, qint64 elapsedms,
                                                 const std::string &statement, AggrInfo aggrInfo /*= AggrInfo()*/)
    {
        const char *script =
            "__robomongoQuery = false; \n"
            "__robomongoIsAggregate = false; \n"
            "__robomongoDbName = '[invalid database]'; \n"
            "__robomongoServerAddress = '[invalid connection]'; \n"
            "__robomongoCollectionName = '[invalid collection]'; \n"
            "if (typeof __robomongoLastRes == 'object' && __robomongoLastRes != null \n"
            "    && __robomongoLastRes instanceof DBQuery) { \n"
            "    __robomongoQuery = true; \n"
            "    __robomongoDbName = __robomongoLastRes._db.getName();\n "
            "    __robomongoServerAddress = __robomongoLastRes._mongo.host; \n"
            "    __robomongoCollectionName = __robomongoLastRes._collection._shortName; \n"
            "    __robomongoQuery = __robomongoLastRes._query; \n"
            "    __robomongoFields = __robomongoLastRes._fields; \n"
            "    __robomongoLimit = __robomongoLastRes._limit; \n"
            "    __robomongoSkip = __robomongoLastRes._skip; \n"
            "    __robomongoBatchSize = __robomongoLastRes._batchSize; \n"
            "    __robomongoOptions = __robomongoLastRes._options; \n"
            "    __robomongoSpecial = __robomongoLastRes._special; \n"
            "} \n"
            "else if (typeof __robomongoLastRes == 'object' && __robomongoLastRes != null \n"
            "         && __robomongoLastRes instanceof DBCommandCursor \n"
            "         && __robomongoAggregateUsed) { \n"
            "    __robomongoAggregateUsed = false; \n"    
            "    __robomongoIsAggregate = true; \n"
            "    __robomongoDbName = __robomongoLastRes._db.getName();\n "
            "    __robomongoServerAddress = __robomongoLastRes._db._mongo.host; \n"
            "    __robomongoCollectionName = __robomongoLastRes._collName; \n"
            "} \n"
            ;

        _scope->exec(script, "(getresultinfo)", false, false, false);

        bool const isQuery = _scope->getBoolean("__robomongoQuery");
        bool const isAggregate = _scope->getBoolean("__robomongoIsAggregate");

        if (isQuery) {
            std::string serverAddress = getString("__robomongoServerAddress");
            std::string dbName = getString("__robomongoDbName");
            std::string collectionName = getString("__robomongoCollectionName");
               
            mongo::BSONObj query = _scope->getObject("__robomongoQuery");
            mongo::BSONObj fields = _scope->getObject("__robomongoFields");

            int limit = _scope->getNumberInt("__robomongoLimit");
            int skip = _scope->getNumberInt("__robomongoSkip");
            int batchSize = _scope->getNumberInt("__robomongoBatchSize");
            int options = _scope->getNumberInt("__robomongoOptions");

            bool special = _scope->getBoolean("__robomongoSpecial");

            MongoQueryInfo info = MongoQueryInfo(CollectionInfo(serverAddress, dbName, collectionName),
                                       query, fields, limit, skip, batchSize, options, special);
            return MongoShellResult(type, output, objects, info, elapsedms);
        }
        else if (isAggregate) {
            std::string const serverAddress = getString("__robomongoServerAddress");
            std::string const dbName = getString("__robomongoDbName");
            std::string const collectionName = getString("__robomongoCollectionName");
            mongo::BSONObj const pipeline = _scope->getObject("__robomongoAggregatePipeline");
            mongo::BSONObj const options = _scope->getObject("__robomongoAggregateOptions");

            // This query can be paging of an original aggr. query, we store the original/unpaged 
            // pipeline object here.
            mongo::BSONObj const origPipeline = aggrInfo.isValid ? aggrInfo.pipeline : pipeline;
            int const skip = aggrInfo.isValid ? aggrInfo.skip : 0;
            int const batchSize = aggrInfo.isValid ? aggrInfo.batchSize : 50;
            int const resultIndex = aggrInfo.isValid ? aggrInfo.resultIndex : -1;

            AggrInfo const newAggrInfo { collectionName, skip, batchSize, origPipeline, options, resultIndex };
            return MongoShellResult(type, output, objects, MongoQueryInfo(), elapsedms, newAggrInfo);
        }

        return MongoShellResult(type, output, objects, MongoQueryInfo(), elapsedms);
    }