예제 #1
0
파일: mongodb.c 프로젝트: adarqui/darqbot
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;
}
예제 #2
0
int test_builder_api( mongo *conn ) {
    int count = 0;
    mongo_cursor cursor[1];

    remove_sample_data( conn );
    insert_sample_data( conn, 10000 );
    mongo_cursor_init( cursor, conn, "test.cursors" );

    while( mongo_cursor_next( cursor ) == MONGO_OK ) {
        count++;
    }
    ASSERT( count == 10000 );

    mongo_cursor_destroy( cursor );

    mongo_cursor_init( cursor, conn, "test.cursors" );
    mongo_cursor_set_limit( cursor, 10 );
    count = 0;
    while( mongo_cursor_next( cursor ) == MONGO_OK ) {
        count++;
    }
    ASSERT( count == 10 );
    mongo_cursor_destroy( cursor );

    return 0;
}
static void find_range( const char *ns ) {
    int i;
    bson bb;
    mongo_cursor *cursor;

    for ( i=0; i < PER_TRIAL; i++ ) {
        int j=0;

        bson_init( &bb );
        bson_append_start_object( &bb, "x" );
        bson_append_int( &bb, "$gt", PER_TRIAL/2 );
        bson_append_int( &bb, "$lt", PER_TRIAL/2 + BATCH_SIZE );
        bson_append_finish_object( &bb );
        bson_finish( &bb );

        cursor = mongo_find( conn, ns, &bb, NULL, 0,0,0 );
        ASSERT( cursor );

        while( mongo_cursor_next( cursor ) == MONGO_OK ) {
            j++;
        }
        ASSERT( j == BATCH_SIZE-1 );

        mongo_cursor_destroy( cursor );
        bson_destroy( &bb );
    }
}
예제 #4
0
파일: mongo.c 프로젝트: martinpetrus/1101
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");
        }
    }
