static void mongo_cursor_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { MongoCursor *cursor = MONGO_CURSOR(object); switch (prop_id) { case PROP_BATCH_SIZE: mongo_cursor_set_batch_size(cursor, g_value_get_uint(value)); break; case PROP_CONNECTION: mongo_cursor_set_connection(cursor, g_value_get_object(value)); break; case PROP_COLLECTION: mongo_cursor_set_collection(cursor, g_value_get_string(value)); break; case PROP_DATABASE: mongo_cursor_set_database(cursor, g_value_get_string(value)); break; case PROP_FIELDS: mongo_cursor_set_fields(cursor, g_value_get_boxed(value)); break; case PROP_FLAGS: mongo_cursor_set_flags(cursor, g_value_get_uint(value)); break; case PROP_LIMIT: mongo_cursor_set_limit(cursor, g_value_get_uint(value)); break; case PROP_QUERY: mongo_cursor_set_query(cursor, g_value_get_boxed(value)); break; case PROP_SKIP: mongo_cursor_set_skip(cursor, g_value_get_uint(value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); } }
int test_bad_query( mongo *conn ) { mongo_cursor cursor[1]; bson b[1]; bson_init( b ); bson_append_start_object( b, "foo" ); bson_append_int( b, "$bad", 1 ); bson_append_finish_object( b ); bson_finish( b ); mongo_cursor_init( cursor, conn, "test.cursors" ); mongo_cursor_set_query( cursor, b ); ASSERT( mongo_cursor_next( cursor ) == MONGO_ERROR ); ASSERT( cursor->err == MONGO_CURSOR_QUERY_FAIL ); ASSERT( cursor->conn->lasterrcode == 10068 ); ASSERT( strlen( cursor->conn->lasterrstr ) > 0 ); mongo_cursor_destroy( cursor ); bson_destroy( b ); return 0; }
int mongodb_retrieve_key_stat(bot_t * bot, char *db, char *key) { bson b; mongo_cursor cursor; int ret = 0; if (!db || !key) { return -1; } debug(bot, "mongodb_retrieve_key: Entered\n"); bson_init(&b); bson_append_string(&b, "key", key); bson_finish(&b); mongo_cursor_init(&cursor, &gi->mongo_conn, db); mongo_cursor_set_query(&cursor, &b); if (mongo_cursor_next(&cursor) == MONGO_OK) { bson_iterator i; debug(bot, "mongodb_retrieve_key: Found!\n"); if (bson_find(&i, mongo_cursor_bson(&cursor), "value")) { ret = (int)bson_iterator_int(&i); } bson_destroy(&b); mongo_cursor_destroy(&cursor); return ret; } debug(bot, "mongodb_retrieve_key: Key not found\n"); bson_destroy(&b); mongo_cursor_destroy(&cursor); return -1; }
int mongo_find_one( mongo *conn, const char *ns, bson *query, bson *fields, bson *out ) { mongo_cursor cursor[1]; mongo_cursor_init( cursor, conn, ns ); mongo_cursor_set_query( cursor, query ); mongo_cursor_set_fields( cursor, fields ); mongo_cursor_set_limit( cursor, 1 ); // Always use slave OK. mongo_cursor_set_options( cursor, MONGO_SLAVE_OK ); if ( mongo_cursor_next( cursor ) == MONGO_OK ) { bson_init_size( out, bson_size( (bson *)&cursor->current ) ); memcpy( out->data, cursor->current.data, bson_size( (bson *)&cursor->current ) ); out->finished = 1; mongo_cursor_destroy( cursor ); return MONGO_OK; } else { mongo_cursor_destroy( cursor ); return MONGO_ERROR; } }
int main() { mongo conn[1]; bson b, empty; mongo_cursor cursor[1]; unsigned char not_utf8[3]; int result = 0; const char *ns = "test.c.validate"; int i=0, j=0; bson bs[BATCH_SIZE]; bson *bp[BATCH_SIZE]; not_utf8[0] = 0xC0; not_utf8[1] = 0xC0; not_utf8[2] = '\0'; INIT_SOCKETS_FOR_WINDOWS; if ( mongo_connect( conn, TEST_SERVER, 27017 ) ) { printf( "failed to connect\n" ); exit( 1 ); } /* Test checking for finished bson. */ bson_init( &b ); bson_append_int( &b, "foo", 1 ); ASSERT( mongo_insert( conn, "test.foo", &b, NULL ) == MONGO_ERROR ); ASSERT( conn->err == MONGO_BSON_NOT_FINISHED ); bson_destroy( &b ); /* Test valid keys. */ bson_init( &b ); result = bson_append_string( &b , "a.b" , "17" ); ASSERT( result == BSON_OK ); ASSERT( b.err & BSON_FIELD_HAS_DOT ); /* Don't set INIT dollar if deb ref fields are being used. */ result = bson_append_string( &b , "$id" , "17" ); ASSERT( result == BSON_OK ); ASSERT( !(b.err & BSON_FIELD_INIT_DOLLAR) ); result = bson_append_string( &b , "$ref" , "17" ); ASSERT( result == BSON_OK ); ASSERT( !(b.err & BSON_FIELD_INIT_DOLLAR) ); result = bson_append_string( &b , "$db" , "17" ); ASSERT( result == BSON_OK ); ASSERT( !(b.err & BSON_FIELD_INIT_DOLLAR) ); result = bson_append_string( &b , "$ab" , "17" ); ASSERT( result == BSON_OK ); ASSERT( b.err & BSON_FIELD_INIT_DOLLAR ); result = bson_append_string( &b , "ab" , "this is valid utf8" ); ASSERT( result == BSON_OK ); ASSERT( ! ( b.err & BSON_NOT_UTF8 ) ); result = bson_append_string( &b , ( const char * )not_utf8, "valid" ); ASSERT( result == BSON_ERROR ); ASSERT( b.err & BSON_NOT_UTF8 ); ASSERT( bson_finish( &b ) == BSON_ERROR ); ASSERT( b.err & BSON_FIELD_HAS_DOT ); ASSERT( b.err & BSON_FIELD_INIT_DOLLAR ); ASSERT( b.err & BSON_NOT_UTF8 ); result = mongo_insert( conn, ns, &b, NULL ); ASSERT( result == MONGO_ERROR ); ASSERT( conn->err & MONGO_BSON_NOT_FINISHED ); result = mongo_update( conn, ns, bson_empty( &empty ), &b, 0, NULL ); ASSERT( result == MONGO_ERROR ); ASSERT( conn->err & MONGO_BSON_NOT_FINISHED ); mongo_cursor_init( cursor, conn, "test.cursors" ); mongo_cursor_set_query( cursor, &b ); result = mongo_cursor_next( cursor ); ASSERT( result == MONGO_ERROR ); ASSERT( cursor->err & MONGO_CURSOR_BSON_ERROR ); ASSERT( cursor->conn->err & MONGO_BSON_NOT_FINISHED ); bson_destroy( &b ); mongo_cursor_destroy( cursor ); /* Test valid strings. */ bson_init( &b ); result = bson_append_string( &b , "foo" , "bar" ); ASSERT( result == BSON_OK ); ASSERT( b.err == 0 ); result = bson_append_string( &b , "foo" , ( const char * )not_utf8 ); ASSERT( result == BSON_ERROR ); ASSERT( b.err & BSON_NOT_UTF8 ); b.err = 0; ASSERT( b.err == 0 ); result = bson_append_regex( &b , "foo" , ( const char * )not_utf8, "s" ); ASSERT( result == BSON_ERROR ); ASSERT( b.err & BSON_NOT_UTF8 ); for ( j=0; j < BATCH_SIZE; j++ ) bp[j] = &bs[j]; for ( j=0; j < BATCH_SIZE; j++ ) make_small_invalid( &bs[j], i ); result = mongo_insert_batch( conn, ns, (const bson **)bp, BATCH_SIZE, NULL, 0 ); ASSERT( result == MONGO_ERROR ); ASSERT( conn->err == MONGO_BSON_INVALID ); for ( j=0; j < BATCH_SIZE; j++ ) bson_destroy( &bs[j] ); bson_destroy( &b ); mongo_cmd_drop_db( conn, "test" ); mongo_disconnect( conn ); mongo_destroy( conn ); return 0; }
int main() { bson b, sub, out; bson_iterator it; mongo conn; mongo_cursor cursor; int result; /* Create a rich document like this one: * * { _id: ObjectId("4d95ea712b752328eb2fc2cc"), * user_id: ObjectId("4d95ea712b752328eb2fc2cd"), * * items: [ * { sku: "col-123", * name: "John Coltrane: Impressions", * price: 1099, * }, * * { sku: "young-456", * name: "Larry Young: Unity", * price: 1199 * } * ], * * address: { * street: "59 18th St.", * zip: 10010 * }, * * total: 2298 * } */ bson_init( &b ); bson_append_new_oid( &b, "_id" ); bson_append_new_oid( &b, "user_id" ); bson_append_start_array( &b, "items" ); bson_append_start_object( &b, "0" ); bson_append_string( &b, "name", "John Coltrane: Impressions" ); bson_append_int( &b, "price", 1099 ); bson_append_finish_object( &b ); bson_append_start_object( &b, "1" ); bson_append_string( &b, "name", "Larry Young: Unity" ); bson_append_int( &b, "price", 1199 ); bson_append_finish_object( &b ); bson_append_finish_object( &b ); bson_append_start_object( &b, "address" ); bson_append_string( &b, "street", "59 18th St." ); bson_append_int( &b, "zip", 10010 ); bson_append_finish_object( &b ); bson_append_int( &b, "total", 2298 ); /* Finish the BSON obj. */ bson_finish( &b ); printf("Here's the whole BSON object:\n"); bson_print( &b ); /* Advance to the 'items' array */ bson_find( &it, &b, "items" ); /* Get the subobject representing items */ bson_iterator_subobject_init( &it, &sub, 0 ); /* Now iterate that object */ printf("And here's the inner sub-object by itself.\n"); bson_print( &sub ); bson_destroy( &sub ); /* Now make a connection to MongoDB. */ if( mongo_client( &conn, "127.0.0.1", 27017 ) != MONGO_OK ) { switch( conn.err ) { case MONGO_CONN_SUCCESS: break; case MONGO_CONN_NO_SOCKET: printf( "FAIL: Could not create a socket!\n" ); break; case MONGO_CONN_FAIL: printf( "FAIL: Could not connect to mongod. Make sure it's listening at 127.0.0.1:27017.\n" ); break; default: printf( "MongoDB connection error number %d.\n", conn.err ); break; } exit( 1 ); } /* Insert the sample BSON document. */ if( mongo_insert( &conn, "test.records", &b, NULL ) != MONGO_OK ) { printf( "FAIL: Failed to insert document with error %d\n", conn.err ); exit( 1 ); } /* Query for the sample document. */ mongo_cursor_init( &cursor, &conn, "test.records" ); mongo_cursor_set_query( &cursor, bson_shared_empty() ); if( mongo_cursor_next( &cursor ) != MONGO_OK ) { printf( "FAIL: Failed to find inserted document." ); exit( 1 ); } printf( "Found saved BSON object:\n" ); bson_print( (bson *)mongo_cursor_bson( &cursor ) ); mongo_cmd_drop_collection( &conn, "test", "records", NULL ); mongo_cursor_destroy( &cursor ); bson_destroy( &b ); mongo_destroy( &conn ); return 0; }
/** * \brief This function tries to load tag group from MongoDB * * \param[in] *vs_ctx The verse server context * \param[in] *oid The pointer at ObjectID of tag group in MongoDB * \param[in] *node The node containing tag group * \param[in] taggroup_id The tag group ID that is requested from database * \param[in] version The version of tag group id that is requested * from database. When version is equal to -1, then * current version is loaded from MongoDB. * * \return This function returns pointer at tag group, when tag group is found. * Otherwise it returns NULL. */ struct VSTagGroup *vs_mongo_taggroup_load_linked(struct VS_CTX *vs_ctx, bson_oid_t *oid, struct VSNode *node, uint16 taggroup_id, uint32 req_version) { struct VSTagGroup *tg = NULL; bson query; mongo_cursor cursor; uint32 node_id = -1, tmp_taggroup_id = -1, current_version = -1, custom_type = -1; int found = 0; bson_iterator tg_data_iter; const bson *bson_tg; bson_init(&query); bson_append_oid(&query, "_id", oid); bson_finish(&query); mongo_cursor_init(&cursor, vs_ctx->mongo_conn, vs_ctx->mongo_tg_ns); mongo_cursor_set_query(&cursor, &query); /* ObjectID should be unique */ while( mongo_cursor_next(&cursor) == MONGO_OK ) { bson_tg = mongo_cursor_bson(&cursor); /* Try to get node id */ if( bson_find(&tg_data_iter, bson_tg, "node_id") == BSON_INT ) { node_id = bson_iterator_int(&tg_data_iter); } /* Try to get tag group id */ if( bson_find(&tg_data_iter, bson_tg, "taggroup_id") == BSON_INT ) { tmp_taggroup_id = bson_iterator_int(&tg_data_iter); } /* ObjectID is ALMOST unique. So it is check, if node id and * tag group id matches */ if(node_id == node->id && tmp_taggroup_id == taggroup_id) { found = 1; break; } } /* When tag group was found, then load required data from MongoDB */ if(found == 1) { /* Try to get current version of tag group */ if( bson_find(&tg_data_iter, bson_tg, "current_version") == BSON_INT ) { current_version = bson_iterator_int(&tg_data_iter); } /* Try to get custom type of tag group */ if( bson_find(&tg_data_iter, bson_tg, "custom_type") == BSON_INT ) { custom_type = bson_iterator_int(&tg_data_iter); } /* Create tag group with specific ID */ if((int)current_version != -1 && (int)custom_type != -1) { tg = vs_taggroup_create(node, taggroup_id, custom_type); if(tg != NULL) { tg->state = ENTITY_CREATED; /* Save ObjectID to tag group */ memcpy(&tg->oid, oid, sizeof(bson_oid_t)); /* Try to get versions of tag group */ if( bson_find(&tg_data_iter, bson_tg, "versions") == BSON_OBJECT ) { bson bson_versions; bson_iterator version_iter; char str_num[15]; /* Initialize sub-object of versions */ bson_iterator_subobject_init(&tg_data_iter, &bson_versions, 0); sprintf(str_num, "%u", req_version); /* Try to find required version of tag group */ if( bson_find(&version_iter, &bson_versions, str_num) == BSON_OBJECT ) { bson bson_version; /* Set version of tag group */ tg->version = tg->saved_version = current_version; bson_iterator_subobject_init(&version_iter, &bson_version, 0); /* Try to load tags */ vs_mongo_taggroup_load_data(tg, &bson_version); } } } } } bson_destroy(&query); mongo_cursor_destroy(&cursor); return tg; }
int main(int argc, char **argv) { bson b[1]; bson query[1]; mongo conn[1]; mongo_cursor cursor[1]; int result; if(mongo_client(conn, "127.0.0.1", 27017) != MONGO_OK) { switch(conn->err) { case MONGO_CONN_SUCCESS: fprintf(stderr, "OK: Connected to MongoDB!\n"); break; case MONGO_CONN_NO_SOCKET: fprintf(stderr, "FAIL: Cloud not create a socket!\n"); break; case MONGO_CONN_FAIL: fprintf(stderr, "FAIL: Could not connect to mongod!."); break; default: fprintf(stderr, "MongoDB connection error number: %d.\n", conn->err); } } bson_init(query); bson_append_string(query, "city_name", "南京"); bson_finish(query); mongo_cursor_init(cursor, conn, "bangboox.city_shop"); mongo_cursor_set_query(cursor, query); while(mongo_cursor_next(cursor) == MONGO_OK) { bson_print((bson *)mongo_cursor_bson(cursor)); } bson_init(b); bson_append_new_oid(b, "_id"); bson_append_new_oid(b, "record_id"); bson_append_start_array(b, "items"); bson_append_start_object(b, "0"); bson_append_string(b, "name", "roy.lieu"); bson_append_int(b, "age", 30); bson_append_finish_object(b); bson_append_start_object(b, "1"); bson_append_string(b, "name", "jimmy.chen"); bson_append_int(b, "age", 35); bson_append_finish_object(b); bson_append_finish_object(b); bson_append_start_object(b, "address"); bson_append_string(b, "stree", "Jufeng RD."); bson_append_int(b, "zip", 232044); bson_append_finish_object(b); bson_finish(b); //printf("\n\n"); //bson_print(b); if(mongo_insert(conn, "test.record", b, NULL) != MONGO_OK) { fprintf(stderr, "FAIL: Failed to insert document whth err: %d\n", conn->err); } return 0; }
static int resolve_block(struct inode * e, uint8_t hash[HASH_LEN], char * buf) { bson query; int res; mongo_cursor curs; bson_iterator i; bson_type bt; const char * key; mongo * conn = get_conn(); bson_init(&query); bson_append_binary(&query, "_id", 0, (char*)hash, 20); bson_finish(&query); mongo_cursor_init(&curs, conn, blocks_name); mongo_cursor_set_query(&curs, &query); mongo_cursor_set_limit(&curs, 1); res = mongo_cursor_next(&curs); bson_destroy(&query); if(res != MONGO_OK) { mongo_cursor_destroy(&curs); return -EIO; } bson_iterator_init(&i, mongo_cursor_bson(&curs)); size_t outsize, compsize = 0; const char * compdata = NULL; uint32_t offset = 0, size = 0; while((bt = bson_iterator_next(&i)) > 0) { key = bson_iterator_key(&i); if(strcmp(key, "data") == 0) { compsize = bson_iterator_bin_len(&i); compdata = bson_iterator_bin_data(&i); } else if(strcmp(key, "offset") == 0) offset = bson_iterator_int(&i); else if(strcmp(key, "size") == 0) size = bson_iterator_int(&i); } if(curs.err != MONGO_CURSOR_EXHAUSTED) { fprintf(stderr, "Error getting extents %d", curs.err); return -EIO; } if(!compdata) { fprintf(stderr, "No data in block?\n"); return -EIO; } outsize = MAX_BLOCK_SIZE; if((res = snappy_uncompress(compdata, compsize, buf + offset, &outsize)) != SNAPPY_OK) { fprintf(stderr, "Error uncompressing block %d\n", res); return -EIO; } if(offset > 0) memset(buf, 0, offset); compsize = outsize + offset; if(compsize < size) memset(buf + compsize, 0, size - compsize); mongo_cursor_destroy(&curs); return 0; }