bson_buffer * bson_ensure_space( bson_buffer * b , const int bytesNeeded ){ int pos = b->cur - b->buf; char * orig = b->buf; int new_size; if (b->finished) { bson_fatal_msg(!!b->buf, "trying to append to finished buffer"); return NULL; } if (pos + bytesNeeded <= b->bufSize) return b; new_size = 1.5 * (b->bufSize + bytesNeeded); b->buf = realloc(b->buf, new_size); if (!b->buf) { bson_fatal_msg(!!b->buf, "realloc() failed"); return NULL; } b->bufSize = new_size; b->cur += b->buf - orig; return b; }
int bson_ensure_space( bson *b, const size_t bytesNeeded ) { size_t pos = _bson_position(b); char *orig = b->data; int new_size; if ( pos + bytesNeeded <= (size_t) b->dataSize ) return BSON_OK; new_size = (int) ( 1.5 * ( b->dataSize + bytesNeeded ) ); if( new_size < b->dataSize ) { if( ( b->dataSize + bytesNeeded ) < INT_MAX ) new_size = INT_MAX; else { b->err = BSON_SIZE_OVERFLOW; return BSON_ERROR; } } if ( ! b->ownsData ) { b->err = BSON_DOES_NOT_OWN_DATA; return BSON_ERROR; } b->data = bson_realloc( b->data, new_size ); if ( !b->data ) bson_fatal_msg( !!b->data, "realloc() failed" ); b->dataSize = new_size; b->cur += b->data - orig; return BSON_OK; }
int bson_ensure_space( bson *b, const int bytesNeeded ) { int pos = b->cur - b->data; char *orig = b->data; int new_size; if ( pos + bytesNeeded <= b->dataSize ) return BSON_OK; new_size = 1.5 * ( b->dataSize + bytesNeeded ); if( new_size < b->dataSize ) { if( ( b->dataSize + bytesNeeded ) < INT_MAX ) new_size = INT_MAX; else { b->err = BSON_SIZE_OVERFLOW; return BSON_ERROR; } } b->data = bson_realloc( b->data, new_size ); if ( !b->data ) bson_fatal_msg( !!b->data, "realloc() failed" ); b->dataSize = new_size; b->cur += b->data - orig; return BSON_OK; }
int bson_ensure_space( bson *b, const size_t bytesNeeded ) { size_t pos = b->cur - b->data; char *orig = b->data; int new_size; check_mongo_object( (void*)b ); if ( (int)pos + bytesNeeded <= (size_t)b->dataSize ) return BSON_OK; new_size = (int)(1.5 * ( b->dataSize + bytesNeeded )); if( new_size < b->dataSize ) { if( ( b->dataSize + bytesNeeded ) < INT_MAX ) new_size = INT_MAX; else { b->err = BSON_SIZE_OVERFLOW; return BSON_ERROR; } } b->data = (char*) bson_realloc( b->data, new_size ); if ( !b->data ) bson_fatal_msg( !!b->data, "realloc() failed" ); b->dataSize = new_size; b->cur += b->data - orig; return BSON_OK; }
void * bson_malloc(int size){ void* p = malloc(size); if (!p) { bson_fatal_msg(0, "malloc() failed"); return 0; } return p; }
mongo_cursor* mongo_find(mongo_connection* conn, const char* ns, bson* query, bson* fields, int nToReturn, int nToSkip, int options) { int sl; int res; mongo_cursor * cursor; char * data; mongo_message * mm = mongo_message_create( 16 + /* header */ 4 + /* options */ strlen( ns ) + 1 + /* ns */ 4 + 4 + /* skip,return */ bson_size( query ) + bson_size( fields ) , 0 , 0 , MONGO_OP_QUERY ); data = &mm->data; data = mongo_data_append32( data , &options ); data = mongo_data_append( data , ns , strlen( ns ) + 1 ); data = mongo_data_append32( data , &nToSkip ); data = mongo_data_append32( data , &nToReturn ); data = mongo_data_append( data , query->data , bson_size( query ) ); if ( fields ) data = mongo_data_append( data , fields->data , bson_size( fields ) ); bson_fatal_msg( (data == ((char*)mm) + mm->head.len), "query building fail!" ); res = mongo_message_send( conn , mm ); if(res != MONGO_OK){ return NULL; } cursor = (mongo_cursor*)bson_malloc(sizeof(mongo_cursor)); res = mongo_read_response( conn, (mongo_reply **)&(cursor->reply) ); if( res != MONGO_OK ) { free( cursor ); return NULL; } sl = strlen(ns)+1; cursor->ns = bson_malloc(sl); if (!cursor->ns){ free(cursor->reply); free( cursor ); return NULL; } memcpy( (void*)cursor->ns, ns, sl ); cursor->conn = conn; cursor->current.data = NULL; cursor->options = options; return (mongo_cursor*)cursor; }
mongo_cursor* mongo_find(mongo_connection* conn, const char* ns, bson* query, bson* fields, int nToReturn, int nToSkip, int options){ int sl; mongo_cursor * cursor; char * data; mongo_message * mm = mongo_message_create(conn->p, 16 + /* header */ 4 + /* options */ strlen( ns ) + 1 + /* ns */ 4 + 4 + /* skip,return */ bson_size( query ) + bson_size( fields ) , 0 , 0 , mongo_op_query ); data = &mm->data; data = mongo_data_append32( data , &options ); data = mongo_data_append( data , ns , strlen( ns ) + 1 ); data = mongo_data_append32( data , &nToSkip ); data = mongo_data_append32( data , &nToReturn ); data = mongo_data_append( data , query->data , bson_size( query ) ); if ( fields ) data = mongo_data_append( data , fields->data , bson_size( fields ) ); bson_fatal_msg( (data == ((char*)mm) + mm->head.len), "query building fail!" ); mongo_message_send( conn , mm ); cursor = (mongo_cursor*)apr_palloc(conn->p, sizeof(mongo_cursor)); MONGO_TRY{ cursor->mm = mongo_read_response(conn); }MONGO_CATCH{ //free(cursor); MONGO_RETHROW(); } sl = strlen(ns)+1; cursor->ns = apr_palloc(conn->p, sl); if (!cursor->ns){ /* //free(cursor->mm); //free(cursor); */ return 0; } memcpy((void*)cursor->ns, ns, sl); /* cast needed to silence GCC warning */ cursor->conn = conn; cursor->current.data = NULL; return cursor; }
static int mongo_cursor_op_query( mongo_cursor *cursor ) { int res; bson empty; char *data; mongo_message *mm; /* Set up default values for query and fields, if necessary. */ if( ! cursor->query ) cursor->query = bson_empty( &empty ); else if( mongo_cursor_bson_valid( cursor, cursor->query ) != MONGO_OK ) return MONGO_ERROR; if( ! cursor->fields ) cursor->fields = bson_empty( &empty ); else if( mongo_cursor_bson_valid( cursor, cursor->fields ) != MONGO_OK ) return MONGO_ERROR; mm = mongo_message_create( 16 + /* header */ 4 + /* options */ strlen( cursor->ns ) + 1 + /* ns */ 4 + 4 + /* skip,return */ bson_size( cursor->query ) + bson_size( cursor->fields ) , 0 , 0 , MONGO_OP_QUERY ); data = &mm->data; data = mongo_data_append32( data , &cursor->options ); data = mongo_data_append( data , cursor->ns , strlen( cursor->ns ) + 1 ); data = mongo_data_append32( data , &cursor->skip ); data = mongo_data_append32( data , &cursor->limit ); data = mongo_data_append( data , cursor->query->data , bson_size( cursor->query ) ); if ( cursor->fields ) data = mongo_data_append( data , cursor->fields->data , bson_size( cursor->fields ) ); bson_fatal_msg( ( data == ( ( char * )mm ) + mm->head.len ), "query building fail!" ); res = mongo_message_send( cursor->conn , mm ); if( res != MONGO_OK ) { return MONGO_ERROR; } res = mongo_read_response( cursor->conn, ( mongo_reply ** )&( cursor->reply ) ); if( res != MONGO_OK ) { return MONGO_ERROR; } cursor->seen += cursor->reply->fields.num; cursor->flags |= MONGO_CURSOR_QUERY_SENT; return MONGO_OK; }
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); }
MONGO_EXPORT int bson_finish( bson *b ) { int i; if( b->err & BSON_NOT_UTF8 ) return BSON_ERROR; if ( ! b->finished ) { bson_fatal_msg(!b->stackPos, "Subobject not finished before bson_finish()."); if ( bson_ensure_space( b, 1 ) == BSON_ERROR ) return BSON_ERROR; bson_append_byte( b, 0 ); if ( _bson_position(b) >= INT32_MAX ) { b->err = BSON_SIZE_OVERFLOW; return BSON_ERROR; } i = ( int ) _bson_position(b); bson_little_endian32( b->data, &i ); b->finished = 1; } return BSON_OK; }
int bson_ensure_space( bson_buffer * b, const int bytesNeeded ){ int pos = b->cur - b->buf; char * orig = b->buf; int new_size; if (b->finished) { b->err = BSON_OBJECT_FINISHED; return BSON_ERROR; } if (pos + bytesNeeded <= b->bufSize) return BSON_OK; new_size = 1.5 * (b->bufSize + bytesNeeded); b->buf = realloc(b->buf, new_size); if (!b->buf) bson_fatal_msg(!!b->buf, "realloc() failed"); b->bufSize = new_size; b->cur += b->buf - orig; return BSON_OK; }
void bson_fatal( int ok ) { bson_fatal_msg( ok, "" ); }
void *bson_realloc( void *ptr, int size ) { void *p; p = bson_realloc_func( ptr, size ); bson_fatal_msg( !!p, "realloc() failed" ); return p; }
void *bson_malloc( int size ) { void *p; p = bson_malloc_func( size ); bson_fatal_msg( !!p, "malloc() failed" ); return p; }
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 ); }
static int mongo_cursor_op_query( mongo_cursor *cursor ) { int res; bson empty; char *data; mongo_message *mm; bson temp; bson_iterator it; /* Clear any errors. */ bson_free( cursor->conn->lasterrstr ); cursor->conn->lasterrstr = NULL; cursor->conn->lasterrcode = 0; cursor->conn->err = 0; cursor->err = 0; /* Set up default values for query and fields, if necessary. */ if( ! cursor->query ) cursor->query = bson_empty( &empty ); else if( mongo_cursor_bson_valid( cursor, cursor->query ) != MONGO_OK ) return MONGO_ERROR; if( ! cursor->fields ) cursor->fields = bson_empty( &empty ); else if( mongo_cursor_bson_valid( cursor, cursor->fields ) != MONGO_OK ) return MONGO_ERROR; mm = mongo_message_create( 16 + /* header */ 4 + /* options */ strlen( cursor->ns ) + 1 + /* ns */ 4 + 4 + /* skip,return */ bson_size( cursor->query ) + bson_size( cursor->fields ) , 0 , 0 , MONGO_OP_QUERY ); data = &mm->data; data = mongo_data_append32( data , &cursor->options ); data = mongo_data_append( data , cursor->ns , strlen( cursor->ns ) + 1 ); data = mongo_data_append32( data , &cursor->skip ); data = mongo_data_append32( data , &cursor->limit ); data = mongo_data_append( data , cursor->query->data , bson_size( cursor->query ) ); if ( cursor->fields ) data = mongo_data_append( data , cursor->fields->data , bson_size( cursor->fields ) ); bson_fatal_msg( ( data == ( ( char * )mm ) + mm->head.len ), "query building fail!" ); res = mongo_message_send( cursor->conn , mm ); if( res != MONGO_OK ) { return MONGO_ERROR; } res = mongo_read_response( cursor->conn, ( mongo_reply ** )&( cursor->reply ) ); if( res != MONGO_OK ) { return MONGO_ERROR; } if( cursor->reply->fields.num == 1 ) { bson_init_data( &temp, &cursor->reply->objs ); if( bson_find( &it, &temp, "$err" ) ) { cursor->conn->lasterrstr = (char *)bson_malloc( bson_iterator_string_len( &it ) ); strcpy( cursor->conn->lasterrstr, bson_iterator_string( &it ) ); bson_find( &it, &temp, "code" ); cursor->conn->lasterrcode = bson_iterator_int( &it ); cursor->err = MONGO_CURSOR_QUERY_FAIL; return MONGO_ERROR; } } cursor->seen += cursor->reply->fields.num; cursor->flags |= MONGO_CURSOR_QUERY_SENT; return MONGO_OK; }
MONGO_EXPORT void *bson_malloc( size_t size ) { void *p; p = bson_malloc_func( size ); bson_fatal_msg( !!p, "malloc() failed" ); return p; }