示例#1
0
static void
test_bson_iter_as_bool (void)
{
   bson_iter_t iter;
   bson_t b;

   bson_init(&b);
   bson_append_int32(&b, "int32[true]", -1, 1);
   bson_append_int32(&b, "int32[false]", -1, 0);
   bson_append_int64(&b, "int64[true]", -1, 1);
   bson_append_int64(&b, "int64[false]", -1, 0);
   bson_append_double(&b, "int64[true]", -1, 1.0);
   bson_append_double(&b, "int64[false]", -1, 0.0);

   bson_iter_init(&iter, &b);
   bson_iter_next(&iter);
   assert_cmpint(true, ==, bson_iter_as_bool(&iter));
   bson_iter_next(&iter);
   assert_cmpint(false, ==, bson_iter_as_bool(&iter));
   bson_iter_next(&iter);
   assert_cmpint(true, ==, bson_iter_as_bool(&iter));
   bson_iter_next(&iter);
   assert_cmpint(false, ==, bson_iter_as_bool(&iter));
   bson_iter_next(&iter);
   assert_cmpint(true, ==, bson_iter_as_bool(&iter));
   bson_iter_next(&iter);
   assert_cmpint(false, ==, bson_iter_as_bool(&iter));

   bson_destroy(&b);
}
示例#2
0
static int mongo_bson_buffer_append_complex(struct bson_buffer* b, const char* name, double r, double i) {
    bson* _b = (bson*) b;
    return (bson_append_start_object(_b, name) == BSON_OK) &&
           (bson_append_double(_b, "r", r) == BSON_OK) && 
           (bson_append_double(_b, "i", i) == BSON_OK) && 
           (bson_append_finish_object(_b) == BSON_OK);
}
示例#3
0
文件: mongodb.c 项目: adarqui/darqbot
int mongodb_fmt_insert_arg(bot_t * bot, char *db, mongo_argument_t ** arg_array)
{
/* example: ^e |^mongotestfmt "%s=name %i=age %f=grade" Darqbot 50 99.9 */

	int i;
	bson b;

	debug(bot, "mongodb_fmt_insert_arg: Entered\n");

	if (!db || !arg_array)
		return -1;

	bson_init(&b);

	for (i = 0; arg_array[i] != NULL; i++) {
		switch (arg_array[i]->type) {
		case MONGODB_ARG_STRING:
			{
				bson_append_string(&b, arg_array[i]->name,
						   arg_array[i]->arg_str);
				break;
			}
		case MONGODB_ARG_INT:
			{
				bson_append_int(&b, arg_array[i]->name,
						arg_array[i]->arg_int);
				break;
			}
		case MONGODB_ARG_DOUBLE:
			{
				bson_append_double(&b, arg_array[i]->name,
						   arg_array[i]->arg_double);
				break;
			}
		case MONGODB_ARG_LONG:
			{
				bson_append_long(&b, arg_array[i]->name,
						 arg_array[i]->arg_long);
				break;
			}
		case MONGODB_ARG_OID:
			{
// FIX
				bson_append_oid(&b, arg_array[i]->name,
						&arg_array[i]->arg_oid);
				break;
			}
		default:
			break;
		}
	}

	bson_finish(&b);

	i = mongo_insert(&gi->mongo_conn, db, &b);

	bson_destroy(&b);

	return 0;
}
示例#4
0
int test_bson_init_finished_size( void ) {
    bson b;
    ALLOW_AND_REQUIRE_MALLOC_BEGIN;
    bson_init( &b );
    ALLOW_AND_REQUIRE_MALLOC_END;
    bson_append_double( &b, "d", 3.14 );
    bson_append_string( &b, "s", "hello" );
    bson_finish( &b );

    bson b2;
    int res;
    res = bson_init_finished_data_size( &b2, (char *) bson_data( &b ), bson_size( &b ), 0 );
    ASSERT( res == MONGO_OK );
    bson_destroy( &b2 );

    res = bson_init_finished_data_size( &b2, (char *) bson_data( &b ), bson_size( &b ) + 1, 0 );
    ASSERT( res == MONGO_OK );
    bson_destroy( &b2 );

    res = bson_init_finished_data_size( &b2, (char *) bson_data( &b ), bson_size( &b ) - 1, 0 );
    ASSERT( res == MONGO_ERROR );
    bson_destroy( &b2 );

    return 0;
}
示例#5
0
static bool
handle_ismaster (mock_server_t   *server,
                 mongoc_stream_t *client,
                 mongoc_rpc_t    *rpc,
                 const bson_t    *doc)
{
   bson_t reply_doc = BSON_INITIALIZER;
   time_t now = time (NULL);

   BSON_ASSERT (server);
   BSON_ASSERT (client);
   BSON_ASSERT (rpc);
   BSON_ASSERT (doc);

   bson_append_bool (&reply_doc, "ismaster", -1, server->isMaster);
   bson_append_int32 (&reply_doc, "maxBsonObjectSize", -1,
                      server->maxBsonObjectSize);
   bson_append_int32 (&reply_doc, "maxMessageSizeBytes", -1,
                      server->maxMessageSizeBytes);
   bson_append_int32 (&reply_doc, "minWireVersion", -1,
                      server->minWireVersion);
   bson_append_int32 (&reply_doc, "maxWireVersion", -1,
                      server->maxWireVersion);
   bson_append_double (&reply_doc, "ok", -1, 1.0);
   bson_append_time_t (&reply_doc, "localtime", -1, now);

   mock_server_reply_simple (server, client, rpc, MONGOC_REPLY_NONE, &reply_doc);

   bson_destroy (&reply_doc);

   return true;
}
示例#6
0
int test_bson_init_finished( void ) {
    bson b;
    ALLOW_AND_REQUIRE_MALLOC_BEGIN;
    bson_init( &b );
    ALLOW_AND_REQUIRE_MALLOC_END;
    bson_append_double( &b, "d", 3.14 );
    bson_append_string( &b, "s", "hello" );
    bson_finish( &b );
    ASSERT( bson_size( &b ) == 29 ); // 29 determined by running this code

    bson b2;
    bson_init_finished_data( &b2, (char *) bson_data( &b ), 0 );
    ASSERT( bson_size( &b ) == bson_size( &b2 ) );
    bson_destroy( &b2 );

    ALLOW_AND_REQUIRE_MALLOC_BEGIN;
    bson_init_finished_data_with_copy( &b2, (char *) bson_data( &b ) );
    ALLOW_AND_REQUIRE_MALLOC_END;
    ASSERT( bson_size( &b ) == bson_size( &b2 ) );
    ALLOW_AND_REQUIRE_FREE_BEGIN;
    bson_destroy( &b2 );
    ALLOW_AND_REQUIRE_FREE_END;

    bson_init_finished_data( &b2, (char *) bson_data( &b ), 1 );
    ASSERT( bson_size( &b ) == bson_size( &b2 ) );
    ALLOW_AND_REQUIRE_FREE_BEGIN;
    bson_destroy( &b2 );
    ALLOW_AND_REQUIRE_FREE_END;

    return 0;
}
static bool append_value(bson_t* bson, const char* key, size_t length, object_t* value) {
    switch (value->type) {
        case type_nil:    bson_append_null(bson, key, length);                       break; 
        case type_bool:   bson_append_bool(bson, key, length, value->b);             break; 
        case type_double: bson_append_double(bson, key, length, value->d);           break; 
        case type_str:    bson_append_utf8(bson, key, length, value->str, value->l); break; 

        case type_int:    append_int(bson, key, length, value->i);                   break; 
        case type_uint:   append_int(bson, key, length, (int64_t)value->u);          break;

        case type_map: {
            bson_t child;
            bson_append_document_begin(bson, key, length, &child);
            append_document(&child, value);
            bson_append_document_end(bson, &child);
        } break;

        case type_array: {
            bson_t child;
            bson_append_array_begin(bson, key, length, &child);
            append_array(&child, value);
            bson_append_array_end(bson, &child);
        } break;

        default:
            return false;
    }
    return true;
}
示例#8
0
bson* cBSON_CreateNumber(double data)
{
    bson *item = bson_alloc();
    if(item){
        bson_init(item);
        bson_append_double(item,"0",data);
        bson_finish(item);
        return item;
    }
    return NULL;
}
示例#9
0
文件: bson-json.c 项目: itay/libbson
static int
_bson_json_read_double (void   *_ctx, /* IN */
                        double  val)  /* IN */
{
   BASIC_YAJL_CB_PREAMBLE;
   BASIC_YAJL_CB_BAIL_IF_NOT_NORMAL ("double");

   bson_append_double (STACK_BSON_CHILD, key, (int)len, val);

   return 1;
}
示例#10
0
static void
test_bson_as_json (void)
{
   bson_oid_t oid;
   bson_decimal128_t decimal128;
   bson_t *b;
   bson_t *b2;
   char *str;
   size_t len;
   int i;

   decimal128.high = 0x3040000000000000ULL;
   decimal128.low  = 0x000000000000000B;
   bson_oid_init_from_string(&oid, "123412341234abcdabcdabcd");

   b = bson_new();
   assert(bson_append_utf8(b, "utf8", -1, "bar", -1));
   assert(bson_append_int32(b, "int32", -1, 1234));
   assert(bson_append_int64(b, "int64", -1, 4321));
   assert(bson_append_double(b, "double", -1, 123.4));
   assert(bson_append_undefined(b, "undefined", -1));
   assert(bson_append_null(b, "null", -1));
   assert(bson_append_oid(b, "oid", -1, &oid));
   assert(bson_append_bool(b, "true", -1, true));
   assert(bson_append_bool(b, "false", -1, false));
   assert(bson_append_time_t(b, "date", -1, time(NULL)));
   assert(bson_append_timestamp(b, "timestamp", -1, (uint32_t)time(NULL), 1234));
   assert(bson_append_regex(b, "regex", -1, "^abcd", "xi"));
   assert(bson_append_dbpointer(b, "dbpointer", -1, "mycollection", &oid));
   assert(bson_append_minkey(b, "minkey", -1));
   assert(bson_append_maxkey(b, "maxkey", -1));
   assert(bson_append_symbol(b, "symbol", -1, "var a = {};", -1));
   assert(bson_append_decimal128(b, "decimal128", -1, &decimal128));

   b2 = bson_new();
   assert(bson_append_int32(b2, "0", -1, 60));
   assert(bson_append_document(b, "document", -1, b2));
   assert(bson_append_array(b, "array", -1, b2));

   {
      const uint8_t binary[] = { 0, 1, 2, 3, 4 };
      assert(bson_append_binary(b, "binary", -1, BSON_SUBTYPE_BINARY,
                                binary, sizeof binary));
   }

   for (i = 0; i < 1000; i++) {
      str = bson_as_json(b, &len);
      bson_free(str);
   }

   bson_destroy(b);
   bson_destroy(b2);
}
示例#11
0
void MongodbObject::EnableForEdit()
{
    bson *newObject = new bson();
    bson_init(newObject);
    
    bson_iterator it;
    bson_iterator_init(&it, objectData->object);

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

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

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

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

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

                
            default:
                break;
        }
    }

    bson_destroy(objectData->object);
    SafeDelete(objectData->object);
    objectData->object = newObject;
}
示例#12
0
static void
json_key_to_bson_key (bson *b, void *val,
		      const gchar *key)
{
  switch (json_object_get_type (val))
    {
    case json_type_boolean:
      bson_append_boolean (b, key, json_object_get_boolean (val));
      break;
    case json_type_double:
      bson_append_double (b, key, json_object_get_double (val));
      break;
    case json_type_int:
      bson_append_int32 (b, key, json_object_get_int (val));
      break;
    case json_type_string:
      bson_append_string (b, key, json_object_get_string (val), -1);
      break;
    case json_type_object:
      {
	bson *sub;

	sub = json_to_bson (val);
	bson_append_document (b, key, sub);
	bson_free (sub);
	break;
      }
    case json_type_array:
      {
	gint pos;
	bson *sub;

	sub = bson_new ();

	for (pos = 0; pos < json_object_array_length (val); pos++)
	  {
	    gchar *nk = g_strdup_printf ("%d", pos);

	    json_key_to_bson_key (sub, json_object_array_get_idx (val, pos),
				  nk);
	    g_free (nk);
	  }
	bson_finish (sub);

	bson_append_array (b, key, sub);
	bson_free (sub);
	break;
      }
    default:
      break;
    }
}
示例#13
0
文件: test-bson.c 项目: dgoon/libbson
static void
test_bson_append_double (void)
{
   bson_t *b;
   bson_t *b2;

   b = bson_new();
   assert(bson_append_double(b, "double", -1, 123.4567));
   b2 = get_bson("test20.bson");
   assert_bson_equal(b, b2);
   bson_destroy(b);
   bson_destroy(b2);
}
示例#14
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);
}
示例#15
0
void bson_from_json_type(bson *b, yajl_val value, const char* key)
{
    if( YAJL_IS_STRING( value ) )
    {
        char* string = YAJL_GET_STRING( value );
        bson_append_string( b, key, string );
    }
    else if( YAJL_IS_NUMBER( value ) )
    {
        char* string = value->u.number.r;
        size_t len = strlen( string );

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

            if( number <= INT_MIN && number <= INT_MAX)
                bson_append_int( b, key, (int)number );
            else
                bson_append_long( b, key, number);
        }
    }
    else if ( YAJL_IS_FALSE( value ) )
    {
        bson_append_bool( b, key, 0 );
    }
    else if ( YAJL_IS_TRUE( value ) )
    {
        bson_append_bool( b, key, 1 );
    }
    else if ( YAJL_IS_ARRAY( value ) )
    {
        bson_append_start_array( b, key );
        bson_from_json_array( b, value );
        bson_append_finish_array( b );
    }
    else if ( YAJL_IS_OBJECT( value ) )
    {
        bson_append_start_object( b, key );
        bson_from_json_object( b, value );
        bson_append_finish_object( b );
    }
}
示例#16
0
 void InitWith(bson *obj)
 {
     bson_iterator it;
     bson_iterator_init(&it, obj);
     
     while (bson_iterator_next(&it))
     {
         const char * key = bson_iterator_key(&it);
         bson_type type = bson_iterator_type(&it);
         
         switch (type)
         {
             case BSON_STRING:
                 bson_append_string(object, key, bson_iterator_string(&it));
                 break;
                 
             case BSON_INT:
                 bson_append_int(object, key, bson_iterator_int(&it));
                 break;
                 
             case BSON_LONG:
                 bson_append_long(object, key, bson_iterator_long(&it));
                 break;
                 
             case BSON_DOUBLE:
                 bson_append_double(object, key, bson_iterator_double(&it));
                 break;
                 
             case BSON_OBJECT:
             {
                 bson sub;
                 
                 bson_iterator_subobject(&it, &sub);
                 bson_append_bson(object, key, &sub);
                 break;
             }
                 
             case BSON_OID:
                 bson_append_oid(object, key, bson_iterator_oid(&it));
                 break;
                 
                 
             default:
                 DVASSERT(false);
                 Logger::Error("[MongodbObjectInternalData::InitWith] Not implemented type: %d", type);
                 break;
         }
     }
 }
