static void find_range( const char *ns ) {
    int i;
    bson bb;
    mongo_cursor *cursor;

    for ( i=0; i < PER_TRIAL; i++ ) {
        int j=0;

        bson_init( &bb );
        bson_append_start_object( &bb, "x" );
        bson_append_int( &bb, "$gt", PER_TRIAL/2 );
        bson_append_int( &bb, "$lt", PER_TRIAL/2 + BATCH_SIZE );
        bson_append_finish_object( &bb );
        bson_finish( &bb );

        cursor = mongo_find( conn, ns, &bb, NULL, 0,0,0 );
        ASSERT( cursor );

        while( mongo_cursor_next( cursor ) == MONGO_OK ) {
            j++;
        }
        ASSERT( j == BATCH_SIZE-1 );

        mongo_cursor_destroy( cursor );
        bson_destroy( &bb );
    }
}
Example #2
0
SEXP rmongo_find(SEXP mongo_conn, SEXP ns, SEXP query, SEXP sort, SEXP fields, SEXP limit, SEXP skip, SEXP options) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _ns = CHAR(STRING_ELT(ns, 0));
    bson* _query = _checkBSON(query);
    bson* _sort = _checkBSON(sort);

    bson* q = _query;
    bson sorted_query;
    if (_sort != NULL && bson_size(_sort) > 5) {
        q = &sorted_query;
        bson_init(q);
        bson_append_bson(q, "$query", _query);
        bson_append_bson(q, "$orderby", _sort);
        bson_finish(q);
    }

    bson* _fields = _checkBSON(fields);
    int _limit = asInteger(limit);
    int _skip = asInteger(skip);
    int _options = 0;
    int i;
    int len = LENGTH(options);
    for (i = 0; i < len; i++)
        _options |= INTEGER(options)[i];

    mongo_cursor* cursor = mongo_find(conn, _ns, q, _fields, _limit, _skip, _options);

    if (q == &sorted_query)
        bson_destroy(&sorted_query);

    return _mongo_cursor_create(cursor);
}
Example #3
0
mongo_cursor* gridfile_get_chunks(gridfile* gfile, int start, int size)