예제 #5
0
int users_search(mongo* conn, User* users, int limit) {
  int pos = 0;
  mongo_cursor cursor[1];
  bson_iterator iterator[1];
  mongo_cursor_init(cursor, conn, "ruby-mongo-console.users");
  // mongo_cursor_set_query
  mongo_cursor_set_limit(cursor, limit);

  while(mongo_cursor_next(cursor) == MONGO_OK) {
    const char* s;

    s = get_field_value(iterator, cursor, "name");
    if (s) {
      users[pos].name_len = strlen(s);
      users[pos].name = new_str(s, users[pos].name_len);
    } else {
      users[pos].name_len = 0;
      users[pos].name = NULL;
    }

    s = get_field_value(iterator, cursor, "bio");
    if (s) {
      users[pos].bio_len = strlen(s);
      users[pos].bio = new_str(s, users[pos].bio_len);
    } else {
      users[pos].bio_len = 0;
      users[pos].bio = NULL;
    }

    pos++;
  }

  mongo_cursor_destroy(cursor);
  return pos;
}
예제 #6
0
int inode_exists(const char * path) {
    bson query, fields;
    mongo * conn = get_conn();
    mongo_cursor curs;
    int res;

    bson_init(&query);
    bson_append_string(&query, "dirents", path);
    bson_finish(&query);

    bson_init(&fields);
    bson_append_int(&fields, "dirents", 1);
    bson_append_int(&fields, "_id", 0);
    bson_finish(&fields);

    mongo_cursor_init(&curs, conn, inodes_name);
    mongo_cursor_set_query(&curs, &query);
    mongo_cursor_set_fields(&curs, &fields);
    mongo_cursor_set_limit(&curs, 1);

    res = mongo_cursor_next(&curs);
    bson_destroy(&query);
    bson_destroy(&fields);
    mongo_cursor_destroy(&curs);

    if(res == 0)
        return 0;
    if(curs.err != MONGO_CURSOR_EXHAUSTED)
        return -EIO;
    return -ENOENT;
}
예제 #7
0
static int
result_iterator(lua_State *L) {
    MongoCursor *cursor = userdata_to_cursor(L, lua_upvalueindex(1));
    // mongo_cursor_set_fields(&cursor->cursor, &cursor->fields);
    // 为了配合接口,根本就不能这样使用
    // mongo_cursor_set_query(&cursor->cursor, &cursor->query);

//    bool has_more = true;  char key[64] = {'\0'};
//    lua_newtable(L); // root {}
//    long index = 0, n = 1;
//    while (MONGO_OK == mongo_cursor_next(&(cursor->cursor))) {
//    	if (has_more) has_more = false;
//    	// printf("$_$---\n");bson_print(&(cursor->cursor).current); printf("------^-^------\n");
//    	// sprintf(key, "%l", index);
//    	// ltoa(index, &key[1], 10);printf("[%s %d]%s\n", __FILE__, __LINE__, key);
//    	// lua_pushstring(L, key);
//
//    	// bson_print(&(cursor->cursor).current);
//    	printf("[%s %d]%d\n", __FILE__, __LINE__, index);
//		bson_to_lua(L, &(cursor->cursor).current); // equal to push the value
//		lua_rawseti(L, -2, n++);
//		index++;
//	}
//    if (has_more) lua_pushnil(L);

    if (MONGO_OK == mongo_cursor_next(&(cursor->cursor))) {
    	bson_to_lua(L, &(cursor->cursor).current);
    } else {
    	lua_pushnil(L);
    }

    return 1;
}
예제 #8
0
SEXP rmongo_cursor_next(SEXP cursor) {
    mongo_cursor* _cursor = _checkCursor(cursor);
    SEXP ret;
    PROTECT(ret = allocVector(LGLSXP, 1));
    LOGICAL(ret)[0] = (_cursor && mongo_cursor_next(_cursor) == MONGO_OK);
    UNPROTECT(1);
    return ret;
}
예제 #9
0
int test_tailable( mongo *conn ) {
    mongo_cursor *cursor;
    bson b;
    int count;

    remove_sample_data( conn );
    create_capped_collection( conn );
    insert_sample_data( conn, 10000 );

    bson_init( &b );
    bson_append_start_object( &b, "$query" );
    bson_append_finish_object( &b );
    bson_append_start_object( &b, "$sort" );
    bson_append_int( &b, "$natural", -1 );
    bson_append_finish_object( &b );
    bson_finish( &b );

    cursor = mongo_find( conn, "test.cursors", &b, bson_shared_empty( ), 0, 0, MONGO_TAILABLE );
    bson_destroy( &b );

    count = 0;
    while( mongo_cursor_next( cursor ) == MONGO_OK )
        count++;

    ASSERT( count == 10000 );

    ASSERT( mongo_cursor_next( cursor ) == MONGO_ERROR );
    ASSERT( cursor->err == MONGO_CURSOR_PENDING );

    insert_sample_data( conn, 10 );

    count = 0;
    while( mongo_cursor_next( cursor ) == MONGO_OK ) {
        count++;
    }

    ASSERT( count == 10 );

    ASSERT( mongo_cursor_next( cursor ) == MONGO_ERROR );
    ASSERT( cursor->err == MONGO_CURSOR_PENDING );

    mongo_cursor_destroy( cursor );
    remove_sample_data( conn );

    return 0;
}
예제 #10
0
파일: MongoCursor.cpp 프로젝트: Kxuan/fibjs
result_t MongoCursor::_nextCursor(int32_t &retVal, AsyncEvent *ac)
{
    if (!ac)
        return CHECK_ERROR(CALL_E_NOSYNC);

    retVal = mongo_cursor_next(m_cursor);
    return 0;
}
예제 #11
0
static void tutorial_empty_query( mongo *conn) {
  mongo_cursor cursor[1];
  mongo_cursor_init( cursor, conn, "tutorial.persons" );

  while( mongo_cursor_next( cursor ) == MONGO_OK )
    bson_print( &cursor->current );

  mongo_cursor_destroy( cursor );
}
예제 #12
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;
}
예제 #13
0
gridfs_offset gridfile_read(gridfile* gfile, gridfs_offset size, char* buf)

