Exemplo n.º 1
0
void find_in_array(bson_iterator *it, char *key_ref, char *value_ref, char *key_needed, char *value_needed) 
{
	char value_ref_found[MONGO_STRING_LENGTH];
	char value_needed_found[MONGO_STRING_LENGTH];

	bson_iterator i;
	
	while(bson_iterator_next(it)) {
		switch(bson_iterator_type(it)){
			case bson_string:
				if (strcmp(bson_iterator_key(it), key_ref) == 0)
					strcpy(value_ref_found, bson_iterator_string(it));
				if (strcmp(bson_iterator_key(it), key_needed) == 0)
					strcpy(value_needed_found, bson_iterator_string(it));
				break;
			case bson_object:
			case bson_array:
				bson_iterator_init(&i, bson_iterator_value(it));
				find_in_array(&i, key_ref, value_ref, key_needed, value_needed);
				break;
			default:
				break;
		}
	}
	
	if (strcmp(value_ref_found, value_ref) == 0)
		strcpy(value_needed, value_needed_found);
}
Exemplo n.º 2
0
Arquivo: t1.c Projeto: CowanSM/ejdb
void testSaveLoad() {
    CU_ASSERT_PTR_NOT_NULL_FATAL(jb);
    bson_oid_t oid;
    EJCOLL *ccoll = ejdbcreatecoll(jb, "contacts", NULL);
    CU_ASSERT_PTR_NOT_NULL(ccoll);

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

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


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

    int btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_OID);
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_STRING);
    CU_ASSERT(!strcmp("name", bson_iterator_key(&it1)));
    CU_ASSERT(!strcmp("Петров Петр", bson_iterator_string(&it1)));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_STRING);
    CU_ASSERT(!strcmp("phone", bson_iterator_key(&it1)));
    CU_ASSERT(!strcmp("333-222-333", bson_iterator_string(&it1)));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_INT);
    CU_ASSERT(!strcmp("age", bson_iterator_key(&it1)));
    CU_ASSERT(33 == bson_iterator_int(&it1));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_LONG);
    CU_ASSERT(!strcmp("longage", bson_iterator_key(&it1)));
    CU_ASSERT(0xFFFFFFFFFF01LL == bson_iterator_long(&it1));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_DOUBLE);
    CU_ASSERT(!strcmp("doubleage", bson_iterator_key(&it1)));
    CU_ASSERT_DOUBLE_EQUAL(bson_iterator_double(&it1), 0.3, 0.1);
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_EOO);
    bson_del(lbson);
}
Exemplo n.º 3
0
int _iterator_getComplex(bson_iterator* iter, struct Rcomplex* z) {
    bson_iterator sub;
    if (bson_iterator_type(iter) != BSON_OBJECT)
        return 0;
    bson_iterator_subiterator(iter, &sub);
    if (bson_iterator_next(&sub) != BSON_DOUBLE || strcmp(bson_iterator_key(&sub), "r") != 0)
        return 0;
    z->r = bson_iterator_double(&sub);
    if (bson_iterator_next(&sub) != BSON_DOUBLE || strcmp(bson_iterator_key(&sub), "i") != 0)
        return 0;
    z->i = bson_iterator_double(&sub);
    if (bson_iterator_next(&sub) != BSON_EOO)
        return 0;
    return 1;
}
Exemplo n.º 4
0
void bson_print_raw( const char * data , int depth ){
    bson_iterator i;
    const char * key;
    int temp;
    char oidhex[25];
    bson_iterator_init( &i , data );

    while ( bson_iterator_next( &i ) ){
        bson_type t = bson_iterator_type( &i );
        if ( t == 0 )
            break;
        key = bson_iterator_key( &i );
        
        for ( temp=0; temp<=depth; temp++ )
            printf( "\t" );
        printf( "%s : %d \t " , key , t );
        switch ( t ){
        case bson_int: printf( "%d" , bson_iterator_int( &i ) ); break;
        case bson_double: printf( "%f" , bson_iterator_double( &i ) ); break;
        case bson_bool: printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); break;
        case bson_string: printf( "%s" , bson_iterator_string( &i ) ); break;
        case bson_null: printf( "null" ); break;
        case bson_oid: bson_oid_to_string(bson_iterator_oid(&i), oidhex); printf( "%s" , oidhex ); break;
        case bson_object:
        case bson_array:
            printf( "\n" );
            bson_print_raw( bson_iterator_value( &i ) , depth + 1 );
            break;
        default:
            fprintf( stderr , "can't print type : %d\n" , t );
        }
        printf( "\n" );
    }
}
Exemplo n.º 5
0
int 
ComputeOperator(BSONElem* val,BSONElem* query){
   bson_iterator q,o;
   bson_type qType,tType;

   bson_iterator_from_buffer(&q, query->value);
   bson_iterator_from_buffer(&o, val->value);
   bson qBson,oBson;
   bson_init_finished_data(&qBson,(char*)query->value);
   bson_init_finished_data(&oBson,(char*)val->value);
   while((qType = bson_iterator_next(&q))) { 
      const char* key = bson_iterator_key(&q);
      BSONElem qVal;
      BSONElemInitFromItr(&qVal,&q);
      int cVal = 0,r;
      uint8_t cont = False;
      int error;
      QueryOpDesc* desc = HTFind(opTable,key,strlen(key)+1);
      if (desc) {
         cont = desc->fn(val,&qVal,&error);
         if (!cont) {
            return False;
         }
      } else {
         // no such operator
         return False;
      }
   }
   //printf("Returning true\n");
   return True;
}
Exemplo n.º 6
0
uint8_t
XTDBRemoveFromIndex(XTDBHandle* handle,BinaryStr* key,BinaryStr* data) {
    bson_oid_t oid;
    bson_iterator q;
    bson_type qType;
    uint8_t ret = True;
    _S_FN(indexRemove);

    bson_iterator_from_buffer(&q, data->data);

    while((qType = bson_iterator_next(&q))) {
        const char* keyVal = bson_iterator_key(&q);
        //printf("%s\n",keyVal);
        DataBaseBE* db;
        BSONElem qVal;
        BSONElemInitFromItr(&qVal,&q);
        db = XTDBGetIndex(handle,keyVal);
        if (db) {
            bson outBson;
            BinaryStr out;
            ConvertToBStr(&qVal,&outBson);
            BsonToBStr(&outBson,&out);
            ret = DBDeleteKeyVal(db,&out,key);
            if (!ret) {
                handle->error = XTDB_NO_ENTRY;
                bson_destroy(&outBson);
                break;
            }
            bson_destroy(&outBson);
        }
    }
    _E_FN(indexRemove);
    return ret;
    //return DBSet(handle->mainDB,&key,&val,False);
}
Exemplo n.º 7
0
int MCPContentPersonAward::set_basic_info_from_bson_result(bson* bson_out, const char* aggregate_name)
{

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

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

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

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

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


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


            LOGG("[GWJ] %s: set MCPContentPersonAwardRecord.kudou[%d]",
                     __FUNCTION__, detail->kudou;
        }
        else if(0 == strcmp(key, "commodity_id"))
Exemplo n.º 8
0
bson_type bson_find( bson_iterator *it, const bson *obj, const char *name ) {
    bson_iterator_init( it, (bson *)obj );
    while( bson_iterator_next( it ) ) {
        if ( strcmp( name, bson_iterator_key( it ) ) == 0 )
            break;
    }
    return bson_iterator_type( it );
}
Exemplo n.º 9
0
void MongodbClient::ReadData(KeyedArchive* archive, void* bsonObj)
{
    if((!archive) || (!bsonObj)) return;

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

        switch (type)
        {
            case BSON_STRING:
                archive->SetString(key, String(bson_iterator_string(&it)));
                break;
                
            case BSON_INT:
                archive->SetInt32(key, bson_iterator_int(&it));
                break;
                
            case BSON_LONG:
                archive->SetInt32(key, (int32)bson_iterator_long(&it));
                break;
                
            case BSON_DOUBLE:
                archive->SetFloat(key, (float32)bson_iterator_double(&it));
                break;
                
            case BSON_OBJECT:
            {
                bson sub;
                
                bson_iterator_subobject(&it, &sub);
                
                KeyedArchive* subArchive = new KeyedArchive();
                ReadData(subArchive, &sub);
                archive->SetArchive(key, subArchive);
                SafeRelease(subArchive);
                break;
            }
                
            case BSON_OID:
                //TODO: add 12-bytes array
                //bson_append_oid(object, key, bson_iterator_oid(&it));
                break;
                
                
            default:
                DVASSERT(false);
                Logger::Error("[MongodbUpdateObject::ReadData] Not implemented type: %d", type);
                break;
        }
    }
}
Exemplo n.º 10
0
void lua_push_bson_table(lua_State *L, bson_iterator *it) {
    bson_type bt;
    lua_push_bsontype_table(L, BSON_OBJECT);
    while ((bt = bson_iterator_next(it)) != BSON_EOO) {
        lua_pushstring(L, bson_iterator_key(it));
        lua_push_bson_value(L, it);
        lua_rawset(L, -3);
    }
}
Exemplo n.º 11
0
MONGO_EXPORT bson_type bson_find( bson_iterator *it, const bson *obj, const char *name ) {
    check_mongo_object( (void*)obj );
    check_mongo_object( it );
    bson_iterator_init( it, (bson *)obj );
    while( bson_iterator_next( it ) ) {
        if ( strcmp( name, bson_iterator_key( it ) ) == 0 )
            break;
    }
    return bson_iterator_type( it );
}
Exemplo n.º 12
0
static void bson_decode_tree(lua_State* L, bson_iterator* b,int set_t){
    const char *key;
    while(bson_iterator_next(b))
    {
        bson_type t = bson_iterator_type(b);
        if(t == 0)
        {
            lua_pushnil(L);
            break;
        }
        key = bson_iterator_key(b);
        if(set_t)
        {
            lua_pushstring(L,key);
        }
        LOG_MSG("type %d,key %s",t,key);
        switch(t)
        {
            case BSON_DOUBLE:
                lua_pushnumber(L,bson_iterator_double(b));
                break;
            case BSON_STRING:
                lua_pushstring(L,bson_iterator_string(b));
                break;
            case BSON_BOOL:
                lua_pushboolean(L,bson_iterator_bool( b ) ? true : false );
                break;
            case BSON_NULL:
                lua_pushnil(L);
                break;
            case BSON_INT:
                lua_pushinteger(L,bson_iterator_int(b));
                break;
            case BSON_OBJECT:
                bson_iterator t;
                bson_iterator_subiterator(b,&t);
                bson_decode_tree(L,&t);
                lua_settable(L,-3);
                break;
            case BSON_ARRAY:
                lua_newtable(L);
                bson_iterator s;
                bson_iterator_subiterator(b,&s);
                bson_decode_tree(L,&s,1);
                break;
            default:
                break;
        }
        if(set_t)
        {
            lua_settable(L,-3);
        }
    }

}
Exemplo n.º 13
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;
}
Exemplo n.º 14
0
void MongodbObject::EnableForEdit()
{
    bson *newObject = new bson();
    bson_init(newObject);
    
    bson_iterator it;
    bson_iterator_init(&it, objectData->object);

    while (bson_iterator_next(&it))
    {
        const char * key = bson_iterator_key(&it);
        bson_type type = bson_iterator_type(&it);
        
        switch (type)
        {
            case BSON_STRING:
                bson_append_string(newObject, key, bson_iterator_string(&it));
                break;

            case BSON_INT:
                bson_append_int(newObject, key, bson_iterator_int(&it));
                break;

            case BSON_LONG:
                bson_append_long(newObject, key, bson_iterator_long(&it));
                break;

            case BSON_DOUBLE:
                bson_append_double(newObject, key, bson_iterator_double(&it));
                break;

            case BSON_OBJECT:
                
                bson sub;
                bson_iterator_subobject(&it, &sub);
                bson_append_bson(newObject, key, &sub);
                break;
                
            case BSON_OID:
                bson_append_oid(newObject, key, bson_iterator_oid(&it));
                break;

                
            default:
                break;
        }
    }

    bson_destroy(objectData->object);
    SafeDelete(objectData->object);
    objectData->object = newObject;
}
Exemplo n.º 15
0
int mongo_create_index(mongo_connection * conn, const char * ns, bson * key, int options, bson * out){
    bson_buffer bb;
    bson b;
    bson_iterator it;
    char name[255] = {'_'};
    int i = 1;
    char idxns[1024];

    bson_iterator_init(&it, key->data);
    while(i < 255 && bson_iterator_next(&it)){
        strncpy(name + i, bson_iterator_key(&it), 255 - i);
        i += strlen(bson_iterator_key(&it));
    }
    name[254] = '\0';

    bson_buffer_init(&bb);
    bson_append_bson(&bb, "key", key);
    bson_append_string(&bb, "ns", ns);
    bson_append_string(&bb, "name", name);
    if (options & MONGO_INDEX_UNIQUE)
        bson_append_bool(&bb, "unique", 1);
    if (options & MONGO_INDEX_DROP_DUPS)
        bson_append_bool(&bb, "dropDups", 1);
    if (options & MONGO_INDEX_BACKGROUND)
        bson_append_bool(&bb, "background", 1);
    if (options & MONGO_INDEX_SPARSE)
        bson_append_bool(&bb, "sparse", 1);

    bson_from_buffer(&b, &bb);

    strncpy(idxns, ns, 1024-16);
    strcpy(strchr(idxns, '.'), ".system.indexes");
    mongo_insert(conn, idxns, &b);
    bson_destroy(&b);

    *strchr(idxns, '.') = '\0'; /* just db not ns */
    return mongo_cmd_get_last_error(conn, idxns, out);
}
Exemplo n.º 16
0
int read_inode(const bson * doc, struct inode * out) {
    bson_iterator i, sub;
    bson_type bt;
    const char * key;

    bson_iterator_init(&i, doc);
    while((bt = bson_iterator_next(&i)) > 0) {
        key = bson_iterator_key(&i);
        if(strcmp(key, "_id") == 0)
            memcpy(&out->oid, bson_iterator_oid(&i), sizeof(bson_oid_t));
        else if(strcmp(key, "mode") == 0)
            out->mode = bson_iterator_int(&i);
        else if(strcmp(key, "owner") == 0)
            out->owner = bson_iterator_long(&i);
        else if(strcmp(key, "group") == 0)
            out->group = bson_iterator_long(&i);
        else if(strcmp(key, "size") == 0)
            out->size = bson_iterator_long(&i);
        else if(strcmp(key, "created") == 0)
            out->created = bson_iterator_time_t(&i);
        else if(strcmp(key, "modified") == 0)
            out->modified = bson_iterator_time_t(&i);
        else if(strcmp(key, "data") == 0) {
            out->datalen = bson_iterator_string_len(&i);
            out->data = malloc(out->datalen + 1);
            strcpy(out->data, bson_iterator_string(&i));
        }
        else if(strcmp(key, "dirents") == 0) {
            while(out->dirents) {
                struct dirent * next = out->dirents->next;
                free(out->dirents);
                out->dirents = next;
            }
            bson_iterator_subiterator(&i, &sub);
            while((bt = bson_iterator_next(&sub)) > 0) {
                int len = bson_iterator_string_len(&sub);
                struct dirent * cde = malloc(sizeof(struct dirent) + len);
                if(!cde)
                    return -ENOMEM;
                strcpy(cde->path, bson_iterator_string(&sub));
                cde->len = bson_iterator_string_len(&sub);
                cde->next = out->dirents;
                out->dirents = cde;
                out->direntcount++;
            }
        }
    }

    return 0;
}
Exemplo n.º 17
0
 void InitWith(bson *obj)
 {
     bson_iterator it;
     bson_iterator_init(&it, obj);
     
     while (bson_iterator_next(&it))
     {
         const char * key = bson_iterator_key(&it);
         bson_type type = bson_iterator_type(&it);
         
         switch (type)
         {
             case BSON_STRING:
                 bson_append_string(object, key, bson_iterator_string(&it));
                 break;
                 
             case BSON_INT:
                 bson_append_int(object, key, bson_iterator_int(&it));
                 break;
                 
             case BSON_LONG:
                 bson_append_long(object, key, bson_iterator_long(&it));
                 break;
                 
             case BSON_DOUBLE:
                 bson_append_double(object, key, bson_iterator_double(&it));
                 break;
                 
             case BSON_OBJECT:
             {
                 bson sub;
                 
                 bson_iterator_subobject(&it, &sub);
                 bson_append_bson(object, key, &sub);
                 break;
             }
                 
             case BSON_OID:
                 bson_append_oid(object, key, bson_iterator_oid(&it));
                 break;
                 
                 
             default:
                 DVASSERT(false);
                 Logger::Error("[MongodbObjectInternalData::InitWith] Not implemented type: %d", type);
                 break;
         }
     }
 }
