Exemple #1
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");
        }
    }
Exemple #2
0
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;
}
Exemple #3
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;
}
static const char* get_field_value(bson_iterator* iterator, mongo_cursor* cursor, char* field) {
  if(bson_find(iterator, mongo_cursor_bson(cursor), field)) {
    return bson_iterator_string(iterator);
  }else{
    return NULL;
  }
}
Exemple #5
0
result_t MongoCursor::_bsonCursor(bson &out, AsyncEvent *ac)
{
    if (!ac)
        return CHECK_ERROR(CALL_E_NOSYNC);

    out = *(mongo_cursor_bson(m_cursor));
    return 0;
}
Exemple #6
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;
}
Exemple #7
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;
}
Exemple #8
0
result_t MongoCursor::next(v8::Local<v8::Object> &retVal)
{
    bool has;
    result_t hr = hasNext(has);

    if (hr < 0)
        return hr;

    if (!has)
        return CALL_RETURN_NULL;

    retVal = decodeObject(mongo_cursor_bson(m_cursor));
    m_state = CUR_NONE;

    return 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;
}
Exemple #10
0
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;
}
Exemple #11
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 );
}
Exemple #12
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;
}
void MongodbClient::DumpDB()
{
    Logger::Debug("***** MONGO DUMP *******");

    bson query;
    bson_empty(&query);
    
    mongo_cursor *cursor = mongo_find(clientData->connection, namespaceName.c_str(), &query, NULL, 0, 0, 0);
    int32 count = 0;
    while( mongo_cursor_next( cursor ) == MONGO_OK )
    {
        const bson *currentObject = mongo_cursor_bson(cursor);
        
        Logger::Debug(Format("BSON[%d]:", count));
        bson_print(currentObject);
        
        ++count;
    }

    mongo_cursor_destroy(cursor);
    
    Logger::Debug("Count: %d", count);
    Logger::Debug("************************");
}
static int resolve_block(struct inode * e, uint8_t hash[HASH_LEN], char * buf) {
    bson query;
    int res;
    mongo_cursor curs;
    bson_iterator i;
    bson_type bt;
    const char * key;
    mongo * conn = get_conn();

    bson_init(&query);
    bson_append_binary(&query, "_id", 0, (char*)hash, 20);
    bson_finish(&query);

    mongo_cursor_init(&curs, conn, blocks_name);
    mongo_cursor_set_query(&curs, &query);
    mongo_cursor_set_limit(&curs, 1);

    res = mongo_cursor_next(&curs);
    bson_destroy(&query);
    if(res != MONGO_OK) {
        mongo_cursor_destroy(&curs);
        return -EIO;
    }

    bson_iterator_init(&i, mongo_cursor_bson(&curs));
    size_t outsize, compsize = 0;
    const char * compdata = NULL;
    uint32_t offset = 0, size = 0;

    while((bt = bson_iterator_next(&i)) > 0) {
        key = bson_iterator_key(&i);
        if(strcmp(key, "data") == 0) {
            compsize = bson_iterator_bin_len(&i);
            compdata = bson_iterator_bin_data(&i);
        }
        else if(strcmp(key, "offset") == 0)
            offset = bson_iterator_int(&i);
        else if(strcmp(key, "size") == 0)
            size = bson_iterator_int(&i);
    }

    if(curs.err != MONGO_CURSOR_EXHAUSTED) {
        fprintf(stderr, "Error getting extents %d", curs.err);
        return -EIO;
    }

    if(!compdata) {
        fprintf(stderr, "No data in block?\n");
        return -EIO;
    }

    outsize = MAX_BLOCK_SIZE;
    if((res = snappy_uncompress(compdata, compsize,
        buf + offset, &outsize)) != SNAPPY_OK) {
        fprintf(stderr, "Error uncompressing block %d\n", res);
        return -EIO;
    }
    if(offset > 0)
        memset(buf, 0, offset);
    compsize = outsize + offset;
    if(compsize < size)
        memset(buf + compsize, 0, size - compsize);
    mongo_cursor_destroy(&curs);

    return 0;
}
Exemple #15
0
int main() {
    bson b, sub, out;
    bson_iterator it;
    mongo conn;
    mongo_cursor cursor;
    int result;

    /* Create a rich document like this one:
    *
    * { _id: ObjectId("4d95ea712b752328eb2fc2cc"),
    * user_id: ObjectId("4d95ea712b752328eb2fc2cd"),
    *
    * items: [
    * { sku: "col-123",
    * name: "John Coltrane: Impressions",
    * price: 1099,
    * },
    *
    * { sku: "young-456",
    * name: "Larry Young: Unity",
    * price: 1199
    * }
    * ],
    *
    * address: {
    * street: "59 18th St.",
    * zip: 10010
    * },
    *
    * total: 2298
    * }
    */
    bson_init( &b );
    bson_append_new_oid( &b, "_id" );
    bson_append_new_oid( &b, "user_id" );

    bson_append_start_array( &b, "items" );
    bson_append_start_object( &b, "0" );
    bson_append_string( &b, "name", "John Coltrane: Impressions" );
    bson_append_int( &b, "price", 1099 );
    bson_append_finish_object( &b );

    bson_append_start_object( &b, "1" );
    bson_append_string( &b, "name", "Larry Young: Unity" );
    bson_append_int( &b, "price", 1199 );
    bson_append_finish_object( &b );
    bson_append_finish_object( &b );

    bson_append_start_object( &b, "address" );
    bson_append_string( &b, "street", "59 18th St." );
    bson_append_int( &b, "zip", 10010 );
    bson_append_finish_object( &b );

    bson_append_int( &b, "total", 2298 );

    /* Finish the BSON obj. */
    bson_finish( &b );
    printf("Here's the whole BSON object:\n");
    bson_print( &b );

    /* Advance to the 'items' array */
    bson_find( &it, &b, "items" );

    /* Get the subobject representing items */
    bson_iterator_subobject_init( &it, &sub, 0 );

    /* Now iterate that object */
    printf("And here's the inner sub-object by itself.\n");
    bson_print( &sub );
    bson_destroy( &sub );

    /* Now make a connection to MongoDB. */
    if( mongo_client( &conn, "127.0.0.1", 27017 ) != MONGO_OK ) {
      switch( conn.err ) {
        case MONGO_CONN_SUCCESS:
          break;
        case MONGO_CONN_NO_SOCKET:
          printf( "FAIL: Could not create a socket!\n" );
          break;
        case MONGO_CONN_FAIL:
          printf( "FAIL: Could not connect to mongod. Make sure it's listening at 127.0.0.1:27017.\n" );
          break;
        default:
          printf( "MongoDB connection error number %d.\n", conn.err );
          break;
      }

      exit( 1 );
    }

    /* Insert the sample BSON document. */
    if( mongo_insert( &conn, "test.records", &b, NULL ) != MONGO_OK ) {
      printf( "FAIL: Failed to insert document with error %d\n", conn.err );
      exit( 1 );
    }

    /* Query for the sample document. */
    mongo_cursor_init( &cursor, &conn, "test.records" );
    mongo_cursor_set_query( &cursor, bson_shared_empty() );
    if( mongo_cursor_next( &cursor ) != MONGO_OK ) {
      printf( "FAIL: Failed to find inserted document." );
      exit( 1 );
    }

    printf( "Found saved BSON object:\n" );
    bson_print( (bson *)mongo_cursor_bson( &cursor ) );

    mongo_cmd_drop_collection( &conn, "test", "records", NULL );
    mongo_cursor_destroy( &cursor );
    bson_destroy( &b );
    mongo_destroy( &conn );

    return 0;
}
Exemple #16
0
/**
 * \brief This function tries to load tag group from MongoDB
 *
 * \param[in] *vs_ctx		The verse server context
 * \param[in] *oid			The pointer at ObjectID of tag group in MongoDB
 * \param[in] *node			The node containing tag group
 * \param[in] taggroup_id	The tag group ID that is requested from database
 * \param[in] version		The version of tag group id that is requested
 * 							from database. When version is equal to -1, then
 * 							current version is loaded from MongoDB.
 *
 * \return This function returns pointer at tag group, when tag group is found.
 * Otherwise it returns NULL.
 */