{
    mongo_cursor* chunks;
    bson chunk;

    int first_chunk;
    int last_chunk;
    int total_chunks;
    gridfs_offset chunksize;
    gridfs_offset contentlength;
    gridfs_offset bytes_left;
    int i;
    bson_iterator it;
    gridfs_offset chunk_len;
    const char * chunk_data;

    contentlength = gridfile_get_contentlength(gfile);
    chunksize = gridfile_get_chunksize(gfile);
    size = (contentlength - gfile->pos < size)
           ? contentlength - gfile->pos
           : size;
    bytes_left = size;

    first_chunk = (gfile->pos)/chunksize;
    last_chunk = (gfile->pos+size-1)/chunksize;
    total_chunks = last_chunk - first_chunk + 1;
    chunks = gridfile_get_chunks(gfile, first_chunk, total_chunks);

    for (i = 0; i < total_chunks; i++) {
        mongo_cursor_next(chunks);
        chunk = chunks->current;
        bson_find(&it, &chunk, "data");
        chunk_len = bson_iterator_bin_len( &it );
        chunk_data = bson_iterator_bin_data( &it );
        if (i == 0) {
            chunk_data += (gfile->pos)%chunksize;
            chunk_len -= (gfile->pos)%chunksize;
        }
        if (bytes_left > chunk_len) {
            memcpy(buf, chunk_data, chunk_len);
            bytes_left -= chunk_len;
            buf += chunk_len;
        } else {
            memcpy(buf, chunk_data, bytes_left);
        }
    }

    mongo_cursor_destroy(chunks);
    gfile->pos = gfile->pos + size;

    return size;
}
예제 #14
0
bson_bool_t mongo_find_one(mongo_connection* conn, const char* ns, bson* query, bson* fields, bson* out){
    mongo_cursor* cursor = mongo_find(conn, ns, query, fields, 1, 0, 0);

    if (cursor && mongo_cursor_next(cursor)){
        bson_copy(out, &cursor->current);
        mongo_cursor_destroy(cursor);
        return 1;
    }else{
        mongo_cursor_destroy(cursor);
        return 0;
    }
}
예제 #15
0
/*
 * res = cursor:next()
 */
