示例#1
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;
}
示例#2
0
int64_t mongo_count( mongo *conn, const char *db, const char *ns, bson *query ) {
    bson cmd;
    bson out; // = {NULL, 0};
    memset(&out, 0, sizeof(out));
    int64_t count = -1;

    bson_init( &cmd );
    bson_append_string( &cmd, "count", ns );
    if ( query && bson_size( query ) > 5 ) /* not empty */
        bson_append_bson( &cmd, "query", query );
    bson_finish( &cmd );

    if( mongo_run_command( conn, db, &cmd, &out ) == MONGO_OK ) {
        bson_iterator it;
        if( bson_find( &it, &out, "n" ) )
            count = bson_iterator_long( &it );
        bson_destroy( &cmd );
        bson_destroy( &out );
        return count;
    } else {
        bson_destroy( &out );
        bson_destroy( &cmd );
        return MONGO_ERROR;
    }
}
示例#3
0
int64_t mongo_count(mongo_connection* conn, const char* db, const char* ns, bson* query){
    bson_buffer bb;
    bson cmd;
    bson out;
    int64_t count = -1;

    bson_buffer_init(&bb);
    bson_append_string(&bb, "count", ns);
    if (query && bson_size(query) > 5) /* not empty */
        bson_append_bson(&bb, "query", query);
    bson_from_buffer(&cmd, &bb);

    MONGO_TRY{
        if(mongo_run_command(conn, db, &cmd, &out)){
            bson_iterator it;
            if(bson_find(&it, &out, "n"))
                count = bson_iterator_long(&it);
        }
    }MONGO_CATCH{
        bson_destroy(&cmd);
        MONGO_RETHROW();
    }
    
    bson_destroy(&cmd);
    bson_destroy(&out);
    return count;
}
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;
        }
    }
}
示例#5
0
gridfs_offset gridfile_get_contentlength( gridfile *gfile ) {
    bson_iterator it;

    bson_find( &it, gfile->meta, "length" );

    if( bson_iterator_type( &it ) == BSON_INT )
        return ( gridfs_offset )bson_iterator_int( &it );
    else
        return ( gridfs_offset )bson_iterator_long( &it );
}
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;
}
示例#7
0
文件: t1.c 项目: 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);
}
 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;
         }
     }
 }