示例#17
0
文件: test-json.c 项目: xpol/libbson
static void
test_bson_as_json_double (void)
{
   size_t len;
   bson_t *b;
   char *str;

   b = bson_new();
   assert(bson_append_double(b, "foo", -1, 123.456));
   assert(bson_append_double(b, "bar", -1, 3));
   assert(bson_append_double(b, "baz", -1, -1));
   assert(bson_append_double(b, "quux", -1, 1.0001));
   assert(bson_append_double(b, "huge", -1, 1e99));
   str = bson_as_json(b, &len);
   ASSERT_CMPSTR (str,
                  "{"
                  " \"foo\" : 123.456,"
                  " \"bar\" : 3,"
                  " \"baz\" : -1,"
                  " \"quux\" : 1.0001,"
                  " \"huge\" : 1e+99 }");
   bson_free(str);
   bson_destroy(b);
}
static void make_medium( bson *out, int i ) {
    bson_init( out );
    bson_append_new_oid( out, "_id" );
    bson_append_int( out, "x", i );
    bson_append_int( out, "integer", 5 );
    bson_append_double( out, "number", 5.05 );
    bson_append_bool( out, "boolean", 0 );

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

    bson_finish( out );
}
示例#19
0
文件: mbson.c 项目: pombredanne/cmoon
static void json_append_to_bson(bson* b, char *key, struct json_object *val)
{
    if (!b || !key || !val) return;

    struct array_list *list;
    enum json_type type;
    bson *sub;
    char tok[64];

    type = json_object_get_type(val);

    switch (type) {
    case json_type_boolean:
        bson_append_boolean(b, key, json_object_get_boolean(val));
        break;
    case json_type_int:
        bson_append_int32(b, key, json_object_get_int(val));
        break;
    case json_type_double:
        bson_append_double(b, key, json_object_get_double(val));
        break;
    case json_type_string:
        bson_append_string(b, key, json_object_get_string(val), -1);
        break;
    case json_type_array:
        sub = bson_new();

        list = json_object_get_array(val);
        for (int pos = 0; pos < list->length; pos++) {
            sprintf(tok, "%d", pos);
            json_append_to_bson(sub, tok, (struct json_object*)list->array[pos]);
        }
        
        bson_finish(sub);
        bson_append_array(b, key, sub);
        bson_free(sub);
        break;
    case json_type_object:
        sub = mbson_new_from_jsonobj(val, true, false);
        bson_append_document(b, key, sub);
        bson_free(sub);
        break;
    default:
        break;
    }
    
    
}
示例#20
0
static void make_medium(bson * out, int i){
    bson_buffer bb;
    bson_buffer_init(&bb);
    bson_append_new_oid(&bb, "_id");
    bson_append_int(&bb, "x", i);
    bson_append_int(&bb, "integer", 5);
    bson_append_double(&bb, "number", 5.05);
    bson_append_bool(&bb, "boolean", 0);

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

    bson_from_buffer(out, &bb);
}
示例#21
0
static void
test_bson_iter_overwrite_double (void)
{
   bson_iter_t iter;
   bson_t b;

   bson_init(&b);
   assert(bson_append_double(&b, "key", -1, 1234.1234));
   assert(bson_iter_init_find(&iter, &b, "key"));
   assert(BSON_ITER_HOLDS_DOUBLE(&iter));
   bson_iter_overwrite_double(&iter, 4641.1234);
   assert(bson_iter_init_find(&iter, &b, "key"));
   assert(BSON_ITER_HOLDS_DOUBLE(&iter));
   assert_cmpint(bson_iter_double(&iter), ==, 4641.1234);
   bson_destroy(&b);
}
示例#22
0
void testTicket135(void) {
    bson bo_test;
    bson_init(&bo_test);
    bson_append_int(&bo_test, "myInt", 10);
    bson_append_double(&bo_test, "myDouble", -50.0);
    bson_finish(&bo_test);

    char* buf = NULL;
    int lenght = 0;
    bson2json(bson_data(&bo_test), &buf, &lenght);
    CU_ASSERT_PTR_NOT_NULL_FATAL(buf);
    CU_ASSERT_PTR_NOT_NULL(strstr(buf, "\"myInt\" : 10"));
    CU_ASSERT_PTR_NOT_NULL(strstr(buf, "\"myDouble\" : -50.000000"));
    bson_destroy(&bo_test);
    TCFREE(buf);
}
示例#23
0
static void
test_bson_as_json_double (void)
{
   size_t len;
   bson_t *b;
   char *str;

   b = bson_new();
   assert(bson_append_double(b, "foo", -1, 123.456));
   str = bson_as_json(b, &len);
   assert(len >= 19);
   assert(!strncmp("{ \"foo\" : 123.456", str, 17));
   assert(!strcmp(" }", str + len - 2));
   bson_free(str);
   bson_destroy(b);
}
示例#24
0
static void
test_double (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_double (&expected, "foo", -1, 1.1);
   BCON_APPEND(&bcon, "foo", BCON_DOUBLE(1.1));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
示例#25
0
bson *
test_bson_generate_full (void)
{
  bson *b, *d, *a, *scope;
  guint8 oid[] = "1234567890ab";

  a = bson_new ();
  bson_append_int32 (a, "0", 32);
  bson_append_int64 (a, "1", (gint64)-42);
  bson_finish (a);

  d = bson_new ();
  bson_append_string (d, "name", "sub-document", -1);
  bson_append_int32 (d, "answer", 42);
  bson_finish (d);

  scope = bson_new ();
  bson_append_string (scope, "v", "hello world", -1);
  bson_finish (scope);

  b = bson_new ();
  bson_append_double (b, "double", 3.14);
  bson_append_string (b, "str", "hello world", -1);
  bson_append_document (b, "doc", d);
  bson_append_array (b, "array", a);
  bson_append_binary (b, "binary0", BSON_BINARY_SUBTYPE_GENERIC,
                      (guint8 *)"foo\0bar", 7);
  bson_append_oid (b, "_id", oid);
  bson_append_boolean (b, "TRUE", FALSE);
  bson_append_utc_datetime (b, "date", 1294860709000);
  bson_append_timestamp (b, "ts", 1294860709000);
  bson_append_null (b, "null");
  bson_append_regex (b, "foobar", "s/foo.*bar/", "i");
  bson_append_javascript (b, "alert", "alert (\"hello world!\");", -1);
  bson_append_symbol (b, "sex", "Marilyn Monroe", -1);
  bson_append_javascript_w_scope (b, "print", "alert (v);", -1, scope);
  bson_append_int32 (b, "int32", 32);
  bson_append_int64 (b, "int64", (gint64)-42);
  bson_finish (b);

  bson_free (d);
  bson_free (a);
  bson_free (scope);

  return b;
}
示例#26
0
void test_example_awesome() {
    bcon_error_t ret;
    bson b[1];

    /* JSON {"BSON": ["awesome", 5.05, 1986]} */

    bcon awesome[] = { "BSON", "[", "awesome", BF(5.05), BI(1986), "]", BEND };
    test_bson_from_bcon( awesome, BCON_OK, BSON_VALID );

    if  (verbose )
        printf("\t--------\n");

    bson_init( b );
    bson_append_start_array( b, "BSON" );
    bson_append_string( b, "0", "awesome" );
    bson_append_double( b, "1", 5.05 );
    bson_append_int( b, "2", 1986 );
    bson_append_finish_array( b );
    ret = bson_finish( b );
    if ( verbose )
        bson_print( b );
    bson_destroy( b );
}
示例#27
0
文件: bjson.c 项目: RepoCorp/mongres
static void
jbson_scalar(void *state, char *token, JsonTokenType tokentype)
{
	json_to_bson_state *_state = (json_to_bson_state *) state;
	double fval;

	if (_state->fname == NULL)
	{
		elog(ERROR, "unsupported json structure");
	}

	switch (tokentype)
	{
		case JSON_TOKEN_STRING:
			bson_append_string(_state->bson, _state->fname, token);
			break;
		case JSON_TOKEN_NUMBER:
			sscanf(token, "%lf", &fval);
			bson_append_double(_state->bson, _state->fname, fval);
			break;
		case JSON_TOKEN_TRUE:
			bson_append_bool(_state->bson, _state->fname, true);
			break;
		case JSON_TOKEN_FALSE:
			bson_append_bool(_state->bson, _state->fname, false);
			break;
		case JSON_TOKEN_NULL:
			bson_append_null(_state->bson, _state->fname);
			break;
		default:
			elog(ERROR, "unexpected token type: %d", tokentype);
			break;
	}

	_state->fname = NULL;
}
示例#28
0
文件: cs_matrix.cpp 项目: HPeX/hermes
    void CSMatrix<Scalar>::export_to_file(const char *filename, const char *var_name, MatrixExportFormat fmt, char* number_format, bool invert_storage)
    {
      switch (fmt)
      {
      case EXPORT_FORMAT_MATRIX_MARKET:
      {
                                        FILE* file = fopen(filename, "w");
                                        if (!file)
                                          throw Exceptions::IOException(Exceptions::IOException::Write, filename);
                                        if (Hermes::Helpers::TypeIsReal<Scalar>::value)
                                          fprintf(file, "%%%%MatrixMarket matrix coordinate real general\n");
                                        else
                                          fprintf(file, "%%%%MatrixMarket matrix coordinate complex general\n");

                                        fprintf(file, "%d %d %d\n", this->size, this->size, this->nnz);

                                        if (invert_storage)
                                          this->switch_orientation();
                                        for (unsigned int j = 0; j < this->size; j++)
                                        {
                                          for (int i = Ap[j]; i < Ap[j + 1]; i++)
                                          {
                                            Hermes::Helpers::fprint_coordinate_num(file, i_coordinate(Ai[i] + 1, j + 1, invert_storage), j_coordinate(Ai[i] + 1, j + 1, invert_storage), Ax[i], number_format);
                                            fprintf(file, "\n");
                                          }
                                        }
                                        if (invert_storage)
                                          this->switch_orientation();

                                        fclose(file);
      }
        break;

      case EXPORT_FORMAT_MATLAB_MATIO:
      {
#ifdef WITH_MATIO
                                       mat_sparse_t sparse;
                                       sparse.nzmax = this->nnz;
                                       if (invert_storage)
                                         this->switch_orientation();

                                       sparse.nir = this->nnz;
                                       sparse.ir = Ai;
                                       sparse.njc = this->size + 1;
                                       sparse.jc = (int *)Ap;
                                       sparse.ndata = this->nnz;

                                       size_t dims[2];
                                       dims[0] = this->size;
                                       dims[1] = this->size;

                                       mat_t *mat = Mat_CreateVer(filename, "", MAT_FT_MAT5);

                                       matvar_t *matvar;

                                       // For complex. No allocation here.
                                       double* Ax_re = nullptr;
                                       double* Ax_im = nullptr;

                                       // For real.
                                       if (Hermes::Helpers::TypeIsReal<Scalar>::value)
                                       {
                                         sparse.data = Ax;
                                         matvar = Mat_VarCreate(var_name, MAT_C_SPARSE, MAT_T_DOUBLE, 2, dims, &sparse, MAT_F_DONT_COPY_DATA);
                                       }
                                       else
                                       {
                                         // For complex.
                                         Ax_re = malloc_with_check<CSMatrix<Scalar>, double>(this->nnz, this);
                                         Ax_im = malloc_with_check<CSMatrix<Scalar>, double>(this->nnz, this);
                                         struct mat_complex_split_t z = { Ax_re, Ax_im };

                                         for (int i = 0; i < this->nnz; i++)
                                         {
                                           Ax_re[i] = ((std::complex<double>)(this->Ax[i])).real();
                                           Ax_im[i] = ((std::complex<double>)(this->Ax[i])).imag();
                                           sparse.data = &z;
                                         }
                                         matvar = Mat_VarCreate(var_name, MAT_C_SPARSE, MAT_T_DOUBLE, 2, dims, &sparse, MAT_F_DONT_COPY_DATA | MAT_F_COMPLEX);
                                       }

                                       if (matvar)
                                       {
                                         Mat_VarWrite(mat, matvar, MAT_COMPRESSION_ZLIB);
                                         Mat_VarFree(matvar);
                                       }
                                       if (invert_storage)
                                         this->switch_orientation();
                                       free_with_check(Ax_re);
                                       free_with_check(Ax_im);
                                       Mat_Close(mat);

                                       if (!matvar)
                                         throw Exceptions::IOException(Exceptions::IOException::Write, filename);
#endif
      }
        break;

      case EXPORT_FORMAT_PLAIN_ASCII:
      {
                                      FILE* file = fopen(filename, "w");
                                      if (!file)
                                        throw Exceptions::IOException(Exceptions::IOException::Write, filename);

                                      if (invert_storage)
                                        this->switch_orientation();
                                      for (unsigned int j = 0; j < this->size; j++)
                                      {
                                        for (int i = Ap[j]; i < Ap[j + 1]; i++)
                                        {
                                          Helpers::fprint_coordinate_num(file, i_coordinate(Ai[i], j, invert_storage), j_coordinate(Ai[i], j, invert_storage), Ax[i], number_format);
                                          fprintf(file, "\n");
                                        }
                                      }
                                      if (invert_storage)
                                        this->switch_orientation();

                                      fclose(file);
      }
        break;
#ifdef WITH_BSON
      case EXPORT_FORMAT_BSON:
      {
        // Init bson
        bson bw;
        bson_init(&bw);

        // Matrix size.
        bson_append_int(&bw, "size", this->size);
        // Nonzeros.
        bson_append_int(&bw, "nnz", this->nnz);

        if (invert_storage)
          this->switch_orientation();

        bson_append_start_array(&bw, "Ap");
        for (unsigned int i = 0; i < this->size; i++)
          bson_append_int(&bw, "p", this->Ap[i]);
        bson_append_finish_array(&bw);

        bson_append_start_array(&bw, "Ai");
        for (unsigned int i = 0; i < this->nnz; i++)
          bson_append_int(&bw, "i", this->Ai[i]);
        bson_append_finish_array(&bw);

        bson_append_start_array(&bw, "Ax");
        for (unsigned int i = 0; i < this->nnz; i++)
          bson_append_double(&bw, "x", real(this->Ax[i]));
        bson_append_finish_array(&bw);

        if (!Hermes::Helpers::TypeIsReal<Scalar>::value)
        {
          bson_append_start_array(&bw, "Ax-imag");
          for (unsigned int i = 0; i < this->nnz; i++)
            bson_append_double(&bw, "x-i", imag(this->Ax[i]));
          bson_append_finish_array(&bw);
        }
        bson_append_finish_array(&bw);

        if (invert_storage)
          this->switch_orientation();

        // Done.
        bson_finish(&bw);

        // Write to disk.
        FILE *fpw;
        fpw = fopen(filename, "wb");
        const char *dataw = (const char *)bson_data(&bw);
        fwrite(dataw, bson_size(&bw), 1, fpw);
        fclose(fpw);

        bson_destroy(&bw);
      }
        break;
#endif
      }
    }
示例#29
0
void clsRasterData::outputToMongoDB(map<string,float> header, int nValid, float** position, float* value,string remoteFilename, gridfs* gfs)
{
	float noData = -9999.0f;
	// prepare binary data
	int rows = int(header["NROWS"]);
	int cols = int(header["NCOLS"]);
	float *data = new float[rows*cols];
	
	int index = 0;
	int dataIndex = 0;
	for (int i = 0; i < rows; ++i)
	{
		for (int j = 0; j < cols; ++j)
		{
			dataIndex = i*cols + j;
			if(index < nValid)
			{
				if(position[index][0] == i && position[index][1] == j) 
				{
					data[dataIndex] = value[index];	
					index++;
				}
				else 
					data[dataIndex] = noData;
			}
			else 
				data[dataIndex] = noData;				
		}
	}
	
	bson *p = (bson*)malloc(sizeof(bson));
	bson_init(p);
	bson_append_string(p, "ID", remoteFilename.c_str());
	bson_append_double(p, "CELLSIZE", header["CELLSIZE"]);
	bson_append_double(p, "NODATA_VALUE", noData);
	bson_append_double(p, "NCOLS", cols);
	bson_append_double(p, "NROWS", rows);
	bson_append_double(p, "XLLCENTER", header["XLLCENTER"]);
	bson_append_double(p, "YLLCENTER", header["YLLCENTER"]);
	bson_finish(p);
		
	gridfile gfile[1];
	gridfile_writer_init(gfile, gfs, remoteFilename.c_str(), "float");

	size_t iID = remoteFilename.find_first_of('_'); 
	int subbasinID = atoi(remoteFilename.substr(0, iID).c_str());
	gfile->id.ints[0] = subbasinID;
	
	for (int k = 0; k < rows; k++)
	{
		gridfile_write_buffer(gfile, (const char*)(data+cols*k), sizeof(float)*cols);
	}
	gridfile_set_metadata(gfile, p);
	int response = gridfile_writer_done(gfile);
	//cout << remoteFilename << "\t" << gfile->id.ints[0] <<  gfile->id.ints[1] << gfile->id.ints[2] << endl;

	gridfile_destroy(gfile);	
	bson_destroy(p);
	free(p);
	
	delete data;
}
示例#30
0
int main(){
    mongo_connection conn[1];
    bson_buffer bb;
    bson obj;
    bson cond;
    int i;
    bson_oid_t oid;
    const char* col = "c.update_test";
    const char* ns = "test.c.update_test";

    INIT_SOCKETS_FOR_WINDOWS;

    if (mongo_connect( conn , TEST_SERVER, 27017 )){
        printf("failed to connect\n");
        exit(1);
    }

    /* if the collection doesn't exist dropping it will fail */
    if ( mongo_cmd_drop_collection(conn, "test", col, NULL) == MONGO_OK
          && mongo_find_one(conn, ns, bson_empty(&obj), bson_empty(&obj), NULL) != MONGO_OK ){
        printf("failed to drop collection\n");
        exit(1);
    }

    bson_oid_gen(&oid);

    { /* insert */
        bson_buffer_init(&bb);
        bson_append_oid(&bb, "_id", &oid);
        bson_append_int(&bb, "a", 3 );
        bson_from_buffer(&obj, &bb);
        mongo_insert(conn, ns, &obj);
        bson_destroy(&obj);
    }

    { /* insert */
        bson op;

        bson_buffer_init(&bb);
        bson_append_oid(&bb, "_id", &oid);
        bson_from_buffer(&cond, &bb);

        bson_buffer_init(&bb);
        {
            bson_append_start_object(&bb, "$inc");
                bson_append_int(&bb, "a", 2 );
            bson_append_finish_object(&bb);
        }
        {
            bson_append_start_object(&bb, "$set");
                bson_append_double(&bb, "b", -1.5 );
            bson_append_finish_object(&bb);
        }
        bson_from_buffer(&op, &bb);

        for (i=0; i<5; i++)
            mongo_update(conn, ns, &cond, &op, 0);

        /* cond is used later */
        bson_destroy(&op);
    }

    if( mongo_find_one(conn, ns, &cond, 0, &obj) != MONGO_OK ){
        printf("Failed to find object\n");
        exit(1);
    } else {
        int fields = 0;
        bson_iterator it;
        bson_iterator_init(&it, obj.data);

        bson_destroy(&cond);

        while(bson_iterator_next(&it)){
            switch(bson_iterator_key(&it)[0]){
                case '_': /* id */
                    ASSERT(bson_iterator_type(&it) == BSON_OID);
                    ASSERT(!memcmp(bson_iterator_oid(&it)->bytes, oid.bytes, 12));
                    fields++;
                    break;
                case 'a':
                    ASSERT(bson_iterator_type(&it) == BSON_INT);
                    ASSERT(bson_iterator_int(&it) == 3 + 5*2);
                    fields++;
                    break;
                case 'b':
                    ASSERT(bson_iterator_type(&it) == BSON_DOUBLE);
                    ASSERT(bson_iterator_double(&it) == -1.5);
                    fields++;
                    break;
            }
        }

        ASSERT(fields == 3);
    }

    bson_destroy(&obj);

    mongo_cmd_drop_db(conn, "test");
    mongo_destroy(conn);
    return 0;
}