static int
cursor_next(lua_State *L) {
	MongoCursor *cursor = userdata_to_cursor(L, 1);

    if (MONGO_OK == mongo_cursor_next(&(cursor->cursor))) {
    	bson_to_lua(L, &(cursor->cursor).current);
    } else {
    	lua_pushnil(L);
    }

    return 1;
}
예제 #16
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_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;
}
예제 #17
0
int mongo_find_one( mongo *conn, const char *ns, bson *query,
                    bson *fields, bson *out ) {

    mongo_cursor *cursor = mongo_find( conn, ns, query, fields, 1, 0, 0 );

    if ( cursor && mongo_cursor_next( cursor ) == MONGO_OK ) {
        bson_copy_basic( out, &cursor->current );
        mongo_cursor_destroy( cursor );
        return MONGO_OK;
    } else {
        mongo_cursor_destroy( cursor );
        return MONGO_ERROR;
    }
}
예제 #18
0
파일: mongodb.c 프로젝트: adarqui/darqbot
int
mongodb_insert_key(bot_t * bot, char *db, char *key, char *value,
		   char *fmt, ...)
{
	bson b;
	mongo_cursor cursor;
	va_list ap;
	char buf[1024], *buf_ptr = "NULL";

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

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

	if (fmt) {
		bz(buf);
		va_start(ap, fmt);
		vsnprintf_buf(buf, fmt, ap);
		va_end(ap);
		buf_ptr = buf;
	}

	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) {
		debug(bot, "mongodb_insert_key: Key already exist\n");
		bson_destroy(&b);
		mongo_cursor_destroy(&cursor);
		return -1;
	}

	bson_init(&b);
	bson_append_string(&b, "key", key);
	bson_append_string(&b, "value", value);
	bson_append_string(&b, "comment", buf_ptr);

	bson_finish(&b);

	mongo_insert(&gi->mongo_conn, db, &b);

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

	return 0;
}
예제 #19
0
int test_multiple_getmore( mongo *conn ) {
    mongo_cursor *cursor;
    int count;

    remove_sample_data( conn );
    create_capped_collection( conn );
    insert_sample_data( conn, 10000 );

    cursor = mongo_find( conn, "test.cursors", bson_shared_empty( ), bson_shared_empty( ), 0, 0, 0 );

    count = 0;
    while( mongo_cursor_next( cursor ) == MONGO_OK )
        count++;

    ASSERT( count == 10000 );

    ASSERT( mongo_cursor_next( cursor ) == MONGO_ERROR );
    ASSERT( cursor->err == MONGO_CURSOR_EXHAUSTED );

    mongo_cursor_destroy( cursor );
    remove_sample_data( conn );
    return 0;
}
예제 #20
0
파일: mongodb.c 프로젝트: adarqui/darqbot
dlist_t *mongodb_dump_keyvalue(bot_t * bot, char *db)
{
	dlist_t *dl, *dptr;
	keyvalue_t *keyvalue_ptr;
	mongo_cursor *cursor;
	bson b;
	bson_iterator bi;

	char *key;
	char *value;

	dl = dptr = NULL;
	cursor = NULL;

	if (!db)
		return NULL;

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

	cursor =
	    mongo_find(&gi->mongo_conn, db, bson_empty(&b),
		       bson_empty(&b), 0, 0, 0);

	while (mongo_cursor_next(cursor) == MONGO_OK) {
		key = value = NULL;

		if (bson_find(&bi, mongo_cursor_bson(cursor), "key")) {
			key = (char *)bson_iterator_string(&bi);
		}
		if (bson_find(&bi, mongo_cursor_bson(cursor), "value")) {
			value = (char *)bson_iterator_string(&bi);
		}

		if (!key && !value)
			continue;

		keyvalue_ptr = (keyvalue_t *) calloc(1, sizeof(keyvalue_t));
		if (key)
			keyvalue_ptr->key = strdup(key);
		if (value)
			keyvalue_ptr->value = strdup(value);

		dlist_Dinsert_after(&dl, keyvalue_ptr);

	}

	mongo_cursor_destroy(cursor);

	return dl;
}
static void find( const char *ns ) {
    bson b;
    int i;
    for ( i=0; i < PER_TRIAL; i++ ) {
        mongo_cursor *cursor;
        make_query( &b );
        cursor = mongo_find( conn, ns, &b, NULL, 0,0,0 );
        ASSERT( cursor );

        while( mongo_cursor_next( cursor ) == MONGO_OK )
            {}

        mongo_cursor_destroy( cursor );
        bson_destroy( &b );
    }
}
예제 #22
0
int test_copy_cursor_data( mongo *conn ) {
    mongo_cursor cursor[1];
    bson b[1];

    insert_sample_data( conn, 10 );
    mongo_cursor_init( cursor, conn, "test.cursors" );

    mongo_cursor_next( cursor );

    ASSERT( bson_copy( b, mongo_cursor_bson( cursor ) ) == MONGO_OK );

    ASSERT( memcmp( (void *)b->data, (void *)(cursor->current).data,
                bson_size( &cursor->current ) ) == 0 );

    mongo_cursor_destroy( cursor );
    bson_destroy( b );

    return 0;
}
예제 #23
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;
}
예제 #24
0
파일: mongodb.c 프로젝트: adarqui/darqbot
char *mongodb_retrieve_key(bot_t * bot, char *db, char *key, char *which)
{
	char *str = NULL;
	bson b;
	mongo_cursor cursor;

	if (!db || !key || !which) {
		return NULL;
	}

	debug(bot,
	      "mongodb_retrieve_key: Entered :db=%s. key=%s, which=%s\n", db,
	      key, which);

	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), which)) {
			str = (char *)bson_iterator_string(&i);
			str = strdup(str);
		}

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

		return str;
	}

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

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

	return NULL;
}
예제 #25
0
static void tutorial_simple_query( mongo *conn ) {
  bson query[1];
  mongo_cursor cursor[1];

  bson_init( query );
  bson_append_int( query, "age", 24 );
  bson_finish( query );

  mongo_cursor_init( cursor, conn, "tutorial.persons" );
  mongo_cursor_set_query( cursor, query );

  while( mongo_cursor_next( cursor ) == MONGO_OK ) {
    bson_iterator iterator[1];
    if ( bson_find( iterator, mongo_cursor_bson( cursor ), "name" )) {
        printf( "name: %s\n", bson_iterator_string( iterator ) );
    }
  }

  bson_destroy( query );
  mongo_cursor_destroy( cursor );
}
예제 #26
0
MONGO_EXPORT int mongo_find_one( mongo *conn, const char *ns, const bson *query,
                    const bson *fields, bson *out ) {

    mongo_cursor cursor[1];
    mongo_cursor_init( cursor, conn, ns );
    mongo_cursor_set_query( cursor, query );
    mongo_cursor_set_fields( cursor, fields );
    mongo_cursor_set_limit( cursor, 1 );

    if ( mongo_cursor_next( cursor ) == MONGO_OK ) {
        bson_init_size( out, bson_size( (bson *)&cursor->current ) );
        memcpy( out->data, cursor->current.data,
            bson_size( (bson *)&cursor->current ) );
        out->finished = 1;
        mongo_cursor_destroy( cursor );
        return MONGO_OK;
    } else {
        mongo_cursor_destroy( cursor );
        return MONGO_ERROR;
    }
}
예제 #27
0
파일: MongoCursor.cpp 프로젝트: ror/fibjs
result_t MongoCursor::hasNext(bool &retVal)
{
    if (!m_bInit)
    {
        result_t hr;

        hr = encodeObject(&m_bbq, v8::Local<v8::Object>::New(Isolate::now()->m_isolate, m_query));
        if (hr < 0)
            return hr;

        m_bInit = true;
    }

    if (m_state == CUR_NONE)
        m_state =
            mongo_cursor_next(m_cursor) == MONGO_OK ?
            CUR_DATA : CUR_NODATA;

    retVal = m_state == CUR_DATA;

    return CHECK_ERROR(m_cursor->m_db->error());
}
예제 #28
0
int test_bad_query( mongo *conn ) {
    mongo_cursor cursor[1];
    bson b[1];

    bson_init( b );
    bson_append_start_object( b, "foo" );
        bson_append_int( b, "$bad", 1 );
    bson_append_finish_object( b );
    bson_finish( b );

    mongo_cursor_init( cursor, conn, "test.cursors" );
    mongo_cursor_set_query( cursor, b );

    ASSERT( mongo_cursor_next( cursor ) == MONGO_ERROR );
    ASSERT( cursor->err == MONGO_CURSOR_QUERY_FAIL );
    ASSERT( cursor->conn->lasterrcode == 10068 );
    ASSERT( strlen( cursor->conn->lasterrstr ) > 0 );

    mongo_cursor_destroy( cursor );
    bson_destroy( b );
    return 0;
}
예제 #29
0
void gridfs_remove_filename(gridfs* gfs, const char* filename )

