void bson_print_raw( const char * data , int depth ){ bson_iterator i; const char * key; int temp; char oidhex[25]; bson_iterator_init( &i , data ); while ( bson_iterator_next( &i ) ){ bson_type t = bson_iterator_type( &i ); if ( t == 0 ) break; key = bson_iterator_key( &i ); for ( temp=0; temp<=depth; temp++ ) printf( "\t" ); printf( "%s : %d \t " , key , t ); switch ( t ){ case bson_int: printf( "%d" , bson_iterator_int( &i ) ); break; case bson_double: printf( "%f" , bson_iterator_double( &i ) ); break; case bson_bool: printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); break; case bson_string: printf( "%s" , bson_iterator_string( &i ) ); break; case bson_null: printf( "null" ); break; case bson_oid: bson_oid_to_string(bson_iterator_oid(&i), oidhex); printf( "%s" , oidhex ); break; case bson_object: case bson_array: printf( "\n" ); bson_print_raw( bson_iterator_value( &i ) , depth + 1 ); break; default: fprintf( stderr , "can't print type : %d\n" , t ); } printf( "\n" ); } }
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(>e_buf); bson_append_int(>e_buf, "$gte", start); bson_from_buffer(>e_bson, >e_buf); bson_append_bson(&query_buf, "n", >e_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); }
//------------------------------------------------------------------------------ int main( int argc, char * argv[] ){ mongo conn; if( mongo_client( &conn , TEST_SERVER, TEST_PORT ) != MONGO_OK ) { std::cout << "failed to connect\n"; return EXIT_FAILURE; } mongo_cursor cursor; mongo_cursor_init( &cursor, &conn, "test.test" ); char hex_oid[25]; while( mongo_cursor_next( &cursor ) == MONGO_OK ) { std::cout << "row:\n"; bson_iterator it; bson_iterator_init( &it, mongo_cursor_bson( &cursor ) ); while( bson_iterator_next( &it ) ) { std::cout << " " << bson_iterator_key( &it ) << " = "; switch( bson_iterator_type( &it ) ) { case BSON_DOUBLE: std::cout << "(double) " << bson_iterator_double( &it ) << std::endl; break; case BSON_INT: std::cout << "(int) " << bson_iterator_int( &it ) << std::endl; break; case BSON_STRING: std::cout << "(string) \"" << bson_iterator_string( &it ) << "\"\n"; break; case BSON_OID: bson_oid_to_string( bson_iterator_oid( &it ), hex_oid ); std::cout << "(oid) \"" << hex_oid << "\"\n"; break; case BSON_OBJECT: std::cout << "(subobject) {...}\n"; break; case BSON_ARRAY: std::cout << "(array) [...]\n"; break; case BSON_TIMESTAMP: std::cout << "(timestamp) [...]\n"; break; default: std::cout << "(type " << bson_iterator_type( &it ) << std::endl; break; } } std::cout << std::endl; } mongo_disconnect( &conn ); return EXIT_SUCCESS; }
void MongodbObject::EnableForEdit() { bson *newObject = new bson(); bson_init(newObject); bson_iterator it; bson_iterator_init(&it, objectData->object); while (bson_iterator_next(&it)) { const char * key = bson_iterator_key(&it); bson_type type = bson_iterator_type(&it); switch (type) { case BSON_STRING: bson_append_string(newObject, key, bson_iterator_string(&it)); break; case BSON_INT: bson_append_int(newObject, key, bson_iterator_int(&it)); break; case BSON_LONG: bson_append_long(newObject, key, bson_iterator_long(&it)); break; case BSON_DOUBLE: bson_append_double(newObject, key, bson_iterator_double(&it)); break; case BSON_OBJECT: bson sub; bson_iterator_subobject(&it, &sub); bson_append_bson(newObject, key, &sub); break; case BSON_OID: bson_append_oid(newObject, key, bson_iterator_oid(&it)); break; default: break; } } bson_destroy(objectData->object); SafeDelete(objectData->object); objectData->object = newObject; }
int read_inode(const bson * doc, struct inode * out) { bson_iterator i, sub; bson_type bt; const char * key; bson_iterator_init(&i, doc); while((bt = bson_iterator_next(&i)) > 0) { key = bson_iterator_key(&i); if(strcmp(key, "_id") == 0) memcpy(&out->oid, bson_iterator_oid(&i), sizeof(bson_oid_t)); else if(strcmp(key, "mode") == 0) out->mode = bson_iterator_int(&i); else if(strcmp(key, "owner") == 0) out->owner = bson_iterator_long(&i); else if(strcmp(key, "group") == 0) out->group = bson_iterator_long(&i); else if(strcmp(key, "size") == 0) out->size = bson_iterator_long(&i); else if(strcmp(key, "created") == 0) out->created = bson_iterator_time_t(&i); else if(strcmp(key, "modified") == 0) out->modified = bson_iterator_time_t(&i); else if(strcmp(key, "data") == 0) { out->datalen = bson_iterator_string_len(&i); out->data = malloc(out->datalen + 1); strcpy(out->data, bson_iterator_string(&i)); } else if(strcmp(key, "dirents") == 0) { while(out->dirents) { struct dirent * next = out->dirents->next; free(out->dirents); out->dirents = next; } bson_iterator_subiterator(&i, &sub); while((bt = bson_iterator_next(&sub)) > 0) { int len = bson_iterator_string_len(&sub); struct dirent * cde = malloc(sizeof(struct dirent) + len); if(!cde) return -ENOMEM; strcpy(cde->path, bson_iterator_string(&sub)); cde->len = bson_iterator_string_len(&sub); cde->next = out->dirents; out->dirents = cde; out->direntcount++; } } } return 0; }
void InitWith(bson *obj) { bson_iterator it; bson_iterator_init(&it, obj); while (bson_iterator_next(&it)) { const char * key = bson_iterator_key(&it); bson_type type = bson_iterator_type(&it); switch (type) { case BSON_STRING: bson_append_string(object, key, bson_iterator_string(&it)); break; case BSON_INT: bson_append_int(object, key, bson_iterator_int(&it)); break; case BSON_LONG: bson_append_long(object, key, bson_iterator_long(&it)); break; case BSON_DOUBLE: bson_append_double(object, key, bson_iterator_double(&it)); break; case BSON_OBJECT: { bson sub; bson_iterator_subobject(&it, &sub); bson_append_bson(object, key, &sub); break; } case BSON_OID: bson_append_oid(object, key, bson_iterator_oid(&it)); break; default: DVASSERT(false); Logger::Error("[MongodbObjectInternalData::InitWith] Not implemented type: %d", type); break; } } }
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( >e ); bson_append_int( >e, "$gte", start ); bson_finish( >e ); bson_append_bson( &query, "n", >e ); bson_destroy( >e ); } 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; }
bson gridfile_get_chunk( gridfile *gfile, int n ) { bson query; bson out; bson_iterator it; bson_oid_t id; 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 ); assert( mongo_find_one( gfile->gfs->client, gfile->gfs->chunks_ns, &query, NULL, &out ) == MONGO_OK ); bson_destroy( &query ); return out; }
bson gridfile_get_chunk(gridfile* gfile, int n) { bson query; bson out; bson_buffer buf; bson_iterator it; bson_oid_t id; bson_buffer_init(&buf); bson_find(&it, gfile->meta, "_id"); id = *bson_iterator_oid(&it); bson_append_oid(&buf, "files_id", &id); bson_append_int(&buf, "n", n); bson_from_buffer(&query, &buf); assert(mongo_find_one(gfile->gfs->client, gfile->gfs->chunks_ns, &query, NULL, &out)); return out; }
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); } }
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); } }
void decodeValue(v8::Local<v8::Object> obj, bson_iterator *it) { bson_type type = bson_iterator_type(it); const char *key = bson_iterator_key(it); switch (type) { case BSON_NULL: obj->Set(v8::String::NewFromUtf8(isolate, key), v8::Null(isolate)); break; case BSON_STRING: obj->Set(v8::String::NewFromUtf8(isolate, key), v8::String::NewFromUtf8(isolate, bson_iterator_string(it))); break; case BSON_BOOL: obj->Set(v8::String::NewFromUtf8(isolate, key), bson_iterator_bool(it) ? v8::True(isolate) : v8::False(isolate)); break; case BSON_INT: obj->Set(v8::String::NewFromUtf8(isolate, key), v8::Number::New(isolate, bson_iterator_int(it))); break; case BSON_LONG: { int64_t num = bson_iterator_long(it); if (num >= -2147483648ll && num <= 2147483647ll) { obj->Set(v8::String::NewFromUtf8(isolate, key), v8::Number::New(isolate, (double) num)); } else { obj_ptr<Int64> int64 = new Int64(num); obj->Set(v8::String::NewFromUtf8(isolate, key), int64->wrap()); } break; } case BSON_DOUBLE: obj->Set(v8::String::NewFromUtf8(isolate, key), v8::Number::New(isolate, bson_iterator_double(it))); break; case BSON_DATE: obj->Set(v8::String::NewFromUtf8(isolate, key), v8::Date::New(isolate, (double) bson_iterator_date(it))); break; case BSON_BINDATA: { obj_ptr<Buffer_base> buf = new Buffer( std::string(bson_iterator_bin_data(it), bson_iterator_bin_len(it))); obj->Set(v8::String::NewFromUtf8(isolate, key), buf->wrap()); break; } case BSON_OID: { obj_ptr<MongoID> oid = new MongoID(bson_iterator_oid(it)); obj->Set(v8::String::NewFromUtf8(isolate, key), oid->wrap()); break; } case BSON_REGEX: { v8::RegExp::Flags flgs = v8::RegExp::kNone; const char *opts = bson_iterator_regex_opts(it); char ch; while ((ch = *opts++) != 0) if (ch == 'm') flgs = (v8::RegExp::Flags) (flgs | v8::RegExp::kMultiline); else if (ch == 'g') flgs = (v8::RegExp::Flags) (flgs | v8::RegExp::kGlobal); else if (ch == 'i') flgs = (v8::RegExp::Flags) (flgs | v8::RegExp::kIgnoreCase); obj->Set(v8::String::NewFromUtf8(isolate, key), v8::RegExp::New(v8::String::NewFromUtf8(isolate, bson_iterator_regex(it)), flgs)); break; } case BSON_OBJECT: case BSON_ARRAY: { bson_iterator it1; bson_iterator_subiterator(it, &it1); obj->Set(v8::String::NewFromUtf8(isolate, key), decodeObject(&it1, type == BSON_ARRAY)); break; } default: printf("unknown type: %d\n", type); break; } }
INT32 _appendValue( CHAR delChar, bson_iterator *pIt, CHAR **ppBuffer, INT32 *pCSVSize, BOOLEAN includeBinary, BOOLEAN includeRegex ) { INT32 rc = SDB_OK ; bson_type type = bson_iterator_type( pIt ) ; INT32 tempSize = 0 ; INT32 base64Size = 0 ; INT32 binType = 0 ; CHAR temp[128] = { 0 } ; const CHAR *pTemp = NULL ; CHAR *pBase64 = NULL ; bson_timestamp_t ts; time_t timer ; struct tm psr; if ( type == BSON_DOUBLE || type == BSON_BOOL || type == BSON_NULL || type == BSON_INT || type == BSON_LONG ) { rc = _appendNonString( delChar, pIt, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } else { rc = _appendString( delChar, &delChar, 1, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } if ( type == BSON_TIMESTAMP ) { ts = bson_iterator_timestamp( pIt ) ; timer = (time_t)ts.t; local_time( &timer, &psr ) ; tempSize = ossSnprintf ( temp, 64, "%04d-%02d-%02d-%02d.%02d.%02d.%06d", psr.tm_year + 1900, psr.tm_mon + 1, psr.tm_mday, psr.tm_hour, psr.tm_min, psr.tm_sec, ts.i ) ; rc = _appendString( delChar, temp, tempSize, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } else if ( type == BSON_DATE ) { timer = bson_iterator_date( pIt ) / 1000 ; local_time( &timer, &psr ) ; tempSize = ossSnprintf ( temp, 64, "%04d-%02d-%02d", psr.tm_year + 1900, psr.tm_mon + 1, psr.tm_mday ) ; rc = _appendString( delChar, temp, tempSize, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } else if ( type == BSON_UNDEFINED ) { rc = _appendString( delChar, CSV_STR_UNDEFINED, CSV_STR_UNDEFINED_SIZE, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } else if ( type == BSON_MINKEY ) { rc = _appendString( delChar, CSV_STR_MINKEY, CSV_STR_MINKEY_SIZE, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } else if ( type == BSON_MAXKEY ) { rc = _appendString( delChar, CSV_STR_MAXKEY, CSV_STR_MAXKEY_SIZE, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } else if ( type == BSON_CODE ) { pTemp = bson_iterator_code( pIt ) ; rc = _appendString( delChar, pTemp, ossStrlen( pTemp ), ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } else if ( type == BSON_STRING || type == BSON_SYMBOL ) { pTemp = bson_iterator_string( pIt ) ; rc = _appendString( delChar, pTemp, ossStrlen( pTemp ), ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } else if ( type == BSON_BINDATA ) { if( TRUE == includeBinary ) { rc = _appendString( delChar, CSV_STR_LEFTBRACKET, 1, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } binType = (INT32)bson_iterator_bin_type( pIt ) ; tempSize = ossSnprintf ( temp, 64, "%d", binType ) ; rc = _appendString( delChar, &temp, tempSize, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } rc = _appendString( delChar, CSV_STR_RIGHTBRACKET, 1, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } pTemp = bson_iterator_bin_data( pIt ) ; tempSize = bson_iterator_bin_len ( pIt ) ; base64Size = getEnBase64Size ( tempSize ) ; pBase64 = (CHAR *)SDB_OSS_MALLOC( base64Size ) ; if( NULL == pBase64 ) { rc = SDB_OOM ; goto error ; } ossMemset( pBase64, 0, base64Size ) ; if ( !base64Encode( pTemp, tempSize, pBase64, base64Size ) ) { rc = SDB_OOM ; goto error ; } rc = _appendString( delChar, pBase64, base64Size - 1, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } else if ( type == BSON_REGEX ) { if( TRUE == includeRegex ) { rc = _appendString( delChar, CSV_STR_BACKSLASH, 1, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } pTemp = bson_iterator_regex( pIt ) ; rc = _appendString( delChar, pTemp, ossStrlen( pTemp ), ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } if( TRUE == includeRegex ) { rc = _appendString( delChar, CSV_STR_BACKSLASH, 1, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } pTemp = bson_iterator_regex_opts( pIt ) ; rc = _appendString( delChar, pTemp, ossStrlen( pTemp ), ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } } else if ( type == BSON_OID ) { bson_oid_to_string( bson_iterator_oid( pIt ), temp ) ; rc = _appendString( delChar, temp, 24, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } else { rc = _appendObj( delChar, pIt, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } rc = _appendString( delChar, &delChar, 1, ppBuffer, pCSVSize ) ; if ( rc ) { goto error ; } } done: SAFE_OSS_FREE( pBase64 ) ; return rc ; error: goto done ; }
void to_json(char *buf, const char * data, int depth){ int array_count = 0; int object_count = 0; bson_iterator i; const char * key; char oidhex[25]; bson_iterator_init( &i , data ); sprintf(buf+strlen(buf),"{"); while ( bson_iterator_next( &i ) ){ bson_type t = bson_iterator_type( &i ); if ( t == 0 ) break; key = bson_iterator_key( &i ); if(object_count > 0){sprintf(buf+strlen(buf),",");} else{object_count=1;} sprintf(buf+strlen(buf), "\"%s\":" , key ); switch ( t ){ case bson_int: sprintf(buf+strlen(buf), "%d" , bson_iterator_int( &i ) ); break; case bson_double: sprintf(buf+strlen(buf), "%f" , bson_iterator_double( &i ) ); break; case bson_bool: sprintf(buf+strlen(buf), "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); break; case bson_string: sprintf(buf+strlen(buf), "\"%s\"" , bson_iterator_string( &i ) ); break; case bson_null: sprintf( buf+strlen(buf),"null" ); break; case bson_oid: bson_oid_to_string(bson_iterator_oid(&i), oidhex); sprintf(buf+strlen(buf), "%s" , oidhex ); break; case bson_object: to_json(buf, bson_iterator_value( &i ) , depth + 1 ); break; case bson_array: sprintf(buf+strlen(buf), "[" ); bson_iterator j; bson_iterator_init( &j , bson_iterator_value(&i) ); array_count =0; while( bson_iterator_next(&j)){ if(array_count > 0){sprintf(buf+strlen(buf),",");} else{array_count=1;} to_json(buf, bson_iterator_value( &j ) , depth + 1 ); } sprintf(buf+strlen(buf), "]" ); break; default: fprintf( stderr , "can't print type : %d\n" , t ); } } sprintf(buf+strlen(buf),"}"); }
Tcl_Obj * mongotcl_bsontolist_raw (Tcl_Interp *interp, Tcl_Obj *listObj, const char *data , int depth) { bson_iterator i; const char *key; bson_timestamp_t ts; char oidhex[25]; bson scope; if (data == NULL) { return listObj; } bson_iterator_from_buffer(&i, data); while (bson_iterator_next (&i)) { bson_type t = bson_iterator_type (&i); if (t == 0) { break; } key = bson_iterator_key (&i); switch (t) { case BSON_DOUBLE: { append_list_type_object (interp, listObj, "double", key, Tcl_NewDoubleObj (bson_iterator_double (&i))); break; } case BSON_STRING: { append_list_type_object (interp, listObj, "string", key, Tcl_NewStringObj (bson_iterator_string (&i), -1)); break; } case BSON_SYMBOL: { append_list_type_object (interp, listObj, "symbol", key, Tcl_NewStringObj (bson_iterator_string (&i), -1)); break; } case BSON_OID: { bson_oid_to_string( bson_iterator_oid( &i ), oidhex ); append_list_type_object (interp, listObj, "oid", key, Tcl_NewStringObj (oidhex, -1)); break; } case BSON_BOOL: { append_list_type_object (interp, listObj, "bool", key, Tcl_NewBooleanObj (bson_iterator_bool (&i))); break; } case BSON_DATE: { append_list_type_object (interp, listObj, "date", key, Tcl_NewLongObj ((long) bson_iterator_date(&i))); break; } case BSON_BINDATA: { unsigned char *bindata = (unsigned char *)bson_iterator_bin_data (&i); int binlen = bson_iterator_bin_len (&i); append_list_type_object (interp, listObj, "bin", key, Tcl_NewByteArrayObj (bindata, binlen)); break; } case BSON_UNDEFINED: { append_list_type_object (interp, listObj, "undefined", key, Tcl_NewObj ()); break; } case BSON_NULL: { append_list_type_object (interp, listObj, "null", key, Tcl_NewObj ()); break; } case BSON_REGEX: { append_list_type_object (interp, listObj, "regex", key, Tcl_NewStringObj (bson_iterator_regex (&i), -1)); break; } case BSON_CODE: { append_list_type_object (interp, listObj, "code", key, Tcl_NewStringObj (bson_iterator_code (&i), -1)); break; } case BSON_CODEWSCOPE: { bson_printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) ); /* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */ bson_iterator_code_scope( &i, &scope ); bson_printf( "\n\t SCOPE: " ); bson_print( &scope ); /* bson_destroy( &scope ); */ /* review - causes free error */ break; } case BSON_INT: { append_list_type_object (interp, listObj, "int", key, Tcl_NewIntObj (bson_iterator_int (&i))); break; } case BSON_LONG: { append_list_type_object (interp, listObj, "long", key, Tcl_NewLongObj ((uint64_t)bson_iterator_long (&i))); break; } case BSON_TIMESTAMP: { char string[64]; ts = bson_iterator_timestamp (&i); snprintf(string, sizeof(string), "%d:%d", ts.i, ts.t); append_list_type_object (interp, listObj, "timestamp", key, Tcl_NewStringObj (bson_iterator_string (&i), -1)); break; } case BSON_ARRAY: { Tcl_Obj *subList = Tcl_NewObj (); subList = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1); append_list_type_object (interp, listObj, "array", key, subList); break; } case BSON_OBJECT: { Tcl_Obj *subList = Tcl_NewObj (); subList = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1); append_list_type_object (interp, listObj, "object", key, subList); break; } default: { append_list_type_object (interp, listObj, "unknown", key, Tcl_NewIntObj (t)); break; } } } return listObj; }
void bson_print_raw( const char *data , int depth ) { bson_iterator i; const char *key; int temp; bson_timestamp_t ts; char oidhex[25]; bson scope; bson_iterator_from_buffer( &i, data ); while ( bson_iterator_next( &i ) ) { bson_type t = bson_iterator_type( &i ); if ( t == 0 ) break; key = bson_iterator_key( &i ); for ( temp=0; temp<=depth; temp++ ) printf( "\t" ); bson_printf( "%s : %d \t " , key , t ); switch ( t ) { case BSON_DOUBLE: printf( "%f" , bson_iterator_double( &i ) ); break; case BSON_STRING: printf( "%s" , bson_iterator_string( &i ) ); break; case BSON_SYMBOL: printf( "SYMBOL: %s" , bson_iterator_string( &i ) ); break; case BSON_OID: bson_oid_to_string( bson_iterator_oid( &i ), oidhex ); printf( "%s" , oidhex ); break; case BSON_BOOL: printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); break; case BSON_DATE: printf( "%ld" , ( long int )bson_iterator_date( &i ) ); break; case BSON_BINDATA: printf( "BSON_BINDATA" ); break; case BSON_UNDEFINED: printf( "BSON_UNDEFINED" ); break; case BSON_NULL: printf( "BSON_NULL" ); break; case BSON_REGEX: printf( "BSON_REGEX: %s", bson_iterator_regex( &i ) ); break; case BSON_CODE: printf( "BSON_CODE: %s", bson_iterator_code( &i ) ); break; case BSON_CODEWSCOPE: printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) ); bson_init( &scope ); bson_iterator_code_scope( &i, &scope ); printf( "\n\t SCOPE: " ); bson_print( &scope ); break; case BSON_INT: printf( "%d" , bson_iterator_int( &i ) ); break; case BSON_LONG: printf( "%lld" , ( long long int )bson_iterator_long( &i ) ); break; case BSON_TIMESTAMP: ts = bson_iterator_timestamp( &i ); printf( "i: %d, t: %d", ts.i, ts.t ); break; case BSON_OBJECT: case BSON_ARRAY: printf( "\n" ); bson_print_raw( bson_iterator_value( &i ) , depth + 1 ); break; default: bson_errprintf( "can't print type : %d\n" , t ); } printf( "\n" ); } }
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; }
INLINE void bson_to_v P2(bson_iterator *, i, svalue_t *, v){ int temp; const char *cp; char oidhex[25]; bson scope; bson_type t; bson_timestamp_t ts; t = bson_iterator_type( i ); switch ( t ) { case BSON_DOUBLE: v->type = T_REAL; v->u.real = bson_iterator_double( i ); break; case BSON_STRING: cp = bson_iterator_string( i ); v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy(cp,"bson_to_v:1"); break; case BSON_SYMBOL: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("SYMBOL","bson_to_v:2"); break; case BSON_OID: bson_oid_to_string( bson_iterator_oid( i ), oidhex ); v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy(oidhex,"bson_to_v:3"); break; case BSON_BOOL: v->type = T_NUMBER; v->u.number = ( long )bson_iterator_bool( i ); break; case BSON_DATE: v->type = T_NUMBER; v->u.number = ( long )bson_iterator_date( i ); break; case BSON_BINDATA: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_BINDATA","bson_to_v:4"); break; case BSON_UNDEFINED: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_UNDEFINED","bson_to_v:5"); break; case BSON_NULL: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_NULL","bson_to_v:6"); break; case BSON_REGEX: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_REGEX","bson_to_v:7"); break; case BSON_CODE: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_CODE","bson_to_v:8"); break; case BSON_CODEWSCOPE: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_CODEWSCOPE","bson_to_v:9"); break; case BSON_INT: v->type = T_NUMBER; v->u.number = ( long )bson_iterator_int( i ); break; case BSON_LONG: v->type = T_NUMBER; v->u.number = ( long )bson_iterator_long( i ); break; case BSON_TIMESTAMP: ts = bson_iterator_timestamp( i ); v->type = T_NUMBER; v->u.number = ( long )ts.t; break; case BSON_OBJECT: bson_to_mapping( bson_iterator_value( i ) , v); break; case BSON_ARRAY: bson_to_array( bson_iterator_value( i ) , v); break; default: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("UNKOWN_TYPE","bson_to_v:10"); break; } }
char* Namespace::bson_to_json(char* val, const char *data, int depth, bool isObject, int bsonSize, int &jsonSize, int &cursor) { char *object; int bufSize; char keybuf[512]; char buf[512]; int c = 0; bson_iterator i; const char *key; int temp; bson_timestamp_t ts; char oidhex[25]; bson scope; bson_iterator_from_buffer(&i, data); //bson_iterator_init(&i, data); if (isObject) { memcpy(&val[cursor], "{", 1); cursor += 1; } else { memcpy(&val[cursor], "[", 1); cursor += 1; } while (bson_iterator_next(&i) && i.progress < bsonSize) { if (c > 0) { memcpy(&val[cursor], ",", 1); cursor += 1; } c++; bson_type t = bson_iterator_type(&i); if (t == 0) { break; } key = bson_iterator_key(&i); if (key[0] != '\0') { sprintf(keybuf, "\"%s\":", key); key = &keybuf[0]; } switch (t) { case BSON_DOUBLE: sprintf(buf, "%s%f", key, bson_iterator_double(&i)); bufSize = strlen(buf); memcpy(&val[cursor], buf, bufSize); cursor += bufSize; break; case BSON_STRING: sprintf(buf, "%s\"%s\"", key, bson_iterator_string(&i)); bufSize = strlen(buf); memcpy(&val[cursor], buf, bufSize); cursor += bufSize; break; case BSON_OID: bson_oid_to_string(bson_iterator_oid(&i), oidhex); sprintf(buf, "%s\"%s\"", key, oidhex); bufSize = strlen(buf); memcpy(&val[cursor], buf, bufSize); cursor += bufSize; break; case BSON_BOOL: sprintf(buf, "%s%s", key, bson_iterator_bool(&i) ? "true" : "false"); bufSize = strlen(buf); memcpy(&val[cursor], buf, bufSize); cursor += bufSize; break; case BSON_NULL: sprintf(buf, "%snull", key); bufSize = strlen(buf); memcpy(&val[cursor], buf, bufSize); cursor += bufSize; break; case BSON_INT: sprintf(buf, "%s%d", key, bson_iterator_int(&i)); bufSize = strlen(buf); memcpy(&val[cursor], buf, bufSize); cursor += bufSize; break; case BSON_OBJECT: sprintf(buf, "%s\0", key); memcpy(&val[cursor], buf, strlen(buf)); cursor += strlen(buf); object = bson_to_json(val, bson_iterator_value(&i), depth + 1, true, bsonSize - cursor, jsonSize, cursor); break; case BSON_ARRAY: sprintf(buf, "%s", key); bufSize = strlen(buf); memcpy(&val[cursor], buf, bufSize); cursor += bufSize; object = bson_to_json(val, bson_iterator_value(&i), depth + 1, false, bsonSize - cursor, jsonSize, cursor); break; default: LOGE("can't print type"); FORCE_LOG_INT("type: ", t); memcpy(&val[cursor], "}", 1); cursor += 1; jsonSize = cursor; return val; } } if (isObject) { memcpy(&val[cursor], "}", 1); cursor += 1; memcpy(&val[cursor], "\0", 1); } else { memcpy(&val[cursor], "]", 1); cursor += 1; } jsonSize = cursor; return val; }
static char * bson_to_json_recurse(StringInfo buf, const char *data, bool is_object) { bson_iterator i; const char *key; char oidhex[25]; char *str; bool first = true; bson_iterator_from_buffer(&i, data); while (bson_iterator_next(&i)) { bson_type t = bson_iterator_type(&i); if (t == 0) break; if (!first) appendStringInfoChar(buf, ','); first = false; if (is_object) { key = bson_iterator_key(&i); appendStringInfo(buf, "\"%s\"", key); appendStringInfoChar(buf, ':'); } switch (t) { case BSON_DOUBLE: appendStringInfo(buf, "%f", bson_iterator_double(&i)); break; case BSON_STRING: str = quote_string(bson_iterator_string(&i)); appendStringInfoString(buf, str); break; case BSON_OID: bson_oid_to_string(bson_iterator_oid(&i), oidhex); str = quote_string(oidhex); appendStringInfoString(buf, str); break; case BSON_BOOL: appendStringInfoString(buf, bson_iterator_bool(&i) ? "true" : "false"); break; case BSON_INT: appendStringInfo(buf, "%d", bson_iterator_int(&i)); break; case BSON_LONG: appendStringInfo(buf, "%lld", (uint64_t) bson_iterator_long(&i)); break; case BSON_NULL: appendStringInfoString(buf, "null"); break; case BSON_OBJECT: appendStringInfoChar(buf, '{'); bson_to_json_recurse(buf, bson_iterator_value(&i), true); appendStringInfoChar(buf, '}'); break; case BSON_ARRAY: appendStringInfoChar(buf, '['); bson_to_json_recurse(buf, bson_iterator_value(&i), false); appendStringInfoChar(buf, ']'); break; case BSON_DATE: case BSON_TIMESTAMP: case BSON_SYMBOL: case BSON_BINDATA: case BSON_UNDEFINED: case BSON_REGEX: case BSON_CODE: case BSON_CODEWSCOPE: ereport(ERROR, (errmsg("unsupported bson type: %d", t))); break; default: elog(ERROR, "unknown bson type: %d", t); break; } } return buf->data; }
void lua_push_bson_value(lua_State *L, bson_iterator *it) { bson_type bt = bson_iterator_type(it); switch (bt) { case BSON_OID: { char xoid[25]; bson_oid_to_string(bson_iterator_oid(it), xoid); lua_pushstring(L, xoid); break; } case BSON_STRING: case BSON_SYMBOL: lua_pushstring(L, bson_iterator_string(it)); break; case BSON_NULL: case BSON_UNDEFINED: lua_push_bsontype_table(L, bt); break; case BSON_INT: lua_pushinteger(L, bson_iterator_int(it)); break; case BSON_LONG: case BSON_DOUBLE: lua_pushnumber(L, (lua_Number) bson_iterator_double(it)); break; case BSON_BOOL: lua_pushboolean(L, bson_iterator_bool(it)); break; case BSON_OBJECT: case BSON_ARRAY: { bson_iterator nit; bson_iterator_subiterator(it, &nit); if (bt == BSON_OBJECT) { lua_push_bson_table(L, &nit); } else { lua_push_bson_array(L, &nit); } break; } case BSON_DATE: { lua_push_bsontype_table(L, bt); lua_pushnumber(L, bson_iterator_date(it)); lua_rawseti(L, -2, 1); break; } case BSON_BINDATA: { lua_push_bsontype_table(L, bt); lua_pushlstring(L, bson_iterator_bin_data(it), bson_iterator_bin_len(it)); break; } case BSON_REGEX: { const char *re = bson_iterator_regex(it); const char *ro = bson_iterator_regex_opts(it); lua_push_bsontype_table(L, bt); lua_pushstring(L, re); lua_rawseti(L, -2, 1); lua_pushstring(L, ro); lua_rawseti(L, -2, 2); break; } default: break; } }
static void bson_print_xstr(TCXSTR* xstr, const char *data, int depth) { bson_iterator i; const char *key; int temp; bson_timestamp_t ts; char oidhex[25]; bson scope; bson_iterator_from_buffer(&i, data); while (bson_iterator_next(&i)) { bson_type t = bson_iterator_type(&i); if (t == 0) break; key = bson_iterator_key(&i); for (temp = 0; temp <= depth; temp++) { tcxstrprintf(xstr, "."); } tcxstrprintf(xstr, "%s(%d)=", key, t); switch (t) { case BSON_DOUBLE: tcxstrprintf(xstr, "%f", bson_iterator_double(&i)); break; case BSON_STRING: tcxstrprintf(xstr, "%s", bson_iterator_string(&i)); break; case BSON_SYMBOL: tcxstrprintf(xstr, "SYMBOL: %s", bson_iterator_string(&i)); break; case BSON_OID: bson_oid_to_string(bson_iterator_oid(&i), oidhex); tcxstrprintf(xstr, "%s", oidhex); break; case BSON_BOOL: tcxstrprintf(xstr, "%s", bson_iterator_bool(&i) ? "true" : "false"); break; case BSON_DATE: tcxstrprintf(xstr, "%lld", (uint64_t) bson_iterator_long(&i)); break; case BSON_BINDATA: tcxstrprintf(xstr, "BSON_BINDATA"); break; case BSON_UNDEFINED: tcxstrprintf(xstr, "BSON_UNDEFINED"); break; case BSON_NULL: tcxstrprintf(xstr, "BSON_NULL"); break; case BSON_REGEX: tcxstrprintf(xstr, "BSON_REGEX: %s", bson_iterator_regex(&i)); break; case BSON_CODE: tcxstrprintf(xstr, "BSON_CODE: %s", bson_iterator_code(&i)); break; case BSON_CODEWSCOPE: tcxstrprintf(xstr, "BSON_CODE_W_SCOPE: %s", bson_iterator_code(&i)); /* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */ bson_iterator_code_scope(&i, &scope); tcxstrprintf(xstr, "\n SCOPE: "); bson_print_xstr(xstr, scope.data, 0); /* bson_destroy( &scope ); */ /* review - causes free error */ break; case BSON_INT: tcxstrprintf(xstr, "%d", bson_iterator_int(&i)); break; case BSON_LONG: tcxstrprintf(xstr, "%lld", (uint64_t) bson_iterator_long(&i)); break; case BSON_TIMESTAMP: ts = bson_iterator_timestamp(&i); tcxstrprintf(xstr, "i: %d, t: %d", ts.i, ts.t); break; case BSON_OBJECT: case BSON_ARRAY: tcxstrprintf(xstr, "\n"); bson_print_xstr(xstr, bson_iterator_value(&i), depth + 1); break; default: fprintf(stderr, "can't print type : %d\n", t); } tcxstrprintf(xstr, "\n"); } }
int mongotcl_bsontoarray_raw (Tcl_Interp *interp, char *arrayName, char *typeArrayName, const char *data , int depth) { bson_iterator i; const char *key; bson_timestamp_t ts; char oidhex[25]; Tcl_Obj *obj; char *type; if (data == NULL) { return TCL_OK; } bson_iterator_from_buffer(&i, data); while (bson_iterator_next (&i)) { bson_type t = bson_iterator_type (&i); if (t == 0) { break; } key = bson_iterator_key (&i); switch (t) { case BSON_DOUBLE: { obj = Tcl_NewDoubleObj (bson_iterator_double (&i)); type = "double"; break; } case BSON_SYMBOL: { obj = Tcl_NewStringObj (bson_iterator_string (&i), -1); type = "symbol"; break; } case BSON_STRING: { obj = Tcl_NewStringObj (bson_iterator_string (&i), -1); type = "string"; break; } case BSON_OID: { bson_oid_to_string( bson_iterator_oid( &i ), oidhex ); obj = Tcl_NewStringObj (oidhex, -1); type = "oid"; break; } case BSON_BOOL: { obj = Tcl_NewBooleanObj (bson_iterator_bool (&i)); type = "bool"; break; } case BSON_DATE: { obj = Tcl_NewLongObj ((long) bson_iterator_date(&i)); type = "date"; break; } case BSON_BINDATA: { unsigned char *bindata = (unsigned char *)bson_iterator_bin_data (&i); int binlen = bson_iterator_bin_len (&i); obj = Tcl_NewByteArrayObj (bindata, binlen); type = "bin"; break; } case BSON_UNDEFINED: { obj = Tcl_NewObj (); type = "undefined"; break; } case BSON_NULL: { obj = Tcl_NewObj (); type = "null"; break; } case BSON_REGEX: { obj = Tcl_NewStringObj (bson_iterator_regex (&i), -1); type = "regex"; break; } case BSON_CODE: { obj = Tcl_NewStringObj (bson_iterator_code (&i), -1); type = "code"; break; } case BSON_CODEWSCOPE: { // bson_printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) ); /* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */ // bson_iterator_code_scope( &i, &scope ); // bson_printf( "\n\t SCOPE: " ); // bson_print( &scope ); /* bson_destroy( &scope ); */ /* review - causes free error */ break; } case BSON_INT: { obj = Tcl_NewIntObj (bson_iterator_int (&i)); type = "int"; break; } case BSON_LONG: { obj = Tcl_NewLongObj ((uint64_t)bson_iterator_long (&i)); type = "long"; break; } case BSON_TIMESTAMP: { char string[64]; ts = bson_iterator_timestamp (&i); snprintf(string, sizeof(string), "%d:%d", ts.i, ts.t); obj = Tcl_NewStringObj (bson_iterator_string (&i), -1); type = "timestamp"; break; } case BSON_ARRAY: { obj = Tcl_NewObj(); obj = mongotcl_bsontolist_raw (interp, obj, bson_iterator_value (&i), depth + 1); type = "array"; break; } case BSON_OBJECT: { Tcl_Obj *subList = Tcl_NewObj (); obj = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1); type = "object"; break; } default: { obj = Tcl_NewIntObj (t); type = "unknown"; break; } } if (Tcl_SetVar2Ex (interp, arrayName, key, obj, TCL_LEAVE_ERR_MSG) == NULL) { return TCL_ERROR; } if (typeArrayName != NULL) { if (Tcl_SetVar2Ex (interp, typeArrayName, key, Tcl_NewStringObj (type, -1), TCL_LEAVE_ERR_MSG) == NULL) { return TCL_ERROR; } } } return TCL_OK; }
static void *threadrace1(void *_tr) { const int iterations = 500; TARGRACE *tr = (TARGRACE*) _tr; bool err = false; bson bq; bson_init_as_query(&bq); bson_append_int(&bq, "tid", tr->id); bson_finish(&bq); bson_type bt; bson_iterator it; void *bsdata; bool saved = false; int lastcnt = 0; EJCOLL *coll = ejdbcreatecoll(jb, "threadrace1", NULL); CU_ASSERT_PTR_NOT_NULL_FATAL(coll); EJQ *q = ejdbcreatequery(jb, &bq, NULL, 0, NULL); TCXSTR *log = tcxstrnew(); for (int i = 0; !err && i < iterations; ++i) { CU_ASSERT_PTR_NOT_NULL_FATAL(q); tcxstrclear(log); bson_oid_t oid2; bson_oid_t *oid = NULL; int cnt = 0; uint32_t count; TCLIST *res = NULL; if (ejdbecode(jb) != 0) { eprint(jb, __LINE__, "threadrace1"); err = true; goto ffinish; } res = ejdbqryexecute(coll, q, &count, 0, log); if (ejdbecode(jb) != 0) { eprint(jb, __LINE__, "threadrace1.ejdbqryexecute"); err = true; goto ffinish; } if (count != 1 && saved) { fprintf(stderr, "%d:COUNT=%d it=%d\n", tr->id, count, i); CU_ASSERT_TRUE(false); goto ffinish; } if (count > 0) { bsdata = TCLISTVALPTR(res, 0); CU_ASSERT_PTR_NOT_NULL_FATAL(bsdata); bt = bson_find_from_buffer(&it, bsdata, "cnt"); CU_ASSERT_EQUAL_FATAL(bt, BSON_INT); cnt = bson_iterator_int(&it); bt = bson_find_from_buffer(&it, bsdata, "_id"); CU_ASSERT_EQUAL_FATAL(bt, BSON_OID); oid = bson_iterator_oid(&it); CU_ASSERT_PTR_NOT_NULL_FATAL(oid); } bson sbs; bson_init(&sbs); if (oid) { bson_append_oid(&sbs, "_id", oid); } bson_append_int(&sbs, "tid", tr->id); bson_append_int(&sbs, "cnt", ++cnt); bson_finish(&sbs); if (!ejdbsavebson(coll, &sbs, &oid2)) { eprint(jb, __LINE__, "threadrace1.ejdbsavebson"); err = true; } saved = true; bson_destroy(&sbs); lastcnt = cnt; ffinish: if (res) tclistdel(res); } if (q) ejdbquerydel(q); if (log) tcxstrdel(log); bson_destroy(&bq); CU_ASSERT_EQUAL(lastcnt, iterations); //fprintf(stderr, "\nThread %d finished", tr->id); return err ? "error" : NULL; }
int main(){ mongo_connection conn[1]; bson_buffer bb; bson obj; bson cond; int i; bson_oid_t oid; const char* col = "c.update_test"; const char* ns = "test.c.update_test"; INIT_SOCKETS_FOR_WINDOWS; if (mongo_connect( conn , TEST_SERVER, 27017 )){ 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_OK && mongo_find_one(conn, ns, bson_empty(&obj), bson_empty(&obj), NULL) != MONGO_OK ){ printf("failed to drop collection\n"); exit(1); } bson_oid_gen(&oid); { /* insert */ bson_buffer_init(&bb); bson_append_oid(&bb, "_id", &oid); bson_append_int(&bb, "a", 3 ); bson_from_buffer(&obj, &bb); mongo_insert(conn, ns, &obj); bson_destroy(&obj); } { /* insert */ bson op; bson_buffer_init(&bb); bson_append_oid(&bb, "_id", &oid); bson_from_buffer(&cond, &bb); bson_buffer_init(&bb); { bson_append_start_object(&bb, "$inc"); bson_append_int(&bb, "a", 2 ); bson_append_finish_object(&bb); } { bson_append_start_object(&bb, "$set"); bson_append_double(&bb, "b", -1.5 ); bson_append_finish_object(&bb); } bson_from_buffer(&op, &bb); for (i=0; i<5; i++) mongo_update(conn, ns, &cond, &op, 0); /* cond is used later */ bson_destroy(&op); } if( mongo_find_one(conn, ns, &cond, 0, &obj) != MONGO_OK ){ printf("Failed to find object\n"); exit(1); } else { int fields = 0; bson_iterator it; bson_iterator_init(&it, obj.data); bson_destroy(&cond); while(bson_iterator_next(&it)){ switch(bson_iterator_key(&it)[0]){ case '_': /* id */ ASSERT(bson_iterator_type(&it) == BSON_OID); ASSERT(!memcmp(bson_iterator_oid(&it)->bytes, oid.bytes, 12)); fields++; break; case 'a': ASSERT(bson_iterator_type(&it) == BSON_INT); ASSERT(bson_iterator_int(&it) == 3 + 5*2); fields++; break; case 'b': ASSERT(bson_iterator_type(&it) == BSON_DOUBLE); ASSERT(bson_iterator_double(&it) == -1.5); fields++; break; } } ASSERT(fields == 3); } bson_destroy(&obj); mongo_cmd_drop_db(conn, "test"); mongo_destroy(conn); return 0; }
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; }
int main(int argc, char *argv[]) { bson_buffer bb; bson b; bson_iterator it, it2, it3; bson_oid_t oid; bson_buffer_init(&bb); bson_append_double(&bb, "d", 3.14); bson_append_string(&bb, "s", "hello"); { bson_buffer *obj = bson_append_start_object(&bb, "o"); bson_buffer *arr = bson_append_start_array(obj, "a"); bson_append_binary(arr, "0", 8, "w\0rld", 5); bson_append_finish_object(arr); bson_append_finish_object(obj); } bson_append_undefined(&bb, "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(&bb, "oid", &oid); bson_append_bool(&bb, "b", 1); bson_append_date(&bb, "date", 0x0102030405060708ULL); bson_append_null(&bb, "n"); bson_append_regex(&bb, "r", "^asdf", "imx"); /* no dbref test (deprecated) */ bson_append_code(&bb, "c", "function(){}"); bson_append_symbol(&bb, "symbol", "SYMBOL"); { bson_buffer scope_buf; bson scope; bson_buffer_init(&scope_buf); bson_append_int(&scope_buf, "i", 123); bson_from_buffer(&scope, &scope_buf); bson_append_code_w_scope(&bb, "cws", "function(){return i}", &scope); bson_destroy(&scope); } /* no timestamp test (internal) */ bson_append_long(&bb, "l", 0x1122334455667788ULL); bson_from_buffer(&b, &bb); bson_iterator_init(&it, b.data); 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(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) == 0x0102030405060708ULL); 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_key(&it), "c")); ASSERT(!strcmp(bson_iterator_string(&it), "function(){}")); 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_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.data); 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_long); ASSERT(bson_iterator_type(&it) == bson_long); ASSERT(!strcmp(bson_iterator_key(&it), "l")); ASSERT(bson_iterator_long(&it) == 0x1122334455667788ULL); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_eoo); ASSERT(bson_iterator_type(&it) == bson_eoo); ASSERT(!bson_iterator_more(&it)); return 0; }
EXPORT void mongo_bson_iterator_oid(struct bson_iterator_* i, void* oid) { memcpy(oid, bson_iterator_oid((bson_iterator*) i), 12); }