{
    bson_iterator it;
    bson_oid_t id;
    bson_buffer gte_buf;
    bson gte_bson;
    bson_buffer query_buf;
    bson query_bson;
    bson_buffer orderby_buf;
    bson orderby_bson;
    bson_buffer command_buf;
    bson command_bson;
    bson_type type;
    char *id_str;
    int id_int;

    type = bson_find(&it, gfile->meta, "_id");
    if( type == bson_oid ) {
        id = *bson_iterator_oid(&it);
        bson_buffer_init(&query_buf);
        bson_append_oid(&query_buf, "files_id", &id);
    } else if (type == bson_string) {
        id_str = bson_iterator_string(&it);
        bson_buffer_init(&query_buf);
        bson_append_string(&query_buf, "files_id", id_str);
    } else if (type == bson_string) {
        id_int = bson_iterator_int(&it);
        bson_buffer_init(&query_buf);
        bson_append_int(&query_buf, "files_id", id_int);
    } else return NULL;

    if (size == 1) {
        bson_append_int(&query_buf, "n", start);
    } else {
        bson_buffer_init(&gte_buf);
        bson_append_int(&gte_buf, "$gte", start);
        bson_from_buffer(&gte_bson, &gte_buf);
        bson_append_bson(&query_buf, "n", &gte_bson);
    }
    bson_from_buffer(&query_bson, &query_buf);

    bson_buffer_init(&orderby_buf);
    bson_append_int(&orderby_buf, "n", 1);
    bson_from_buffer(&orderby_bson, &orderby_buf);

    bson_buffer_init(&command_buf);
    bson_append_bson(&command_buf, "query", &query_bson);
    bson_append_bson(&command_buf, "orderby", &orderby_bson);
    bson_from_buffer(&command_bson, &command_buf);

    return mongo_find(gfile->gfs->client, gfile->gfs->chunks_ns,
                      &command_bson, NULL, size, 0, 0);
}
Example #4
0
bson_bool_t mongo_find_one(mongo_connection* conn, const char* ns, bson* query, bson* fields, bson* out){
    mongo_cursor* cursor = mongo_find(conn, ns, query, fields, 1, 0, 0);

    if (cursor && mongo_cursor_next(cursor)){
        bson_copy(out, &cursor->current);
        mongo_cursor_destroy(cursor);
        return 1;
    }else{
        mongo_cursor_destroy(cursor);
        return 0;
    }
}
Example #5
0
SEXP mongo_get_database_collections(SEXP mongo_conn, SEXP db) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _db = CHAR(STRING_ELT(db, 0));
    int len = strlen(_db);
    char ns[512];
    strcpy(ns, _db);
    strcpy(ns+len, ".system.namespaces");
    bson empty;
    bson_empty(&empty);
    mongo_cursor* cursor = mongo_find(conn, ns, NULL, &empty, 0, 0, 0);
    int count = 0;
    while (cursor && mongo_cursor_next(cursor) == MONGO_OK) {
        bson_iterator iter;
        if (bson_find(&iter, &cursor->current, "name")) {
            const char* name = bson_iterator_string(&iter);
            if (strstr(name, ".system.") || strchr(name, '$'))
                continue;
            ++count;
        }
    }
    mongo_cursor_destroy(cursor);
    cursor = mongo_find(conn, ns, &empty, &empty, 0, 0, 0);
    SEXP ret;
    PROTECT(ret = allocVector(STRSXP, count));
    int i = 0;
    while (cursor && mongo_cursor_next(cursor) == MONGO_OK) {
        bson_iterator iter;
        if (bson_find(&iter, &cursor->current, "name")) {
            const char* name = bson_iterator_string(&iter);
            if (strstr(name, ".system.") || strchr(name, '$'))
                continue;
            SET_STRING_ELT(ret, i++, mkChar(name));
        }
    }
    mongo_cursor_destroy(cursor);
    UNPROTECT(1);
    return ret;
}
Example #6
0
int mongo_find_one( mongo *conn, const char *ns, bson *query,
                    bson *fields, bson *out ) {

    mongo_cursor *cursor = mongo_find( conn, ns, query, fields, 1, 0, 0 );

    if ( cursor && mongo_cursor_next( cursor ) == MONGO_OK ) {
        bson_copy_basic( out, &cursor->current );
        mongo_cursor_destroy( cursor );
        return MONGO_OK;
    } else {
        mongo_cursor_destroy( cursor );
        return MONGO_ERROR;
    }
}
static ngx_int_t ngx_http_mongo_authenticate(ngx_log_t *log, ngx_http_gridfs_loc_conf_t *gridfs_loc_conf) {
    ngx_http_mongo_connection_t* mongo_conn;
    ngx_http_mongo_auth_t *mongo_auth;
    mongo_cursor *cursor = NULL;
    bson empty;
    char *test;
    int error;

    mongo_conn = ngx_http_get_mongo_connection( gridfs_loc_conf->mongo );
    if (mongo_conn == NULL) {
        ngx_log_error(NGX_LOG_ERR, log, 0,
                  "Mongo Connection not found: \"%V\"", &gridfs_loc_conf->mongo);
    }

    // Authenticate
    if (gridfs_loc_conf->user.data != NULL && gridfs_loc_conf->pass.data != NULL) {
        if (mongo_cmd_authenticate( &mongo_conn->conn,
				    (const char*)gridfs_loc_conf->db.data,
				    (const char*)gridfs_loc_conf->user.data,
				    (const char*)gridfs_loc_conf->pass.data )
	    != MONGO_OK) {
            ngx_log_error(NGX_LOG_ERR, log, 0,
                          "Invalid mongo user/pass: %s/%s",
                          gridfs_loc_conf->user.data,
                          gridfs_loc_conf->pass.data);
            return NGX_ERROR;
        }

        mongo_auth = ngx_array_push(mongo_conn->auths);
        mongo_auth->db = gridfs_loc_conf->db;
        mongo_auth->user = gridfs_loc_conf->user;
        mongo_auth->pass = gridfs_loc_conf->pass;
    }

    // Run a test command to test authentication.
    test = (char*)malloc( gridfs_loc_conf->db.len + sizeof(".test"));
    ngx_cpystrn((u_char*)test, (u_char*)gridfs_loc_conf->db.data, gridfs_loc_conf->db.len+1);
    ngx_cpystrn((u_char*)(test+gridfs_loc_conf->db.len),(u_char*)".test", sizeof(".test"));
    bson_empty(&empty);
    cursor = mongo_find(&mongo_conn->conn, test, &empty, NULL, 0, 0, 0);
    error =  mongo_cmd_get_last_error(&mongo_conn->conn, (char*)gridfs_loc_conf->db.data, NULL);
    free(test);
    mongo_cursor_destroy(cursor);
    if (error) {
        ngx_log_error(NGX_LOG_ERR, log, 0, "Authentication Required");
        return NGX_ERROR;
    }

    return NGX_OK;
}
Example #8
0
dlist_t *mongodb_dump_keyvalue(bot_t * bot, char *db)
{
	dlist_t *dl, *dptr;
	keyvalue_t *keyvalue_ptr;
	mongo_cursor *cursor;
	bson b;
	bson_iterator bi;

	char *key;
	char *value;

	dl = dptr = NULL;
	cursor = NULL;

	if (!db)
		return NULL;

	debug(bot, "mongodb_dump_keyvalue: Entered\n");

	cursor =
	    mongo_find(&gi->mongo_conn, db, bson_empty(&b),
		       bson_empty(&b), 0, 0, 0);

	while (mongo_cursor_next(cursor) == MONGO_OK) {
		key = value = NULL;

		if (bson_find(&bi, mongo_cursor_bson(cursor), "key")) {
			key = (char *)bson_iterator_string(&bi);
		}
		if (bson_find(&bi, mongo_cursor_bson(cursor), "value")) {
			value = (char *)bson_iterator_string(&bi);
		}

		if (!key && !value)
			continue;

		keyvalue_ptr = (keyvalue_t *) calloc(1, sizeof(keyvalue_t));
		if (key)
			keyvalue_ptr->key = strdup(key);
		if (value)
			keyvalue_ptr->value = strdup(value);

		dlist_Dinsert_after(&dl, keyvalue_ptr);

	}

	mongo_cursor_destroy(cursor);

	return dl;
}
static void find( const char *ns ) {
    bson b;
    int i;
    for ( i=0; i < PER_TRIAL; i++ ) {
        mongo_cursor *cursor;
        make_query( &b );
        cursor = mongo_find( conn, ns, &b, NULL, 0,0,0 );
        ASSERT( cursor );

        while( mongo_cursor_next( cursor ) == MONGO_OK )
            {}

        mongo_cursor_destroy( cursor );
        bson_destroy( &b );
    }
}
Example #10
0
int test_tailable( mongo *conn ) {
    mongo_cursor *cursor;
    bson b;
    int count;

    remove_sample_data( conn );
    create_capped_collection( conn );
    insert_sample_data( conn, 10000 );

    bson_init( &b );
    bson_append_start_object( &b, "$query" );
    bson_append_finish_object( &b );
    bson_append_start_object( &b, "$sort" );
    bson_append_int( &b, "$natural", -1 );
    bson_append_finish_object( &b );
    bson_finish( &b );

    cursor = mongo_find( conn, "test.cursors", &b, bson_shared_empty( ), 0, 0, MONGO_TAILABLE );
    bson_destroy( &b );

    count = 0;
    while( mongo_cursor_next( cursor ) == MONGO_OK )
        count++;

    ASSERT( count == 10000 );

    ASSERT( mongo_cursor_next( cursor ) == MONGO_ERROR );
    ASSERT( cursor->err == MONGO_CURSOR_PENDING );

    insert_sample_data( conn, 10 );

    count = 0;
    while( mongo_cursor_next( cursor ) == MONGO_OK ) {
        count++;
    }

    ASSERT( count == 10 );

    ASSERT( mongo_cursor_next( cursor ) == MONGO_ERROR );
    ASSERT( cursor->err == MONGO_CURSOR_PENDING );

    mongo_cursor_destroy( cursor );
    remove_sample_data( conn );

    return 0;
}
int TMongoDriver::find(const QString &ns, const QVariantMap &criteria, const QVariantMap &orderBy,
                       const QStringList &fields, int limit, int skip, int options)
{
    int num = -1;
    mongo_clear_errors(mongoConnection);
    mongo_cursor *cursor = mongo_find(mongoConnection, qPrintable(ns), (bson *)TBson::toBson(criteria, orderBy).data(),
                                      (bson *)TBson::toBson(fields).data(), limit, skip, options);
    mongoCursor->setCursor(cursor);

    if (!cursor) {
        tSystemError("MongoDB Error: %s", mongoConnection->lasterrstr);
    } else {
        if (cursor->reply) {
            num = cursor->reply->fields.num;
        }
    }
    return num;
}
MONGO_EXPORT mongo_cursor *gridfile_get_chunks( gridfile *gfile, int start, int size ) {
    bson_iterator it;
    bson_oid_t id;
    bson gte;
    bson query;
    bson orderby;
    bson command;
    mongo_cursor *cursor;

    bson_find( &it, gfile->meta, "_id" );
    id = *bson_iterator_oid( &it );

    bson_init( &query );
    bson_append_oid( &query, "files_id", &id );
    if ( size == 1 ) {
        bson_append_int( &query, "n", start );
    }
    else {
        bson_init( &gte );
        bson_append_int( &gte, "$gte", start );
        bson_finish( &gte );
        bson_append_bson( &query, "n", &gte );
        bson_destroy( &gte );
    }
    bson_finish( &query );

    bson_init( &orderby );
    bson_append_int( &orderby, "n", 1 );
    bson_finish( &orderby );

    bson_init( &command );
    bson_append_bson( &command, "query", &query );
    bson_append_bson( &command, "orderby", &orderby );
    bson_finish( &command );

    cursor = mongo_find( gfile->gfs->client, gfile->gfs->chunks_ns,
                         &command, NULL, size, 0, 0 );

    bson_destroy( &command );
    bson_destroy( &query );
    bson_destroy( &orderby );

    return cursor;
}
Example #13
0
void gridfs_remove_filename(gridfs* gfs, const char* filename )