struct VSTagGroup *vs_mongo_taggroup_load_linked(struct VS_CTX *vs_ctx,
		bson_oid_t *oid,
		struct VSNode *node,
		uint16 taggroup_id,
		uint32 req_version)
{
	struct VSTagGroup *tg = NULL;
	bson query;
	mongo_cursor cursor;
	uint32 node_id = -1, tmp_taggroup_id = -1,
			current_version = -1, custom_type = -1;
	int found = 0;
	bson_iterator tg_data_iter;
	const bson *bson_tg;

	bson_init(&query);
	bson_append_oid(&query, "_id", oid);
	bson_finish(&query);

	mongo_cursor_init(&cursor, vs_ctx->mongo_conn, vs_ctx->mongo_tg_ns);
	mongo_cursor_set_query(&cursor, &query);

	/* ObjectID should be unique */
	while( mongo_cursor_next(&cursor) == MONGO_OK ) {
		bson_tg = mongo_cursor_bson(&cursor);

		/* Try to get node id */
		if( bson_find(&tg_data_iter, bson_tg, "node_id") == BSON_INT ) {
			node_id = bson_iterator_int(&tg_data_iter);
		}

		/* Try to get tag group id */
		if( bson_find(&tg_data_iter, bson_tg, "taggroup_id") == BSON_INT ) {
			tmp_taggroup_id = bson_iterator_int(&tg_data_iter);
		}

		/* ObjectID is ALMOST unique. So it is check, if node id and
		 * tag group id matches */
		if(node_id == node->id && tmp_taggroup_id == taggroup_id) {
			found = 1;
			break;
		}
	}

	/* When tag group was found, then load required data from MongoDB */
	if(found == 1) {

		/* Try to get current version of tag group */
		if( bson_find(&tg_data_iter, bson_tg, "current_version") == BSON_INT ) {
			current_version = bson_iterator_int(&tg_data_iter);
		}

		/* Try to get custom type of tag group */
		if( bson_find(&tg_data_iter, bson_tg, "custom_type") == BSON_INT ) {
			custom_type = bson_iterator_int(&tg_data_iter);
		}

		/* Create tag group with specific ID */
		if((int)current_version != -1 && (int)custom_type != -1) {
			tg = vs_taggroup_create(node, taggroup_id, custom_type);

			if(tg != NULL) {

				tg->state = ENTITY_CREATED;

				/* Save ObjectID to tag group */
				memcpy(&tg->oid, oid, sizeof(bson_oid_t));

				/* Try to get versions of tag group */
				if( bson_find(&tg_data_iter, bson_tg, "versions") == BSON_OBJECT ) {
					bson bson_versions;
					bson_iterator version_iter;
					char str_num[15];

					/* Initialize sub-object of versions */
					bson_iterator_subobject_init(&tg_data_iter, &bson_versions, 0);

					sprintf(str_num, "%u", req_version);

					/* Try to find required version of tag group */
					if( bson_find(&version_iter, &bson_versions, str_num) == BSON_OBJECT ) {
						bson bson_version;

						/* Set version of tag group */
						tg->version = tg->saved_version = current_version;

						bson_iterator_subobject_init(&version_iter, &bson_version, 0);

						/* Try to load tags */
						vs_mongo_taggroup_load_data(tg, &bson_version);
					}
				}
			}
		}
	}

	bson_destroy(&query);
	mongo_cursor_destroy(&cursor);

	return tg;
}
Exemple #17
0
int main(int argc, char **argv)
{
	bson b[1];
	bson query[1];
	mongo conn[1];
	mongo_cursor cursor[1];
	int result;

	if(mongo_client(conn, "127.0.0.1", 27017) != MONGO_OK) {
		switch(conn->err)
		{
			case MONGO_CONN_SUCCESS:
				fprintf(stderr, "OK: Connected to MongoDB!\n");
				break;
			case MONGO_CONN_NO_SOCKET:
				fprintf(stderr, "FAIL: Cloud not create a socket!\n");
				break;
			case MONGO_CONN_FAIL:
				fprintf(stderr, "FAIL: Could not connect to mongod!.");
				break;
			default:
				fprintf(stderr, "MongoDB connection error number: %d.\n", conn->err);
		}
	}


	bson_init(query);
	bson_append_string(query, "city_name", "南京");
	bson_finish(query);

	mongo_cursor_init(cursor, conn, "bangboox.city_shop");
	mongo_cursor_set_query(cursor, query);
	while(mongo_cursor_next(cursor) == MONGO_OK) {
		bson_print((bson *)mongo_cursor_bson(cursor));
	}

	bson_init(b);

	bson_append_new_oid(b, "_id");
	bson_append_new_oid(b, "record_id");

	bson_append_start_array(b, "items");
		bson_append_start_object(b, "0");
			bson_append_string(b, "name", "roy.lieu");
			bson_append_int(b, "age", 30);
		bson_append_finish_object(b);

		bson_append_start_object(b, "1");
			bson_append_string(b, "name", "jimmy.chen");
			bson_append_int(b, "age", 35);
		bson_append_finish_object(b);
	bson_append_finish_object(b);

	bson_append_start_object(b, "address");
	bson_append_string(b, "stree", "Jufeng RD.");
	bson_append_int(b, "zip", 232044);
	bson_append_finish_object(b);

	bson_finish(b);

	//printf("\n\n");
	//bson_print(b);
	
	if(mongo_insert(conn, "test.record", b, NULL) != MONGO_OK) {
		fprintf(stderr, "FAIL: Failed to insert document whth err: %d\n", conn->err);
	}

	return 0;
}