static void make_large( bson *out, int i ) {
    int num;
    char numstr[4];
    bson_init( out );

    bson_append_new_oid( out, "_id" );
    bson_append_int( out, "x", i );
    bson_append_string( out, "base_url", "http://www.example.com/test-me" );
    bson_append_int( out, "total_word_count", 6743 );
    bson_append_int( out, "access_time", 999 ); /*TODO use date*/

    bson_append_start_object( out, "meta_tags" );
    bson_append_string( out, "description", "i am a long description string" );
    bson_append_string( out, "author", "Holly Man" );
    bson_append_string( out, "dynamically_created_meta_tag", "who know\n what" );
    bson_append_finish_object( out );

    bson_append_start_object( out, "page_structure" );
    bson_append_int( out, "counted_tags", 3450 );
    bson_append_int( out, "no_of_js_attached", 10 );
    bson_append_int( out, "no_of_images", 6 );
    bson_append_finish_object( out );


    bson_append_start_array( out, "harvested_words" );
    for ( num=0; num < 14*20; num++ ) {
        bson_numstr( numstr, num );
        bson_append_string( out, numstr, words[num%14] );
    }
    bson_append_finish_object( out );

    bson_finish( out );
}
Beispiel #2
0
void test_example_wikipedia_bson(size_t iterations) {
    bcon_error_t ret;
    size_t i;
    bson b[1];
    for (i = 0; i < iterations; i++) {
        bson_init( b );
        bson_append_string( b, "firstName", "John" );
        bson_append_string( b, "lastName" , "Smith" );
        bson_append_int( b, "age"      , 25);
        bson_append_start_object( b, "address" );
            bson_append_string( b, "streetAddress", "21 2nd Street" );
            bson_append_string( b, "city"         , "New York" );
            bson_append_string( b, "state"        , "NY" );
            bson_append_string( b, "postalCode"   , "10021" );
        bson_append_finish_object( b );
        bson_append_start_array( b, "phoneNumber" );
            bson_append_start_object( b, "0" );
                bson_append_string( b, "type"  , "home" );
                bson_append_string( b, "number", "212 555-1234" );
            bson_append_finish_object( b );
            bson_append_start_object( b, "1" );
                bson_append_string( b, "type"  , "fax" );
                bson_append_string( b, "number", "646 555-4567" );
            bson_append_finish_object( b );
        bson_append_finish_array( b );
        ret = bson_finish( b );
        bson_destroy( b );
    }
    assert(ret == BSON_OK);
}
Beispiel #3
0
static int mongo_bson_buffer_append_complex(struct bson_buffer* b, const char* name, double r, double i) {
    bson* _b = (bson*) b;
    return (bson_append_start_object(_b, name) == BSON_OK) &&
           (bson_append_double(_b, "r", r) == BSON_OK) && 
           (bson_append_double(_b, "i", i) == BSON_OK) && 
           (bson_append_finish_object(_b) == BSON_OK);
}
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 );
    }
}
Beispiel #5
0
/**
 * \brief This function tries to add new tag group to MongoDB
 */
int vs_mongo_taggroup_add_new(struct VS_CTX *vs_ctx,
		struct VSNode *node,
		struct VSTagGroup *tg)
{
	bson bson_tg;
	int ret;
	bson_init(&bson_tg);

	bson_oid_gen(&tg->oid);
	bson_append_oid(&bson_tg, "_id", &tg->oid);
	bson_append_int(&bson_tg, "node_id", node->id);
	bson_append_int(&bson_tg, "taggroup_id", tg->id);
	bson_append_int(&bson_tg, "custom_type", tg->custom_type);
	bson_append_int(&bson_tg, "current_version", tg->version);

	bson_append_start_object(&bson_tg, "versions");
	vs_mongo_taggroup_save_version(tg, &bson_tg, UINT32_MAX);
	bson_append_finish_object(&bson_tg);

	bson_finish(&bson_tg);

	ret = mongo_insert(vs_ctx->mongo_conn, vs_ctx->mongo_tg_ns, &bson_tg, 0);

	bson_destroy(&bson_tg);

	if(ret != MONGO_OK) {
		v_print_log(VRS_PRINT_ERROR,
				"Unable to write tag group %d of node %d to MongoDB: %s, error: %s\n",
				tg->id, node->id, vs_ctx->mongo_tg_ns,
				mongo_get_server_err_string(vs_ctx->mongo_conn));
		return 0;
	}