{
    bson query;
    bson_buffer buf;
    mongo_cursor* files;
    bson file;
    bson_iterator it;
    bson_oid_t id;
    bson b;

    bson_buffer_init(&buf);
    bson_append_string(&buf, "filename", filename);
    bson_from_buffer(&query, &buf);
    files = mongo_find(gfs->client, gfs->files_ns, &query, NULL, 0, 0, 0);
    bson_destroy(&query);

    /* Remove each file and it's chunks from files named filename */
    while (mongo_cursor_next(files)) {
        file = files->current;
        bson_find(&it, &file, "_id");
        id = *bson_iterator_oid(&it);

        /* Remove the file with the specified id */
        bson_buffer_init(&buf);
        bson_append_oid(&buf, "_id", &id);
        bson_from_buffer(&b, &buf);
        mongo_remove( gfs->client, gfs->files_ns, &b);
        bson_destroy(&b);

        /* Remove all chunks from the file with the specified id */
        bson_buffer_init(&buf);
        bson_append_oid(&buf, "files_id", &id);
        bson_from_buffer(&b, &buf);
        mongo_remove( gfs->client, gfs->chunks_ns, &b);
        bson_destroy(&b);
    }

}
예제 #30
0
파일: mongodb.c 프로젝트: adarqui/darqbot
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;
}