Пример #1
0
void f_bson_buf_find(void){
	int size;
	bson b[1];
	bson  sub;
	svalue_t v;
	bson_iterator it;

	size = (sp-1)->u.buf->size;
	bson_init_empty(b);
	bson_init_size( b, size );
	memcpy(b->data, (sp-1)->u.buf->item, size);
	b->finished = 1;

	/* buff不合法 */
	if(bson_size(b) != size){
		pop_n_elems(st_num_arg);
		push_number(0);
		goto free_bson;
	}
	/* 找不到数据 */
	if(!bson_find( &it, b, sp->u.string )){
		pop_n_elems(st_num_arg);
		push_number(0);
		goto free_bson;
	}
	bson_to_v(&it, &v);
	free_buffer((sp-1)->u.buf);
	pop_stack();
	*sp = v;
free_bson:
	bson_destroy( b );
}
Пример #2
0
bson* cBSON_CreateNull()
{
    bson *item = bson_alloc();
    if(item){
        bson_init_empty(item);
        bson_finish(item);
        return item;
    }
    return NULL;
}
Пример #3
0
MONGO_EXPORT void bson_iterator_code_scope_init( const bson_iterator *i, bson *scope, bson_bool_t copyData ) {
    if ( bson_iterator_type( i ) == BSON_CODEWSCOPE ) {
        int codeLen = bson_finished_data_size( bson_iterator_value( i )+4 );
        const char * scopeData = bson_iterator_value( i )+8+codeLen;
        if( copyData )
            bson_init_finished_data_with_copy( scope, scopeData );
        else
            bson_init_finished_data( scope, (char *)scopeData, 0 );
    }
    else {
        bson_init_empty( scope );
    }
}
Пример #4
0
void f_map_bson_buf (void){

	buffer_t *buf;
	bson b[1];

	bson_init_empty(b);
	map_to_bson(sp, b);
	buf = allocate_buffer(bson_size(b));

	memcpy(buf->item, b->data, bson_size(b));
	pop_stack();
	push_refed_buffer(buf);
	bson_destroy( b );
}
Пример #5
0
void f_cmd_map_bson_buf (void){
	int b_size;
	int s_size;
	buffer_t *buf;
	bson b[1];

	bson_init_empty(b);
	map_to_bson(sp, b);

	b_size = bson_size(b);
	s_size = strlen((sp-1)->u.string);
	buf = allocate_buffer(b_size + s_size + 1);
	memcpy(buf->item, (sp-1)->u.string, s_size);
	memcpy(buf->item+s_size, " ", 1);
	memcpy(buf->item+s_size+1, b->data, b_size);
	pop_n_elems(st_num_arg);
	push_refed_buffer(buf);
	bson_destroy( b );
}
Пример #6
0
int test_bson_empty( void ) {
    const bson *empty1;
    empty1 = bson_shared_empty();
    ASSERT( empty1->data );
    ASSERT( bson_size(empty1) > 0 );

    ALLOW_AND_REQUIRE_MALLOC_BEGIN;
    bson * empty2 = bson_alloc();
    ALLOW_AND_REQUIRE_MALLOC_END;
    memset( empty2, 0, sizeof( bson) );
    bson_init_empty( empty2 );
    ASSERT( empty2->data );
    ASSERT( bson_size( empty2 ) > 0 );
    bson_destroy( empty2 );
    ALLOW_AND_REQUIRE_FREE_BEGIN;
    bson_dealloc( empty2 );
    ALLOW_AND_REQUIRE_FREE_END;

    return 0;
}
Пример #7
0
void f_buf_bson_map (void){
	int size;
	bson b[1];
	svalue_t v;

	size = sp->u.buf->size;
	bson_init_empty(b);
	bson_init_size( b, size );
	memcpy(b->data, sp->u.buf->item, size);
	b->finished = 1;
	free_buffer(sp->u.buf);
	/* buff数据不合法 */
	if(bson_size(b) != size){
		pop_stack();
		push_number(0);
		goto free_bson;
	}
	bson_to_mapping(b->data, &v);
	*sp = v;
free_bson:
	bson_destroy( b );
}
Пример #8
0
SEXP mongo_get_database_collections(SEXP mongo_conn, SEXP db) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _db = CHAR(STRING_ELT(db, 0));
    int len = strlen(_db);
    char ns[512];
    strcpy(ns, _db);
    strcpy(ns+len, ".system.namespaces");
    bson empty;
    bson_init_empty(&empty);
    mongo_cursor* cursor = mongo_find(conn, ns, NULL, &empty, 0, 0, 0);
    int count = 0;
    while (cursor && mongo_cursor_next(cursor) == MONGO_OK) {
        bson_iterator iter;
        if (bson_find(&iter, &cursor->current, "name")) {
            const char* name = bson_iterator_string(&iter);
            if (strstr(name, ".system.") || strchr(name, '$'))
                continue;
            ++count;
        }
    }
    mongo_cursor_destroy(cursor);
    cursor = mongo_find(conn, ns, &empty, &empty, 0, 0, 0);
    SEXP ret;
    PROTECT(ret = allocVector(STRSXP, count));
    int i = 0;
    while (cursor && mongo_cursor_next(cursor) == MONGO_OK) {
        bson_iterator iter;
        if (bson_find(&iter, &cursor->current, "name")) {
            const char* name = bson_iterator_string(&iter);
            if (strstr(name, ".system.") || strchr(name, '$'))
                continue;
            SET_STRING_ELT(ret, i++, mkChar(name));
        }
    }
    mongo_cursor_destroy(cursor);
    UNPROTECT(1);
    return ret;
}
Пример #9
0
static
void test_random_write2( void ) {
    mongo conn[1];
    gridfs gfs[1];
    gridfile gfile[1];
    bson meta;
    char *buf = (char*)bson_malloc( LARGE );
    char *zeroedbuf = (char*)bson_malloc( LARGE );
    int n;

    if( buf == NULL ) {
        printf( "Failed to allocate" );
        exit( 1 );
    }

    srand( 123 ); // Init with a predictable value

    INIT_SOCKETS_FOR_WINDOWS;
    CONN_CLIENT_TEST;

    fill_buffer_randomly( buf, ( int64_t )LARGE );
    memset( zeroedbuf, 0, LARGE ); 

    bson_init_empty( &meta );

    GFS_INIT;

    /* This portion of the test we will write zeroes by using new API gridfile_set_size
       function gridfile_expand tested implicitally by using gridfile_set_size making file larger */
    gridfile_init( gfs, &meta, gfile );
    gridfile_writer_init( gfile, gfs, "random_access", "text/html", 0 );
    gridfile_set_size( gfile, LARGE ); // New API, this zero fills the file
    gridfile_writer_done( gfile );    
    test_gridfile( gfs, zeroedbuf, LARGE, "random_access", "text/html" ); // Test zero filled file

    /* This portion of the test we will write zeroes by using new API gridfile_set_size then we will truncate the file */
    gridfile_init( gfs, &meta, gfile );
    gridfile_writer_init( gfile, gfs, "random_access", "text/html", 0 );
    gridfile_set_size( gfile, LARGE ); // New API, this zero fills the file with LARGE bytes
    gridfile_truncate( gfile, LARGE / 2 ); // Let's truncate the file now
    gridfile_writer_done( gfile );    
    test_gridfile( gfs, zeroedbuf, LARGE / 2, "random_access", "text/html" ); // Test zero filled file truncated by half

    /* Let's re-create the file, now let's randomly write real data */
    gridfile_init( gfs, &meta, gfile );
    gridfile_writer_init( gfile, gfs, "random_access", "text/html", 0 );   
    gridfile_set_size( gfile, LARGE ); // We need to reserve LARGE bytes on file before writing backwards

    /* Here we will write backwards the file with our random numbers. We will use a 3072 size buffer to cross over buffers
       given the fact 256K is not a multiple of 3072. Let's stress the buffering logic a little bit */
    for( n = LARGE / 3072 - 1; n >= 0; n-- ) {
        gridfile_seek( gfile, 3072 * n );
        ASSERT( gridfile_write_buffer( gfile, buf + ( n * 3072 ), 3072 ) == 3072 );
    }
    gridfile_writer_done( gfile );
    test_gridfile( gfs, buf, LARGE, "random_access", "text/html" );

    gridfs_destroy( gfs );
    mongo_destroy( conn );

    free( buf );
    free( zeroedbuf );
}