	return 1;
}
void test_index_helper( mongo *conn ) {
    int ret;

    bson b, out;
    bson_iterator it;

    bson_init( &b );
    bson_append_int( &b, "foo", -1 );
    bson_finish( &b );

    mongo_create_index( conn, "test.bar", &b, NULL, MONGO_INDEX_SPARSE | MONGO_INDEX_UNIQUE, &out );

    bson_destroy( &b );
    bson_destroy( &out );

    bson_init( &b );
    bson_append_start_object( &b, "key" );
    bson_append_int( &b, "foo", -1 );
    bson_append_finish_object( &b );

    bson_finish( &b );

    ret = mongo_find_one( conn, "test.system.indexes", &b, NULL, &out );
    ASSERT( ret == MONGO_OK );

    bson_print( &out );

    bson_iterator_init( &it, &out );

    ASSERT( bson_find( &it, &out, "unique" ) );
    ASSERT( bson_find( &it, &out, "sparse" ) );

    bson_destroy( &b );
    bson_destroy( &out );
}
Beispiel #7
0
MONGO_EXPORT int mongo_cmd_add_user( mongo *conn, const char *db, const char *user, const char *pass ) {
    bson user_obj;
    bson pass_obj;
    char hex_digest[33];
    char *ns = bson_malloc( strlen( db ) + strlen( ".system.users" ) + 1 );
    int res;

    strcpy( ns, db );
    strcpy( ns+strlen( db ), ".system.users" );

    mongo_pass_digest( user, pass, hex_digest );

    bson_init( &user_obj );
    bson_append_string( &user_obj, "user", user );
    bson_finish( &user_obj );

    bson_init( &pass_obj );
    bson_append_start_object( &pass_obj, "$set" );
    bson_append_string( &pass_obj, "pwd", hex_digest );
    bson_append_finish_object( &pass_obj );
    bson_finish( &pass_obj );

    res = mongo_update( conn, ns, &user_obj, &pass_obj, MONGO_UPDATE_UPSERT );

    bson_free( ns );
    bson_destroy( &user_obj );
    bson_destroy( &pass_obj );

    return res;
}
Beispiel #8
0
int update_filesize(struct inode * e, off_t newsize) {
    bson cond, doc;
    mongo * conn = get_conn();
    int res;

    if(newsize < e->size)
        return 0;

    e->size = newsize;

    bson_init(&cond);
    bson_append_oid(&cond, "_id", &e->oid);
    bson_finish(&cond);

    bson_init(&doc);
    bson_append_start_object(&doc, "$set");
    bson_append_long(&doc, "size", newsize);
    bson_append_finish_object(&doc);
    bson_finish(&doc);

    res = mongo_update(conn, inodes_name, &cond, &doc, 0, NULL);
    bson_destroy(&cond);
    bson_destroy(&doc);

    if(res != 0)
        return -EIO;
    return 0;
}
Beispiel #9
0
/*
 *  实现抽象接口:
 */
