Ejemplo n.º 1
0
void testCheckDuplicates2(void) {
    bson b, b2;
    bson_iterator it, sit;
    bson_type bt;

    bson_init(&b);
    bson_append_start_array(&b, "array");
    bson_append_int(&b, "0", 1);
    bson_append_finish_array(&b);
    bson_append_start_array(&b, "array");
    bson_append_int(&b, "0", 3);
    bson_append_int(&b, "1", 4);
    bson_append_finish_array(&b);
    bson_finish(&b);
    CU_ASSERT_FALSE_FATAL(b.err);

    CU_ASSERT_TRUE_FATAL(bson_check_duplicate_keys(&b));

    bson_init(&b2);
    bson_fix_duplicate_keys(&b, &b2);
    bson_finish(&b2);
    CU_ASSERT_FALSE_FATAL(b2.err);

    CU_ASSERT_FALSE_FATAL(bson_check_duplicate_keys(&b2));

    BSON_ITERATOR_INIT(&it, &b2);
    bt = bson_iterator_next(&it);
    CU_ASSERT_EQUAL_FATAL(bt, BSON_ARRAY);
    CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&it), "array");
    BSON_ITERATOR_SUBITERATOR(&it, &sit);
    
    bt = bson_iterator_next(&sit);
    CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&sit), "0");
    CU_ASSERT_TRUE_FATAL(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL_FATAL(bson_iterator_int(&sit), 1);
    bt = bson_iterator_next(&sit);
    CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&sit), "1");
    CU_ASSERT_TRUE_FATAL(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL_FATAL(bson_iterator_int(&sit), 3);
    bt = bson_iterator_next(&sit);
    CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&sit), "2");
    CU_ASSERT_TRUE_FATAL(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL_FATAL(bson_iterator_int(&sit), 4);
    
    bt = bson_iterator_next(&sit);
    CU_ASSERT_EQUAL_FATAL(bt, BSON_EOO);

    bson_destroy(&b2);
    bson_destroy(&b);
}
Ejemplo n.º 2
0
/**
 * \brief This function tries to load tag group data from MongoDB
 */