int64 MongodbObject::GetInt64(const String &fieldname)
{
    int64 retValue = 0;
    
    bson_iterator it;
    bson_iterator_init(&it, objectData->object);
    
    bson_iterator foundIt;
    bool found = objectData->FindField(&it, &foundIt, fieldname, true);
    if(found)
    {
        retValue = bson_iterator_long(&foundIt);
    }
    
    return retValue;
}
示例#10
0
int gridfile_get_numchunks( gridfile *gfile ) {
    bson_iterator it;
    gridfs_offset length;
    gridfs_offset chunkSize;
    double numchunks;

    bson_find( &it, gfile->meta, "length" );

    if( bson_iterator_type( &it ) == BSON_INT )
        length = ( gridfs_offset )bson_iterator_int( &it );
    else
        length = ( gridfs_offset )bson_iterator_long( &it );

    bson_find( &it, gfile->meta, "chunkSize" );
    chunkSize = bson_iterator_int( &it );
    numchunks = ( ( double )length/( double )chunkSize );
    return ( numchunks - ( int )numchunks > 0 )
           ? ( int )( numchunks+1 )
           : ( int )( numchunks );
}
示例#11
0
文件: bjson.c 项目: RepoCorp/mongres
static char *
bson_to_json_recurse(StringInfo buf, const char *data, bool is_object)
{
	bson_iterator i;
	const char *key;
	char oidhex[25];
	char *str;
	bool first = true;

	bson_iterator_from_buffer(&i, data);

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

		if (!first)
			appendStringInfoChar(buf, ',');
		first = false;

		if (is_object)
		{
			key = bson_iterator_key(&i);

			appendStringInfo(buf, "\"%s\"", key);
			appendStringInfoChar(buf, ':');
		}
		switch (t)
		{
			case BSON_DOUBLE:
				appendStringInfo(buf, "%f", bson_iterator_double(&i));
				break;
			case BSON_STRING:
				str = quote_string(bson_iterator_string(&i));
				appendStringInfoString(buf, str);
				break;
			case BSON_OID:
				bson_oid_to_string(bson_iterator_oid(&i), oidhex);
				str = quote_string(oidhex);
				appendStringInfoString(buf, str);
				break;
			case BSON_BOOL:
				appendStringInfoString(buf, bson_iterator_bool(&i) ? "true" : "false");
				break;
			case BSON_INT:
				appendStringInfo(buf, "%d", bson_iterator_int(&i));
				break;
			case BSON_LONG:
				appendStringInfo(buf, "%lld", (uint64_t) bson_iterator_long(&i));
				break;
			case BSON_NULL:
				appendStringInfoString(buf, "null");
				break;
			case BSON_OBJECT:
				appendStringInfoChar(buf, '{');
				bson_to_json_recurse(buf, bson_iterator_value(&i), true);
				appendStringInfoChar(buf, '}');
				break;
			case BSON_ARRAY:
				appendStringInfoChar(buf, '[');
				bson_to_json_recurse(buf, bson_iterator_value(&i), false);
				appendStringInfoChar(buf, ']');
				break;
			case BSON_DATE:
			case BSON_TIMESTAMP:
			case BSON_SYMBOL:
			case BSON_BINDATA:
			case BSON_UNDEFINED:
			case BSON_REGEX:
			case BSON_CODE:
			case BSON_CODEWSCOPE:
				ereport(ERROR,
						(errmsg("unsupported bson type: %d", t)));
				break;
			default:
				elog(ERROR, "unknown bson type: %d", t);
				break;
		}
	}

	return buf->data;
}
示例#12
0
文件: bson.c 项目: halayudha/kdengine
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" );
    }
}
示例#13
0
void json_from_bson_long(yajl_gen *g, bson_iterator *it)
{
    int64_t value = bson_iterator_long( it );

    yajl_gen_integer( *g, value );
}
示例#14
0
Tcl_Obj *
mongotcl_bsontolist_raw (Tcl_Interp *interp, Tcl_Obj *listObj, const char *data , int depth) {
    bson_iterator i;
    const char *key;
    bson_timestamp_t ts;
    char oidhex[25];
    bson scope;

	if (data == NULL) {
		return listObj;
	}

    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);

		switch (t) {
			case BSON_DOUBLE: {
				append_list_type_object (interp, listObj, "double", key, Tcl_NewDoubleObj (bson_iterator_double (&i)));
				break;
			}

			case BSON_STRING: {
				append_list_type_object (interp, listObj, "string", key, Tcl_NewStringObj (bson_iterator_string (&i), -1));
				break;
			}

			case BSON_SYMBOL: {
				append_list_type_object (interp, listObj, "symbol", key, Tcl_NewStringObj (bson_iterator_string (&i), -1));
				break;
			}

			case BSON_OID: {
				bson_oid_to_string( bson_iterator_oid( &i ), oidhex );
				append_list_type_object (interp, listObj, "oid", key, Tcl_NewStringObj (oidhex, -1));
				break;
			}

			case BSON_BOOL: {
			append_list_type_object (interp, listObj, "bool", key, Tcl_NewBooleanObj (bson_iterator_bool (&i)));
				break;
			}

			case BSON_DATE: {
				append_list_type_object (interp, listObj, "date", key, Tcl_NewLongObj ((long) bson_iterator_date(&i)));
				break;
			}

			case BSON_BINDATA: {
				unsigned char *bindata = (unsigned char *)bson_iterator_bin_data (&i);
				int binlen = bson_iterator_bin_len (&i);

				append_list_type_object (interp, listObj, "bin", key, Tcl_NewByteArrayObj (bindata, binlen));
				break;
			}

			case BSON_UNDEFINED: {
				append_list_type_object (interp, listObj, "undefined", key, Tcl_NewObj ());
				break;
			}

			case BSON_NULL: {
				append_list_type_object (interp, listObj, "null", key, Tcl_NewObj ());
				break;
			}

			case BSON_REGEX: {
				append_list_type_object (interp, listObj, "regex", key, Tcl_NewStringObj (bson_iterator_regex (&i), -1));
				break;
			}

			case BSON_CODE: {
				append_list_type_object (interp, listObj, "code", key, Tcl_NewStringObj (bson_iterator_code (&i), -1));
				break;
			}

			case BSON_CODEWSCOPE: {
				bson_printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) );
				/* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */
				bson_iterator_code_scope( &i, &scope );
				bson_printf( "\n\t SCOPE: " );
				bson_print( &scope );
				/* bson_destroy( &scope ); */ /* review - causes free error */
				break;
			}

			case BSON_INT: {
				append_list_type_object (interp, listObj, "int", key, Tcl_NewIntObj (bson_iterator_int (&i)));
				break;
			}

			case BSON_LONG: {
				append_list_type_object (interp, listObj, "long", key, Tcl_NewLongObj ((uint64_t)bson_iterator_long (&i)));
				break;
			}

			case BSON_TIMESTAMP: {
				char string[64];

				ts = bson_iterator_timestamp (&i);
				snprintf(string, sizeof(string), "%d:%d", ts.i, ts.t);
				append_list_type_object (interp, listObj, "timestamp", key, Tcl_NewStringObj (bson_iterator_string (&i), -1));
				break;
			}

			case BSON_ARRAY: {
				Tcl_Obj *subList = Tcl_NewObj ();

				subList = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1);
				append_list_type_object (interp, listObj, "array", key, subList);
				break;
			}

			case BSON_OBJECT: {
				Tcl_Obj *subList = Tcl_NewObj ();

				subList = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1);
				append_list_type_object (interp, listObj, "object", key, subList);
				break;
			}

			default: {
				append_list_type_object (interp, listObj, "unknown", key, Tcl_NewIntObj (t));
				break;
			}
		}
    }
    return listObj;
}
示例#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
/**
 * \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;
		}
	}
}
示例#17
0
int
mongotcl_bsontoarray_raw (Tcl_Interp *interp, char *arrayName, char *typeArrayName, const char *data , int depth) {
    bson_iterator i;
    const char *key;
    bson_timestamp_t ts;
    char oidhex[25];
	Tcl_Obj *obj;
	char *type;

	if (data == NULL) {
		return TCL_OK;
	}

    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);

        switch (t) {
			case BSON_DOUBLE: {
				obj = Tcl_NewDoubleObj (bson_iterator_double (&i));
				type = "double";
				break;
		}

			case BSON_SYMBOL: {
				obj = Tcl_NewStringObj (bson_iterator_string (&i), -1);
				type = "symbol";
				break;
			}

			case BSON_STRING: {
				obj = Tcl_NewStringObj (bson_iterator_string (&i), -1);
				type = "string";
				break;
			}

			case BSON_OID: {
				bson_oid_to_string( bson_iterator_oid( &i ), oidhex );
				obj = Tcl_NewStringObj (oidhex, -1);
				type = "oid";
				break;
			}

			case BSON_BOOL: {
				obj = Tcl_NewBooleanObj (bson_iterator_bool (&i));
				type = "bool";
				break;
			}

			case BSON_DATE: {
				obj = Tcl_NewLongObj ((long) bson_iterator_date(&i));
				type = "date";
				break;
			}

			case BSON_BINDATA: {
				unsigned char *bindata = (unsigned char *)bson_iterator_bin_data (&i);
				int binlen = bson_iterator_bin_len (&i);

				obj = Tcl_NewByteArrayObj (bindata, binlen);
				type = "bin";
				break;
			}

			case BSON_UNDEFINED: {
				obj = Tcl_NewObj ();
				type = "undefined";
				break;
			}

			case BSON_NULL: {
				obj = Tcl_NewObj ();
				type = "null";
				break;
			}

			case BSON_REGEX: {
				obj = Tcl_NewStringObj (bson_iterator_regex (&i), -1);
				type = "regex";
				break;
			}

			case BSON_CODE: {
				obj = Tcl_NewStringObj (bson_iterator_code (&i), -1);
				type = "code";
				break;
			}

			case BSON_CODEWSCOPE: {
				// bson_printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) );
				/* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */
				// bson_iterator_code_scope( &i, &scope );
				// bson_printf( "\n\t SCOPE: " );
				// bson_print( &scope );
				/* bson_destroy( &scope ); */ /* review - causes free error */
				break;
			}

			case BSON_INT: {
				obj = Tcl_NewIntObj (bson_iterator_int (&i));
				type = "int";
				break;
			}

			case BSON_LONG: {
				obj = Tcl_NewLongObj ((uint64_t)bson_iterator_long (&i));
				type = "long";
				break;
			}

			case BSON_TIMESTAMP: {
				char string[64];

				ts = bson_iterator_timestamp (&i);
				snprintf(string, sizeof(string), "%d:%d", ts.i, ts.t);
				obj = Tcl_NewStringObj (bson_iterator_string (&i), -1);
				type = "timestamp";
				break;
			}

			case BSON_ARRAY: {
				obj = Tcl_NewObj();
				obj = mongotcl_bsontolist_raw (interp, obj, bson_iterator_value (&i), depth + 1);
				type = "array";

				break;
			}

			case BSON_OBJECT: {
				Tcl_Obj *subList = Tcl_NewObj ();

				obj = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1);
				type = "object";
				break;
			}

			default: {
				obj = Tcl_NewIntObj (t);
				type = "unknown";
				break;
			}
		}

		if (Tcl_SetVar2Ex (interp, arrayName, key, obj, TCL_LEAVE_ERR_MSG) == NULL) {
			return TCL_ERROR;
		}

		if (typeArrayName != NULL) {
			if (Tcl_SetVar2Ex (interp, typeArrayName, key, Tcl_NewStringObj (type, -1), TCL_LEAVE_ERR_MSG) == NULL) {
				return TCL_ERROR;
			}
		}
    }
	return TCL_OK; 
}
示例#18
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;
}
示例#19
0
EXPORT mxint64 mongo_bson_iterator_long(struct bson_iterator_* i) {
    return bson_iterator_long((bson_iterator*)i);
}
示例#20
0
文件: mg.c 项目: carriercomm/mudos-1
INLINE void bson_to_v P2(bson_iterator *, i, svalue_t *, v){
	int temp;   
	const char *cp;
    char oidhex[25];
    bson scope;
    bson_type t;
    bson_timestamp_t ts;
    t = bson_iterator_type( i );
    switch ( t ) {
    case BSON_DOUBLE:
    	v->type = T_REAL;
		v->u.real = bson_iterator_double( i );
        break;
    case BSON_STRING:
    	cp = bson_iterator_string( i );
    	v->type = T_STRING;
    	v->subtype = STRING_MALLOC;
    	v->u.string = string_copy(cp,"bson_to_v:1");
        break;
    case BSON_SYMBOL:
       	v->type = T_STRING;
    	v->subtype = STRING_MALLOC;
    	v->u.string = string_copy("SYMBOL","bson_to_v:2");
        break;
    case BSON_OID:
        bson_oid_to_string( bson_iterator_oid( i ), oidhex );
        v->type = T_STRING;
    	v->subtype = STRING_MALLOC;
    	v->u.string = string_copy(oidhex,"bson_to_v:3");
        break;
    case BSON_BOOL:
    	v->type = T_NUMBER;
		v->u.number = ( long )bson_iterator_bool( i );
        break;
    case BSON_DATE:
        v->type = T_NUMBER;
		v->u.number = ( long )bson_iterator_date( i );
        break;
    case BSON_BINDATA:
    	v->type = T_STRING;
    	v->subtype = STRING_MALLOC;
    	v->u.string = string_copy("BSON_BINDATA","bson_to_v:4");
        break;
    case BSON_UNDEFINED:
    	v->type = T_STRING;
    	v->subtype = STRING_MALLOC;
    	v->u.string = string_copy("BSON_UNDEFINED","bson_to_v:5");
        break;
    case BSON_NULL:
    	v->type = T_STRING;
    	v->subtype = STRING_MALLOC;
        v->u.string = string_copy("BSON_NULL","bson_to_v:6");
        break;
    case BSON_REGEX:
        v->type = T_STRING;
    	v->subtype = STRING_MALLOC;
    	v->u.string = string_copy("BSON_REGEX","bson_to_v:7");
        break;
    case BSON_CODE:
        v->type = T_STRING;
    	v->subtype = STRING_MALLOC;
    	v->u.string = string_copy("BSON_CODE","bson_to_v:8");
        break;
    case BSON_CODEWSCOPE:
        v->type = T_STRING;
    	v->subtype = STRING_MALLOC;
    	v->u.string = string_copy("BSON_CODEWSCOPE","bson_to_v:9");
        break;
    case BSON_INT:
        v->type = T_NUMBER;
		v->u.number = ( long )bson_iterator_int( i );
        break;
    case BSON_LONG:
    	v->type = T_NUMBER;
		v->u.number = ( long )bson_iterator_long( i );
        break;
    case BSON_TIMESTAMP:
        ts = bson_iterator_timestamp( i );
        v->type = T_NUMBER;
		v->u.number = ( long )ts.t;
        break;
    case BSON_OBJECT:
    	bson_to_mapping( bson_iterator_value( i ) , v);
    	break;
    case BSON_ARRAY:
        bson_to_array( bson_iterator_value( i ) , v);
        break;
    default:
    	v->type = T_STRING;
    	v->subtype = STRING_MALLOC;
    	v->u.string = string_copy("UNKOWN_TYPE","bson_to_v:10");
    	break;
    }
}
示例#21
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;
}
示例#22
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;
}
示例#23
0
文件: luabson.c 项目: alextooter/ejdb
static void bson_print_xstr(TCXSTR* xstr, 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++) {
            tcxstrprintf(xstr, ".");
        }
        tcxstrprintf(xstr, "%s(%d)=", key, t);
        switch (t) {
            case BSON_DOUBLE:
                tcxstrprintf(xstr, "%f", bson_iterator_double(&i));
                break;
            case BSON_STRING:
                tcxstrprintf(xstr, "%s", bson_iterator_string(&i));
                break;
            case BSON_SYMBOL:
                tcxstrprintf(xstr, "SYMBOL: %s", bson_iterator_string(&i));
                break;
            case BSON_OID:
                bson_oid_to_string(bson_iterator_oid(&i), oidhex);
                tcxstrprintf(xstr, "%s", oidhex);
                break;
            case BSON_BOOL:
                tcxstrprintf(xstr, "%s", bson_iterator_bool(&i) ? "true" : "false");
                break;
            case BSON_DATE:
                tcxstrprintf(xstr, "%lld", (uint64_t) bson_iterator_long(&i));
                break;
            case BSON_BINDATA:
                tcxstrprintf(xstr, "BSON_BINDATA");
                break;
            case BSON_UNDEFINED:
                tcxstrprintf(xstr, "BSON_UNDEFINED");
                break;
            case BSON_NULL:
                tcxstrprintf(xstr, "BSON_NULL");
                break;
            case BSON_REGEX:
                tcxstrprintf(xstr, "BSON_REGEX: %s", bson_iterator_regex(&i));
                break;
            case BSON_CODE:
                tcxstrprintf(xstr, "BSON_CODE: %s", bson_iterator_code(&i));
                break;
            case BSON_CODEWSCOPE:
                tcxstrprintf(xstr, "BSON_CODE_W_SCOPE: %s", bson_iterator_code(&i));
                /* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */
                bson_iterator_code_scope(&i, &scope);
                tcxstrprintf(xstr, "\n  SCOPE: ");
                bson_print_xstr(xstr, scope.data, 0);
                /* bson_destroy( &scope ); */ /* review - causes free error */
                break;
            case BSON_INT:
                tcxstrprintf(xstr, "%d", bson_iterator_int(&i));
                break;
            case BSON_LONG:
                tcxstrprintf(xstr, "%lld", (uint64_t) bson_iterator_long(&i));
                break;
            case BSON_TIMESTAMP:
                ts = bson_iterator_timestamp(&i);
                tcxstrprintf(xstr, "i: %d, t: %d", ts.i, ts.t);
                break;
            case BSON_OBJECT:
            case BSON_ARRAY:
                tcxstrprintf(xstr, "\n");
                bson_print_xstr(xstr, bson_iterator_value(&i), depth + 1);
                break;
            default:
                fprintf(stderr, "can't print type : %d\n", t);
        }
        tcxstrprintf(xstr, "\n");
    }
}
示例#24
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;
}