int NotEqualDecorator::custom_result_set_query(bson* query)
{
    int size = this->fitler_conditions.size();
    LOGD("[GWJ] %s: filter_size[%d]", __FUNCTION__, size);

    if(size > 0)
    {
        const char* column_name = this->fitler_conditions[0].table_column;

        if(column_name == NULL)
        {
            LOGD("[GWJ] %s: end. Error filter column name!!", __FUNCTION__);
            return -1;
        }

        LOGD("[GWJ] %s:  start. column[%s], size[%d]", __FUNCTION__, column_name, size);
        bson_append_start_object(query, column_name);

        bson_append_string(query, "$ne", this->fitler_conditions[0].value);

        LOGD("[GWJ] %s: [%s] not equal[%s]",
             __FUNCTION__, column_name, this->fitler_conditions[0].value);

        bson_append_finish_object(query);
    }
    else if(size == 0)
    {
        LOGD("[GWJ] %s: BiggerThanDecorator no filter Error!", __FUNCTION__);
        return -1;
    }

    LOGD("[GWJ] %s: BiggerThanDecorator end", __FUNCTION__);
    return 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;
}
Beispiel #11
0
static void
jbson_object_end(void *state)
{
	json_to_bson_state *_state = (json_to_bson_state *) state;

	if (_state->lex->lex_level == 0)
		return;
	bson_append_finish_object(_state->bson);
}
Beispiel #12
0
bool encodeObject(bson *bb, const char *name, v8::Local<v8::Value> element,
                  bool doJson)
{
    v8::Local<v8::Object> object = element->ToObject();

    if (doJson)
    {
        v8::Local<v8::Value> jsonFun = object->Get(
                                           v8::String::NewFromUtf8(isolate, "toJSON",
                                                   v8::String::kNormalString, 6));

        if (!IsEmpty(jsonFun) && jsonFun->IsFunction())
        {
            v8::Local<v8::Value> p = v8::String::NewFromUtf8(isolate, name ? name : "");
            v8::Local<v8::Value> element1 = v8::Local<v8::Function>::Cast(
                                                jsonFun)->Call(object, 1, &p);

            if (name)
            {
                encodeValue(bb, name, element1, false);
                return true;
            }

            if (!element1->IsObject())
                return false;

            object = element1->ToObject();
        }
    }

    if (!name
            && (object->IsDate() || object->IsArray() || object->IsRegExp()
                || Buffer_base::getInstance(object)))
        return false;

    if (name)
        bson_append_start_object(bb, name);

    v8::Local<v8::Array> properties = object->GetPropertyNames();

    for (int i = 0; i < (int) properties->Length(); i++)
    {
        v8::Local<v8::Value> prop_name = properties->Get(i);
        v8::Local<v8::Value> prop_val = object->Get(prop_name);

        v8::String::Utf8Value n(prop_name);
        const char *pname = ToCString(n);

        encodeValue(bb, pname, prop_val);
    }

    if (name)
        bson_append_finish_object(bb);

    return true;
}
int main() {
	/*
	 * We assume objects in the form of {_id:<any_id>, list:[{a:<int>,b:<int>}, ...]}
	 */
	mongo conn[1];
	mongo_init(conn);
	if(MONGO_OK != mongo_client(conn, "127.0.0.1", 27017))
		return 1;
	bson b[1], b_result[1];
	/*create the aggregation command in bson*/
	bson_init(b);
		bson_append_string(b, "aggregate", "agg");
		bson_append_start_array(b, "pipeline");
			bson_append_start_object(b,"0");
				bson_append_string(b, "$unwind", "$list");
			bson_append_finish_object(b);
			bson_append_start_object(b,"1");
				bson_append_start_object(b,"$group");
					bson_append_string(b,"_id", "$list");
					bson_append_start_object(b, "distinct_count");
						bson_append_int(b, "$sum", 1);
					bson_append_finish_object(b);
				bson_append_finish_object(b);
			bson_append_finish_object(b);
		bson_append_finish_array(b);
	bson_finish(b);

	/*So you can see your command*/
	bson_print(b);

	/*run the command*/
	mongo_run_command(conn, "test", b, b_result);

	/*command results*/
	bson_print(b_result);

	bson_destroy(b_result);
	bson_destroy(b);
	mongo_destroy(conn);
	return 0;
}
Beispiel #14
0
void bson_from_json_type(bson *b, yajl_val value, const char* key)
{
    if( YAJL_IS_STRING( value ) )
    {
        char* string = YAJL_GET_STRING( value );
        bson_append_string( b, key, string );
    }
    else if( YAJL_IS_NUMBER( value ) )
    {
        char* string = value->u.number.r;
        size_t len = strlen( string );

        // Hack to detect a double, since 'flags' is always set to double.
        if( memchr( string, '.', len ) ||
                memchr( string, 'e', len ) ||
                memchr( string, 'E', len ) )
        {
            double number = YAJL_GET_DOUBLE( value );
            bson_append_double( b, key, number );
        }
        else
        {
            uint64_t number = YAJL_GET_INTEGER( value );

            if( number <= INT_MIN && number <= INT_MAX)
                bson_append_int( b, key, (int)number );
            else
                bson_append_long( b, key, number);
        }
    }
    else if ( YAJL_IS_FALSE( value ) )
    {
        bson_append_bool( b, key, 0 );
    }
    else if ( YAJL_IS_TRUE( value ) )
    {
        bson_append_bool( b, key, 1 );
    }
    else if ( YAJL_IS_ARRAY( value ) )
    {
        bson_append_start_array( b, key );
        bson_from_json_array( b, value );
        bson_append_finish_array( b );
    }
    else if ( YAJL_IS_OBJECT( value ) )
    {
        bson_append_start_object( b, key );
        bson_from_json_object( b, value );
        bson_append_finish_object( b );
    }
}
static void make_medium( bson *out, int i ) {
    bson_init( out );
    bson_append_new_oid( out, "_id" );
    bson_append_int( out, "x", i );
    bson_append_int( out, "integer", 5 );
    bson_append_double( out, "number", 5.05 );
    bson_append_bool( out, "boolean", 0 );

    bson_append_start_array( out, "array" );
    bson_append_string( out, "0", "test" );
    bson_append_string( out, "1", "benchmark" );
    bson_append_finish_object( out );

    bson_finish( out );
}
Beispiel #16
0
static void make_medium(bson * out, int i){
    bson_buffer bb;
    bson_buffer_init(&bb);
    bson_append_new_oid(&bb, "_id");
    bson_append_int(&bb, "x", i);
    bson_append_int(&bb, "integer", 5);
    bson_append_double(&bb, "number", 5.05);
    bson_append_bool(&bb, "boolean", 0);

    bson_append_start_array(&bb, "array");
    bson_append_string(&bb, "0", "test");
    bson_append_string(&bb, "1", "benchmark");
    bson_append_finish_object(&bb);

    bson_from_buffer(out, &bb);
}
Beispiel #17
0
int main(int argc, char *argv[])
{
    const char * test_server = (argc > 1 ? argv[1] : TEST_SERVER);

    bson_iterator it[1], it2[1];
    bson b[1];
    bson sub[1];
    bson copy[1];
    bson_type type;

    bson_init( b );
    bson_append_string( b, "foo", "hello" );

    {
        bson_append_start_object( b, "o" );
          bson_append_string( b, "bar", "goodbye" );
        bson_append_finish_object( b );
    }

    bson_iterator_init( it, b );

    bson_iterator_next( it );
    type = bson_iterator_next( it );

    ASSERT( BSON_OBJECT == type );

    bson_iterator_subobject_init( it, sub, 0 );
    ASSERT( sub->finished == 1 );

    bson_iterator_init( it2, sub );

    type = bson_iterator_next( it2 );
    ASSERT( BSON_STRING == type );
    type = bson_iterator_next( it2 );
    ASSERT( BSON_EOO == type );

    bson_copy( copy, sub );

    ASSERT( 1 == copy->finished );
    ASSERT( 0 == copy->stackPos );
    ASSERT( 0 == copy->err );

    bson_destroy( copy );
    bson_destroy( b );

    return 0;
}
Beispiel #18
0
int commit_inode(struct inode * e) {
    bson cond, doc;
    mongo * conn = get_conn();
    char istr[4];
    struct dirent * cde = e->dirents;
    int res;

    bson_init(&doc);
    bson_append_start_object(&doc, "$set");
    bson_append_start_array(&doc, "dirents");
    res = 0;
    while(cde) {
        bson_numstr(istr, res++);
        bson_append_string(&doc, istr, cde->path);
        cde = cde->next;
    }
    bson_append_finish_array(&doc);

    bson_append_int(&doc, "mode", e->mode);
    bson_append_long(&doc, "owner", e->owner);
    bson_append_long(&doc, "group", e->group);
    bson_append_long(&doc, "size", e->size);
    bson_append_time_t(&doc, "created", e->created);
    bson_append_time_t(&doc, "modified", e->modified);
    if(e->data && e->datalen > 0)
        bson_append_string_n(&doc, "data", e->data, e->datalen);
    bson_append_finish_object(&doc);
    bson_finish(&doc);

    bson_init(&cond);
    bson_append_oid(&cond, "_id", &e->oid);
    bson_finish(&cond);

    res = mongo_update(conn, inodes_name, &cond, &doc,
        MONGO_UPDATE_UPSERT, NULL);
    bson_destroy(&cond);
    bson_destroy(&doc);
    if(res != MONGO_OK) {
        fprintf(stderr, "Error committing inode %s\n",
            mongo_get_server_err_string(conn));
        return -EIO;
    }
    return 0;
}
Beispiel #19
0
static void tutorial_update( mongo *conn ) {
  bson cond[1], op[1];

  bson_init( cond );
    bson_append_string( cond, "name", "Joe");
    bson_append_int( cond, "age", 33);
  bson_finish( cond );

  bson_init( op );
    bson_append_start_object( op, "$inc" );
      bson_append_int( op, "visits", 1 );
    bson_append_finish_object( op );
  bson_finish( op );

  mongo_update( conn, "tutorial.persons", cond, op, MONGO_UPDATE_BASIC, 0 );

  bson_destroy( cond );
  bson_destroy( op );
}
Beispiel #20
0
/* Update DLR */
static void dlr_mongodb_update(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status)
{
    DBPoolConn *pconn;
    bson cond, op;
    bson_buffer cond_buf, op_buf;
    mongo_connection *conn = NULL;

    pconn = dbpool_conn_consume(pool);
    if (pconn == NULL) {
        return;
    }
    conn = (mongo_connection*)pconn->conn;

    bson_buffer_init(&cond_buf);
    bson_append_string(&cond_buf, octstr_get_cstr(fields->field_smsc), octstr_get_cstr(smsc));
    bson_append_string(&cond_buf, octstr_get_cstr(fields->field_ts), octstr_get_cstr(ts));

    if (dst) {
        bson_append_string(&cond_buf, octstr_get_cstr(fields->field_dst), octstr_get_cstr(dst));
    }

    bson_from_buffer(&cond, &cond_buf);

    bson_buffer_init(&op_buf);
    {
        bson_buffer *sub = bson_append_start_object(&op_buf, "$set");
        bson_append_int(sub, octstr_get_cstr(fields->field_status), status);
        bson_append_finish_object(sub);
    }
    bson_from_buffer(&op, &op_buf);

    MONGO_TRY {
        mongo_update(conn, mongodb_namespace, &cond, &op, 0);
    } MONGO_CATCH {
        mongodb_error("dlr_mongodb_update", conn->exception.type);
    }

    dbpool_conn_produce(pconn);
    bson_destroy(&cond);
    bson_destroy(&op);
}
Beispiel #21
0
int test_bson_deep_nesting( void ) {
    int i;
    bson b[1];
    bson_init( b );

    for ( i = 0; i < 128; ++i )
    {
        bson_append_start_object( b, "sub" );
        bson_append_string( b, "hello", "hi" );
    }

    for ( i = 0; i < 128; ++i )
    {
        bson_append_finish_object( b );
    }

    bson_finish( b );
    bson_destroy( b );

    return 0;
}
Beispiel #22
0
int do_trunc(struct inode * e, off_t off) {
    bson cond;
    int res;
    mongo * conn = get_conn();

    if(off > e->size) {
        e->size = off;
        return 0;
    }

    pthread_mutex_lock(&e->wr_lock);
    if(e->wr_extent) {
        if(off < 0 && (res = serialize_extent(e, e->wr_extent)) != 0)
            return res;
        e->wr_extent->nnodes = 0;
    }
    e->wr_age = time(NULL);
    pthread_mutex_unlock(&e->wr_lock);

    bson_init(&cond);
    bson_append_oid(&cond, "inode", &e->oid);
    if(off > 0) {
        bson_append_start_object(&cond, "start");
        bson_append_long(&cond, "$gte", off);
        bson_append_finish_object(&cond);
    }
    bson_finish(&cond);

    res = mongo_remove(conn, extents_name, &cond, NULL);
    bson_destroy(&cond);
    if(res != 0) {
        fprintf(stderr, "Error removing extents in do_truncate\n");
        return -EIO;
    }

    e->size = off;

    return 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;
}
Beispiel #24
0
int _tmain(int argc, _TCHAR* argv[]) {
    setlocale(LC_ALL, "en_US.UTF-8");
    jb = ejdbnew();
    if (!ejdbopen(jb, "addressbook", JBOWRITER | JBOCREAT | JBOTRUNC)) {
        return 1;
    }

    //Get or create collection 'contacts'
    EJCOLL *coll = ejdbcreatecoll(jb, "contacts", NULL);

    bson bsrec;
    bson_oid_t oid;

    //One record
    bson_init(&bsrec);
    bson_append_string(&bsrec, "name", "John Travolta");
    bson_append_string(&bsrec, "phone", "333-222-333");
    bson_append_int(&bsrec, "age", 58);
    bson_finish(&bsrec);
    ejdbsavebson(coll, &bsrec, &oid);
    fprintf(stderr, "\nSaved Travolta");
    bson_destroy(&bsrec);


    //Another record
    bson_init(&bsrec);
    bson_append_string(&bsrec, "name", "Bruce Willis");
    bson_append_string(&bsrec, "phone", "222-333-222");
    bson_append_int(&bsrec, "age", 57);
    bson_finish(&bsrec);
    ejdbsavebson(coll, &bsrec, &oid);
    fprintf(stderr, "\nSaved Bruce Willis");
    bson_destroy(&bsrec);


    //Now select one record.
    //Query: {'name' : {'$begin' : 'Bru'}} //Name starts with 'Bru' string
    bson bq1;
    bson_init_as_query(&bq1);
    bson_append_start_object(&bq1, "name");
    bson_append_string(&bq1, "$begin", "Bru");
    bson_append_finish_object(&bq1);
    bson_finish(&bq1);
    EJQ *q1 = ejdbcreatequery(jb, &bq1, NULL, 0, NULL);

    uint32_t count;
    TCLIST *res = ejdbqryexecute(coll, q1, &count, 0, NULL);
    fprintf(stderr, "\n\nRecords found: %d\n", count);

    for (int i = 0; i < TCLISTNUM(res); ++i) {
        void *bsdata = TCLISTVALPTR(res, i);
        bson_print_raw((char*) bsdata, 0);
    }
    fprintf(stderr, "\n");

    //Dispose result set
    tclistdel(res);

    //Dispose query
    ejdbquerydel(q1);
    bson_destroy(&bq1);

    //Close database
    ejdbclose(jb);
    ejdbdel(jb);

	getc(stdin);
    return 0;
}
Beispiel #25
0
static switch_status_t my_on_reporting(switch_core_session_t *session)
{
	switch_status_t status = SWITCH_STATUS_SUCCESS;
	switch_channel_t *channel = switch_core_session_get_channel(session);
	switch_event_header_t *hi;
	switch_caller_profile_t *caller_profile;
	switch_app_log_t *app_log;
	bson cdr;
	int is_b;
	char *tmp;

	if (globals.shutdown) {
		return SWITCH_STATUS_SUCCESS;
	}

	is_b = channel && switch_channel_get_originator_caller_profile(channel);
	if (!globals.log_b && is_b) {
		const char *force_cdr = switch_channel_get_variable(channel, SWITCH_FORCE_PROCESS_CDR_VARIABLE);
		if (!switch_true(force_cdr)) {
			return SWITCH_STATUS_SUCCESS;
		}
	}

	bson_init(&cdr);

	/* Channel data */
	bson_append_start_object(&cdr, "channel_data");
	bson_append_string(&cdr, "state", switch_channel_state_name(switch_channel_get_state(channel)));
	bson_append_string(&cdr, "direction", switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound");
	bson_append_int(&cdr, "state_number", switch_channel_get_state(channel));

	if ((tmp = switch_channel_get_flag_string(channel))) {
		bson_append_string(&cdr, "flags", tmp);
		free(tmp);
	}

	if ((tmp = switch_channel_get_cap_string(channel))) {
		bson_append_string(&cdr, "caps", tmp);
		free(tmp);
	}
	bson_append_finish_object(&cdr);				/* channel_data */


	/* Channel variables */
	bson_append_start_object(&cdr, "variables");

	if ((hi = switch_channel_variable_first(channel))) {
		for (; hi; hi = hi->next) {
			if (!zstr(hi->name) && !zstr(hi->value)) {
				bson_append_string(&cdr, hi->name, hi->value);
			}
		}
		switch_channel_variable_last(channel);
	}

	bson_append_finish_object(&cdr);				/* variables */


	/* App log */
	if ((app_log = switch_core_session_get_app_log(session))) {
		switch_app_log_t *ap;

		bson_append_start_object(&cdr, "app_log");
		for (ap = app_log; ap; ap = ap->next) {
			bson_append_start_object(&cdr, "application");
			bson_append_string(&cdr, "app_name", ap->app);
			bson_append_string(&cdr, "app_data", ap->arg);
			bson_append_long(&cdr, "app_stamp", ap->stamp);
			bson_append_finish_object(&cdr);		/* application */
		}

		bson_append_finish_object(&cdr);			/* app_log */
	}


	/* Callflow */
	caller_profile = switch_channel_get_caller_profile(channel);

	while (caller_profile) {
		bson_append_start_object(&cdr, "callflow");

		if (!zstr(caller_profile->dialplan)) {
			bson_append_string(&cdr, "dialplan", caller_profile->dialplan);
		}

		if (!zstr(caller_profile->profile_index)) {
			bson_append_string(&cdr, "profile_index", caller_profile->profile_index);
		}

		if (caller_profile->caller_extension) {
			switch_caller_application_t *ap;

			bson_append_start_object(&cdr, "extension");

			bson_append_string(&cdr, "name", caller_profile->caller_extension->extension_name);
			bson_append_string(&cdr, "number", caller_profile->caller_extension->extension_number);

			if (caller_profile->caller_extension->current_application) {
				bson_append_string(&cdr, "current_app", caller_profile->caller_extension->current_application->application_name);
			}

			for (ap = caller_profile->caller_extension->applications; ap; ap = ap->next) {
				bson_append_start_object(&cdr, "application");
				if (ap == caller_profile->caller_extension->current_application) {
					bson_append_bool(&cdr, "last_executed", 1);
				}
				bson_append_string(&cdr, "app_name", ap->application_name);
				bson_append_string(&cdr, "app_data", ap->application_data);
				bson_append_finish_object(&cdr);
			}

			if (caller_profile->caller_extension->children) {
				switch_caller_profile_t *cp = NULL;

				for (cp = caller_profile->caller_extension->children; cp; cp = cp->next) {

					if (!cp->caller_extension) {
						continue;
					}

					bson_append_start_object(&cdr, "sub_extensions");
					bson_append_start_object(&cdr, "extension");

					bson_append_string(&cdr, "name", cp->caller_extension->extension_name);
					bson_append_string(&cdr, "number", cp->caller_extension->extension_number);
					bson_append_string(&cdr, "dialplan", cp->dialplan);
					if (cp->caller_extension->current_application) {
						bson_append_string(&cdr, "current_app", cp->caller_extension->current_application->application_name);
					}

					for (ap = cp->caller_extension->applications; ap; ap = ap->next) {
						bson_append_start_object(&cdr, "application");
						if (ap == cp->caller_extension->current_application) {
							bson_append_bool(&cdr, "last_executed", 1);
						}
						bson_append_string(&cdr, "app_name", ap->application_name);
						bson_append_string(&cdr, "app_data", ap->application_data);
						bson_append_finish_object(&cdr);
					}

					bson_append_finish_object(&cdr);	/* extension */
					bson_append_finish_object(&cdr);	/* sub_extensions */
				}
			}

			bson_append_finish_object(&cdr);			/* extension */
		}

		bson_append_start_object(&cdr, "caller_profile");
		set_bson_profile_data(&cdr, caller_profile);

		if (caller_profile->origination_caller_profile) {
			switch_caller_profile_t *cp = NULL;

			bson_append_start_object(&cdr, "origination");
			for (cp = caller_profile->origination_caller_profile; cp; cp = cp->next) {
				bson_append_start_object(&cdr, "origination_caller_profile");
				set_bson_profile_data(&cdr, cp);
				bson_append_finish_object(&cdr);
			}
			bson_append_finish_object(&cdr);			/* origination */
		}

		if (caller_profile->originator_caller_profile) {
			switch_caller_profile_t *cp = NULL;

			bson_append_start_object(&cdr, "originator");
			for (cp = caller_profile->originator_caller_profile; cp; cp = cp->next) {
				bson_append_start_object(&cdr, "originator_caller_profile");
				set_bson_profile_data(&cdr, cp);
				bson_append_finish_object(&cdr);
			}
			bson_append_finish_object(&cdr);			/* originator */
		}

		if (caller_profile->originatee_caller_profile) {
			switch_caller_profile_t *cp = NULL;

			bson_append_start_object(&cdr, "originatee");
			for (cp = caller_profile->originatee_caller_profile; cp; cp = cp->next) {
				bson_append_start_object(&cdr, "originatee_caller_profile");
				set_bson_profile_data(&cdr, cp);
				bson_append_finish_object(&cdr);
			}
			bson_append_finish_object(&cdr);			/* originatee */
		}

		bson_append_finish_object(&cdr);				/* caller_profile */

		/* Timestamps */
		if (caller_profile->times) {
			bson_append_start_object(&cdr, "times");

			/* Insert timestamps as long ints (microseconds) to preserve accuracy */
			bson_append_long(&cdr, "created_time", caller_profile->times->created);
			bson_append_long(&cdr, "profile_created_time", caller_profile->times->profile_created);
			bson_append_long(&cdr, "progress_time", caller_profile->times->progress);
			bson_append_long(&cdr, "progress_media_time", caller_profile->times->progress_media);
			bson_append_long(&cdr, "answered_time", caller_profile->times->answered);
			bson_append_long(&cdr, "bridged_time", caller_profile->times->bridged);
			bson_append_long(&cdr, "last_hold_time", caller_profile->times->last_hold);
			bson_append_long(&cdr, "hold_accum_time", caller_profile->times->hold_accum);
			bson_append_long(&cdr, "hangup_time", caller_profile->times->hungup);
			bson_append_long(&cdr, "resurrect_time", caller_profile->times->resurrected);
			bson_append_long(&cdr, "transfer_time", caller_profile->times->transferred);
			bson_append_finish_object(&cdr);			/* times */
		}

		bson_append_finish_object(&cdr);				/* callflow */
		caller_profile = caller_profile->next;
	}

	bson_finish(&cdr);

	switch_mutex_lock(globals.mongo_mutex);

	if (mongo_insert(globals.mongo_conn, globals.mongo_namespace, &cdr) != MONGO_OK) {
		if (globals.mongo_conn->err == MONGO_IO_ERROR) {
			mongo_error_t db_status;
			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "MongoDB connection failed; attempting reconnect...\n");
			db_status = mongo_reconnect(globals.mongo_conn);

			if (db_status != MONGO_OK) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MongoDB reconnect failed with error code %d\n", db_status);
				status = SWITCH_STATUS_FALSE;
			} else {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "MongoDB connection re-established.\n");
				if (mongo_insert(globals.mongo_conn, globals.mongo_namespace, &cdr) != MONGO_OK) {
					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mongo_insert: error code %d\n", globals.mongo_conn->err);
					status = SWITCH_STATUS_FALSE;
				}
			}

		} else {
			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mongo_insert: error code %d\n", globals.mongo_conn->err);
			status = SWITCH_STATUS_FALSE;
		}
	}

	switch_mutex_unlock(globals.mongo_mutex);
	bson_destroy(&cdr);

	return status;
}
Beispiel #26
0
int bson_append_finish_array( bson *b ) {
    return bson_append_finish_object( b );
}
Beispiel #27
0
Datei: t1.c Projekt: CowanSM/ejdb
void testBuildQuery1() {
    CU_ASSERT_PTR_NOT_NULL_FATAL(jb);
    /*
     Query = {
        "name" : Петров Петр,
        "age"  : 33,
        "family" : {
            "wife" : {
                "name"  : "Jeniffer",
                "age"   : {"$gt" : 25},
                "phone" : "444-111"
            },
            "children" : [
                {
                    "name" : "Dasha",
                    "age" : {"$in" : [1, 4, 10]}
                }
            ]
         }
     */
    bson q1;
    bson_init_as_query(&q1);
    bson_append_string(&q1, "name", "Петров Петр");
    bson_append_int(&q1, "age", 33);

    bson q1family_wife;
    bson_init_as_query(&q1family_wife);
    bson_append_string(&q1family_wife, "name", "Jeniffer");
    bson_append_start_object(&q1family_wife, "age");
    bson_append_int(&q1family_wife, "$gt", 25);
    bson_append_finish_object(&q1family_wife);

    bson_append_string(&q1family_wife, "phone", "444-111");
    bson_finish(&q1family_wife);

    bson q1family_child;
    bson_init_as_query(&q1family_child);
    bson_append_string(&q1family_child, "name", "Dasha");

    //"age" : {"$in" : [1, 4, 10]}
    bson q1family_child_age_IN;
    bson_init_as_query(&q1family_child_age_IN);
    bson_append_start_array(&q1family_child_age_IN, "$in");
    bson_append_int(&q1family_child_age_IN, "0", 1);
    bson_append_int(&q1family_child_age_IN, "1", 4);
    bson_append_int(&q1family_child_age_IN, "2", 10);
    bson_append_finish_array(&q1family_child_age_IN);
    bson_finish(&q1family_child_age_IN);
    bson_append_bson(&q1family_child, "age", &q1family_child_age_IN);
    bson_finish(&q1family_child);

    bson q1family;
    bson_init_as_query(&q1family);
    bson_append_bson(&q1family, "wife", &q1family_wife);
    bson_append_start_array(&q1family, "children");
    bson_append_bson(&q1family, "0", &q1family_child);
    bson_append_finish_array(&q1family);
    bson_finish(&q1family);

    bson_append_bson(&q1, "family", &q1family);
    bson_finish(&q1);

    CU_ASSERT_FALSE_FATAL(q1.err);
    CU_ASSERT_FALSE_FATAL(q1family.err);
    CU_ASSERT_FALSE_FATAL(q1family_wife.err);
    CU_ASSERT_FALSE_FATAL(q1family_child.err);
    CU_ASSERT_FALSE_FATAL(q1family_child_age_IN.err);

    EJQ *ejq = ejdbcreatequery(jb, &q1, NULL, 0, NULL);
    CU_ASSERT_PTR_NOT_NULL_FATAL(ejq);

    bson_destroy(&q1);
    bson_destroy(&q1family);
    bson_destroy(&q1family_wife);
    bson_destroy(&q1family_child);
    bson_destroy(&q1family_child_age_IN);

    CU_ASSERT_PTR_NOT_NULL_FATAL(ejq->qobjlist);
    TCLIST *qmap = ejq->qobjlist;
    CU_ASSERT_EQUAL(qmap->num, 7);

    for (int i = 0; i < TCLISTNUM(qmap); ++i) {

        const EJQF *qf = TCLISTVALPTR(qmap, i);
        CU_ASSERT_PTR_NOT_NULL_FATAL(qf);
        const char* key = qf->fpath;

        switch (i) {
            case 0:
            {
                CU_ASSERT_STRING_EQUAL(key, "name");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "Петров Петр");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 1:
            {
                CU_ASSERT_STRING_EQUAL(key, "age");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "33");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCNUMEQ);
                break;
            }
            case 2:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.wife.name");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "Jeniffer");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 3:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.wife.age");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "25");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCNUMGT);
                break;
            }
            case 4:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.wife.phone");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "444-111");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 5:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.children.0.name");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "Dasha");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 6:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.children.0.age");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_EQUAL(qf->ftype, BSON_ARRAY);
                TCLIST *al = tclistload(qf->expr, qf->exprsz);
                char* als = tcstrjoin(al, ',');
                CU_ASSERT_STRING_EQUAL(als, "1,4,10");
                TCFREE(als);
                tclistdel(al);
                CU_ASSERT_EQUAL(qf->tcop, TDBQCNUMOREQ);
                break;
            }
        }
    }

    ejdbquerydel(ejq);
}
Beispiel #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);
}
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;
}
Beispiel #30
0
void testBSONExportImport2(void) {
    EJDB *jb = ejdbnew();
    CU_ASSERT_TRUE_FATAL(ejdbopen(jb, "dbt4_export", JBOWRITER | JBOCREAT | JBOTRUNC));
    EJCOLL *coll = ejdbcreatecoll(jb, "col1", NULL);
    if (!coll) {
        eprint(jb, __LINE__, "testBSONExportImport2");
    }
    CU_ASSERT_TRUE(coll != NULL);
    bson_oid_t oid;
    const char *log = NULL;

    bson bv1;
    bson_init(&bv1);
    bson_append_int(&bv1, "a", 1);
    bson_append_string(&bv1, "c", "d");
    bson_finish(&bv1);
    ejdbsavebson(coll, &bv1, &oid);
    bson_destroy(&bv1);

    EJCOLLOPTS copts = {0};
    copts.large = true;
    copts.records = 200000;
    coll = ejdbcreatecoll(jb, "col2", &copts);
    if (!coll) {
        eprint(jb, __LINE__, "testBSONExportImport2");
    }
    CU_ASSERT_TRUE(coll != NULL);
    CU_ASSERT_TRUE(ejdbsetindex(coll, "f", JBIDXSTR | JBIDXNUM));
    bson_init(&bv1);
    bson_append_int(&bv1, "e", 1);
    bson_append_string(&bv1, "f", "g");
    bson_finish(&bv1);
    ejdbsavebson(coll, &bv1, &oid);
    bson_destroy(&bv1);

    bson_init(&bv1);
    bson_append_int(&bv1, "e", 2);
    bson_append_string(&bv1, "f", "g2");
    bson_finish(&bv1);
    ejdbsavebson(coll, &bv1, &oid);
    bson_destroy(&bv1);

    bson cmd;
    bson_init(&cmd);
    bson_append_start_object(&cmd, "export");
    bson_append_string(&cmd, "path", "testBSONExportImport2");
    bson_append_start_array(&cmd, "cnames");
    bson_append_string(&cmd, "0", "col1");
    bson_append_string(&cmd, "1", "col2");
    bson_append_finish_array(&cmd);
    bson_append_finish_object(&cmd);
    bson_finish(&cmd);
    bson *bret = ejdbcommand(jb, &cmd);
    CU_ASSERT_PTR_NOT_NULL_FATAL(bret);
    bson_destroy(&cmd);

    bson_iterator it;
    bson_iterator_init(&it, bret);
    CU_ASSERT_TRUE(bson_find_fieldpath_value("error", &it) == BSON_EOO);
    bson_iterator_init(&it, bret);
    CU_ASSERT_TRUE(bson_compare_long(0, bson_data(bret), "errorCode") == 0);
    bson_iterator_init(&it, bret);
    CU_ASSERT_TRUE(bson_find_fieldpath_value("log", &it) == BSON_STRING);
    bson_del(bret);

    bson *ometa = ejdbmeta(jb);
    CU_ASSERT_TRUE_FATAL(ometa != NULL);

    ejdbclose(jb);
    ejdbdel(jb);

    //Restore data:
    jb = ejdbnew();
    CU_ASSERT_TRUE_FATAL(ejdbopen(jb, "dbt4_export", JBOWRITER | JBOCREAT));

    coll = ejdbgetcoll(jb, "col1");
    CU_ASSERT_PTR_NOT_NULL_FATAL(coll);
    bson_init(&bv1);
    bson_append_int(&bv1, "e", 2);
    bson_finish(&bv1);
    CU_ASSERT_TRUE(ejdbsavebson(coll, &bv1, &oid));
    bson_destroy(&bv1);

    bson_init(&cmd);
    bson_append_start_object(&cmd, "import");
    bson_append_string(&cmd, "path", "testBSONExportImport2");
    bson_append_int(&cmd, "mode", JBIMPORTREPLACE);
    bson_append_start_array(&cmd, "cnames");
    bson_append_string(&cmd, "0", "col1");
    bson_append_string(&cmd, "1", "col2");
    bson_append_finish_array(&cmd);
    bson_append_finish_object(&cmd);
    bson_finish(&cmd);
    bret = ejdbcommand(jb, &cmd);
    CU_ASSERT_PTR_NOT_NULL_FATAL(bret);
    bson_destroy(&cmd);

    bson_iterator_init(&it, bret);
    CU_ASSERT_TRUE_FATAL(bson_find_fieldpath_value("log", &it) == BSON_STRING);
    log = bson_iterator_string(&it);

    CU_ASSERT_PTR_NOT_NULL(strstr(log, "Replacing all data in 'col1'"));
    CU_ASSERT_PTR_NOT_NULL(strstr(log, "1 objects imported into 'col1'"));
    CU_ASSERT_PTR_NOT_NULL(strstr(log, "2 objects imported into 'col2'"));
    bson_del(bret);
    log = NULL;

    bson *nmeta = ejdbmeta(jb);
    CU_ASSERT_TRUE_FATAL(nmeta != NULL);

    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.0.name", strlen("collections.0.name")) == 0);
    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.0.records", strlen("collections.0.records")) == 0);
    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.name", strlen("collections.1.name")) == 0);
    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.records", strlen("collections.1.records")) == 0);
    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.options.buckets", strlen("collections.1.options.buckets")) == 0);
    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.options.large", strlen("collections.1.options.large")) == 0);
    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.0.field", strlen("collections.1.indexes.0.field")) == 0);
    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.0.type", strlen("collections.1.indexes.0.type")) == 0);
    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.0.records", strlen("collections.1.indexes.0.records")) == 0);
    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.1.field", strlen("collections.1.indexes.1.field")) == 0);
    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.1.type", strlen("collections.1.indexes.1.type")) == 0);
    CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.1.records", strlen("collections.1.indexes.1.records")) == 0);

    ejdbclose(jb);
    ejdbdel(jb);

    jb = ejdbnew();
    CU_ASSERT_TRUE_FATAL(ejdbopen(jb, "dbt4_export", JBOWRITER | JBOCREAT | JBOTRUNC));

    coll = ejdbcreatecoll(jb, "col1", NULL);
    CU_ASSERT_PTR_NOT_NULL_FATAL(coll);
    bson_init(&bv1);
    bson_append_int(&bv1, "e", 2);
    bson_finish(&bv1);
    CU_ASSERT_TRUE(ejdbsavebson(coll, &bv1, &oid));

    EJQ *q = ejdbcreatequery(jb, &bv1, NULL, 0, NULL);
    CU_ASSERT_PTR_NOT_NULL_FATAL(q);
    uint32_t count = 0;
    ejdbqryexecute(coll, q, &count, JBQRYCOUNT, NULL);
    CU_ASSERT_EQUAL(count, 1);

    bson_init(&cmd);
    bson_append_start_object(&cmd, "import");
    bson_append_string(&cmd, "path", "testBSONExportImport2");
    bson_append_int(&cmd, "mode", JBIMPORTUPDATE);
    bson_append_finish_object(&cmd);
    bson_finish(&cmd);
    bret = ejdbcommand(jb, &cmd);
    CU_ASSERT_PTR_NOT_NULL_FATAL(bret);
    bson_destroy(&cmd);
    bson_del(bret);

    coll = ejdbcreatecoll(jb, "col1", NULL);
    ejdbqryexecute(coll, q, &count, JBQRYCOUNT, NULL);
    CU_ASSERT_EQUAL(count, 1);

    ejdbquerydel(q);
    bson_destroy(&bv1);
    ejdbclose(jb);
    ejdbdel(jb);

    bson_del(ometa);
    bson_del(nmeta);
}