static void vs_mongo_taggroup_load_data(struct VSTagGroup *tg,
		bson *bson_version)
{
	bson_iterator version_data_iter;

	/* Try to get tags of tag group */
	if( bson_find(&version_data_iter, bson_version, "tags") == BSON_OBJECT ) {
		struct VSTag *tag;
		bson bson_tag;
		bson_iterator tags_iter, tag_iter;
		const char *key;
		int tag_id, data_type = -1, count = -1, tag_custom_type = -1;

		bson_iterator_subiterator(&version_data_iter, &tags_iter);

		while( bson_iterator_next(&tags_iter) == BSON_OBJECT ) {
			key = bson_iterator_key(&tags_iter);
			sscanf(key, "%d", &tag_id);

			bson_iterator_subobject_init(&tags_iter, &bson_tag, 0);

			if( bson_find(&tag_iter, &bson_tag, "data_type") == BSON_INT) {
				data_type = bson_iterator_int(&tag_iter);
			}

			if( bson_find(&tag_iter, &bson_tag, "count") == BSON_INT) {
				count = bson_iterator_int(&tag_iter);
			}

			if( bson_find(&tag_iter, &bson_tag, "custom_type") == BSON_INT) {
				tag_custom_type = bson_iterator_int(&tag_iter);
			}

			if(data_type != -1 && count != -1 && tag_custom_type != -1) {
				/* Create tag with specific ID */
				tag = vs_tag_create(tg, tag_id, data_type, count, tag_custom_type);

				if(tag != NULL) {
					tag->state = ENTITY_CREATED;

					vs_mongo_tag_load_data(tag, &bson_tag);

					tag->flag = TAG_INITIALIZED;
				}
			}
		}
	}
}
Ejemplo n.º 3
0
static duo_code_t
_duo_bson_response(struct duo_ctx *ctx, bson *resp)
{
    bson obj;
    bson_iterator it;
    duo_code_t ret;
    const char *p;
    int code;
    
    bson_init(&obj, (char *)ctx->body, 0);
    
    ret = DUO_SERVER_ERROR;
    
    if (ctx->body_len <= 0 || bson_size(&obj) > ctx->body_len) {
        _duo_seterr(ctx, "invalid BSON response");
        return (ret);
    }
    _BSON_FIND(ctx, &it, &obj, "stat", bson_string);
    p = bson_iterator_string(&it);
    
    if (strcasecmp(p, "OK") == 0) {
        _BSON_FIND(ctx, &it, &obj, "response", bson_object);
        if (resp)
            bson_iterator_subobject(&it, resp);
        ret = DUO_OK;
    } else if (strcasecmp(p, "FAIL") == 0) {
        _BSON_FIND(ctx, &it, &obj, "code", bson_int);
        code = bson_iterator_int(&it);
        _BSON_FIND(ctx, &it, &obj, "message", bson_string);
        _duo_seterr(ctx, "%d: %s", code, bson_iterator_string(&it));
        ret = DUO_FAIL;
    }
    return (ret);
}
Ejemplo n.º 4
0
int MCPContentPersonAward::set_basic_info_from_bson_result(bson* bson_out, const char* aggregate_name)
{

    LOGD("[GWJ] %s:  start", __FUNCTION__);

    const char* key;
    bson_iterator it[1];
    stringstream ss;

    MCPContentPersonAwardRecord* detail = ObjectPoolFactory<MCPContentPersonAwardRecord>::instance().fetch_object();
    bson_iterator_init(it, bson_out);

    while(bson_iterator_next(it))
    {
        ss.clear();
        key = bson_iterator_key(it);

        if(0 == strcmp(key, "priority"))
        {
            detail->priority = bson_iterator_string(it);


            LOGG("[GWJ] %s: set MCPContentPersonAwardRecord.priority[%s]",
                     __FUNCTION__, detail->priority.c_str());
        }
        else if(0 == strcmp(key, "kudou"))
        {
            detail->kudou = bson_iterator_int(it);


            LOGG("[GWJ] %s: set MCPContentPersonAwardRecord.kudou[%d]",
                     __FUNCTION__, detail->kudou;
        }
        else if(0 == strcmp(key, "commodity_id"))
Ejemplo n.º 5
0
int mongodb_update_key_stat(bot_t * bot, char *db, char *key, int value)
{
	bson b;
	bson_iterator i;
	mongo_cursor cursor;
	int ret = 0;

	if (!db || !key || value < 0) {
		return -1;
	}

	debug(bot, "mongodb_update_key_stat: Entered\n");

	bson_init(&b);
	bson_append_string(&b, "key", key);
	bson_finish(&b);
	mongo_cursor_init(&cursor, &gi->mongo_conn, db);
	mongo_cursor_set_query(&cursor, &b);

	if (mongo_cursor_next(&cursor) != MONGO_OK) {
		mongodb_insert_key_stat(bot, db, key, 0);

		mongo_cursor_init(&cursor, &gi->mongo_conn, db);
		mongo_cursor_set_query(&cursor, &b);

		if (mongo_cursor_next(&cursor) != MONGO_OK)
			goto cleanup;
	}

	debug(bot, "mongodb_update_key_stat: Found!\n");
	if (bson_find(&i, mongo_cursor_bson(&cursor), "value")) {
		bson c;

		ret = (int)bson_iterator_int(&i);
		ret++;

		bson_init(&c);
		bson_append_string(&c, "key", key);
		bson_append_int(&c, "value", ret);
		bson_finish(&c);

		debug(bot, "mongodb_update_key_stat: updating to %i\n", ret);

		mongo_update(&gi->mongo_conn, db, &b, &c, 0);

		bson_destroy(&c);
		bson_destroy(&b);
		mongo_cursor_destroy(&cursor);

		return ret;
	}

	debug(bot, "mongodb_update_key_stat: Key not found\n");

 cleanup:
	bson_destroy(&b);
	mongo_cursor_destroy(&cursor);

	return -1;
}
Ejemplo n.º 6
0
scale_t *get_signatures(mongo *conn) {
    scale_t *scales;

    bson query[1];
    mongo_cursor cursor[1];

    bson_init(query);
    bson_finish(query);

    mongo_cursor_init(cursor, conn, "1101.scales");
    mongo_cursor_set_query(cursor, query);

    int num_scales = 0;
    while (mongo_cursor_next(cursor) == MONGO_OK) {
        num_scales++;
        scales = (scale_t *) realloc(scales, sizeof(scale_t) * num_scales);

        bson_iterator iterator[1];
        bson b[1];
        bson_init(b);
        int i;
        if (bson_find(iterator, mongo_cursor_bson(cursor), "name")) {
            printf("name: %s, scale: ", bson_iterator_string(iterator));
            strcpy(scales[num_scales - 1].description, bson_iterator_string(iterator));
        }
        if (bson_find(iterator, mongo_cursor_bson(cursor), "scale")) {
            int scale = bson_iterator_int(iterator);
            scales[num_scales - 1].mask = scale;
            for (i = 11; i >= 0; i--) {
                printf("%s", (scale >> i) & 1 ? "1" : "0");
            }
            printf("\n");
        }
    }
Ejemplo n.º 7
0
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" );
    }
}
Ejemplo n.º 8
0
gridfs_offset gridfile_get_contentlength(gridfile* gfile)

{
    bson_iterator it;

    bson_find(&it, gfile->meta, "length");
    return (gridfs_offset)bson_iterator_int( &it );
}
Ejemplo n.º 9
0
int gridfile_get_numchunks(gridfile* gfile)

{
    bson_iterator it;
    gridfs_offset length;
    gridfs_offset chunkSize;
    double numchunks;

    bson_find(&it, gfile->meta, "length");
    length = bson_iterator_int(&it);
    bson_find(&it, gfile->meta, "chunkSize");
    chunkSize = bson_iterator_int(&it);
    numchunks = ((double)length/(double)chunkSize);
    return (numchunks - (int)numchunks > 0)
           ? (int)(numchunks+1)
           : (int)(numchunks);
}
Ejemplo n.º 10
0
int gridfile_get_chunksize(gridfile* gfile)

{
    bson_iterator it;

    bson_find(&it, gfile->meta, "chunkSize");
    return bson_iterator_int(&it);
}
Ejemplo n.º 11
0
void MongodbClient::ReadData(KeyedArchive* archive, void* bsonObj)
{
    if((!archive) || (!bsonObj)) return;

    bson_iterator it;
    bson_iterator_init(&it, (bson*)bsonObj);
    
    while (bson_iterator_next(&it))
    {
        String key = String(bson_iterator_key(&it));
        bson_type type = bson_iterator_type(&it);
        
        if(key == "_id") continue; // ignore _id

        switch (type)
        {
            case BSON_STRING:
                archive->SetString(key, String(bson_iterator_string(&it)));
                break;
                
            case BSON_INT:
                archive->SetInt32(key, bson_iterator_int(&it));
                break;
                
            case BSON_LONG:
                archive->SetInt32(key, (int32)bson_iterator_long(&it));
                break;
                
            case BSON_DOUBLE:
                archive->SetFloat(key, (float32)bson_iterator_double(&it));
                break;
                
            case BSON_OBJECT:
            {
                bson sub;
                
                bson_iterator_subobject(&it, &sub);
                
                KeyedArchive* subArchive = new KeyedArchive();
                ReadData(subArchive, &sub);
                archive->SetArchive(key, subArchive);
                SafeRelease(subArchive);
                break;
            }
                
            case BSON_OID:
                //TODO: add 12-bytes array
                //bson_append_oid(object, key, bson_iterator_oid(&it));
                break;
                
                
            default:
                DVASSERT(false);
                Logger::Error("[MongodbUpdateObject::ReadData] Not implemented type: %d", type);
                break;
        }
    }
}
Ejemplo n.º 12
0
gridfs_offset gridfile_get_contentlength( gridfile *gfile ) {
    bson_iterator it;

    bson_find( &it, gfile->meta, "length" );

    if( bson_iterator_type( &it ) == BSON_INT )
        return ( gridfs_offset )bson_iterator_int( &it );
    else
        return ( gridfs_offset )bson_iterator_long( &it );
}
Ejemplo n.º 13
0
static void bson_decode_tree(lua_State* L, bson_iterator* b,int set_t){
    const char *key;
    while(bson_iterator_next(b))
    {
        bson_type t = bson_iterator_type(b);
        if(t == 0)
        {
            lua_pushnil(L);
            break;
        }
        key = bson_iterator_key(b);
        if(set_t)
        {
            lua_pushstring(L,key);
        }
        LOG_MSG("type %d,key %s",t,key);
        switch(t)
        {
            case BSON_DOUBLE:
                lua_pushnumber(L,bson_iterator_double(b));
                break;
            case BSON_STRING:
                lua_pushstring(L,bson_iterator_string(b));
                break;
            case BSON_BOOL:
                lua_pushboolean(L,bson_iterator_bool( b ) ? true : false );
                break;
            case BSON_NULL:
                lua_pushnil(L);
                break;
            case BSON_INT:
                lua_pushinteger(L,bson_iterator_int(b));
                break;
            case BSON_OBJECT:
                bson_iterator t;
                bson_iterator_subiterator(b,&t);
                bson_decode_tree(L,&t);
                lua_settable(L,-3);
                break;
            case BSON_ARRAY:
                lua_newtable(L);
                bson_iterator s;
                bson_iterator_subiterator(b,&s);
                bson_decode_tree(L,&s,1);
                break;
            default:
                break;
        }
        if(set_t)
        {
            lua_settable(L,-3);
        }
    }

}
Ejemplo n.º 14
0
MONGO_EXPORT int gridfile_get_numchunks( gridfile *gfile ) {
    bson_iterator it;
    gridfs_offset length;
    gridfs_offset chunkSize;
    double numchunks;

    bson_find( &it, gfile->meta, "length" );

    if( bson_iterator_type( &it ) == BSON_INT )
        length = ( gridfs_offset )bson_iterator_int( &it );
    else
        length = ( gridfs_offset )bson_iterator_long( &it );

    bson_find( &it, gfile->meta, "chunkSize" );
    chunkSize = bson_iterator_int( &it );
    numchunks = ( ( double )length/( double )chunkSize );
    return ( numchunks - ( int )numchunks > 0 )
           ? ( int )( numchunks+1 )
           : ( int )( numchunks );
}
Ejemplo n.º 15
0
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(&gte_buf);
        bson_append_int(&gte_buf, "$gte", start);
        bson_from_buffer(&gte_bson, &gte_buf);
        bson_append_bson(&query_buf, "n", &gte_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);
}
Ejemplo n.º 16
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;
}
Ejemplo n.º 17
0
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;
}
Ejemplo n.º 18
0
Archivo: t1.c Proyecto: CowanSM/ejdb
void testSaveLoad() {
    CU_ASSERT_PTR_NOT_NULL_FATAL(jb);
    bson_oid_t oid;
    EJCOLL *ccoll = ejdbcreatecoll(jb, "contacts", NULL);
    CU_ASSERT_PTR_NOT_NULL(ccoll);

    //Save record
    bson a1;
    bson_init(&a1);

    bson_append_string(&a1, "name", "Петров Петр");
    bson_append_string(&a1, "phone", "333-222-333");
    bson_append_int(&a1, "age", 33);
    bson_append_long(&a1, "longage", 0xFFFFFFFFFF01LL);
    bson_append_double(&a1, "doubleage", 0.333333);
    bson_finish(&a1);
    ejdbsavebson(ccoll, &a1, &oid);
    bson_destroy(&a1);


    bson *lbson = ejdbloadbson(ccoll, &oid);
    CU_ASSERT_PTR_NOT_NULL(lbson);
    bson_iterator it1;
    bson_iterator_init(&it1, lbson);

    int btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_OID);
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_STRING);
    CU_ASSERT(!strcmp("name", bson_iterator_key(&it1)));
    CU_ASSERT(!strcmp("Петров Петр", bson_iterator_string(&it1)));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_STRING);
    CU_ASSERT(!strcmp("phone", bson_iterator_key(&it1)));
    CU_ASSERT(!strcmp("333-222-333", bson_iterator_string(&it1)));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_INT);
    CU_ASSERT(!strcmp("age", bson_iterator_key(&it1)));
    CU_ASSERT(33 == bson_iterator_int(&it1));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_LONG);
    CU_ASSERT(!strcmp("longage", bson_iterator_key(&it1)));
    CU_ASSERT(0xFFFFFFFFFF01LL == bson_iterator_long(&it1));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_DOUBLE);
    CU_ASSERT(!strcmp("doubleage", bson_iterator_key(&it1)));
    CU_ASSERT_DOUBLE_EQUAL(bson_iterator_double(&it1), 0.3, 0.1);
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_EOO);
    bson_del(lbson);
}
Ejemplo n.º 19
0
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;
}
Ejemplo n.º 20
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;
         }
     }
 }
