Ejemplo n.º 1
0
Archivo: bson.c Proyecto: kldeng/marsdb
MONGO_EXPORT int bson_size( const bson *b ) {
    int i;
    if ( ! b || ! b->data )
        return 0;
    bson_little_endian32( &i, b->data );
    return i;
}
Ejemplo n.º 2
0
/* Always calls free(mm) */
void mongo_message_send(mongo_connection * conn, mongo_message* mm){
    mongo_header head; /* little endian */
    bson_little_endian32(&head.len, &mm->head.len);
    bson_little_endian32(&head.id, &mm->head.id);
    bson_little_endian32(&head.responseTo, &mm->head.responseTo);
    bson_little_endian32(&head.op, &mm->head.op);
    
    MONGO_TRY{
        looping_write(conn, &head, sizeof(head));
        looping_write(conn, &mm->data, mm->head.len - sizeof(head));
    }MONGO_CATCH{
        //free(mm);
        MONGO_RETHROW();
    }
    //free(mm);
}
Ejemplo n.º 3
0
static inline int ElemToLong(BSONElem* val2,int64_t* outVal){

   int32_t int32;
   int64_t d2;
   int64_t lng;
   double doVal;

   switch(val2->type) {
      case BSON_INT:
         bson_little_endian32(&int32,val2->value);
         d2 = int32;
         break;
      case BSON_LONG:
      case BSON_DATE:
         bson_little_endian64(&lng,val2->value);
         d2 = lng;
         break;
      case BSON_DOUBLE:
         bson_little_endian64(&doVal,val2->value);
         d2 = doVal;
         break;
      default:
         return -1;
   }
   *outVal = d2;
   return 0;
}
Ejemplo n.º 4
0
MONGO_EXPORT int bson_size( const bson *b ) {
    int i;
    if ( ! b || ! b->data )
        return 0;
    check_mongo_object( (void*)b );
    bson_little_endian32( &i, b->data );
    return i;
}
Ejemplo n.º 5
0
mongo_reply * mongo_read_response( mongo_connection * conn ){
    mongo_header head; /* header from network */
    mongo_reply_fields fields; /* header from network */
    mongo_reply * out; /* native endian */
    int len;

    looping_read(conn, &head, sizeof(head));
    looping_read(conn, &fields, sizeof(fields));

    bson_little_endian32(&len, &head.len);

    if (len < sizeof(head)+sizeof(fields) || len > 64*1024*1024)
        MONGO_THROW(MONGO_EXCEPT_NETWORK); /* most likely corruption */

    out = (mongo_reply*)bson_malloc(len);

    out->head.len = len;
    bson_little_endian32(&out->head.id, &head.id);
    bson_little_endian32(&out->head.responseTo, &head.responseTo);
    bson_little_endian32(&out->head.op, &head.op);

    bson_little_endian32(&out->fields.flag, &fields.flag);
    bson_little_endian64(&out->fields.cursorID, &fields.cursorID);
    bson_little_endian32(&out->fields.start, &fields.start);
    bson_little_endian32(&out->fields.num, &fields.num);

    MONGO_TRY{
        looping_read(conn, &out->objs, len-sizeof(head)-sizeof(fields));
    }MONGO_CATCH{
        free(out);
        MONGO_RETHROW();
    }

    return out;
}
Ejemplo n.º 6
0
mongo_reply * mongo_read_response( mongo_connection * conn ){
    mongo_header head; /* header from network */
    mongo_reply_fields fields; /* header from network */
    mongo_reply * out; /* native endian */
    int len;

    looping_read(conn, &head, sizeof(head));
    looping_read(conn, &fields, sizeof(fields));

    bson_little_endian32(&len, &head.len);
    out = (mongo_reply*)apr_palloc(conn->p, len);

    out->head.len = len;
    bson_little_endian32(&out->head.id, &head.id);
    bson_little_endian32(&out->head.responseTo, &head.responseTo);
    bson_little_endian32(&out->head.op, &head.op);

    bson_little_endian32(&out->fields.flag, &fields.flag);
    bson_little_endian64(&out->fields.cursorID, &fields.cursorID);
    bson_little_endian32(&out->fields.start, &fields.start);
    bson_little_endian32(&out->fields.num, &fields.num);

    MONGO_TRY{
        looping_read(conn, &out->objs, len-sizeof(head)-sizeof(fields));
    }MONGO_CATCH{
        //free(out);
        MONGO_RETHROW();
    }

    return out;
}
Ejemplo n.º 7
0
void bson_iterator_code_scope(const bson_iterator * i, bson * scope){
    if (bson_iterator_type(i) == bson_codewscope){
        int code_len;
        bson_little_endian32(&code_len, bson_iterator_value(i)+4);
        bson_init(scope, (void*)(bson_iterator_value(i)+8+code_len), 0);
    }else{
        bson_empty(scope);
    }
}
Ejemplo n.º 8
0
void bson_iterator_code_scope( const bson_iterator *i, bson *scope ) {
    if ( bson_iterator_type( i ) == BSON_CODEWSCOPE ) {
        int code_len;
        bson_little_endian32( &code_len, bson_iterator_value( i )+4 );
        bson_init_data( scope, ( void * )( bson_iterator_value( i )+8+code_len ) );
    } else {
        bson_empty( scope );
    }
}
Ejemplo n.º 9
0
char* Namespace::getNextRecordData(int &recordSize)
{
	Record *rec;

	if (extentCursor.isNull()) // the first time after connection the extent cursor will be null
	{
		extentCursor = firstExtentLoc; // so, get the first extent of the colection
		if (_DEBUG)
			LOGI("getting first extent");
	}

	if (extentCursor.isNull()) // if first extent is null, then no extents exist
		return NULL;  // no extents

	Extent* extent = db->getExtent(extentCursor);

	if (recordCursor == -1)	// the first time the record cursor will be null as well
	{
		recordCursor = extent->firstRecord;	// get the first record of the extent

		if (recordCursor == -1)	// if first record is null, then no records exist
			return NULL;  // no records

		rec = extent->getRecord(recordCursor);
	}
	else // else, the cursor shows the previous record, should take the next one and return its data
	{
		rec = extent->getRecord(recordCursor);

		if (rec->nextRecLoc == -1) // if its next record is null, then go to next extent
		{
			if (extent->nextExtent.isNull()) // if the next extent is null, then no more extents
			{
				return NULL;  // no more records
			}
			else							// proceed with the next extent
			{
				extentCursor = extent->nextExtent;
				recordCursor = -1;
				return getNextRecordData(recordSize);
			}
		}
		else
		{
			recordCursor = rec->nextRecLoc;	// next record exists, so put the cursor on it
			if (_DEBUG)
				LOGI("getting next record");
			rec = extent->getRecord(recordCursor);
		}
	}

	int bsonSize = 0;
	bson_little_endian32( &bsonSize, rec->data);
	char *val = (char*) malloc((bsonSize + 128) * sizeof(char));
	int cur = 0;
	return bson_to_json(val, rec->data, 0, true, 0, recordSize, cur);
}
Ejemplo n.º 10
0
char * bson_buffer_finish( bson_buffer * b ){
    int i;
    if ( ! b->finished ){
        if ( ! bson_ensure_space( b , 1 ) ) return 0;
        bson_append_byte( b , 0 );
        i = b->cur - b->buf;
        bson_little_endian32(b->buf, &i);
        b->finished = 1;
    }
    return b->buf;
}
Ejemplo n.º 11
0
/**
 * Add null byte, mark as finished, and return buffer.
 * Note that the buffer will now be owned by the bson
 * object created from a call to bson_from_buffer.
 * This buffer is then deallocated by calling
 * bson_destroy().
 */
