Пример #1
0
SEXP mongo_get_databases(SEXP mongo_conn) {
    mongo* conn = _checkMongo(mongo_conn);
    bson out;
    if (mongo_simple_int_command(conn, "admin", "listDatabases", 1, &out) != MONGO_OK) {
        bson_destroy(&out);
        return R_NilValue;
    }
    bson_iterator it, databases, database;
    bson_iterator_init(&it, &out);
    bson_iterator_next(&it);
    bson_iterator_subiterator(&it, &databases);
    int count = 0;
    while (bson_iterator_next(&databases)) {
        bson_iterator_subiterator(&databases, &database);
        bson_iterator_next(&database);
        const char* name = bson_iterator_string(&database);
        if (strcmp(name, "admin") != 0 && strcmp(name, "local") != 0)
            count++;
    }
    SEXP ret;
    PROTECT(ret = allocVector(STRSXP, count));
    bson_iterator_subiterator(&it, &databases);
    int i = 0;
    while (bson_iterator_next(&databases)) {
        bson_iterator_subiterator(&databases, &database);
        bson_iterator_next(&database);
        const char* name = bson_iterator_string(&database);
        if (strcmp(name, "admin") != 0 && strcmp(name, "local") != 0)
            SET_STRING_ELT(ret, i++, mkChar(name));
    }
    bson_destroy(&out);
    UNPROTECT(1);
    return ret;
}
Пример #2
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);
        }
    }

}
Пример #3
0
void BSONElemItrInit(BSONElemItr * it,BSONElem* val) {
   it->val = val;
   it->useItr = False;
   if (it->val->type == BSON_ARRAY) {
      it->useItr = True;
      bson_iterator_subiterator(val->itr,&it->it);
   }
   it->more = True;
   
}
Пример #4
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;
}
Пример #5
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;
}
Пример #6
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;
				}
			}
		}
	}
}
Пример #7
0
uint8_t QueryOpINInt(BSONElem* val,BSONElem* qVal,int * err) {
   int cVal;
   bson_iterator si;    
   bson_type t;
   
   if (!val->value) {
      return False;
   } 

   bson_iterator_subiterator(qVal->itr,&si);
   while ((t=bson_iterator_next(&si))) {
      BSONElemInitFromItr(qVal,&si);
      if((!CompareElem(val,qVal,&cVal) )&& (cVal == 0) ) {
         return True;
      }
   } 
   return False;

}
Пример #8
0
void json_from_bson_type(bson_type t, yajl_gen *g, bson_iterator *it)
{
    switch( t )
    {
    case BSON_ARRAY:
    {
        bson_iterator subi[1];
        bson_iterator_subiterator( it, subi );
        json_from_bson_array( g, subi );
        break;
    }
    case BSON_OBJECT:
    {
        bson sub[1];
        bson_iterator_subobject( it, sub );
        bson_iterator subi[1];
        bson_iterator_init( subi, sub );
        json_from_bson_object( g, subi );
        break;
    }
    case BSON_DOUBLE:
        json_from_bson_double( g, it );
        break;
    case BSON_STRING:
        json_from_bson_string( g, it );
        break;
    case BSON_BOOL:
        json_from_bson_bool( g, it );
        break;
    case BSON_NULL:
        json_from_bson_null( g );
        break;
    case BSON_INT:
        json_from_bson_int( g, it );
        break;
    case BSON_LONG:
        json_from_bson_long( g, it );
        break;
    default:
        break;
    }
}
Пример #9
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;
	}