Ejemplo n.º 21
0
int32 MongodbObject::GetInt32(const String &fieldname)
{
    int32 retValue = 0;
    
    bson_iterator it;
    bson_iterator_init(&it, objectData->object);
    
    bson_iterator foundIt;
    bool found = objectData->FindField(&it, &foundIt, fieldname, true);
    if(found)
    {
        retValue = bson_iterator_int(&foundIt);
    }
    
    return retValue;
}
Ejemplo n.º 22
0
UInfo*	MongoLink::query(int uid) {
	UInfo* uinfo = NULL;
	bson query[1];
	mongo_cursor cursor[1];

	bson_init( query );
	bson_append_int( query, "uid", uid );
	bson_finish( query );

	mongo_cursor_init( cursor, &m_mongo, m_strDBName.c_str() );
	mongo_cursor_set_query( cursor, query );

	if( mongo_cursor_next( cursor ) == MONGO_OK ) {
		bson_iterator iterator[1];
		uinfo = new UInfo();
		//uinfo->linkid = 0;

		if ( bson_find( iterator, mongo_cursor_bson( cursor ), "uid" ) != BSON_EOO ) {
			uinfo->uid= bson_iterator_int( iterator );			
		} 

		if ( bson_find( iterator, mongo_cursor_bson( cursor ), "router" ) != BSON_EOO ) {
			uinfo->router= bson_iterator_string( iterator );
		}		

		if ( bson_find( iterator, mongo_cursor_bson( cursor ), "dispatcher" ) != BSON_EOO ) {
			uinfo->dispatcher= bson_iterator_string( iterator );
		}	

		if ( bson_find( iterator, mongo_cursor_bson( cursor ), "proxy" ) != BSON_EOO ) {
			uinfo->proxy= bson_iterator_string( iterator );
		}	

	} else {
		LOG(TAG_ROUTER, "query uid failed, uid=%d.", uid);
	}

exit:
	bson_destroy( query );
	mongo_cursor_destroy( cursor );

	return uinfo;
}
Ejemplo n.º 23
0
int mongodb_retrieve_key_stat(bot_t * bot, char *db, char *key)
{
	bson b;
	mongo_cursor cursor;
	int ret = 0;

	if (!db || !key) {
		return -1;
	}

	debug(bot, "mongodb_retrieve_key: Entered\n");

	bson_init(&b);
	bson_append_string(&b, "key", key);
	bson_finish(&b);
	mongo_cursor_init(&cursor, &gi->mongo_conn, db);
	mongo_cursor_set_query(&cursor, &b);

	if (mongo_cursor_next(&cursor) == MONGO_OK) {
		bson_iterator i;

		debug(bot, "mongodb_retrieve_key: Found!\n");
		if (bson_find(&i, mongo_cursor_bson(&cursor), "value")) {
			ret = (int)bson_iterator_int(&i);
		}

		bson_destroy(&b);
		mongo_cursor_destroy(&cursor);

		return ret;
	}

	debug(bot, "mongodb_retrieve_key: Key not found\n");

	bson_destroy(&b);
	mongo_cursor_destroy(&cursor);

	return -1;
}
Ejemplo n.º 24
0
static int mongo_cmd_get_error_helper( mongo *conn, const char *db,
                                       bson *realout, const char *cmdtype ) {

    bson out = {NULL,0};
    bson_bool_t haserror = 0;

    /* Reset last error codes. */
    conn->lasterrcode = 0;
    bson_free( conn->lasterrstr );
    conn->lasterrstr = NULL;

    /* If there's an error, store its code and string in the connection object. */
    if( mongo_simple_int_command( conn, db, cmdtype, 1, &out ) == MONGO_OK ) {
        bson_iterator it;
        haserror = ( bson_find( &it, &out, "err" ) != BSON_NULL );
        if( haserror ) {
            conn->lasterrstr = ( char * )bson_malloc( bson_iterator_string_len( &it ) );
            if( conn->lasterrstr ) {
                strcpy( conn->lasterrstr, bson_iterator_string( &it ) );
            }

            if( bson_find( &it, &out, "code" ) != BSON_NULL )
                conn->lasterrcode = bson_iterator_int( &it );
        }
    }

    if( realout )
        *realout = out; /* transfer of ownership */
    else
        bson_destroy( &out );

    if( haserror )
        return MONGO_ERROR;
    else
        return MONGO_OK;
}
Ejemplo n.º 25
0
    void CSMatrix<Scalar>::import_from_file(const char *filename, const char *var_name, MatrixExportFormat fmt, bool invert_storage)
    {
      this->free();

      switch (fmt)
      {
      case EXPORT_FORMAT_MATRIX_MARKET:
        throw Exceptions::MethodNotOverridenException("CSMatrix<Scalar>::import_from_file - Matrix Market");
        break;
      case EXPORT_FORMAT_MATLAB_MATIO:
      {
#ifdef WITH_MATIO
                                       mat_t    *matfp;
                                       matvar_t *matvar;

                                       matfp = Mat_Open(filename, MAT_ACC_RDONLY);

                                       if (!matfp)
                                         throw Exceptions::IOException(Exceptions::IOException::Read, filename);

                                       matvar = Mat_VarRead(matfp, var_name);

                                       if (matvar)
                                       {
                                         mat_sparse_t *sparse = (mat_sparse_t *)matvar->data;

                                         this->nnz = sparse->nir;
                                         this->Ax = malloc_with_check<CSMatrix<Scalar>, Scalar>(this->nnz, this);
                                         this->Ai = malloc_with_check<CSMatrix<Scalar>, int>(this->nnz, this);
                                         this->size = sparse->njc;
                                         this->Ap = malloc_with_check<CSMatrix<Scalar>, int>(this->size + 1, this);

                                         void* data = nullptr;
                                         if (Hermes::Helpers::TypeIsReal<Scalar>::value)
                                           data = sparse->data;
                                         else
                                         {
                                           std::complex<double>* complex_data = malloc_with_check<CSMatrix<Scalar>, std::complex<double> >(this->nnz, this);
                                           double* real_array = (double*)((mat_complex_split_t*)sparse->data)->Re;
                                           double* imag_array = (double*)((mat_complex_split_t*)sparse->data)->Im;
                                           for (int i = 0; i < this->nnz; i++)
                                             complex_data[i] = std::complex<double>(real_array[i], imag_array[i]);
                                           data = (void*)complex_data;
                                         }
                                         memcpy(this->Ax, data, this->nnz * sizeof(Scalar));
                                         if (!Hermes::Helpers::TypeIsReal<Scalar>::value)
                                           free_with_check(data);
                                         memcpy(this->Ap, sparse->jc, this->size * sizeof(int));
                                         this->Ap[this->size] = this->nnz;
                                         memcpy(this->Ai, sparse->ir, this->nnz * sizeof(int));

                                         if (invert_storage)
                                           this->switch_orientation();
                                       }

                                       Mat_Close(matfp);

                                       if (!matvar)
                                         throw Exceptions::IOException(Exceptions::IOException::Read, filename);
#else
                                       throw Exceptions::Exception("MATIO not included.");
#endif
      }
        break;

      case EXPORT_FORMAT_PLAIN_ASCII:
        throw Exceptions::MethodNotOverridenException("CSMatrix<Scalar>::import_from_file - Simple format");

#ifdef WITH_BSON
      case EXPORT_FORMAT_BSON:
      {
                               FILE *fpr;
                               fpr = fopen(filename, "rb");

                               // file size:
                               fseek(fpr, 0, SEEK_END);
                               int size = ftell(fpr);
                               rewind(fpr);

                               // allocate memory to contain the whole file:
                               char *datar = malloc_with_check<char>(size);
                               fread(datar, size, 1, fpr);
                               fclose(fpr);

                               bson br;
                               bson_init_finished_data(&br, datar, 0);

                               bson_iterator it;
                               bson sub;
                               bson_find(&it, &br, "size");
                               this->size = bson_iterator_int(&it);
                               bson_find(&it, &br, "nnz");
                               this->nnz = bson_iterator_int(&it);

                               this->Ap = malloc_with_check<CSMatrix<Scalar>, int>(this->size + 1, this);
                               this->Ai = malloc_with_check<CSMatrix<Scalar>, int>(nnz, this);
                               this->Ax = malloc_with_check<CSMatrix<Scalar>, Scalar>(nnz, this);

                               // coeffs
                               bson_iterator it_coeffs;
                               bson_find(&it_coeffs, &br, "Ap");
                               bson_iterator_subobject_init(&it_coeffs, &sub, 0);
                               bson_iterator_init(&it, &sub);
                               int index_coeff = 0;
                               while (bson_iterator_next(&it))
                                 this->Ap[index_coeff++] = bson_iterator_int(&it);
                               this->Ap[this->size] = this->nnz;

                               bson_find(&it_coeffs, &br, "Ai");
                               bson_iterator_subobject_init(&it_coeffs, &sub, 0);
                               bson_iterator_init(&it, &sub);
                               index_coeff = 0;
                               while (bson_iterator_next(&it))
                                 this->Ai[index_coeff++] = bson_iterator_int(&it);

                                bson_find(&it_coeffs, &br, "Ax");
                                bson_iterator_subobject_init(&it_coeffs, &sub, 0);
                                bson_iterator_init(&it, &sub);
                                index_coeff = 0;
                                while (bson_iterator_next(&it))
                                  this->Ax[index_coeff++] = bson_iterator_double(&it);
                               
                                if (!Hermes::Helpers::TypeIsReal<Scalar>::value)
                               {
                                 bson_find(&it_coeffs, &br, "Ax-imag");
                                 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
                                 bson_iterator_init(&it, &sub);
                                 index_coeff = 0;
                                 while (bson_iterator_next(&it))
                                   ((std::complex<double>)this->Ax[index_coeff++]).imag(bson_iterator_double(&it));
                               }

                                if (invert_storage)
                                  this->switch_orientation();

                               bson_destroy(&br);
                               free_with_check(datar);
      }
#endif
        break;
      }
    }