int bson_buffer_finish( bson_buffer * b ){
    int i;
    if ( ! b->finished ){
        if ( bson_ensure_space( b, 1 ) == BSON_ERROR ) return BSON_ERROR;
        bson_append_byte( b, 0 );
        i = b->cur - b->buf;
        bson_little_endian32(b->buf, &i);
        b->finished = 1;
    }

    return BSON_OK;
}
Ejemplo n.º 12
0
int bson_append_finish_object( bson *b ) {
    char *start;
    int i;
    if ( bson_ensure_space( b, 1 ) == BSON_ERROR ) return BSON_ERROR;
    bson_append_byte( b , 0 );

    start = b->data + b->stack[ --b->stackPos ];
    i = b->cur - start;
    bson_little_endian32( start, &i );

    return BSON_OK;
}
Ejemplo n.º 13
0
bson_buffer * bson_append_finish_object( bson_buffer * b ){
    char * start;
    int i;
    if ( ! bson_ensure_space( b , 1 ) ) return 0;
    bson_append_byte( b , 0 );
    
    start = b->buf + b->stack[ --b->stackPos ];
    i = b->cur - start;
    bson_little_endian32(start, &i);

    return b;
}
Ejemplo n.º 14
0
MONGO_EXPORT void bson_iterator_code_scope( const bson_iterator *i, bson *scope ) {
    check_mongo_object( (void*)scope );
    check_mongo_object( (void*)i );
    if ( bson_iterator_type( i ) == BSON_CODEWSCOPE ) {
        int code_len;
        bson_little_endian32( &code_len, bson_iterator_value( i )+4 );
        bson_init_data( scope, ( void * )( bson_iterator_value( i )+8+code_len ) );
        _bson_reset( scope );
        scope->finished = 1;
    }
    else {
        bson_empty( scope );
    }
}
Ejemplo n.º 15
0
/* Always calls bson_free(mm) */
int mongo_message_send( mongo *conn, mongo_message *mm ) {
    mongo_header head; /* little endian */
    int res;
    bson_little_endian32( &head.len, &mm->head.len );
    bson_little_endian32( &head.id, &mm->head.id );
    bson_little_endian32( &head.responseTo, &mm->head.responseTo );
    bson_little_endian32( &head.op, &mm->head.op );

    res = mongo_write_socket( conn, &head, sizeof( head ) );
    if( res != MONGO_OK ) {
        bson_free( mm );
        return res;
    }

    res = mongo_write_socket( conn, &mm->data, mm->head.len - sizeof( head ) );
    if( res != MONGO_OK ) {
        bson_free( mm );
        return res;
    }

    bson_free( mm );
    return MONGO_OK;
}
Ejemplo n.º 16
0
char* Namespace::getNextBatch(int &count)
{
	if (_DEBUG)
		LOGI("get next batch");

	int batchSize = 0;
	int sizes[count];
	int jsonSize = 0;
	char *records[count]; //= (char**)malloc(count*sizeof(char*));

	int lastRetrievedRecord = -1, recordSize, skip = 0;

	int i = 0;
	Extent *extent = getFirstExtent();
	while (extent != NULL)
	{
		lastRetrievedRecord = -1;
		Record *record = extent->getFirstRecord();
		while (record != NULL && i < count)
		{
			recordSize = 0;

			int bsonSize = 0;
			bson_little_endian32( &bsonSize, record->data);
			//FORCE_LOG_INT("bsonSize: ", bsonSize);
			char *val = (char*) malloc((bsonSize + 100) * sizeof(char));
			int cur = 0;
			val = bson_to_json(val, record->data, 0, true, bsonSize, recordSize,
					cur);

			records[i] = val;
			batchSize += recordSize;
			sizes[i] = recordSize;
			jsonSize += recordSize;

			record = extent->getRecord(record->nextRecLoc);

			i++;
		}

		extent = db->getExtent(extent->nextExtent);
	}

	count = i;
	FORCE_LOG_INT("count: ", i);

	return serializeJSON(records, jsonSize, count, sizes);
}
Ejemplo n.º 17
0
int bson_finish( bson *b ) {
    int i;

    if( b->err & BSON_NOT_UTF8 )
        return BSON_ERROR;

    if ( ! b->finished ) {
        if ( bson_ensure_space( b, 1 ) == BSON_ERROR ) return BSON_ERROR;
        bson_append_byte( b, 0 );
        i = b->cur - b->data;
        bson_little_endian32( b->data, &i );
        b->finished = 1;
    }

    return BSON_OK;
}
Ejemplo n.º 18
0
MONGO_EXPORT int bson_append_finish_object( bson *b ) {
    char *start;
    int i;
    if (!b) return BSON_ERROR;
    if (!b->stackPos) { b->err = BSON_NOT_IN_SUBOBJECT; return BSON_ERROR; }
    if ( bson_ensure_space( b, 1 ) == BSON_ERROR ) return BSON_ERROR;
    bson_append_byte( b , 0 );

    start = b->data + b->stack[ --b->stackPos ];
    if ( b->cur - start >= INT32_MAX ) {
        b->err = BSON_SIZE_OVERFLOW;
        return BSON_ERROR;
    }
    i = ( int )( b->cur - start );
    bson_little_endian32( start, &i );

    return BSON_OK;
}
Ejemplo n.º 19
0
MONGO_EXPORT int bson_finish( bson *b ) {
    int i;

    if( b->err & BSON_NOT_UTF8 )
        return BSON_ERROR;

    if ( ! b->finished ) {
        if ( bson_ensure_space( b, 1 ) == BSON_ERROR ) return BSON_ERROR;
        bson_append_byte( b, 0 );
        if ( b->cur - b->data >= INT32_MAX ) {
            b->err = BSON_SIZE_OVERFLOW;
            return BSON_ERROR;
        }
        i = ( int )( b->cur - b->data );
        bson_little_endian32( b->data, &i );
        b->finished = 1;
    }

    return BSON_OK;
}
Ejemplo n.º 20
0
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;
}
Ejemplo n.º 21
0
int mongo_read_response( mongo *conn, mongo_reply **reply ) {
    mongo_header head; /* header from network */
    mongo_reply_fields fields; /* header from network */
    mongo_reply *out;  /* native endian */
    unsigned int len;
    int res;

    mongo_read_socket( conn, &head, sizeof( head ) );
    mongo_read_socket( conn, &fields, sizeof( fields ) );

    bson_little_endian32( &len, &head.len );

    if ( len < sizeof( head )+sizeof( fields ) || len > 64*1024*1024 )
        return MONGO_READ_SIZE_ERROR;  /* most likely corruption */

    out = ( mongo_reply * )bson_malloc( len );

    out->head.len = len;
    bson_little_endian32( &out->head.id, &head.id );
    bson_little_endian32( &out->head.responseTo, &head.responseTo );
    bson_little_endian32( &out->head.op, &head.op );

    bson_little_endian32( &out->fields.flag, &fields.flag );
    bson_little_endian64( &out->fields.cursorID, &fields.cursorID );
    bson_little_endian32( &out->fields.start, &fields.start );
    bson_little_endian32( &out->fields.num, &fields.num );

    res = mongo_read_socket( conn, &out->objs, len-sizeof( head )-sizeof( fields ) );
    if( res != MONGO_OK ) {
        bson_free( out );
        return res;
    }

    *reply = out;

    return MONGO_OK;
}
Ejemplo n.º 22
0
MONGO_EXPORT int bson_iterator_timestamp_time( const bson_iterator *i ) {
    int time;
    bson_little_endian32( &time, bson_iterator_value( i ) + 4 );
    return time;
}
Ejemplo n.º 23
0
static void bson_append32_as_int( bson *b, int data ) {
    bson_little_endian32( b->cur, &data );
    b->cur += 4;
}
Ejemplo n.º 24
0
void bson_append32( bson *b, const void *data ) {
    bson_little_endian32( b->cur, data );
    b->cur += 4;
}
Ejemplo n.º 25
0
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;
}
Ejemplo n.º 26
0
int bson_iterator_int_raw( const bson_iterator *i ) {
    int out;
    bson_little_endian32( &out, bson_iterator_value( i ) );
    return out;
}
Ejemplo n.º 27
0
char *mongo_data_append32( char *start , const void *data ) {
    bson_little_endian32( start , data );
    return start + 4;
}
Ejemplo n.º 28
0
MONGO_EXPORT int bson_iterator_timestamp_increment( const bson_iterator *i ) {
    int increment;
    bson_little_endian32( &increment, bson_iterator_value( i ) );
    return increment;
}
Ejemplo n.º 29
0
static void bson_append32( bson *b, const void *data ) {
    check_mongo_object( (void*)b );
    bson_little_endian32( b->cur, data );
    b->cur += 4;
}
Ejemplo n.º 30
0
MONGO_EXPORT int bson_iterator_timestamp_increment( const bson_iterator *i ) {
    int increment;
    check_mongo_object( (void*)i );
    bson_little_endian32( &increment, bson_iterator_value( i ) );
    return increment;
}