Пример #10
0
EXPORT mxArray* mongo_bson_array_value(struct bson_iterator_* i) {
    bson_type sub_type, common_type;
    struct Rcomplex z;
    bson_iterator sub[MAXDIM+1];
    mwSize ndims = 0;
    mwSize count[MAXDIM+1];
    mwSize dim[MAXDIM+1];
    mwSize* mdim = dim + 1;
    mwSize sizes[MAXDIM+1];
    mxArray* ret;
    mwSize depth, j, len, ofs;
    int isRow = 0;
    sub[0] = *(bson_iterator*)i;
    /* count number of dimensions.  This is equal to the number of
       consecutive array markers in the BSON */
    do {
        bson_iterator_subiterator(&sub[ndims], &sub[ndims+1]);
        if (++ndims > MAXDIM) {
            mexPrintf("Max dimensions (%d) exceeded. Use an iterator\n", MAXDIM);
            return 0;
        }
        sub_type = bson_iterator_next(&sub[ndims]);
    }
    while (sub_type == BSON_ARRAY);

    /* get the first data value's type */
    switch (common_type = sub_type) {
    case BSON_INT: ;
    case BSON_LONG: ;
    case BSON_DOUBLE: ;
 /* case BSON_STRING: ; */
    case BSON_BOOL: ;
    case BSON_DATE:
        break;
    case BSON_OBJECT:
        if (_iterator_getComplex(&sub[ndims], &z))
            break;
        /* fall thru to default */
    default:
        /* including empty array */
        mexPrintf("Unable to convert array - invalid type (%d)", common_type);
        return 0;
    }

    /* initial lowest level count */
    for (j = 0; j <= ndims; j++)
        count[j] = 1;
    while ((sub_type = bson_iterator_next(&sub[ndims])) != BSON_EOO) {
        if (sub_type != common_type) {
            mexPrintf("Unable to convert array - inconsistent types");
            return 0;
        }
        if (sub_type == BSON_OBJECT && !_iterator_getComplex(&sub[ndims], &z)) {
            mexPrintf("Unable to convert array - invalid subobject");
            return 0;
        }
        ++count[ndims];
    }

    /* step through rest of array -- checking common type and dimensions */
    memset(dim, 0, sizeof(dim));
    depth = ndims;
    while (depth >= 1) {
        sub_type = bson_iterator_next(&sub[depth]);
        switch (sub_type) {
        case BSON_EOO:
            if (dim[depth] == 0)
                dim[depth] = count[depth];
            else if (dim[depth] != count[depth]) {
                mexPrintf("Unable to convert array - inconsistent dimensions");
                return 0;
            }
            depth--;
            break;
        case BSON_ARRAY:
            count[depth]++;
            bson_iterator_subiterator(&sub[depth], &sub[depth+1]);
            if (++depth > ndims) {
                mexPrintf("Unable to convert array - inconsistent dimensions");
                return 0;
            }
            count[depth] = 0;
            break;
        case BSON_INT: ;
        case BSON_LONG: ;
        case BSON_DOUBLE: ;
/*      case BSON_STRING: ; */
        case BSON_BOOL: ;
        case BSON_DATE: ;
GotEl:  {
            if (depth != ndims || sub_type != common_type) {
                mexPrintf("Unable to convert array - inconsistent dimensions or types");
                return 0;
            }
            count[depth]++;
            break;
        }
        case BSON_OBJECT:
            if (_iterator_getComplex(&sub[depth], &z))
                goto GotEl;
            /* fall thru to default */
        default:
            mexPrintf("Unable to convert array - invalid type (%d)", sub_type);
            return 0;
        }
    }

    if (ndims > 1) {
        j = dim[ndims];            /* reverse row and column */
        dim[ndims] = dim[ndims-1];
        dim[ndims-1] = j;
    }
    /* calculate offset each dimension multiplies it's index by */
    len = 1;
    for (depth = ndims; depth > 0; depth--) {
        sizes[depth] = len;
        len *= dim[depth];
    }

    if (ndims > 1) {
        reverse(mdim, ndims); /* reverse dimensions for Matlab */
        j = sizes[ndims];
        sizes[ndims] = sizes[ndims-1];
        sizes[ndims-1] = j;
    } else {
        isRow = 1;
        ndims = 2;
        mdim[1] = mdim[0];
        mdim[0] = 1;
    }
/*
    for (j = 1; j <= ndims; j++)
        mexPrintf("%d ", dim[j]);
    mexPrintf("\n");

    for (j = 1; j <= ndims; j++)
        mexPrintf("%d ", sizes[j]);
    mexPrintf("\n");
*/
    switch (common_type) {
    case BSON_INT:    ret = mxCreateNumericArray(ndims, mdim, mxINT32_CLASS, mxREAL); break;
    case BSON_LONG:   ret = mxCreateNumericArray(ndims, mdim, mxINT64_CLASS, mxREAL); break;
    case BSON_DATE:
    case BSON_DOUBLE: ret = mxCreateNumericArray(ndims, mdim, mxDOUBLE_CLASS, mxREAL); break;
/*  case BSON_STRING: */
    case BSON_BOOL:   ret = mxCreateLogicalArray(ndims, mdim); break;
    case BSON_OBJECT: ret = mxCreateNumericArray(ndims, mdim, mxDOUBLE_CLASS, mxCOMPLEX); break;
    default:
        /* never reaches here */
        ret = 0;
    }

    if (isRow)
        ndims--;
    /* step through array(s) again, pulling out values */
    bson_iterator_subiterator(&sub[0], &sub[1]);
    depth = 1;
    count[depth] = 0;
    while (depth >= 1) {
        sub_type = bson_iterator_next(&sub[depth]);
        count[depth]++;
        if (sub_type == BSON_EOO) {
            depth--;
        } else if (sub_type == BSON_ARRAY) {
            bson_iterator_subiterator(&sub[depth], &sub[depth+1]);
            depth++;
            count[depth] = 0;
        } else {
            ofs = 0;
            for (j = 1; j <= ndims; j++)
                ofs += sizes[j] * (count[j] - 1);

            switch (sub_type) {
                case BSON_INT:
                    ((int*)mxGetData(ret))[ofs] = bson_iterator_int(&sub[depth]);
                    break;
                case BSON_DATE:
                    mxGetPr(ret)[ofs] = 719529.0 + bson_iterator_date(&sub[depth]) / (1000.0 * 60 * 60 * 24);
                    break;
                case BSON_DOUBLE: 
                    mxGetPr(ret)[ofs] = bson_iterator_double(&sub[depth]);
                    break;
                case BSON_LONG:
                    ((int64_t*)mxGetData(ret))[ofs] = bson_iterator_long(&sub[depth]);
                    break;
                case BSON_STRING:
                    break;
                case BSON_BOOL: ;
                    ((mxLogical*)mxGetData(ret))[ofs] = bson_iterator_bool(&sub[depth]);
                    break;
                case BSON_OBJECT:
                    _iterator_getComplex(&sub[depth], &z);
                    mxGetPr(ret)[ofs] = z.r;
                    mxGetPi(ret)[ofs] = z.i;
                    break;
                default: ;
                    /* never reaches here */
            }
        }
    }

    return ret;
}
Пример #11
0
/**
 * \brief This function tries to load tag from MongoDB
 */