Ejemplo n.º 26
0
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" );
    }
}
Ejemplo n.º 27
0
//! Read raster data from MongoDB
int clsRasterData::ReadFromMongoDB(gridfs *gfs, const char* remoteFilename)
{
	gridfile gfile[1];
	bson b[1];
	bson_init(b);
	bson_append_string(b, "filename",  remoteFilename);
	bson_finish(b);  
	int flag = gridfs_find_query(gfs, b, gfile); 
	if(0 != flag)
	{  
		throw ModelException("clsRasterData", "ReadFromMongoDB", "The file " + string(remoteFilename) + " does not exist.");
	}

	size_t length = (size_t)gridfile_get_contentlength(gfile);
	char* buf = (char*)malloc(length);
	gridfile_read (gfile, length, buf);
	float *data = (float*)buf;

	bson bmeta[1];
	gridfile_get_metadata(gfile, bmeta);
	bson_iterator iterator[1];
	if ( bson_find( iterator, bmeta, "NCOLS" )) 
		m_headers["NCOLS"] = (float)bson_iterator_int(iterator);
	if ( bson_find( iterator, bmeta, "NROWS" )) 
		m_headers["NROWS"] = (float)bson_iterator_int(iterator);
	if ( bson_find( iterator, bmeta, "NODATA_VALUE" )) 
		m_headers["NODATA_VALUE"] = (float)bson_iterator_double(iterator);
	if ( bson_find( iterator, bmeta, "XLLCENTER" )) 
		m_headers["XLLCENTER"] = (float)bson_iterator_double(iterator);
	if ( bson_find( iterator, bmeta, "YLLCENTER" )) 
		m_headers["YLLCENTER"] = (float)bson_iterator_double(iterator);
	if ( bson_find( iterator, bmeta, "CELLSIZE" )) 
	{
		m_headers["CELLSIZE"] = (float)bson_iterator_double(iterator);
		//m_headers["DY"] = m_headers["DX"];
	}
	
	int nRows = (int)m_headers["NROWS"];
	int nCols = (int)m_headers["NCOLS"];

	vector<float> values;
	vector<int> positionRows;
	vector<int> positionCols;
	//get all valid values
	float nodataFloat = m_headers["NODATA_VALUE"];
	for (int i = 0; i < nRows; ++i)
	{
		for (int j = 0; j < nCols; ++j)
		{
			int index = i*nCols + j;
			float value = data[index];
			if(FloatEqual(nodataFloat, value)) 
				continue;
			values.push_back(value);
			positionRows.push_back(i);
			positionCols.push_back(j);
		}
	}

	//create float array
	m_nRows = values.size();
	m_rasterData = new float[m_nRows];
	m_rasterPositionData = new float*[m_nRows];
	for (int i = 0; i < m_nRows; ++i)
	{
		m_rasterData[i] = values.at(i);
		m_rasterPositionData[i] = new float[2];
		m_rasterPositionData[i][0] = float(positionRows.at(i));
		m_rasterPositionData[i][1] = float(positionCols.at(i));
	}

	bson_destroy(b);
	gridfile_destroy(gfile);

	free(buf);

	return 0;
}
Ejemplo n.º 28
0
void testCheckDuplicates(void) {
    bson bs, bs2;
    bson_iterator it;
    bson_type bt;
    
    bson_init(&bs);
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "b", 2);
    bson_append_null(&bs, "c");
    bson_append_start_object(&bs, "d");
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "e", 0);
    bson_append_int(&bs, "d", 1);
    bson_append_finish_object(&bs);
    bson_finish(&bs);
    CU_ASSERT_FALSE_FATAL(bs.err);

    CU_ASSERT_FALSE(bson_check_duplicate_keys(&bs));

    bson_destroy(&bs);

    bson_init(&bs);
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "b", 2);
    bson_append_null(&bs, "c");
    bson_append_start_object(&bs, "d");
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "e", 0);
    bson_append_int(&bs, "e", 1);
    bson_append_finish_object(&bs);
    bson_finish(&bs);
    CU_ASSERT_FALSE_FATAL(bs.err);

    CU_ASSERT_TRUE(bson_check_duplicate_keys(&bs));

    bson_init(&bs2);
    bson_fix_duplicate_keys(&bs, &bs2);
    bson_finish(&bs2);

    CU_ASSERT_FALSE(bson_check_duplicate_keys(&bs2));
    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("d.e", &it);
    CU_ASSERT_TRUE(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL(bson_iterator_int(&it), 1);

    bson_destroy(&bs2);

    bson_init(&bs);
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "b", 2);
    bson_append_null(&bs, "c");
    bson_append_start_object(&bs, "d");
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "e", 0);
    bson_append_int(&bs, "d", 1);
    bson_append_finish_object(&bs);
    bson_append_start_array(&bs, "f");
    bson_append_start_object(&bs, "0");
    bson_append_string(&bs, "a", "a");
    bson_append_string(&bs, "b", "b");
    bson_append_int(&bs, "c", 1);
    bson_append_finish_object(&bs);
    bson_append_start_object(&bs, "1");
    bson_append_string(&bs, "a", "a");
    bson_append_string(&bs, "b", "b");
    bson_append_int(&bs, "c", 1);
    bson_append_finish_object(&bs);
    bson_append_finish_array(&bs);
    bson_finish(&bs);
    CU_ASSERT_FALSE_FATAL(bs.err);

    CU_ASSERT_FALSE(bson_check_duplicate_keys(&bs));

    bson_init(&bs);
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "b", 2);
    bson_append_null(&bs, "c");
    bson_append_start_object(&bs, "d");
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "e", 0);
    bson_append_int(&bs, "d", 1);
    bson_append_start_object(&bs, "q");
    bson_append_int(&bs, "w", 0);
    bson_append_finish_object(&bs);
    bson_append_finish_object(&bs);
    bson_append_start_array(&bs, "f");
    bson_append_start_object(&bs, "0");
    bson_append_string(&bs, "a", "a");
    bson_append_string(&bs, "b", "b");
    bson_append_int(&bs, "a", 1);
    bson_append_finish_object(&bs);
    bson_append_start_object(&bs, "1");
    bson_append_string(&bs, "a", "a");
    bson_append_string(&bs, "b", "b");
    bson_append_int(&bs, "c", 1);
    bson_append_finish_object(&bs);
    bson_append_finish_array(&bs);
    bson_append_start_object(&bs, "a");
    bson_append_finish_object(&bs);
    bson_append_start_object(&bs, "d");
    bson_append_start_object(&bs, "q");
    bson_append_int(&bs, "e", 1);
    bson_append_finish_object(&bs);
    bson_append_finish_object(&bs);
    bson_finish(&bs);
    CU_ASSERT_FALSE_FATAL(bs.err);

    CU_ASSERT_TRUE(bson_check_duplicate_keys(&bs));

    bson_init(&bs2);
    bson_fix_duplicate_keys(&bs, &bs2);
    bson_finish(&bs2);

    CU_ASSERT_FALSE(bson_check_duplicate_keys(&bs2));
    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("f.0.a", &it);
    CU_ASSERT_TRUE(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL(bson_iterator_int(&it), 1);
    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("f.1.a", &it);
    CU_ASSERT_TRUE(BSON_IS_STRING_TYPE(bt));
    CU_ASSERT_FALSE(strcmp(bson_iterator_string(&it), "a"));

    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("a", &it);
    CU_ASSERT_EQUAL(bt, BSON_OBJECT);

    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("d.q.w", &it);
    CU_ASSERT_TRUE(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL(bson_iterator_int(&it), 0);
    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("d.q.e", &it);
    CU_ASSERT_TRUE(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL(bson_iterator_int(&it), 1);
}
Ejemplo n.º 29
0
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;
}
Ejemplo n.º 30
0
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;
}