/* * cursor,err = db:query(ns, query) */ int cursor_create(lua_State *L, mongo *connection, const char *ns, const Query *query, int nToReturn, int nToSkip, const QueryCursor *fieldsToReturn, int queryOptions, int batchSize) { MongoCursor **cursor = (MongoCursor **) lua_newuserdata(L, sizeof(MongoCursor *)); *cursor = (MongoCursor *)malloc(sizeof(MongoCursor)); mongo_cursor_init( &(*cursor)->cursor, connection, ns ); // mongo_cursor_set_options(*cursor, queryOptions); // mongo_cursor_set_skip( &(*cursor)->cursor, nToSkip ); // mongo_cursor_set_limit( &(*cursor)->cursor, nToReturn ); bson_copy(&(*cursor)->fields, fieldsToReturn->condition); // mongo_cursor_set_fields( (*cursor)->cursor, fieldsToReturn->condition ); bson_copy(&(*cursor)->query, query->condition); mongo_cursor_set_query( &(*cursor)->cursor, &(*cursor)->query ); // // while(mongo_cursor_next(&(*cursor)->cursor) == MONGO_OK) { // printf("iterator1: \t"); // bson_print(&((*cursor)->cursor.current)); // } luaL_getmetatable(L, LUAMONGO_CURSOR); lua_setmetatable(L, -2); return 1; }
EXPORT void mongo_bson_empty(struct bson_** b) { bson empty; bson* b_ = (bson*)malloc(sizeof(bson)); bson_empty(&empty); bson_copy(b_, &empty); *b = (struct bson_*)b_; }
EXPORT void mongo_bson_iterator_code_scope(struct bson_iterator_* i, struct bson_buffer** b) { bson* _b = (bson*)malloc(sizeof(bson)); bson scope; bson_iterator_code_scope((bson_iterator*) i, &scope); bson_copy(_b, &scope); *b = (struct bson_buffer*)_b; }
void _mongoc_cursor_cursorid_init_with_reply (mongoc_cursor_t *cursor, bson_t *reply, uint32_t server_id) { mongoc_cursor_cursorid_t *cid; cursor->sent = true; cursor->server_id = server_id; cid = (mongoc_cursor_cursorid_t *)cursor->iface_data; BSON_ASSERT (cid); bson_destroy (&cid->array); if (!bson_steal (&cid->array, reply)) { bson_steal (&cid->array, bson_copy (reply)); } if (!_mongoc_cursor_cursorid_start_batch (cursor)) { bson_set_error (&cursor->error, MONGOC_ERROR_CURSOR, MONGOC_ERROR_CURSOR_INVALID_CURSOR, "Couldn't parse cursor document"); } }
mongoc_cursor_t * _mongoc_cursor_cmd_new_from_reply (mongoc_client_t *client, const bson_t *cmd, const bson_t *opts, bson_t *reply) { mongoc_cursor_t *cursor = _mongoc_cursor_cmd_new (client, NULL, cmd, opts, NULL, NULL, NULL); data_cmd_t *data = (data_cmd_t *) cursor->impl.data; data->reading_from = CMD_RESPONSE; cursor->state = IN_BATCH; bson_destroy (&data->response.reply); if (!bson_steal (&data->response.reply, reply)) { bson_destroy (&data->response.reply); BSON_ASSERT (bson_steal (&data->response.reply, bson_copy (reply))); } if (!_mongoc_cursor_start_reading_response (cursor, &data->response)) { bson_set_error (&cursor->error, MONGOC_ERROR_CURSOR, MONGOC_ERROR_CURSOR_INVALID_CURSOR, "Couldn't parse cursor document"); } return cursor; }
int gridfile_init( gridfs *gfs, bson *meta, gridfile *gfile ) { gfile->gfs = gfs; gfile->pos = 0; gfile->meta = ( bson * )bson_malloc( sizeof( bson ) ); if ( gfile->meta == NULL ) return MONGO_ERROR; bson_copy( gfile->meta, meta ); return MONGO_OK; }
int gridfile_init(gridfs* gfs, bson* meta, gridfile* gfile) { gfile->gfs = gfs; gfile->pos = 0; gfile->meta = (bson*)malloc(sizeof(bson)); if (gfile->meta == NULL) return FALSE; bson_copy(gfile->meta, meta); return TRUE; }
int mongo_gridfile_get_metadata(struct gridfile_* gf, struct bson_** out) { bson meta, *b; gridfile_get_metadata((gridfile*)gf, &meta); if (bson_size(&meta) <= 5) return 0; b = (bson*)malloc(sizeof(bson)); bson_copy(b, &meta); *out = (struct bson_*) b; return 1; }
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; } }
static void test_bson_copy (void) { bson_t b; bson_t *c; bson_init(&b); assert(bson_append_int32(&b, "foobar", -1, 1234)); c = bson_copy(&b); assert_bson_equal(&b, c); bson_destroy(c); bson_destroy(&b); }
bool mongoc_find_and_modify_opts_set_fields (mongoc_find_and_modify_opts_t *opts, const bson_t *fields) { BSON_ASSERT (opts); if (fields) { _mongoc_bson_destroy_if_set (opts->fields); opts->fields = bson_copy (fields); return true; } return false; }
bool mongoc_find_and_modify_opts_set_update (mongoc_find_and_modify_opts_t *opts, const bson_t *update) { BSON_ASSERT (opts); if (update) { _mongoc_bson_destroy_if_set (opts->update); opts->update = bson_copy (update); return true; } return false; }
bool mongoc_find_and_modify_opts_set_sort (mongoc_find_and_modify_opts_t *opts, const bson_t *sort) { BSON_ASSERT (opts); if (sort) { _mongoc_bson_destroy_if_set (opts->sort); opts->sort = bson_copy (sort); return true; } return false; }
static bool with_transaction_callback_runner (mongoc_client_session_t *session, void *ctx, bson_t **reply, bson_error_t *error) { cb_ctx_t *cb_ctx = (cb_ctx_t *) ctx; bson_t local_reply; bson_t operation; bson_t operations; bson_t *test; bson_iter_t iter; bool res = false; test = &(cb_ctx->callback); if (bson_has_field (test, "operation")) { bson_lookup_doc (test, "operation", &operation); res = json_test_operation (cb_ctx->ctx, test, &operation, cb_ctx->ctx->collection, session, &local_reply); } else { ASSERT (bson_has_field (test, "operations")); bson_lookup_doc (test, "operations", &operations); BSON_ASSERT (bson_iter_init (&iter, &operations)); bson_init (&local_reply); while (bson_iter_next (&iter)) { bson_destroy (&local_reply); bson_iter_bson (&iter, &operation); res = json_test_operation (cb_ctx->ctx, test, &operation, cb_ctx->ctx->collection, session, &local_reply); if (!res) { break; } } } *reply = bson_copy (&local_reply); bson_destroy (&local_reply); return res; }
int 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) == MONGO_OK){ bson_copy(out, &cursor->current); mongo_cursor_destroy(cursor); return MONGO_OK; } else{ mongo_cursor_destroy(cursor); return MONGO_ERROR; } }
int main(int argc, char *argv[]) { const char * test_server = (argc > 1 ? argv[1] : TEST_SERVER); bson_iterator it[1], it2[1]; bson b[1]; bson sub[1]; bson copy[1]; bson_type type; bson_init( b ); bson_append_string( b, "foo", "hello" ); { bson_append_start_object( b, "o" ); bson_append_string( b, "bar", "goodbye" ); bson_append_finish_object( b ); } bson_iterator_init( it, b ); bson_iterator_next( it ); type = bson_iterator_next( it ); ASSERT( BSON_OBJECT == type ); bson_iterator_subobject_init( it, sub, 0 ); ASSERT( sub->finished == 1 ); bson_iterator_init( it2, sub ); type = bson_iterator_next( it2 ); ASSERT( BSON_STRING == type ); type = bson_iterator_next( it2 ); ASSERT( BSON_EOO == type ); bson_copy( copy, sub ); ASSERT( 1 == copy->finished ); ASSERT( 0 == copy->stackPos ); ASSERT( 0 == copy->err ); bson_destroy( copy ); bson_destroy( b ); return 0; }
uint8_t XTDBInsert(XTDBHandle* handle,bson* newVal) { _S_FN(insert); handle->gen++; bson_oid_t oid; bson_oid_gen(&oid); BinaryStr key,val; uint8_t ret; bson copyObj; handle->lastOp = XTDB_OP_INSERT; key.data = oid.bytes; key.len = sizeof(oid.bytes); if (bson_copy(©Obj,newVal)) { handle->error = XTDB_INVALID_BSON; return False; } bson_unfinish_object(©Obj); if (bson_append_oid(©Obj,"_id",&oid)) { handle->error = XTDB_NO_MEM; bson_destroy(©Obj); return False; } if (bson_finish(©Obj)) { handle->error = XTDB_NO_MEM; bson_destroy(©Obj); return False; } //bson_print(©Obj); BsonToBStr(©Obj,&val); if (XTDBInsertToIndex(handle,&key,&val) == False) { assert(0); return False; } ret = DBSet(handle->mainDB,&key,&val,False); _E_FN(insert); if (!ret) { handle->error = DBGetLastError(handle->mainDB); } else { memcpy(&handle->status.insertStatus.newOid,&oid,sizeof(oid)); } return ret; }
int test_copy_cursor_data( mongo *conn ) { mongo_cursor cursor[1]; bson b[1]; insert_sample_data( conn, 10 ); mongo_cursor_init( cursor, conn, "test.cursors" ); mongo_cursor_next( cursor ); ASSERT( bson_copy( b, mongo_cursor_bson( cursor ) ) == MONGO_OK ); ASSERT( memcmp( (void *)b->data, (void *)(cursor->current).data, bson_size( &cursor->current ) ) == 0 ); mongo_cursor_destroy( cursor ); bson_destroy( b ); return 0; }
static void append_documents_from_cmd (const mongoc_cmd_t *cmd, mongoc_apm_command_started_t *event) { int32_t doc_len; bson_t doc; const uint8_t *pos; const char *field_name; bson_t bson; char str[16]; const char *key; uint32_t i; if (!cmd->payload || !cmd->payload_size) { return; } if (!event->command_owned) { event->command = bson_copy (event->command); event->command_owned = true; } /* make array from outgoing OP_MSG payload type 1 on an "insert", * "update", or "delete" command. */ field_name = _mongoc_get_documents_field_name (cmd->command_name); BSON_ASSERT (field_name); BSON_ASSERT (BSON_APPEND_ARRAY_BEGIN (event->command, field_name, &bson)); pos = cmd->payload; i = 0; while (pos < cmd->payload + cmd->payload_size) { memcpy (&doc_len, pos, sizeof (doc_len)); doc_len = BSON_UINT32_FROM_LE (doc_len); BSON_ASSERT (bson_init_static (&doc, pos, (size_t) doc_len)); bson_uint32_to_string (i, &key, str, sizeof (str)); BSON_APPEND_DOCUMENT (&bson, key, &doc); pos += doc_len; i++; } bson_append_array_end (event->command, &bson); }
uint8_t XTDBBsonAppendOid(XTDBHandle* handle,bson* in , bson* out,bson_oid_t * oid) { if (bson_copy(out,in)) { handle->error = XTDB_INVALID_BSON; return False; } bson_unfinish_object(out); if (bson_append_oid(out,"_id",oid)) { handle->error = XTDB_NO_MEM; bson_destroy(out); return False; } if (bson_finish(out)) { handle->error = XTDB_NO_MEM; bson_destroy(out); return False; } return True; }
MONGO_EXPORT void gridfile_get_chunk( gridfile *gfile, int n, bson* out ) { bson query; bson_iterator it; bson_oid_t id; int result; bson_init( &query ); bson_find( &it, gfile->meta, "_id" ); id = *bson_iterator_oid( &it ); bson_append_oid( &query, "files_id", &id ); bson_append_int( &query, "n", n ); bson_finish( &query ); result = (mongo_find_one(gfile->gfs->client, gfile->gfs->chunks_ns, &query, NULL, out ) == MONGO_OK ); bson_destroy( &query ); if (!result) { bson empty; bson_empty(&empty); bson_copy(out, &empty); } }
static int Encode(lua_State *L){ bson* b = bson_encode(L); if(b == NULL){ LOG_ERROR("null"); return -1; } bson_print(b); bson* bson_field = (bson*) lua_newuserdata(L,sizeof(bson)); if(bson_field == NULL) { LOG_ERROR("null"); return -1; } int ret = bson_copy(bson_field,b); if(ret != 0) { LOG_ERROR("copy fail"); } //bson_print(bson_field); luaL_getmetatable(L,"bson"); lua_setmetatable(L,-2); bson_destroy(b); return 1; }
int test_bson_generic( void ) { bson_iterator it, it2, it3; bson_oid_t oid; bson_timestamp_t ts; bson_timestamp_t ts_result; bson b[1]; bson copy[1]; bson scope[1]; ts.i = 1; ts.t = 2; bson_init( b ); bson_append_double( b, "d", 3.14 ); bson_append_string( b, "s", "hello" ); bson_append_string_n( b, "s_n", "goodbye cruel world", 7 ); { bson_append_start_object( b, "o" ); bson_append_start_array( b, "a" ); bson_append_binary( b, "0", 8, "w\0rld", 5 ); bson_append_finish_object( b ); bson_append_finish_object( b ); } bson_append_undefined( b, "u" ); bson_oid_from_string( &oid, "010203040506070809101112" ); ASSERT( !memcmp( oid.bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12 ) ); bson_append_oid( b, "oid", &oid ); bson_append_bool( b, "b", 1 ); bson_append_date( b, "date", 0x0102030405060708 ); bson_append_null( b, "n" ); bson_append_regex( b, "r", "^asdf", "imx" ); /* no dbref test (deprecated) */ bson_append_code( b, "c", "function(){}" ); bson_append_code_n( b, "c_n", "function(){}garbage", 12 ); bson_append_symbol( b, "symbol", "symbol" ); bson_append_symbol_n( b, "symbol_n", "symbol and garbage", 6 ); { bson_init( scope ); bson_append_int( scope, "i", 123 ); bson_finish( scope ); bson_append_code_w_scope( b, "cws", "function(){return i}", scope ); bson_destroy( scope ); } bson_append_timestamp( b, "timestamp", &ts ); bson_append_long( b, "l", 0x1122334455667788 ); /* Ensure that we can't copy a non-finished object. */ ASSERT( bson_copy( copy, b ) == BSON_ERROR ); bson_finish( b ); ASSERT( b->err == BSON_VALID ); /* Test append after finish. */ ASSERT( bson_append_string( b, "foo", "bar" ) == BSON_ERROR ); ASSERT( b->err & BSON_ALREADY_FINISHED ); ASSERT( bson_copy( copy, b ) == BSON_OK ); ASSERT( 1 == copy->finished ); ASSERT( 0 == copy->err ); bson_destroy( copy ); bson_print( b ); bson_iterator_init( &it, b ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( bson_iterator_type( &it ) == BSON_DOUBLE ); ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) ); ASSERT( bson_iterator_double( &it ) == 3.14 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_STRING ); ASSERT( bson_iterator_type( &it ) == BSON_STRING ); ASSERT( !strcmp( bson_iterator_key( &it ), "s" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "hello" ) ); ASSERT( strcmp( bson_iterator_string( &it ), "" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_STRING ); ASSERT( bson_iterator_type( &it ) == BSON_STRING ); ASSERT( !strcmp( bson_iterator_key( &it ), "s_n" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "goodbye" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_OBJECT ); ASSERT( bson_iterator_type( &it ) == BSON_OBJECT ); ASSERT( !strcmp( bson_iterator_key( &it ), "o" ) ); bson_iterator_subiterator( &it, &it2 ); ASSERT( bson_iterator_more( &it2 ) ); ASSERT( bson_iterator_next( &it2 ) == BSON_ARRAY ); ASSERT( bson_iterator_type( &it2 ) == BSON_ARRAY ); ASSERT( !strcmp( bson_iterator_key( &it2 ), "a" ) ); bson_iterator_subiterator( &it2, &it3 ); ASSERT( bson_iterator_more( &it3 ) ); ASSERT( bson_iterator_next( &it3 ) == BSON_BINDATA ); ASSERT( bson_iterator_type( &it3 ) == BSON_BINDATA ); ASSERT( !strcmp( bson_iterator_key( &it3 ), "0" ) ); ASSERT( bson_iterator_bin_type( &it3 ) == 8 ); ASSERT( bson_iterator_bin_len( &it3 ) == 5 ); ASSERT( !memcmp( bson_iterator_bin_data( &it3 ), "w\0rld", 5 ) ); ASSERT( bson_iterator_more( &it3 ) ); ASSERT( bson_iterator_next( &it3 ) == BSON_EOO ); ASSERT( bson_iterator_type( &it3 ) == BSON_EOO ); ASSERT( !bson_iterator_more( &it3 ) ); ASSERT( bson_iterator_more( &it2 ) ); ASSERT( bson_iterator_next( &it2 ) == BSON_EOO ); ASSERT( bson_iterator_type( &it2 ) == BSON_EOO ); ASSERT( !bson_iterator_more( &it2 ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_UNDEFINED ); ASSERT( bson_iterator_type( &it ) == BSON_UNDEFINED ); ASSERT( !strcmp( bson_iterator_key( &it ), "u" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_OID ); ASSERT( bson_iterator_type( &it ) == BSON_OID ); ASSERT( !strcmp( bson_iterator_key( &it ), "oid" ) ); ASSERT( !memcmp( bson_iterator_oid( &it )->bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12 ) ); ASSERT( bson_iterator_oid( &it )->ints[0] == oid.ints[0] ); ASSERT( bson_iterator_oid( &it )->ints[1] == oid.ints[1] ); ASSERT( bson_iterator_oid( &it )->ints[2] == oid.ints[2] ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_BOOL ); ASSERT( bson_iterator_type( &it ) == BSON_BOOL ); ASSERT( !strcmp( bson_iterator_key( &it ), "b" ) ); ASSERT( bson_iterator_bool( &it ) == 1 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DATE ); ASSERT( bson_iterator_type( &it ) == BSON_DATE ); ASSERT( !strcmp( bson_iterator_key( &it ), "date" ) ); ASSERT( bson_iterator_date( &it ) == 0x0102030405060708 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_NULL ); ASSERT( bson_iterator_type( &it ) == BSON_NULL ); ASSERT( !strcmp( bson_iterator_key( &it ), "n" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_REGEX ); ASSERT( bson_iterator_type( &it ) == BSON_REGEX ); ASSERT( !strcmp( bson_iterator_key( &it ), "r" ) ); ASSERT( !strcmp( bson_iterator_regex( &it ), "^asdf" ) ); ASSERT( !strcmp( bson_iterator_regex_opts( &it ), "imx" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_CODE ); ASSERT( bson_iterator_type( &it ) == BSON_CODE ); ASSERT( !strcmp( bson_iterator_code(&it), "function(){}") ); ASSERT( !strcmp( bson_iterator_key( &it ), "c" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_CODE ); ASSERT( bson_iterator_type( &it ) == BSON_CODE ); ASSERT( !strcmp( bson_iterator_key( &it ), "c_n" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "" ) ); ASSERT( !strcmp( bson_iterator_code( &it ), "function(){}" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_SYMBOL ); ASSERT( bson_iterator_type( &it ) == BSON_SYMBOL ); ASSERT( !strcmp( bson_iterator_key( &it ), "symbol" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "symbol" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_SYMBOL ); ASSERT( bson_iterator_type( &it ) == BSON_SYMBOL ); ASSERT( !strcmp( bson_iterator_key( &it ), "symbol_n" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "symbol" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_CODEWSCOPE ); ASSERT( bson_iterator_type( &it ) == BSON_CODEWSCOPE ); ASSERT( !strcmp( bson_iterator_key( &it ), "cws" ) ); ASSERT( !strcmp( bson_iterator_code( &it ), "function(){return i}" ) ); { bson scope; bson_iterator_code_scope( &it, &scope ); bson_iterator_init( &it2, &scope ); ASSERT( bson_iterator_more( &it2 ) ); ASSERT( bson_iterator_next( &it2 ) == BSON_INT ); ASSERT( bson_iterator_type( &it2 ) == BSON_INT ); ASSERT( !strcmp( bson_iterator_key( &it2 ), "i" ) ); ASSERT( bson_iterator_int( &it2 ) == 123 ); ASSERT( bson_iterator_more( &it2 ) ); ASSERT( bson_iterator_next( &it2 ) == BSON_EOO ); ASSERT( bson_iterator_type( &it2 ) == BSON_EOO ); ASSERT( !bson_iterator_more( &it2 ) ); } ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_TIMESTAMP ); ASSERT( bson_iterator_type( &it ) == BSON_TIMESTAMP ); ASSERT( !strcmp( bson_iterator_key( &it ), "timestamp" ) ); ts_result = bson_iterator_timestamp( &it ); ASSERT( ts_result.i == 1 ); ASSERT( ts_result.t == 2 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_LONG ); ASSERT( bson_iterator_type( &it ) == BSON_LONG ); ASSERT( !strcmp( bson_iterator_key( &it ), "l" ) ); ASSERT( bson_iterator_long( &it ) == 0x1122334455667788 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_EOO ); ASSERT( bson_iterator_type( &it ) == BSON_EOO ); ASSERT( !bson_iterator_more( &it ) ); bson_destroy( b ); { bson bsrc[1]; bson_init( bsrc ); bson_append_double( bsrc, "d", 3.14 ); bson_finish( bsrc ); ASSERT( bsrc->err == BSON_VALID ); bson_init( b ); bson_append_double( b, "", 3.14 ); /* test empty name (in general) */ bson_iterator_init( &it, bsrc ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( bson_iterator_type( &it ) == BSON_DOUBLE ); bson_append_element( b, "d", &it ); bson_append_element( b, 0, &it ); /* test null */ bson_append_element( b, "", &it ); /* test empty name */ bson_finish( b ); ASSERT( b->err == BSON_VALID ); /* bson_print( b ); */ bson_iterator_init( &it, b ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( !strcmp( bson_iterator_key( &it ), "" ) ); ASSERT( bson_iterator_double( &it ) == 3.14 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) ); ASSERT( bson_iterator_double( &it ) == 3.14 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) ); ASSERT( bson_iterator_double( &it ) == 3.14 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( !strcmp( bson_iterator_key( &it ), "" ) ); ASSERT( bson_iterator_double( &it ) == 3.14 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_EOO ); ASSERT( !bson_iterator_more( &it ) ); bson_destroy( bsrc ); bson_destroy( b ); } return 0; }
bool _mongoc_client_recv_gle (mongoc_client_t *client, uint32_t hint, bson_t **gle_doc, bson_error_t *error) { mongoc_buffer_t buffer; mongoc_rpc_t rpc; bson_iter_t iter; bool ret = false; bson_t b; ENTRY; bson_return_val_if_fail (client, false); bson_return_val_if_fail (hint, false); if (gle_doc) { *gle_doc = NULL; } _mongoc_buffer_init (&buffer, NULL, 0, NULL, NULL); if (!_mongoc_cluster_try_recv (&client->cluster, &rpc, &buffer, hint, error)) { GOTO (cleanup); } if (rpc.header.opcode != MONGOC_OPCODE_REPLY) { bson_set_error (error, MONGOC_ERROR_PROTOCOL, MONGOC_ERROR_PROTOCOL_INVALID_REPLY, "Received message other than OP_REPLY."); GOTO (cleanup); } if (_mongoc_rpc_reply_get_first (&rpc.reply, &b)) { if (gle_doc) { *gle_doc = bson_copy (&b); } if ((rpc.reply.flags & MONGOC_REPLY_QUERY_FAILURE)) { _bson_to_error (&b, error); bson_destroy (&b); GOTO (cleanup); } if (!bson_iter_init_find (&iter, &b, "ok") || BSON_ITER_HOLDS_DOUBLE (&iter)) { if (bson_iter_double (&iter) == 0.0) { _bson_to_error (&b, error); } } bson_destroy (&b); } ret = true; cleanup: _mongoc_buffer_destroy (&buffer); RETURN (ret); }
TBson::TBson(const TBsonObject *bson) { bsonData = (bson) ? bson_copy((const bson_t *)bson) : bson_new(); }
TBson::TBson(const TBson &other) { bsonData = bson_copy(other.bsonData); }
void InitFinished(bson *obj) { bson_copy(object, obj); bson_finish(object); }
INT32 main ( INT32 argc, CHAR **argv ) { // initialize local variables CHAR *pHostName = NULL ; CHAR *pServiceName = NULL ; CHAR *pUsr = NULL ; CHAR *pPasswd = NULL ; // define a connetion handle; use to connect to database sdbConnectionHandle connection = 0 ; // define a collection space handle sdbCSHandle collectionspace = 0 ; // define a collection handle sdbCollectionHandle collection = 0 ; // define a cursor handle for query sdbCursorHandle cursor = 0 ; sdbCursorHandle cursor1 = 0 ; // define local variables // initialize them before use INT32 rc = SDB_OK ; bson obj ; bson rule ; bson record ; bson updatecondition ; bson tmp ; bson_iterator it ; // verify syntax if ( 5 != argc ) { displaySyntax ( (CHAR*)argv[0] ) ; exit ( 0 ) ; } // read argument pHostName = (CHAR*)argv[1] ; pServiceName = (CHAR*)argv[2] ; pUsr = (CHAR*)argv[3] ; pPasswd = (CHAR*)argv[4] ; // connect to database rc = sdbConnect ( pHostName, pServiceName, pUsr, pPasswd, &connection ) ; if( rc!=SDB_OK ) { printf("Failed to connet to database, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // create collection space rc = sdbCreateCollectionSpace ( connection, COLLECTION_SPACE_NAME, SDB_PAGESIZE_4K, &collectionspace ) ; if( rc!=SDB_OK ) { printf("Failed to create collection space, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // create collection in a specified colletion space. rc = sdbCreateCollection ( collectionspace, COLLECTION_NAME, &collection ) ; if( rc!=SDB_OK ) { printf("Failed to create collection, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // prepare record bson_init( &record ) ; bson_append_int( &record, "age", 10 ) ; rc = bson_finish( &record ) ; CHECK_RC ( rc, "Failed to build bson" ) ; // insert record into database rc = sdbInsert( collection, &record ) ; if( rc!=SDB_OK ) { printf("Failed to insert, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } bson_destroy( &record ) ; // query all the record in this collection rc = sdbQuery(collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ; if( rc!=SDB_OK ) { printf("Failed to query, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // get the record from cursor bson_init(&obj) ; bson_init(&tmp) ; rc=sdbNext( cursor, &obj ) ; if ( rc!= SDB_OK ) { printf("Failed to get next, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } rc = bson_copy( &tmp, &obj ) ; CHECK_RC ( rc, "Failed to copy bson" ) ; printf("Before update, the record is:\n") ; bson_print( &tmp ) ; // set the update condition using "_id" bson_find( &it, &obj, "_id" ) ; bson_init( &updatecondition ) ; bson_append_element( &updatecondition, NULL, &it ) ; rc = bson_finish( &updatecondition ) ; CHECK_RC ( rc, "Failed to build bson" ) ; // set the update rule bson_init( &rule ) ; bson_append_start_object ( &rule, "$set" ) ; bson_append_int ( &rule, "age", 99 ) ; bson_append_finish_object ( &rule ) ; rc = bson_finish ( &rule ) ; CHECK_RC ( rc, "Failed to build bson" ) ; // update rc = sdbUpdate(collection, &rule, &updatecondition, NULL ) ; if ( rc!=SDB_OK ) { printf("Failed to update, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } bson_destroy(&obj) ; bson_destroy(&rule) ; bson_destroy(&updatecondition) ; // query all the record in this collection again rc = sdbQuery(collection, NULL, NULL, NULL, NULL, 0, -1, &cursor1 ) ; if( rc!=SDB_OK ) { printf("Failed to query, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // get record from cursor1 bson_init(&obj) ; rc=sdbNext( cursor1, &obj ) ; if ( rc!= SDB_OK ) { printf("Failed to get next, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } printf("after update, the record is:\n") ; bson_print( &obj ) ; bson_destroy(&obj) ; done: // drop collection space rc = sdbDropCollectionSpace( connection, COLLECTION_SPACE_NAME ) ; if ( rc != SDB_OK ) { printf("Failed to drop collection space, rc = %d" OSS_NEWLINE, rc ) ; } // disconnect the connection sdbDisconnect ( connection ) ; // release the local variables sdbReleaseCursor ( cursor ) ; sdbReleaseCursor ( cursor1 ) ; sdbReleaseCollection ( collection ) ; sdbReleaseCS ( collectionspace ) ; sdbReleaseConnection ( connection ) ; return 0; error: goto done ; }
bool _mongoc_client_recv_gle (mongoc_client_t *client, mongoc_server_stream_t *server_stream, bson_t **gle_doc, bson_error_t *error) { mongoc_buffer_t buffer; mongoc_rpc_t rpc; bson_iter_t iter; bool ret = false; bson_t b; ENTRY; BSON_ASSERT (client); BSON_ASSERT (server_stream); if (gle_doc) { *gle_doc = NULL; } _mongoc_buffer_init (&buffer, NULL, 0, NULL, NULL); if (!mongoc_cluster_try_recv (&client->cluster, &rpc, &buffer, server_stream, error)) { mongoc_topology_invalidate_server (client->topology, server_stream->sd->id); GOTO (cleanup); } if (rpc.header.opcode != MONGOC_OPCODE_REPLY) { bson_set_error (error, MONGOC_ERROR_PROTOCOL, MONGOC_ERROR_PROTOCOL_INVALID_REPLY, "Received message other than OP_REPLY."); GOTO (cleanup); } if (_mongoc_rpc_reply_get_first (&rpc.reply, &b)) { if ((rpc.reply.flags & MONGOC_REPLY_QUERY_FAILURE)) { _bson_to_error (&b, error); bson_destroy (&b); GOTO (cleanup); } if (gle_doc) { *gle_doc = bson_copy (&b); } if (!bson_iter_init_find (&iter, &b, "ok") || BSON_ITER_HOLDS_DOUBLE (&iter)) { if (bson_iter_double (&iter) == 0.0) { _bson_to_error (&b, error); } } bson_destroy (&b); ret = true; } cleanup: _mongoc_buffer_destroy (&buffer); RETURN (ret); }
EXPORT void mongo_gridfile_get_descriptor(struct gridfile_* gf, struct bson_** out) { bson* b = (bson*)malloc(sizeof(bson)); bson_copy(b, ((gridfile*)gf)->meta); *out = (struct bson_*) b; }