Exemplo n.º 18
0
int srld_find(bson_iterator *i, int keyc, char **keyv, char *data, int depth)
{
    bson_iterator_init(i, data);
    while(bson_iterator_next(i)){
        if (strcmp(keyv[depth], bson_iterator_key(i)) == 0) {
            //printf("%s == %s, depth %d\n", keyv[depth], bson_iterator_key(i), depth);
            if (depth == keyc-1) {
                return 1;
            } else {
                return srld_find(i, keyc, keyv, (char *)bson_iterator_value(i), depth+1);
            }
        }
    }
    return 0;
}
Exemplo n.º 19
0
/**
 * \brief This function tries to load tag group data from MongoDB
 */
static void vs_mongo_taggroup_load_data(struct VSTagGroup *tg,
		bson *bson_version)
{
	bson_iterator version_data_iter;

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

		bson_iterator_subiterator(&version_data_iter, &tags_iter);

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

			bson_iterator_subobject_init(&tags_iter, &bson_tag, 0);

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

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

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

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

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

					vs_mongo_tag_load_data(tag, &bson_tag);

					tag->flag = TAG_INITIALIZED;
				}
			}
		}
	}
}
Exemplo n.º 20
0
void json_from_bson_object(yajl_gen *g, bson_iterator *it)
{
    yajl_gen_map_open( *g );

    while( bson_iterator_next( it ) )
    {
        bson_type t = bson_iterator_type( it );
        const char* key = bson_iterator_key( it );

        yajl_gen_string( *g, (const unsigned char*)key, strlen(key) );

        json_from_bson_type( t, g, it );
    }

    yajl_gen_map_close( *g );
}
Exemplo n.º 21
0
int
QueryGetIndexesToUse(BinaryStr* query,ListNode* head){
   LIST_INIT(head);
   bson_iterator q,o;
   bson_type qType,tType;

   bson_iterator_from_buffer(&q, query->data);
   while((qType =bson_iterator_next(&q))) {
      const char* key = bson_iterator_key(&q);
      if (qType != BSON_ARRAY && qType != BSON_OBJECT ) {
         ListNode* node2 = (ListNode*) malloc(sizeof(*node2));
         node2->data = strdup(key);
         LIST_ADD_AFTER(node2,LIST_LAST(head));
      } 
   }
   return 0;
}
Exemplo n.º 22
0
int 
QueryMatch(BinaryStr* query, BinaryStr* obj ) {
   bson_iterator q,o;
   bson_type qType,tType;
   bson qBson,oBson;
   BSONElem objElem;

   bson_iterator_from_buffer(&q, query->data);
   bson_iterator_from_buffer(&o, obj->data);
   bson_init_finished_data(&qBson,(char*)query->data);
   bson_init_finished_data(&oBson,(char*)obj->data);


   while((qType = bson_iterator_next(&q))) {
      // for all keys in query
      char *newKey;
      const char* key = bson_iterator_key(&q);
      char* keyEnd;
      BSONElem qVal,oVal;
      newKey = strdup(key);
      if (!newKey) {
         return False;
      }
      keyEnd = strchr(newKey,'.');
      if (keyEnd) {
         *keyEnd = '\0';
         keyEnd ++;
      } else {
         //printf("No dot \n");
         keyEnd = newKey+strlen(newKey);
      }
      BSONElemInitFromItr(&qVal,&q);
      tType = bson_find(&o,&oBson,newKey) ;
      if (!tType) {
         return False;
      }
      BSONElemInitFromItr(&oVal,&o);
      if (!QueryElemMatch(keyEnd,&oVal,&qVal)) {
         free(newKey);
         return False;
      }
      free(newKey);
   }
   return True;
}
Exemplo n.º 23
0
bson_buffer * bson_append_element( bson_buffer * b, const char * name_or_null, const bson_iterator* elem){
    bson_iterator next = *elem;
    int size;

    bson_iterator_next(&next);
    size = next.cur - elem->cur;

    if (name_or_null == NULL){
        bson_ensure_space(b, size);
        bson_append(b, elem->cur, size);
    }else{
        int data_size = size - 2 - strlen(bson_iterator_key(elem));
        bson_append_estart(b, elem->cur[0], name_or_null, data_size);
        bson_append(b, bson_iterator_value(elem), data_size);
    }

    return b;
}
Exemplo n.º 24
0
bool bson_cmp( const bson *a, const bson *b )
{
    assert( a );
    assert( b );

    bson_iterator *a_it, *b_it;
    bool equal;
    size_t a_keys_count, b_keys_count;

    equal = true;
    a_it = malloc( sizeof( bson_iterator ) );
    b_it = malloc( sizeof( bson_iterator ) );
    bson_iterator_init( a_it, a );
    bson_iterator_init( b_it, b );
    a_keys_count = bson_keypars_count( a );
    b_keys_count = bson_keypars_count( b );

    if( a_keys_count == b_keys_count )
    {
        while( bson_iterator_next( a_it ) )
        {
            bson_type a_key_type = bson_iterator_type( a_it );
            const char *a_key = bson_iterator_key( a_it );
            bson_type b_key_type = bson_find( b_it, b, a_key );

            if( b_key_type == BSON_EOO ||
                    a_key_type != b_key_type ||
                    !bson_values_are_equal( a_key_type, a_it, b_it ) )
            {
                equal = false;
                break;
            }
        }
    }
    else
    {
        equal = false;
    }

    free( a_it );
    free( b_it );

    return equal;
}
Exemplo n.º 25
0
MONGO_EXPORT int bson_append_element( bson *b, const char *name_or_null, const bson_iterator *elem ) {
    bson_iterator next = *elem;
    size_t size;

    bson_iterator_next( &next );
    size = next.cur - elem->cur;

    if ( name_or_null == NULL ) {
        if( bson_ensure_space( b, size ) == BSON_ERROR )
            return BSON_ERROR;
        bson_append( b, elem->cur, size );
    }
    else {
        size_t data_size = size - 2 - strlen( bson_iterator_key( elem ) );
        bson_append_estart( b, elem->cur[0], name_or_null, data_size );
        bson_append( b, bson_iterator_value( elem ), data_size );
    }

    return BSON_OK;
}
Exemplo n.º 26
0
	bool FindField(bson_iterator *itIn, bson_iterator *itOut, const String &fieldname, bool recursive)
	{
		bool found = false;
		while(!found && bson_iterator_next(itIn))
		{
			String itKey = String(bson_iterator_key(itIn));
			if(fieldname == itKey)
			{
				*itOut = *itIn;
				found = true;
			}
			else if(    (recursive && (BSON_OBJECT == bson_iterator_type(itIn)))
                    ||  (recursive && (BSON_ARRAY == bson_iterator_type(itIn))))
			{
				bson_iterator subIt;
				bson_iterator_subiterator(itIn, &subIt);

				found = FindField(&subIt, itOut, fieldname, recursive);
			}
		}

		return found;
	}