{
    bson query;
    bson_buffer buf;
    mongo_cursor* files;
    bson file;
    bson_iterator it;
    bson_oid_t id;
    bson b;

    bson_buffer_init(&buf);
    bson_append_string(&buf, "filename", filename);
    bson_from_buffer(&query, &buf);
    files = mongo_find(gfs->client, gfs->files_ns, &query, NULL, 0, 0, 0);
    bson_destroy(&query);

    /* Remove each file and it's chunks from files named filename */
    while (mongo_cursor_next(files)) {
        file = files->current;
        bson_find(&it, &file, "_id");
        id = *bson_iterator_oid(&it);

        /* Remove the file with the specified id */
        bson_buffer_init(&buf);
        bson_append_oid(&buf, "_id", &id);
        bson_from_buffer(&b, &buf);
        mongo_remove( gfs->client, gfs->files_ns, &b);
        bson_destroy(&b);

        /* Remove all chunks from the file with the specified id */
        bson_buffer_init(&buf);
        bson_append_oid(&buf, "files_id", &id);
        bson_from_buffer(&b, &buf);
        mongo_remove( gfs->client, gfs->chunks_ns, &b);
        bson_destroy(&b);
    }

}
Example #14
0
int test_multiple_getmore( mongo *conn ) {
    mongo_cursor *cursor;
    int count;

    remove_sample_data( conn );
    create_capped_collection( conn );
    insert_sample_data( conn, 10000 );

    cursor = mongo_find( conn, "test.cursors", bson_shared_empty( ), bson_shared_empty( ), 0, 0, 0 );

    count = 0;
    while( mongo_cursor_next( cursor ) == MONGO_OK )
        count++;

    ASSERT( count == 10000 );

    ASSERT( mongo_cursor_next( cursor ) == MONGO_ERROR );
    ASSERT( cursor->err == MONGO_CURSOR_EXHAUSTED );

    mongo_cursor_destroy( cursor );
    remove_sample_data( conn );
    return 0;
}
void MongodbClient::DumpDB()
{
    Logger::Debug("***** MONGO DUMP *******");

    bson query;
    bson_empty(&query);
    
    mongo_cursor *cursor = mongo_find(clientData->connection, namespaceName.c_str(), &query, NULL, 0, 0, 0);
    int32 count = 0;
    while( mongo_cursor_next( cursor ) == MONGO_OK )
    {
        const bson *currentObject = mongo_cursor_bson(cursor);
        
        Logger::Debug(Format("BSON[%d]:", count));
        bson_print(currentObject);
        
        ++count;
    }

    mongo_cursor_destroy(cursor);
    
    Logger::Debug("Count: %d", count);
    Logger::Debug("************************");
}
Example #16
0
int main(){
    mongo_connection conn[1];
    mongo_connection_options opts;
    bson_buffer bb;
    bson b;
    mongo_cursor * cursor;
    int i;
    char hex_oid[25];

    const char * col = "c.simple";
    const char * ns = "test.c.simple";

    INIT_SOCKETS_FOR_WINDOWS;
    
    strncpy(opts.host, TEST_SERVER, 255);
    opts.host[254] = '\0';
    opts.port = 27017;

    if (mongo_connect( conn , &opts )){
        printf("failed to connect\n");
        exit(1);
    }

    /* if the collection doesn't exist dropping it will fail */
    if (!mongo_cmd_drop_collection(conn, "test", col, NULL)
          && mongo_find_one(conn, ns, bson_empty(&b), bson_empty(&b), NULL)){
        printf("failed to drop collection\n");
        exit(1);
    }

    for(i=0; i< 5; i++){
        bson_buffer_init( & bb );

        bson_append_new_oid( &bb, "_id" );
        bson_append_double( &bb , "a" , 17 );
        bson_append_int( &bb , "b" , 17 );
        bson_append_string( &bb , "c" , "17" );

        {
            bson_buffer * sub = bson_append_start_object(  &bb , "d" );
            bson_append_int( sub, "i", 71 );
            bson_append_finish_object(sub);
        }
        {
            bson_buffer * arr = bson_append_start_array(  &bb , "e" );
            bson_append_int( arr, "0", 71 );
            bson_append_string( arr, "1", "71" );
            bson_append_finish_object(arr);
        }

        bson_from_buffer(&b, &bb);
        mongo_insert( conn , ns , &b );
        bson_destroy(&b);
    }
    
    cursor = mongo_find( conn , ns , bson_empty(&b) , 0 , 0 , 0 , 0 );

    while (mongo_cursor_next(cursor)){
        bson_iterator it;
        bson_iterator_init(&it, cursor->current.data);
        while(bson_iterator_next(&it)){
            fprintf(stderr, "  %s: ", bson_iterator_key(&it));

            switch(bson_iterator_type(&it)){
                case bson_double:
                    fprintf(stderr, "(double) %e\n", bson_iterator_double(&it));
                    break;
                case bson_int:
                    fprintf(stderr, "(int) %d\n", bson_iterator_int(&it));
                    break;
                case bson_string:
                    fprintf(stderr, "(string) \"%s\"\n", bson_iterator_string(&it));
                    break;
                case bson_oid:
                    bson_oid_to_string(bson_iterator_oid(&it), hex_oid);
                    fprintf(stderr, "(oid) \"%s\"\n", hex_oid);
                    break;
                case bson_object:
                    fprintf(stderr, "(subobject) {...}\n");
                    break;
                case bson_array:
                    fprintf(stderr, "(array) [...]\n");
                    break;
                default:
                    fprintf(stderr, "(type %d)\n", bson_iterator_type(&it));
                    break;
            }
        }
        fprintf(stderr, "\n");
    }

    mongo_cursor_destroy(cursor);
    mongo_cmd_drop_db(conn, "test");
    mongo_destroy( conn );
    return 0;
}
static ngx_int_t ngx_http_gridfs_mongod_connect(ngx_conf_t* directive, ngx_http_gridfs_loc_conf_t* gridfs_conf) {
    mongo_connection_options options;
    bson empty;
    bson_bool_t error;
    char* test;

    if (gridfs_conf->mongod_conn->connected) {
        ngx_conf_log_error(NGX_LOG_ERR, directive, 0,
			 "Mongo Connection is duplicate");
        return NGX_ERROR;
    }

    /* Connect to a mongod */
    ngx_cpystrn( (u_char*)options.host, 
		 gridfs_conf->mongod_host.data, 
		 gridfs_conf->mongod_host.len + 1 );
    options.port = gridfs_conf->mongod_port;
    switch (mongo_connect( gridfs_conf->mongod_conn, &options )) {
        case mongo_conn_success:
            break;
        case mongo_conn_bad_arg:
	    ngx_conf_log_error(NGX_LOG_ERR, directive, 0,
			       "Mongo Exception: Bad Arguments");
            return NGX_ERROR;
        case mongo_conn_no_socket:
            ngx_conf_log_error(NGX_LOG_ERR, directive, 0,
			       "Mongo Exception: No Socket");
            return NGX_ERROR;
        case mongo_conn_fail:
            ngx_conf_log_error(NGX_LOG_ERR, directive, 0,
			       "Mongo Exception: Connection Failure %s:%i;",
			       options.host,options.port/*
							  gridfs_conf->mongod_port*/);
            return NGX_ERROR;
        case mongo_conn_not_master:
            ngx_conf_log_error(NGX_LOG_ERR, directive, 0,
			       "Mongo Exception: Not Master");
            return NGX_ERROR;
        default:
            ngx_conf_log_error(NGX_LOG_ERR, directive, 0,
			       "Mongo Exception: Unknown Error");
            return NGX_ERROR;
    }
    
    /* Authenticate with the user and password */
    if (gridfs_conf->mongod_user.data != NULL && gridfs_conf->mongod_pass.data != NULL) {
        if (!mongo_cmd_authenticate(gridfs_conf->mongod_conn, 
				   (const char*)gridfs_conf->gridfs_db.data, 
				   (const char*)gridfs_conf->mongod_user.data, 
				   (const char*)gridfs_conf->mongod_pass.data)) {
	    ngx_conf_log_error(NGX_LOG_ERR, directive, 0,
			       "Invalid mongo user/pass: %s/%s", 
			       gridfs_conf->mongod_user.data, 
			       gridfs_conf->mongod_pass.data);
	    return NGX_ERROR;
        }
    }
    
    /* Run a test command to test authentication. */
    test = (char*)malloc( gridfs_conf->gridfs_db.len + sizeof(".test"));
    ngx_cpystrn((u_char*)test, (u_char*)gridfs_conf->gridfs_db.data, gridfs_conf->gridfs_db.len+1);
    ngx_cpystrn((u_char*)(test+gridfs_conf->gridfs_db.len),(u_char*)".test", sizeof(".test"));
    bson_empty(&empty);
    mongo_find(gridfs_conf->mongod_conn, test, &empty, NULL, 0, 0, 0);
    error =  mongo_cmd_get_last_error(gridfs_conf->mongod_conn, (char*)gridfs_conf->gridfs_db.data, NULL);
    free(test);
    if (error) {
      ngx_conf_log_error(NGX_LOG_ERR, directive, 0, "Authentication Required");
      return NGX_ERROR;
    }

    return NGX_OK;
}