MONGO_EXPORT const char *bson_iterator_string( const bson_iterator *i ) { check_mongo_object( (void*)i ); switch ( bson_iterator_type( i ) ) { case BSON_STRING: case BSON_SYMBOL: return bson_iterator_value( i ) + 4; default: return ""; } }
bson_type bson_iterator_next( bson_iterator * i ){ int ds; if ( i->first ){ i->first = 0; return (bson_type)(*i->cur); } switch ( bson_iterator_type(i) ){ case bson_eoo: return bson_eoo; /* don't advance */ case bson_undefined: case bson_null: ds = 0; break; case bson_bool: ds = 1; break; case bson_int: ds = 4; break; case bson_long: case bson_double: case bson_timestamp: case bson_date: ds = 8; break; case bson_oid: ds = 12; break; case bson_string: case bson_symbol: case bson_code: ds = 4 + bson_iterator_int_raw(i); break; case bson_bindata: ds = 5 + bson_iterator_int_raw(i); break; case bson_object: case bson_array: case bson_codewscope: ds = bson_iterator_int_raw(i); break; case bson_dbref: ds = 4+12 + bson_iterator_int_raw(i); break; case bson_regex: { const char * s = bson_iterator_value(i); const char * p = s; p += strlen(p)+1; p += strlen(p)+1; ds = p-s; break; } default: { char msg[] = "unknown type: 000000000000"; bson_numstr(msg+14, (unsigned)(i->cur[0])); bson_fatal_msg(0, msg); return 0; } } i->cur += 1 + strlen( i->cur + 1 ) + 1 + ds; return (bson_type)(*i->cur); }
int srld_find(bson_iterator *i, int keyc, char **keyv, char *data, int depth) { bson_iterator_init(i, data); while(bson_iterator_next(i)){ if (strcmp(keyv[depth], bson_iterator_key(i)) == 0) { //printf("%s == %s, depth %d\n", keyv[depth], bson_iterator_key(i), depth); if (depth == keyc-1) { return 1; } else { return srld_find(i, keyc, keyv, (char *)bson_iterator_value(i), depth+1); } } } return 0; }
static void mongo_replset_check_seed( mongo_connection* conn ) { bson out; bson hosts; const char* data; bson_iterator it; bson_iterator it_sub; const char* host_string; mongo_host_port *host_port = NULL; out.data = NULL; out.owned = 1; hosts.data = NULL; hosts.owned = 1; if( mongo_simple_int_command(conn, "admin", "ismaster", 1, &out) == MONGO_OK ) { if( bson_find( &it, &out, "hosts" ) ) { data = bson_iterator_value( &it ); bson_iterator_init( &it_sub, data ); /* Iterate over host list, adding each host to the * connection's host list. */ while( bson_iterator_next( &it_sub ) ) { host_string = bson_iterator_string( &it_sub ); host_port = bson_malloc( sizeof( mongo_host_port ) ); mongo_parse_host( host_string, host_port ); if( host_port ) { mongo_replset_add_node( &conn->replset->hosts, host_port->host, host_port->port ); free( host_port ); host_port = NULL; } } } } bson_destroy( &out ); bson_destroy( &hosts ); mongo_close_socket( conn->sock ); conn->sock = 0; conn->connected = 0; }
bson_buffer * bson_append_element( bson_buffer * b, const char * name_or_null, const bson_iterator* elem){ bson_iterator next = *elem; int size; bson_iterator_next(&next); size = next.cur - elem->cur; if (name_or_null == NULL){ bson_ensure_space(b, size); bson_append(b, elem->cur, size); }else{ int data_size = size - 2 - strlen(bson_iterator_key(elem)); bson_append_estart(b, elem->cur[0], name_or_null, data_size); bson_append(b, bson_iterator_value(elem), data_size); } return b; }
MONGO_EXPORT int bson_append_element( bson *b, const char *name_or_null, const bson_iterator *elem ) { bson_iterator next = *elem; size_t size; bson_iterator_next( &next ); size = next.cur - elem->cur; if ( name_or_null == NULL ) { if( bson_ensure_space( b, size ) == BSON_ERROR ) return BSON_ERROR; bson_append( b, elem->cur, size ); } else { size_t data_size = size - 2 - strlen( bson_iterator_key( elem ) ); bson_append_estart( b, elem->cur[0], name_or_null, data_size ); bson_append( b, bson_iterator_value( elem ), data_size ); } return BSON_OK; }
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_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_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: fprintf( stderr , "can't print type : %d\n" , t ); } printf( "\n" ); } }
bson_bool_t bson_iterator_bool_raw( const bson_iterator *i ) { return bson_iterator_value( i )[0]; }
int64_t bson_iterator_long_raw( const bson_iterator *i ) { int64_t out; bson_little_endian64( &out, bson_iterator_value( i ) ); return out; }
double bson_iterator_double_raw( const bson_iterator *i ) { double out; bson_little_endian64( &out, bson_iterator_value( i ) ); return out; }
int bson_iterator_int_raw( const bson_iterator *i ) { int out; bson_little_endian32( &out, bson_iterator_value( i ) ); return out; }
void bson_iterator_subiterator( const bson_iterator *i, bson_iterator *sub ) { bson_iterator_from_buffer( sub, bson_iterator_value( i ) ); }
const char *bson_iterator_regex_opts( const bson_iterator *i ) { const char *p = bson_iterator_value( i ); return p + strlen( p ) + 1; }
const char *bson_iterator_bin_data( const bson_iterator *i ) { return ( bson_iterator_bin_type( i ) == BSON_BIN_BINARY_OLD ) ? bson_iterator_value( i ) + 9 : bson_iterator_value( i ) + 5; }
const char *bson_iterator_string( const bson_iterator *i ) { return bson_iterator_value( i ) + 4; }
bson_oid_t *bson_iterator_oid( const bson_iterator *i ) { return ( bson_oid_t * )bson_iterator_value( i ); }
const char* BsonObj::GetValue(const char *key) const { bson_iterator *i; bson_find(i, this->obj, key); const char *value = bson_iterator_value(i); return value; }
MONGO_EXPORT int bson_iterator_timestamp_increment( const bson_iterator *i ) { int increment; bson_little_endian32( &increment, bson_iterator_value( i ) ); return increment; }
MONGO_EXPORT int bson_iterator_timestamp_time( const bson_iterator *i ) { int time; bson_little_endian32( &time, bson_iterator_value( i ) + 4 ); return time; }
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),"}"); }
bson_timestamp_t bson_iterator_timestamp( const bson_iterator *i ) { bson_timestamp_t ts; bson_little_endian32( &( ts.i ), bson_iterator_value( i ) ); bson_little_endian32( &( ts.t ), bson_iterator_value( i ) + 4 ); return ts; }
const char * bson_iterator_bin_data( const bson_iterator * i ){ return bson_iterator_value( i ) + 5; }
char bson_iterator_bin_type( const bson_iterator *i ) { return bson_iterator_value( i )[4]; }
void bson_iterator_subobject(const bson_iterator * i, bson * sub){ bson_init(sub, (char*)bson_iterator_value(i), 0); }
const char *bson_iterator_regex( const bson_iterator *i ) { return bson_iterator_value( i ); }
void bson_iterator_subiterator(const bson_iterator * i, bson_iterator * sub){ bson_iterator_init(sub, bson_iterator_value(i)); }
void bson_iterator_subobject( const bson_iterator *i, bson *sub ) { bson_init_data( sub, ( char * )bson_iterator_value( i ) ); _bson_reset( sub ); sub->finished = 1; }
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" ); } }
const char *gridfile_get_field( gridfile *gfile, const char *name ) { bson_iterator it; bson_find( &it, gfile->meta, name ); return bson_iterator_value( &it ); }
bson_type bson_iterator_next( bson_iterator *i ) { int ds; if ( i->first ) { i->first = 0; return ( bson_type )( *i->cur ); } switch ( bson_iterator_type( i ) ) { case BSON_EOO: return BSON_EOO; /* don't advance */ case BSON_UNDEFINED: case BSON_NULL: ds = 0; break; case BSON_BOOL: ds = 1; break; case BSON_INT: ds = 4; break; case BSON_LONG: case BSON_DOUBLE: case BSON_TIMESTAMP: case BSON_DATE: ds = 8; break; case BSON_OID: ds = 12; break; case BSON_STRING: case BSON_SYMBOL: case BSON_CODE: ds = 4 + bson_iterator_int_raw( i ); break; case BSON_BINDATA: ds = 5 + bson_iterator_int_raw( i ); break; case BSON_OBJECT: case BSON_ARRAY: case BSON_CODEWSCOPE: ds = bson_iterator_int_raw( i ); break; case BSON_DBREF: ds = 4+12 + bson_iterator_int_raw( i ); break; case BSON_REGEX: { const char *s = bson_iterator_value( i ); const char *p = s; p += strlen( p )+1; p += strlen( p )+1; ds = p-s; break; } default: { char msg[] = "unknown type: 000000000000"; bson_numstr( msg+14, ( unsigned )( i->cur[0] ) ); bson_fatal_msg( 0, msg ); return 0; } } i->cur += 1 + strlen( i->cur + 1 ) + 1 + ds; return ( bson_type )( *i->cur ); }