static void vs_mongo_tag_load_data(struct VSTag *tag,
		bson *bson_tag)
{
	bson_iterator tag_iter;

	if( bson_find(&tag_iter, bson_tag, "data") == BSON_ARRAY) {
		bson_iterator data_iter;
		uint8 val_uint8;
		uint16 val_uint16;
		uint32 val_uint32;
		uint64 val_uint64;
		real32 val_real32;
		real64 val_real64;
		const char *str_value;
		int value_index = 0;

		bson_iterator_subiterator(&tag_iter, &data_iter);

		/* Go through all values */
		switch(tag->data_type) {
		case VRS_VALUE_TYPE_UINT8:
			while( bson_iterator_next(&data_iter) == BSON_INT ) {
				val_uint8 = (uint8)bson_iterator_int(&data_iter);
				vs_tag_set_values(tag, 1, value_index, &val_uint8);
				value_index++;
			}
			break;
		case VRS_VALUE_TYPE_UINT16:
			while( bson_iterator_next(&data_iter) == BSON_INT ) {
				val_uint16 = (uint16)bson_iterator_int(&data_iter);
				vs_tag_set_values(tag, 1, value_index, &val_uint16);
				value_index++;
			}
			break;
		case VRS_VALUE_TYPE_UINT32:
			while( bson_iterator_next(&data_iter) == BSON_INT ) {
				val_uint32 = (uint32)bson_iterator_int(&data_iter);
				vs_tag_set_values(tag, 1, value_index, &val_uint32);
				value_index++;
			}
			break;
		case VRS_VALUE_TYPE_UINT64:
			while( bson_iterator_next(&data_iter) == BSON_LONG ) {
				val_uint64 = (uint64)bson_iterator_long(&data_iter);
				vs_tag_set_values(tag, 1, value_index, &val_uint64);
				value_index++;
			}
			break;
		case VRS_VALUE_TYPE_REAL16:
			/* TODO: add support for float16 */
			break;
		case VRS_VALUE_TYPE_REAL32:
			while( bson_iterator_next(&data_iter) == BSON_DOUBLE ) {
				val_real32 = (real32)bson_iterator_double(&data_iter);
				vs_tag_set_values(tag, 1, value_index, &val_real32);
				value_index++;
			}
			break;
		case VRS_VALUE_TYPE_REAL64:
			while( bson_iterator_next(&data_iter) == BSON_DOUBLE ) {
				val_real64 = (real64)bson_iterator_double(&data_iter);
				vs_tag_set_values(tag, 1, value_index, &val_real64);
				value_index++;
			}
			break;
		case VRS_VALUE_TYPE_STRING8:
			str_value = bson_iterator_string(&data_iter);
			strcpy(tag->value, str_value);
			break;
		}
	}
}
Пример #12
0
int test_bson_generic( void ) {

   bson_iterator it, it2, it3;
   bson_oid_t oid;
   bson_timestamp_t ts;
   bson_timestamp_t ts_result;
   bson b[1];
   bson copy[1];
   bson scope[1];

   ts.i = 1;
   ts.t = 2;

   bson_init( b );
   bson_append_double( b, "d", 3.14 );
   bson_append_string( b, "s", "hello" );
   bson_append_string_n( b, "s_n", "goodbye cruel world", 7 );

   {
       bson_append_start_object( b, "o" );
       bson_append_start_array( b, "a" );
       bson_append_binary( b, "0", 8, "w\0rld", 5 );
       bson_append_finish_object( b );
       bson_append_finish_object( b );
   }

   bson_append_undefined( b, "u" );

   bson_oid_from_string( &oid, "010203040506070809101112" );
   ASSERT( !memcmp( oid.bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12 ) );
   bson_append_oid( b, "oid", &oid );

   bson_append_bool( b, "b", 1 );
   bson_append_date( b, "date", 0x0102030405060708 );
   bson_append_null( b, "n" );
   bson_append_regex( b, "r", "^asdf", "imx" );
   /* no dbref test (deprecated) */
   bson_append_code( b, "c", "function(){}" );
   bson_append_code_n( b, "c_n", "function(){}garbage", 12 );
   bson_append_symbol( b, "symbol", "symbol" );
   bson_append_symbol_n( b, "symbol_n", "symbol and garbage", 6 );

   {
       bson_init( scope );
       bson_append_int( scope, "i", 123 );
       bson_finish( scope );

       bson_append_code_w_scope( b, "cws", "function(){return i}", scope );
       bson_destroy( scope );
   }

   bson_append_timestamp( b, "timestamp", &ts );
   bson_append_long( b, "l", 0x1122334455667788 );

   /* Ensure that we can't copy a non-finished object. */
   ASSERT( bson_copy( copy, b ) == BSON_ERROR );

   bson_finish( b );

   ASSERT( b->err == BSON_VALID );

   /* Test append after finish. */
   ASSERT( bson_append_string( b, "foo", "bar" ) == BSON_ERROR );
   ASSERT( b->err & BSON_ALREADY_FINISHED );

   ASSERT( bson_copy( copy, b ) == BSON_OK );

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

   bson_destroy( copy );

   bson_print( b );

   bson_iterator_init( &it, b );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
   ASSERT( bson_iterator_type( &it ) == BSON_DOUBLE );
   ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) );
   ASSERT( bson_iterator_double( &it ) == 3.14 );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_STRING );
   ASSERT( bson_iterator_type( &it ) == BSON_STRING );
   ASSERT( !strcmp( bson_iterator_key( &it ), "s" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "hello" ) );
   ASSERT( strcmp( bson_iterator_string( &it ), "" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_STRING );
   ASSERT( bson_iterator_type( &it ) == BSON_STRING );
   ASSERT( !strcmp( bson_iterator_key( &it ), "s_n" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "goodbye" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_OBJECT );
   ASSERT( bson_iterator_type( &it ) == BSON_OBJECT );
   ASSERT( !strcmp( bson_iterator_key( &it ), "o" ) );
   bson_iterator_subiterator( &it, &it2 );

   ASSERT( bson_iterator_more( &it2 ) );
   ASSERT( bson_iterator_next( &it2 ) == BSON_ARRAY );
   ASSERT( bson_iterator_type( &it2 ) == BSON_ARRAY );
   ASSERT( !strcmp( bson_iterator_key( &it2 ), "a" ) );
   bson_iterator_subiterator( &it2, &it3 );

   ASSERT( bson_iterator_more( &it3 ) );
   ASSERT( bson_iterator_next( &it3 ) == BSON_BINDATA );
   ASSERT( bson_iterator_type( &it3 ) == BSON_BINDATA );
   ASSERT( !strcmp( bson_iterator_key( &it3 ), "0" ) );
   ASSERT( bson_iterator_bin_type( &it3 ) == 8 );
   ASSERT( bson_iterator_bin_len( &it3 ) == 5 );
   ASSERT( !memcmp( bson_iterator_bin_data( &it3 ), "w\0rld", 5 ) );

   ASSERT( bson_iterator_more( &it3 ) );
   ASSERT( bson_iterator_next( &it3 ) == BSON_EOO );
   ASSERT( bson_iterator_type( &it3 ) == BSON_EOO );
   ASSERT( !bson_iterator_more( &it3 ) );

   ASSERT( bson_iterator_more( &it2 ) );
   ASSERT( bson_iterator_next( &it2 ) == BSON_EOO );
   ASSERT( bson_iterator_type( &it2 ) == BSON_EOO );
   ASSERT( !bson_iterator_more( &it2 ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_UNDEFINED );
   ASSERT( bson_iterator_type( &it ) == BSON_UNDEFINED );
   ASSERT( !strcmp( bson_iterator_key( &it ), "u" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_OID );
   ASSERT( bson_iterator_type( &it ) == BSON_OID );
   ASSERT( !strcmp( bson_iterator_key( &it ), "oid" ) );
   ASSERT( !memcmp( bson_iterator_oid( &it )->bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12 ) );
   ASSERT( bson_iterator_oid( &it )->ints[0] == oid.ints[0] );
   ASSERT( bson_iterator_oid( &it )->ints[1] == oid.ints[1] );
   ASSERT( bson_iterator_oid( &it )->ints[2] == oid.ints[2] );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_BOOL );
   ASSERT( bson_iterator_type( &it ) == BSON_BOOL );
   ASSERT( !strcmp( bson_iterator_key( &it ), "b" ) );
   ASSERT( bson_iterator_bool( &it ) == 1 );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_DATE );
   ASSERT( bson_iterator_type( &it ) == BSON_DATE );
   ASSERT( !strcmp( bson_iterator_key( &it ), "date" ) );
   ASSERT( bson_iterator_date( &it ) == 0x0102030405060708 );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_NULL );
   ASSERT( bson_iterator_type( &it ) == BSON_NULL );
   ASSERT( !strcmp( bson_iterator_key( &it ), "n" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_REGEX );
   ASSERT( bson_iterator_type( &it ) == BSON_REGEX );
   ASSERT( !strcmp( bson_iterator_key( &it ), "r" ) );
   ASSERT( !strcmp( bson_iterator_regex( &it ), "^asdf" ) );
   ASSERT( !strcmp( bson_iterator_regex_opts( &it ), "imx" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_CODE );
   ASSERT( bson_iterator_type( &it ) == BSON_CODE );
   ASSERT( !strcmp( bson_iterator_code(&it), "function(){}") );
   ASSERT( !strcmp( bson_iterator_key( &it ), "c" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_CODE );
   ASSERT( bson_iterator_type( &it ) == BSON_CODE );
   ASSERT( !strcmp( bson_iterator_key( &it ), "c_n" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "" ) );
   ASSERT( !strcmp( bson_iterator_code( &it ), "function(){}" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_SYMBOL );
   ASSERT( bson_iterator_type( &it ) == BSON_SYMBOL );
   ASSERT( !strcmp( bson_iterator_key( &it ), "symbol" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "symbol" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_SYMBOL );
   ASSERT( bson_iterator_type( &it ) == BSON_SYMBOL );
   ASSERT( !strcmp( bson_iterator_key( &it ), "symbol_n" ) );
   ASSERT( !strcmp( bson_iterator_string( &it ), "symbol" ) );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_CODEWSCOPE );
   ASSERT( bson_iterator_type( &it ) == BSON_CODEWSCOPE );
   ASSERT( !strcmp( bson_iterator_key( &it ), "cws" ) );
   ASSERT( !strcmp( bson_iterator_code( &it ), "function(){return i}" ) );

   {
       bson scope;
       bson_iterator_code_scope( &it, &scope );
       bson_iterator_init( &it2, &scope );

       ASSERT( bson_iterator_more( &it2 ) );
       ASSERT( bson_iterator_next( &it2 ) == BSON_INT );
       ASSERT( bson_iterator_type( &it2 ) == BSON_INT );
       ASSERT( !strcmp( bson_iterator_key( &it2 ), "i" ) );
       ASSERT( bson_iterator_int( &it2 ) == 123 );

       ASSERT( bson_iterator_more( &it2 ) );
       ASSERT( bson_iterator_next( &it2 ) == BSON_EOO );
       ASSERT( bson_iterator_type( &it2 ) == BSON_EOO );
       ASSERT( !bson_iterator_more( &it2 ) );
   }

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_TIMESTAMP );
   ASSERT( bson_iterator_type( &it ) == BSON_TIMESTAMP );
   ASSERT( !strcmp( bson_iterator_key( &it ), "timestamp" ) );
   ts_result = bson_iterator_timestamp( &it );
   ASSERT( ts_result.i == 1 );
   ASSERT( ts_result.t == 2 );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_LONG );
   ASSERT( bson_iterator_type( &it ) == BSON_LONG );
   ASSERT( !strcmp( bson_iterator_key( &it ), "l" ) );
   ASSERT( bson_iterator_long( &it ) == 0x1122334455667788 );

   ASSERT( bson_iterator_more( &it ) );
   ASSERT( bson_iterator_next( &it ) == BSON_EOO );
   ASSERT( bson_iterator_type( &it ) == BSON_EOO );
   ASSERT( !bson_iterator_more( &it ) );

   bson_destroy( b );

   {
       bson bsrc[1];
       bson_init( bsrc );
       bson_append_double( bsrc, "d", 3.14 );
       bson_finish( bsrc );
       ASSERT( bsrc->err == BSON_VALID );
       bson_init( b );
       bson_append_double( b, "", 3.14 ); /* test empty name (in general) */
       bson_iterator_init( &it, bsrc );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
       ASSERT( bson_iterator_type( &it ) == BSON_DOUBLE );
       bson_append_element( b, "d", &it );
       bson_append_element( b, 0, &it ); /* test null */
       bson_append_element( b, "", &it ); /* test empty name */
       bson_finish( b );
       ASSERT( b->err == BSON_VALID );
       /* bson_print( b ); */
       bson_iterator_init( &it, b );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
       ASSERT( !strcmp( bson_iterator_key( &it ), "" ) );
       ASSERT( bson_iterator_double( &it ) == 3.14 );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
       ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) );
       ASSERT( bson_iterator_double( &it ) == 3.14 );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
       ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) );
       ASSERT( bson_iterator_double( &it ) == 3.14 );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE );
       ASSERT( !strcmp( bson_iterator_key( &it ), "" ) );
       ASSERT( bson_iterator_double( &it ) == 3.14 );
       ASSERT( bson_iterator_more( &it ) );
       ASSERT( bson_iterator_next( &it ) == BSON_EOO );
       ASSERT( !bson_iterator_more( &it ) );
       bson_destroy( bsrc );
       bson_destroy( b );
   }

   return 0;
}
Пример #13
0
int main(int argc, char *argv[])
{
    bson_buffer bb;
    bson b;
    bson_iterator it, it2, it3;
    bson_oid_t oid;

    bson_buffer_init(&bb);
    bson_append_double(&bb, "d", 3.14);
    bson_append_string(&bb, "s", "hello");

    {
        bson_buffer *obj = bson_append_start_object(&bb, "o");
        bson_buffer *arr = bson_append_start_array(obj, "a");
        bson_append_binary(arr, "0", 8, "w\0rld", 5);
        bson_append_finish_object(arr);
        bson_append_finish_object(obj);
    }

    bson_append_undefined(&bb, "u");

    bson_oid_from_string(&oid, "010203040506070809101112");
    ASSERT(!memcmp(oid.bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12));
    bson_append_oid(&bb, "oid", &oid);

    bson_append_bool(&bb, "b", 1);
    bson_append_date(&bb, "date", 0x0102030405060708ULL);
    bson_append_null(&bb, "n");
    bson_append_regex(&bb, "r", "^asdf", "imx");
    /* no dbref test (deprecated) */
    bson_append_code(&bb, "c", "function(){}");
    bson_append_symbol(&bb, "symbol", "SYMBOL");

    {
        bson_buffer scope_buf;
        bson scope;
        bson_buffer_init(&scope_buf);
        bson_append_int(&scope_buf, "i", 123);
        bson_from_buffer(&scope, &scope_buf);

        bson_append_code_w_scope(&bb, "cws", "function(){return i}", &scope);
        bson_destroy(&scope);
    }

    /* no timestamp test (internal) */
    bson_append_long(&bb, "l", 0x1122334455667788ULL);

    bson_from_buffer(&b, &bb);

    bson_iterator_init(&it, b.data);
    
    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_double);
    ASSERT(bson_iterator_type(&it) == bson_double);
    ASSERT(!strcmp(bson_iterator_key(&it), "d"));
    ASSERT(bson_iterator_double(&it) == 3.14);

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_string);
    ASSERT(bson_iterator_type(&it) == bson_string);
    ASSERT(!strcmp(bson_iterator_key(&it), "s"));
    ASSERT(!strcmp(bson_iterator_string(&it), "hello"));

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_object);
    ASSERT(bson_iterator_type(&it) == bson_object);
    ASSERT(!strcmp(bson_iterator_key(&it), "o"));
    bson_iterator_subiterator(&it, &it2);

    ASSERT(bson_iterator_more(&it2));
    ASSERT(bson_iterator_next(&it2) == bson_array);
    ASSERT(bson_iterator_type(&it2) == bson_array);
    ASSERT(!strcmp(bson_iterator_key(&it2), "a"));
    bson_iterator_subiterator(&it2, &it3);

    ASSERT(bson_iterator_more(&it3));
    ASSERT(bson_iterator_next(&it3) == bson_bindata);
    ASSERT(bson_iterator_type(&it3) == bson_bindata);
    ASSERT(!strcmp(bson_iterator_key(&it3), "0"));
    ASSERT(bson_iterator_bin_type(&it3) == 8);
    ASSERT(bson_iterator_bin_len(&it3) == 5);
    ASSERT(!memcmp(bson_iterator_bin_data(&it3), "w\0rld", 5));
    
    ASSERT(bson_iterator_more(&it3));
    ASSERT(bson_iterator_next(&it3) == bson_eoo);
    ASSERT(bson_iterator_type(&it3) == bson_eoo);
    ASSERT(!bson_iterator_more(&it3));

    ASSERT(bson_iterator_more(&it2));
    ASSERT(bson_iterator_next(&it2) == bson_eoo);
    ASSERT(bson_iterator_type(&it2) == bson_eoo);
    ASSERT(!bson_iterator_more(&it2));

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_undefined);
    ASSERT(bson_iterator_type(&it) == bson_undefined);
    ASSERT(!strcmp(bson_iterator_key(&it), "u"));


    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_oid);
    ASSERT(bson_iterator_type(&it) == bson_oid);
    ASSERT(!strcmp(bson_iterator_key(&it), "oid"));
    ASSERT(!memcmp(bson_iterator_oid(&it)->bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12));
    ASSERT(bson_iterator_oid(&it)->ints[0] == oid.ints[0]);
    ASSERT(bson_iterator_oid(&it)->ints[1] == oid.ints[1]);
    ASSERT(bson_iterator_oid(&it)->ints[2] == oid.ints[2]);

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_bool);
    ASSERT(bson_iterator_type(&it) == bson_bool);
    ASSERT(!strcmp(bson_iterator_key(&it), "b"));
    ASSERT(bson_iterator_bool(&it) == 1);

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_date);
    ASSERT(bson_iterator_type(&it) == bson_date);
    ASSERT(!strcmp(bson_iterator_key(&it), "date"));
    ASSERT(bson_iterator_date(&it) == 0x0102030405060708ULL);

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_null);
    ASSERT(bson_iterator_type(&it) == bson_null);
    ASSERT(!strcmp(bson_iterator_key(&it), "n"));

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_regex);
    ASSERT(bson_iterator_type(&it) == bson_regex);
    ASSERT(!strcmp(bson_iterator_key(&it), "r"));
    ASSERT(!strcmp(bson_iterator_regex(&it), "^asdf"));
    ASSERT(!strcmp(bson_iterator_regex_opts(&it), "imx"));

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_code);
    ASSERT(bson_iterator_type(&it) == bson_code);
    ASSERT(!strcmp(bson_iterator_key(&it), "c"));
    ASSERT(!strcmp(bson_iterator_string(&it), "function(){}"));
    ASSERT(!strcmp(bson_iterator_code(&it), "function(){}"));

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_symbol);
    ASSERT(bson_iterator_type(&it) == bson_symbol);
    ASSERT(!strcmp(bson_iterator_key(&it), "symbol"));
    ASSERT(!strcmp(bson_iterator_string(&it), "SYMBOL"));

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_codewscope);
    ASSERT(bson_iterator_type(&it) == bson_codewscope);
    ASSERT(!strcmp(bson_iterator_key(&it), "cws"));
    ASSERT(!strcmp(bson_iterator_code(&it), "function(){return i}"));

    {
        bson scope;
        bson_iterator_code_scope(&it, &scope);
        bson_iterator_init(&it2, scope.data);

        ASSERT(bson_iterator_more(&it2));
        ASSERT(bson_iterator_next(&it2) == bson_int);
        ASSERT(bson_iterator_type(&it2) == bson_int);
        ASSERT(!strcmp(bson_iterator_key(&it2), "i"));
        ASSERT(bson_iterator_int(&it2) == 123);

        ASSERT(bson_iterator_more(&it2));
        ASSERT(bson_iterator_next(&it2) == bson_eoo);
        ASSERT(bson_iterator_type(&it2) == bson_eoo);
        ASSERT(!bson_iterator_more(&it2));
    }

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_long);
    ASSERT(bson_iterator_type(&it) == bson_long);
    ASSERT(!strcmp(bson_iterator_key(&it), "l"));
    ASSERT(bson_iterator_long(&it) == 0x1122334455667788ULL);

    ASSERT(bson_iterator_more(&it));
    ASSERT(bson_iterator_next(&it) == bson_eoo);
    ASSERT(bson_iterator_type(&it) == bson_eoo);
    ASSERT(!bson_iterator_more(&it));
    
    return 0;
}
Пример #14
0
void lua_push_bson_value(lua_State *L, bson_iterator *it) {
    bson_type bt = bson_iterator_type(it);
    switch (bt) {
        case BSON_OID:
        {
            char xoid[25];
            bson_oid_to_string(bson_iterator_oid(it), xoid);
            lua_pushstring(L, xoid);
            break;
        }
        case BSON_STRING:
        case BSON_SYMBOL:
            lua_pushstring(L, bson_iterator_string(it));
            break;
        case BSON_NULL:
        case BSON_UNDEFINED:
            lua_push_bsontype_table(L, bt);
            break;
        case BSON_INT:
            lua_pushinteger(L, bson_iterator_int(it));
            break;
        case BSON_LONG:
        case BSON_DOUBLE:
            lua_pushnumber(L, (lua_Number) bson_iterator_double(it));
            break;
        case BSON_BOOL:
            lua_pushboolean(L, bson_iterator_bool(it));
            break;
        case BSON_OBJECT:
        case BSON_ARRAY:
        {
            bson_iterator nit;
            bson_iterator_subiterator(it, &nit);
            if (bt == BSON_OBJECT) {
                lua_push_bson_table(L, &nit);
            } else {
                lua_push_bson_array(L, &nit);
            }
            break;
        }
        case BSON_DATE:
        {
            lua_push_bsontype_table(L, bt);
            lua_pushnumber(L, bson_iterator_date(it));
            lua_rawseti(L, -2, 1);
            break;
        }
        case BSON_BINDATA:
        {
            lua_push_bsontype_table(L, bt);
            lua_pushlstring(L, bson_iterator_bin_data(it), bson_iterator_bin_len(it));
            break;
        }
        case BSON_REGEX:
        {
            const char *re = bson_iterator_regex(it);
            const char *ro = bson_iterator_regex_opts(it);
            lua_push_bsontype_table(L, bt);
            lua_pushstring(L, re);
            lua_rawseti(L, -2, 1);
            lua_pushstring(L, ro);
            lua_rawseti(L, -2, 2);
            break;
        }
        default:
            break;
    }
}
Пример #15
0
void decodeValue(v8::Local<v8::Object> obj, bson_iterator *it)
{
    bson_type type = bson_iterator_type(it);
    const char *key = bson_iterator_key(it);

    switch (type)
    {
    case BSON_NULL:
        obj->Set(v8::String::NewFromUtf8(isolate, key), v8::Null(isolate));
        break;
    case BSON_STRING:
        obj->Set(v8::String::NewFromUtf8(isolate, key),
                 v8::String::NewFromUtf8(isolate, bson_iterator_string(it)));
        break;
    case BSON_BOOL:
        obj->Set(v8::String::NewFromUtf8(isolate, key),
                 bson_iterator_bool(it) ? v8::True(isolate) : v8::False(isolate));
        break;
    case BSON_INT:
        obj->Set(v8::String::NewFromUtf8(isolate, key), v8::Number::New(isolate, bson_iterator_int(it)));
        break;
    case BSON_LONG:
    {
        int64_t num = bson_iterator_long(it);
        if (num >= -2147483648ll && num <= 2147483647ll)
        {
            obj->Set(v8::String::NewFromUtf8(isolate, key),
                     v8::Number::New(isolate, (double) num));
        }
        else
        {
            obj_ptr<Int64> int64 = new Int64(num);
            obj->Set(v8::String::NewFromUtf8(isolate, key), int64->wrap());
        }
        break;
    }
    case BSON_DOUBLE:
        obj->Set(v8::String::NewFromUtf8(isolate, key),
                 v8::Number::New(isolate, bson_iterator_double(it)));
        break;
    case BSON_DATE:
        obj->Set(v8::String::NewFromUtf8(isolate, key),
                 v8::Date::New(isolate, (double) bson_iterator_date(it)));
        break;
    case BSON_BINDATA:
    {
        obj_ptr<Buffer_base> buf = new Buffer(
            std::string(bson_iterator_bin_data(it),
                        bson_iterator_bin_len(it)));

        obj->Set(v8::String::NewFromUtf8(isolate, key), buf->wrap());
        break;
    }
    case BSON_OID:
    {
        obj_ptr<MongoID> oid = new MongoID(bson_iterator_oid(it));
        obj->Set(v8::String::NewFromUtf8(isolate, key), oid->wrap());
        break;
    }
    case BSON_REGEX:
    {
        v8::RegExp::Flags flgs = v8::RegExp::kNone;
        const char *opts = bson_iterator_regex_opts(it);
        char ch;

        while ((ch = *opts++) != 0)
            if (ch == 'm')
                flgs = (v8::RegExp::Flags) (flgs | v8::RegExp::kMultiline);
            else if (ch == 'g')
                flgs = (v8::RegExp::Flags) (flgs | v8::RegExp::kGlobal);
            else if (ch == 'i')
                flgs = (v8::RegExp::Flags) (flgs | v8::RegExp::kIgnoreCase);

        obj->Set(v8::String::NewFromUtf8(isolate, key),
                 v8::RegExp::New(v8::String::NewFromUtf8(isolate, bson_iterator_regex(it)),
                                 flgs));
        break;
    }
    case BSON_OBJECT:
    case BSON_ARRAY:
    {
        bson_iterator it1;

        bson_iterator_subiterator(it, &it1);
        obj->Set(v8::String::NewFromUtf8(isolate, key), decodeObject(&it1, type == BSON_ARRAY));
        break;
    }
    default:
        printf("unknown type: %d\n", type);
        break;
    }
}
Пример #16
0
EXPORT void mongo_bson_subiterator(struct bson_iterator_* i, struct bson_iterator_** si) {
    bson_iterator* sub = (bson_iterator*)malloc(sizeof(bson_iterator));
    bson_iterator_subiterator((bson_iterator*)i, sub);
    *si = (struct bson_iterator_*)sub;
}
Пример #17
0
int 
QueryMatch2(BinaryStr* query, BinaryStr* obj ) {
   bson_iterator q,o;
   bson_type qType,tType;
   bson qBson,oBson;

   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
      const char* key = bson_iterator_key(&q);
      //printf("Key %s \n",key);
      BSONElem val1,val2;

      BSONElemInit(&val1,bson_iterator_type(&q),(char*) bson_iterator_value(&q),0,&q);
      tType = bson_find(&o,&oBson,key); 
      if (!tType) {

         BSONElemInit(&val2,BSON_EOO,NULL,0,NULL);
         if (!CompareValue(&val2,&val1)) {
            return False;
         }
         continue;
      } else if (tType == BSON_OBJECT && qType == BSON_OBJECT) {
         BinaryStr qData,oData;
         qData.data = (char*) bson_iterator_value(&q);
         oData.data = (char*) bson_iterator_value(&o);
         if (!QueryMatch(&qData,&oData)) {
            return False;
         }
      } else if (tType == BSON_ARRAY && qType != BSON_OBJECT && qType != BSON_ARRAY) {
         bson_iterator si;
         bson_type t;
         bson_iterator_subiterator(&o,&si);
         uint8_t found = False; 
         while ((t=bson_iterator_next(&si))){
            //BSONElemInit(&val2,bson_iterator_type(&si),(char*) bson_iterator_value(&si),0,&si);
            BSONElemInitFromItr(&val2,&si);
            //BSONElemInit(&val2,bson_iterator_type(&si),(char*) bson_iterator_value(&si),0,&si);
            if (CompareValue(&val2,&val1)) {
               found = True;
               break;
            }
         }
         if (!found) {
            return False;
         } 
      } else {

         //BSONElemInit(&val2,bson_iterator_type(&o),(char*) bson_iterator_value(&o),0,&o);
         BSONElemInitFromItr(&val2,&o);
         if (!CompareValue(&val2,&val1)) {
            return False;
         }
      }
   }

   return True;
}
Пример #18
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);
}
Пример #19
0
bool bson_values_are_equal( bson_type t, const bson_iterator *it1, const bson_iterator *it2 )
{
    assert( it1 );
    assert( it2 );

    bool match;

    switch( t )
    {
    case BSON_ARRAY:
    {
        bson_iterator *sub_it1 = malloc( sizeof( bson_iterator ) );
        bson_iterator *sub_it2 = malloc( sizeof( bson_iterator ) );
        bson_iterator_subiterator( it1, sub_it1 );
        bson_iterator_subiterator( it2, sub_it2 );
        match = bson_cmp_array( sub_it1, sub_it2 );

        free( sub_it1 );
        free( sub_it2 );
        break;
    }
    case BSON_OBJECT:
    {
        bson first_sub[1], second_sub[1];

        bson_iterator_subobject( it1, first_sub );
        bson_iterator_subobject( it2, second_sub );
        match = bson_cmp( first_sub, second_sub );

        break;
    }
    case BSON_DOUBLE:
    {
        double first = bson_iterator_double( it1 );
        double second = bson_iterator_double( it2 );
        match = (bool)(first == second);
        break;
    }
    case BSON_STRING:
    {
        const char* first = bson_iterator_value( it1 );
        const char* second = bson_iterator_value( it2 );
        match = (bool)(0 == strcmp( first, second ));
        break;
    }
    case BSON_BOOL:
    {
        bson_bool_t first = bson_iterator_bool( it1 );
        bson_bool_t second = bson_iterator_bool( it2 );
        match = (bool)(first == second);
        break;
    }
    case BSON_NULL:
    {
        match = true;
        break;
    }
    case BSON_INT:
    {
        int first = bson_iterator_int( it1 );
        int second = bson_iterator_int( it2 );
        match = (bool)(first == second);
        break;
    }
    case BSON_LONG:
    {
        int64_t first = bson_iterator_long( it1 );
        int64_t second = bson_iterator_long( it2 );
        match = (bool)(first == second);
        break;
    }
    default:
        break;
    }

    return match;
}