Exemplo n.º 27
0
uint8_t
QueryObjMatch(bson* obj,bson* query) {
   bson_iterator oItr,qItr; 
   bson_type o,q;

   bson_iterator_init(&oItr,obj);
   bson_iterator_init(&qItr,query);

   while ((o=bson_iterator_next(&oItr))) {
      BSONElem oVal,qVal;
      int cVal;
      q = bson_find(&qItr,query,bson_iterator_key(&oItr));
      if (!q) {
         return False;
      }
      if ( (o == BSON_OBJECT && q != BSON_OBJECT ) ||
           (q == BSON_OBJECT && o != BSON_OBJECT ) ) {
         return False;
      }
      if ( o == q && o == BSON_OBJECT) {
         bson o1,q1;
         bson_iterator_subobject(&oItr,&o1);
         bson_iterator_subobject(&qItr,&q1);
         return QueryObjMatch(&o1,&q1);
      }
      BSONElemInitFromItr(&oVal,&oItr);
      BSONElemInitFromItr(&qVal,&qItr);
      if (CompareElem(&oVal,&qVal,&cVal)) {
         return False;
      }
      if (cVal) {
         return False;
      }
   }
   return True;
}
Exemplo n.º 28
0
clsSiteData::clsSiteData(int id, const bson *info)
{
    m_ID = id;
    bson_iterator it[1];
    bson_iterator_init(it, info);
    while (bson_iterator_next(it))
    {
        if (strcmp("Name", bson_iterator_key(it)) == 0)
            m_Name = bson_iterator_string(it);
            //else if(strcmp("AREA", bson_iterator_key(it)) == 0)
            //	m_Area = (float)bson_iterator_double(it);
        else if (strcmp("LocalX", bson_iterator_key(it)) == 0)
            m_XPR = (float) bson_iterator_double(it);
        else if (strcmp("LocalY", bson_iterator_key(it)) == 0)
            m_YPR = (float) bson_iterator_double(it);
        else if (strcmp("Lat", bson_iterator_key(it)) == 0)
            m_Latitude = (float) bson_iterator_double(it);
        else if (strcmp("Long", bson_iterator_key(it)) == 0)
            m_Longitude = (float) bson_iterator_double(it);
        else if (strcmp("Elevation", bson_iterator_key(it)) == 0)
            m_Elevation = (float) bson_iterator_double(it);
    }
}
Exemplo n.º 29
0
void bson_print_raw( const char *data , int depth ) {
    bson_iterator i;
    const char *key;
    int temp;
    bson_timestamp_t ts;
    char oidhex[25];
    bson scope;
    bson_iterator_from_buffer( &i, data );

    while ( bson_iterator_next( &i ) ) {
        bson_type t = bson_iterator_type( &i );
        if ( t == 0 )
            break;
        key = bson_iterator_key( &i );

        for ( temp=0; temp<=depth; temp++ )
            printf( "\t" );
        bson_printf( "%s : %d \t " , key , t );
        switch ( t ) {
        case BSON_DOUBLE:
            printf( "%f" , bson_iterator_double( &i ) );
            break;
        case BSON_STRING:
            printf( "%s" , bson_iterator_string( &i ) );
            break;
        case BSON_SYMBOL:
            printf( "SYMBOL: %s" , bson_iterator_string( &i ) );
            break;
        case BSON_OID:
            bson_oid_to_string( bson_iterator_oid( &i ), oidhex );
            printf( "%s" , oidhex );
            break;
        case BSON_BOOL:
            printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" );
            break;
        case BSON_DATE:
            printf( "%ld" , ( long int )bson_iterator_date( &i ) );
            break;
        case BSON_BINDATA:
            printf( "BSON_BINDATA" );
            break;
        case BSON_UNDEFINED:
            printf( "BSON_UNDEFINED" );
            break;
        case BSON_NULL:
            printf( "BSON_NULL" );
            break;
        case BSON_REGEX:
            printf( "BSON_REGEX: %s", bson_iterator_regex( &i ) );
            break;
        case BSON_CODE:
            printf( "BSON_CODE: %s", bson_iterator_code( &i ) );
            break;
        case BSON_CODEWSCOPE:
            printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) );
            bson_init( &scope );
            bson_iterator_code_scope( &i, &scope );
            printf( "\n\t SCOPE: " );
            bson_print( &scope );
            break;
        case BSON_INT:
            printf( "%d" , bson_iterator_int( &i ) );
            break;
        case BSON_LONG:
            printf( "%lld" , ( long long int )bson_iterator_long( &i ) );
            break;
        case BSON_TIMESTAMP:
            ts = bson_iterator_timestamp( &i );
            printf( "i: %d, t: %d", ts.i, ts.t );
            break;
        case BSON_OBJECT:
        case BSON_ARRAY:
            printf( "\n" );
            bson_print_raw( bson_iterator_value( &i ) , depth + 1 );
            break;
        default:
            bson_errprintf( "can't print type : %d\n" , t );
        }
        printf( "\n" );
    }
}
Exemplo n.º 30
0
// Checked
uint8_t
XTDBInsertToIndex(XTDBHandle* handle,BinaryStr* key,BinaryStr* data) {
    bson_oid_t oid;
    bson_iterator q;
    bson_type qType;
    uint8_t ret = True;
    _S_FN(indexInsert);

    if (!tcmaprnum(handle->indexes)) {
        _E_FN(indexInsert);
        return True;
    }

    bson_iterator_from_buffer(&q, data->data);
    while((qType = bson_iterator_next(&q))) {
        const char* keyVal = bson_iterator_key(&q);
        //printf("keyval %s\n",keyVal);
        DataBaseBE* db;
        BSONElem qVal;
        BSONElemInitFromItr(&qVal,&q);
        db = XTDBGetIndex(handle,keyVal);
        if (db) {
            bson outBson;
            BinaryStr out;
            if (qType == BSON_ARRAY) {
                assert(0);
                bson_iterator si;
                bson_type t;
                bson_iterator_subiterator(&q,&si);
                while ((t=bson_iterator_next(&si))) {
                    if (ConvertToBStr(&qVal,&outBson)) {
                        handle->error = XTDB_NO_MEM;
                        return False;
                    }
                    BsonToBStr(&outBson,&out);
                    ret = DBSet(db,&out,key,True);
                    bson_destroy(&outBson);
                    if (ret) {
                        handle->error = XTDB_IO_ERR;
                        goto error;
                    }
                }
            } else {

                if (ConvertToBStr(&qVal,&outBson)) {
                    handle->error = XTDB_NO_MEM;
                    return False;
                }
                BsonToBStr(&outBson,&out);
                ret = DBSet(db,&out,key,True);
                bson_destroy(&outBson);
                if (!ret) {
                    handle->error = XTDB_IO_ERR;
                    goto error;
                }
            }
        }

    }
    _E_FN(indexInsert);

    return ret;
error:
    // roll back is required?
    // can simply return error unnecessarily inserted elements will be eventually corrected when index is accessed
    return ret;
    //return DBSet(handle->mainDB,&key,&val,False);
}