static gboolean sim_parser_connect_test3 (void) { bson_t *bson_connect = bson_new (); bson_t child; SimParser *parser = NULL; SimCommand *cmd = NULL; gboolean result = FALSE; uint8_t uuid[]={0x07,0x92,0xd6,0x72,0xf4,0xce,0x11,0xe4,0x9d,0xe2,0x00,0x0c,0x29,0xd9,0x46,0xde}; bson_append_document_begin (bson_connect,"connect", -1, &child); BSON_APPEND_INT32 (&child, "id", 10); BSON_APPEND_INT32 (&child, "type", SIM_SESSION_TYPE_WEB); BSON_APPEND_UTF8 (&child, "version", "x.x.x"); if (bson_append_binary (&child, "sensor_id", -1, BSON_SUBTYPE_UUID, uuid, 16) == FALSE) return FALSE; bson_append_document_end (bson_connect, &child); do{ if ((parser = sim_parser_new()) == NULL) { result = FALSE; break; } if ((cmd = sim_parser_bson (parser, bson_get_data (bson_connect), bson_connect->len)) != NULL) { result = FALSE; break; } result = TRUE; } while (0); return result; }
static void log_buffer_notrunc(const uint8_t *buf, uintptr_t length) { if(buf == NULL || length == 0) { return; } bson b; bson_init(&b); bson_append_string(&b, "type", "buffer"); if(range_is_readable(buf, length) != 0) { bson_append_binary(&b, "buffer", BSON_BIN_BINARY, (const char *) buf, length); char checksum[64]; sha1(buf, length, checksum); bson_append_string(&b, "checksum", checksum); } else { bson_append_string(&b, "buffer", "<INVALID POINTER>"); bson_append_string(&b, "checksum", "???"); } bson_finish(&b); log_raw(bson_data(&b), bson_size(&b)); bson_destroy(&b); }
void chtmtag_attrs(const void *chtmtag, bson *b) { #if !__x86_64__ return; #endif uint16_t count = *((const uint16_t *) chtmtag + 1); const wchar_t **ptr = (const wchar_t **)((const uint8_t *) chtmtag + 32); while (count-- != 0) { const wchar_t *key = ptr[0]; uintptr_t keylen = (uintptr_t) ptr[1]; const wchar_t *value = ptr[2]; uintptr_t valuelen = (uintptr_t) ptr[3]; char *utf8key = utf8_wstring(key, keylen); char *utf8val = utf8_wstring(value, valuelen); uint32_t utf8vallen = *(uint32_t *) utf8val; bson_append_binary(b, utf8key+4, BSON_BIN_BINARY, utf8val+4, utf8vallen); mem_free(utf8val); ptr += 40 / sizeof(uintptr_t); } }
/* {{{ MongoDriver\BSON\Binary */ void VariantToBsonConverter::_convertBinary(bson_t *bson, const char *key, Object v) { String data = v.o_get(s_MongoBsonBinary_data, false, s_MongoBsonBinary_className).toString(); int64_t type = v.o_get(s_MongoBsonBinary_type, false, s_MongoBsonBinary_className).toInt64(); bson_append_binary(bson, key, -1, (bson_subtype_t) type, (const unsigned char*) data.c_str(), data.length()); }
/* Check a correct CONNECT message */ static gboolean sim_parser_connect_test1 (void) { bson_t *bson_connect = bson_new (); bson_t child; uint8_t uuid[]={0x07,0x92,0xd6,0x72,0xf4,0xce,0x11,0xe4,0x9d,0xe2,0x00,0x0c,0x29,0xd9,0x46,0xde}; SimParser *parser = NULL; SimCommand *cmd = NULL; gboolean result = FALSE; bson_append_document_begin (bson_connect,"connect", -1, &child); BSON_APPEND_INT32 (&child, "id", 10); BSON_APPEND_INT32 (&child, "type", SIM_SESSION_TYPE_WEB); BSON_APPEND_UTF8 (&child, "version", "5.0.1"); if (bson_append_binary (&child, "sensor_id", -1, BSON_SUBTYPE_UUID, uuid, 16) == FALSE) return FALSE; bson_append_document_end (bson_connect, &child); /* Check */ bson_iter_t iter; bson_iter_init (&iter, bson_connect); do{ if ((parser = sim_parser_new()) == NULL) { result = FALSE; break; } if ((cmd = sim_parser_bson (parser, bson_get_data (bson_connect), bson_connect->len)) == NULL) { result = FALSE; break; } if (cmd->type != SIM_COMMAND_TYPE_CONNECT) { result = FALSE; break; } if (cmd->data.connect.sensor_ver->major != 5 || cmd->data.connect.sensor_ver->minor != 0 || cmd->data.connect.sensor_ver->micro != 1) { result = FALSE; break; } if (cmd->data.connect.sensor_id == NULL) { result = FALSE; break; } /* Check uuid */ SimUuid * uuidbin = sim_uuid_new_from_bin (uuid); gboolean test = sim_uuid_equal (uuidbin, cmd->data.connect.sensor_id); g_object_unref (uuidbin); if (!test) { result = FALSE; break; } result = TRUE; } while (0); bson_destroy (bson_connect); g_object_unref (parser); return result; }
void ej_bson_append_uuid( struct _bson *b, const unsigned char *key, const ej_uuid_t *p_uuid) { bson_append_binary(b, key, BSON_BINARY_SUBTYPE_UUID, (const unsigned char *) p_uuid, sizeof(*p_uuid)); }
static void log_buffer(bson *b, const char *idx, const uint8_t *buf, uintptr_t length) { uintptr_t trunclength = length < BUFFER_LOG_MAX ? length : BUFFER_LOG_MAX; if(buf == NULL) { trunclength = 0; } if(range_is_readable(buf, length) != 0) { bson_append_binary(b, idx, BSON_BIN_BINARY, (const char *) buf, trunclength); } else { bson_append_binary(b, idx, BSON_BIN_BINARY, "<INVALID POINTER>", 17); } }
static void log_buffer(const char *buf, size_t length) { size_t trunclength = min(length, BUFFER_LOG_MAX); if (buf == NULL) { trunclength = 0; } bson_append_binary( g_bson, g_istr, BSON_BIN_BINARY, buf, trunclength ); }
void mongoBinDataToBSON(const Object& value, const char* key, bson_t* bson) { const String& binary = value->o_get("bin").toString(); bson_append_binary(bson, key, -1, (bson_subtype_t) value->o_get("type").toInt32(), (const uint8_t*) binary.c_str(), binary.size() ); }
static bson *chunk_new( bson_oid_t id, int chunkNumber, const char *data, int len ) { bson *b = bson_malloc( sizeof( bson ) ); bson_init( b ); bson_append_oid( b, "files_id", &id ); bson_append_int( b, "n", chunkNumber ); bson_append_binary( b, "data", BSON_BIN_BINARY, data, len ); bson_finish( b ); return b; }
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); }
void log_wstring(bson *b, const char *idx, const wchar_t *str, int length) { if(str == NULL) { bson_append_string_n(b, idx, "", 0); return; } if(range_is_readable(str, length) != 0) { int ret, utf8len; char *utf8s = utf8_wstring(str, length); utf8len = *(int *) utf8s; ret = bson_append_binary(b, idx, BSON_BIN_BINARY, utf8s+4, utf8len); if(ret == BSON_ERROR) { pipe("CRITICAL:Error creating bson wstring, error %x, utf8len %d.", b->err, utf8len); } mem_free(utf8s); } else { bson_append_binary(b, idx, BSON_BIN_BINARY, "<INVALID POINTER>", 17); } }
static void test_bson_append_binary (void) { const static bson_uint8_t binary[] = { '1', '2', '3', '4' }; bson_t *b; bson_t *b2; b = bson_new(); assert(bson_append_binary(b, "binary", -1, BSON_SUBTYPE_USER, binary, 4)); b2 = get_bson("test24.bson"); assert_bson_equal(b, b2); bson_destroy(b); bson_destroy(b2); }
static void test_bson_append_binary_deprecated (void) { const static bson_uint8_t binary[] = { '1', '2', '3', '4' }; bson_t *b; bson_t *b2; b = bson_new(); assert(bson_append_binary(b, "binary", -1, BSON_SUBTYPE_BINARY_DEPRECATED, binary, 4)); b2 = get_bson("binary_deprecated.bson"); assert_bson_equal(b, b2); bson_destroy(b); bson_destroy(b2); }
static void log_wstring(const wchar_t *str, int length) { if (str == NULL) { bson_append_string_n( g_bson, g_istr, "", 0 ); return; } int ret; char * utf8s = utf8_wstring(str, length); int utf8len = * (int *) utf8s; ret = bson_append_binary( g_bson, g_istr, BSON_BIN_BINARY, utf8s+4, utf8len ); if (ret == BSON_ERROR) { char tmp[64]; snprintf(tmp, 64, "dbg bson err wstring %x utf8len %d", g_bson->err, utf8len); debug_message(tmp); } free(utf8s); }
static bson * chunk_new(bson_oid_t id, int chunkNumber, const char * data, int len) { bson * b; bson_buffer buf; b = (bson *)malloc(sizeof(bson)); if (b == NULL) return NULL; bson_buffer_init(&buf); bson_append_oid(&buf, "files_id", &id); bson_append_int(&buf, "n", chunkNumber); bson_append_binary(&buf, "data", 2, data, len); bson_from_buffer(b, &buf); return b; }
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; }
void test_bson_binary (void) { bson *b; b = bson_new (); ok (bson_append_binary (b, "binary0", BSON_BINARY_SUBTYPE_GENERIC, (guint8 *)"foo\0bar", 7), "bson_append_binary(), type 0 works"); ok (bson_append_binary (b, "binary2", BSON_BINARY_SUBTYPE_BINARY, (guint8 *)"\0\0\0\7foo\0bar", 11), "bson_append_binary(), type 2 works"); bson_finish (b); cmp_ok (bson_size (b), "==", 51, "BSON binary element size check"); ok (memcmp (bson_data (b), "\063\000\000\000\005\142\151\156\141\162\171\060\000\007\000" "\000\000\000\146\157\157\000\142\141\162\005\142\151\156\141" "\162\171\062\000\013\000\000\000\002\000\000\000\007\146\157" "\157\000\142\141\162\000", bson_size (b)) == 0, "BSON binary element contents check"); bson_free (b); b = bson_new (); ok (bson_append_binary (b, NULL, BSON_BINARY_SUBTYPE_GENERIC, (guint8 *)"foo\0bar", 7) == FALSE, "bson_append_binary() without a key name should fail"); ok (bson_append_binary (b, "binary1", BSON_BINARY_SUBTYPE_GENERIC, NULL, 10) == FALSE, "bson_append_binary () without binary data should fail"); ok (bson_append_binary (b, "binary3", BSON_BINARY_SUBTYPE_GENERIC, (guint8 *)"foo\0bar", -1) == FALSE, "bson_append_binary () with an invalid length should fail"); ok (bson_append_binary (NULL, "binary1", BSON_BINARY_SUBTYPE_GENERIC, (guint8 *)"foo\0bar", 7) == FALSE, "bson_append_binary () without a BSON object should fail"); bson_finish (b); cmp_ok (bson_size (b), "==", 5, "BSON object should be empty"); ok (bson_append_binary (b, "binary", BSON_BINARY_SUBTYPE_GENERIC, (guint8 *)"foo\0bar", 7) == FALSE, "Appending to a finished element should fail"); bson_free (b); }
/** * _mongoc_gridfs_file_flush_page: * * Unconditionally flushes the file's current page to the database. * The page to flush is determined by page->n. * * Side Effects: * * On success, file->page is properly destroyed and set to NULL. * * Returns: * * True on success; false otherwise. */ static bool _mongoc_gridfs_file_flush_page (mongoc_gridfs_file_t *file) { bson_t *selector, *update; bool r; const uint8_t *buf; uint32_t len; ENTRY; BSON_ASSERT (file); BSON_ASSERT (file->page); buf = _mongoc_gridfs_file_page_get_data (file->page); len = _mongoc_gridfs_file_page_get_len (file->page); selector = bson_new (); bson_append_value (selector, "files_id", -1, &file->files_id); bson_append_int32 (selector, "n", -1, file->n); update = bson_sized_new (file->chunk_size + 100); bson_append_value (update, "files_id", -1, &file->files_id); bson_append_int32 (update, "n", -1, file->n); bson_append_binary (update, "data", -1, BSON_SUBTYPE_BINARY, buf, len); r = mongoc_collection_update (file->gridfs->chunks, MONGOC_UPDATE_UPSERT, selector, update, NULL, &file->error); bson_destroy (selector); bson_destroy (update); if (r) { _mongoc_gridfs_file_page_destroy (file->page); file->page = NULL; r = mongoc_gridfs_file_save (file); } RETURN (r); }
void log_wstring(bson *b, const char *idx, const wchar_t *str, int length) { if(str == NULL || length == 0) { bson_append_string_n(b, idx, "", 0); return; } char *utf8s = copy_utf8_wstring(str, length); if(utf8s != NULL) { int utf8len = *(int *) utf8s; if(bson_append_string_n(b, idx, utf8s+4, utf8len) == BSON_ERROR) { pipe("CRITICAL:Error creating bson wstring, error %x, utf8len %d.", b->err, utf8len); } mem_free(utf8s); } else { bson_append_binary(b, idx, BSON_BIN_BINARY, "<INVALID POINTER>", 17); } }
static void test_binary (void) { bson_t bcon, expected; bson_init (&bcon); bson_init (&expected); bson_append_binary ( &expected, "foo", -1, BSON_SUBTYPE_BINARY, (uint8_t *) "deadbeef", 8); BCON_APPEND (&bcon, "foo", BCON_BIN (BSON_SUBTYPE_BINARY, (const uint8_t *) "deadbeef", 8), NULL); bson_eq_bson (&bcon, &expected); bson_destroy (&bcon); bson_destroy (&expected); }
static int resolve_block(struct inode * e, uint8_t hash[HASH_LEN], char * buf) { bson query; int res; mongo_cursor curs; bson_iterator i; bson_type bt; const char * key; mongo * conn = get_conn(); bson_init(&query); bson_append_binary(&query, "_id", 0, (char*)hash, 20); bson_finish(&query); mongo_cursor_init(&curs, conn, blocks_name); mongo_cursor_set_query(&curs, &query); mongo_cursor_set_limit(&curs, 1); res = mongo_cursor_next(&curs); bson_destroy(&query); if(res != MONGO_OK) { mongo_cursor_destroy(&curs); return -EIO; } bson_iterator_init(&i, mongo_cursor_bson(&curs)); size_t outsize, compsize = 0; const char * compdata = NULL; uint32_t offset = 0, size = 0; while((bt = bson_iterator_next(&i)) > 0) { key = bson_iterator_key(&i); if(strcmp(key, "data") == 0) { compsize = bson_iterator_bin_len(&i); compdata = bson_iterator_bin_data(&i); } else if(strcmp(key, "offset") == 0) offset = bson_iterator_int(&i); else if(strcmp(key, "size") == 0) size = bson_iterator_int(&i); } if(curs.err != MONGO_CURSOR_EXHAUSTED) { fprintf(stderr, "Error getting extents %d", curs.err); return -EIO; } if(!compdata) { fprintf(stderr, "No data in block?\n"); return -EIO; } outsize = MAX_BLOCK_SIZE; if((res = snappy_uncompress(compdata, compsize, buf + offset, &outsize)) != SNAPPY_OK) { fprintf(stderr, "Error uncompressing block %d\n", res); return -EIO; } if(offset > 0) memset(buf, 0, offset); compsize = outsize + offset; if(compsize < size) memset(buf + compsize, 0, size - compsize); mongo_cursor_destroy(&curs); return 0; }
/* * Add key-op-value to a bson filter document */ int db_mongodb_bson_filter_add(bson_t *doc, const db_key_t* _k, const db_op_t* _op, const db_val_t* _v, int idx) { bson_t mdoc; db_key_t tkey; const db_val_t *tval; int vtype; str ocmp; tkey = _k[idx]; tval = _v + idx; vtype = VAL_TYPE(tval); /* OP_EQ is handled separately */ if(!strcmp(_op[idx], OP_LT)) { ocmp.s = "$lt"; ocmp.len = 3; } else if(!strcmp(_op[idx], OP_LEQ)) { ocmp.s = "$lte"; ocmp.len = 4; } else if(!strcmp(_op[idx], OP_GT)) { ocmp.s = "$gt"; ocmp.len = 3; } else if(!strcmp(_op[idx], OP_GEQ)) { ocmp.s = "$gte"; ocmp.len = 4; } else if(!strcmp(_op[idx], OP_NEQ) || !strcmp(_op[idx], "!=")) { ocmp.s = "$ne"; ocmp.len = 3; } else { LM_ERR("unsuported match operator: %s\n", _op[idx]); goto error; } if(!bson_append_document_begin(doc, tkey->s, tkey->len, &mdoc)) { LM_ERR("failed to append start to bson doc %.*s %s ... [%d]\n", tkey->len, tkey->s, ocmp.s, idx); goto error; } if(VAL_NULL(tval)) { if(!bson_append_null(&mdoc, ocmp.s, ocmp.len)) { LM_ERR("failed to append null to bson doc %.*s %s null [%d]\n", tkey->len, tkey->s, ocmp.s, idx); goto error; } goto done; } switch(vtype) { case DB1_INT: if(!bson_append_int32(&mdoc, ocmp.s, ocmp.len, VAL_INT(tval))) { LM_ERR("failed to append int to bson doc %.*s %s %d [%d]\n", tkey->len, tkey->s, ocmp.s, VAL_INT(tval), idx); goto error; } break; case DB1_BIGINT: if(!bson_append_int64(&mdoc, ocmp.s, ocmp.len, VAL_BIGINT(tval ))) { LM_ERR("failed to append bigint to bson doc %.*s %s %lld [%d]\n", tkey->len, tkey->s, ocmp.s, VAL_BIGINT(tval), idx); goto error; } return -1; case DB1_DOUBLE: if(!bson_append_double(&mdoc, ocmp.s, ocmp.len, VAL_DOUBLE(tval))) { LM_ERR("failed to append double to bson doc %.*s %s %f [%d]\n", tkey->len, tkey->s, ocmp.s, VAL_DOUBLE(tval), idx); goto error; } break; case DB1_STRING: if(!bson_append_utf8(&mdoc, ocmp.s, ocmp.len, VAL_STRING(tval), strlen(VAL_STRING(tval))) ) { LM_ERR("failed to append string to bson doc %.*s %s %s [%d]\n", tkey->len, tkey->s, ocmp.s, VAL_STRING(tval), idx); goto error; } break; case DB1_STR: if(!bson_append_utf8(&mdoc, ocmp.s, ocmp.len, VAL_STR(tval).s, VAL_STR(tval).len) ) { LM_ERR("failed to append str to bson doc %.*s %s %.*s [%d]\n", tkey->len, tkey->s, ocmp.s, VAL_STR(tval).len, VAL_STR(tval).s, idx); goto error; } break; case DB1_DATETIME: if(!bson_append_time_t(&mdoc, ocmp.s, ocmp.len, VAL_TIME(tval))) { LM_ERR("failed to append time to bson doc %.*s %s %ld [%d]\n", tkey->len, tkey->s, ocmp.s, VAL_TIME(tval), idx); goto error; } break; case DB1_BLOB: if(!bson_append_binary(&mdoc, ocmp.s, ocmp.len, BSON_SUBTYPE_BINARY, (const uint8_t *)VAL_BLOB(tval).s, VAL_BLOB(tval).len) ) { LM_ERR("failed to append blob to bson doc %.*s %s [bin] [%d]\n", tkey->len, tkey->s, ocmp.s, idx); goto error; } break; case DB1_BITMAP: if(!bson_append_int32(&mdoc, ocmp.s, ocmp.len, VAL_INT(tval))) { LM_ERR("failed to append bitmap to bson doc %.*s %s %d [%d]\n", tkey->len, tkey->s, ocmp.s, VAL_INT(tval), idx); goto error; } break; default: LM_ERR("val type [%d] not supported\n", vtype); goto error; } done: if(!bson_append_document_end(doc, &mdoc)) { LM_ERR("failed to append end to bson doc %.*s %s ... [%d]\n", tkey->len, tkey->s, ocmp.s, idx); goto error; } return 0; error: return -1; }
void MongodbObject::AddData(const String &fieldname, uint8 *data, int32 dataSize) { BSON_VERIFY(bson_append_binary(objectData->object, fieldname.c_str(), BSON_BIN_BINARY, (const char *)data, dataSize)); }
INT32 main ( INT32 argc, CHAR **argv ) { // initialize local variables CHAR *pHostName = NULL ; CHAR *pServiceName = NULL ; CHAR *pUsr = NULL ; CHAR *pPasswd = NULL ; // define a connetion handle; use to connect to database sdbConnectionHandle connection = 0 ; // define a collection space handle sdbCSHandle collectionspace = 0 ; // define a collection handle sdbCollectionHandle collection = 0 ; // define a cursor handle for query sdbCursorHandle cursor = 0 ; // define local variables // initialize them before use bson obj ; bson rule ; bson condition ; INT32 rc = SDB_OK ; // read argument pHostName = (CHAR*)argv[1] ; pServiceName = (CHAR*)argv[2] ; pUsr = (CHAR*)argv[3] ; pPasswd = (CHAR*)argv[4] ; // verify syntax if ( 5 != argc ) { displaySyntax ( (CHAR*)argv[0] ) ; exit ( 0 ) ; } // connect to database rc = sdbConnect ( pHostName, pServiceName, pUsr, pPasswd, &connection ) ; if( rc!=SDB_OK ) { printf("Failed to connet to database, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // create collection space rc = sdbCreateCollectionSpace ( connection, COLLECTION_SPACE_NAME, SDB_PAGESIZE_4K, &collectionspace ) ; if( rc!=SDB_OK ) { printf("Failed to create collection space, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // create collection in a specified colletion space. rc = sdbCreateCollection ( collectionspace, COLLECTION_NAME, &collection ) ; if( rc!=SDB_OK ) { printf("Failed to create collection, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // insert records to the collection bson_init( &obj ) ; // insert a English record createEnglishRecord ( &obj ) ; rc = sdbInsert ( collection, &obj ) ; if ( rc ) { printf ( "Failed to insert record, rc = %d" OSS_NEWLINE, rc ) ; } bson_destroy ( &obj ) ; // query the records // the result is in the cursor handle rc = sdbQuery(collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ; if( rc!=SDB_OK ) { printf("Failed to query, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // update the record // let's set the rule and query condition first // here,we make the condition to be NULL // so all the records will be update bson_init( &rule ) ; bson_append_start_object ( &rule, "$set" ) ; bson_append_start_object ( &rule, "dev_id" ) ; bson_append_binary ( &rule, "id_rand", BSON_BIN_BINARY, "a", 1 ) ; bson_append_start_object ( &rule, "id_s" ) ; bson_append_int ( &rule, "year", 2005 ) ; bson_append_int ( &rule, "mon", 11 ) ; bson_append_int ( &rule, "day", 10 ) ; bson_append_int ( &rule, "num", 1024 ) ; bson_append_finish_object ( &rule ) ; //bson_append_binary ( &rule, "id_rand", BSON_BIN_BINARY, "a", 1 ) ; bson_append_finish_object ( &rule ) ; bson_append_finish_object ( &rule ) ; rc = bson_finish ( &rule ) ; CHECK_RC ( rc, "Failed to build bson" ) ; printf("The update rule is:") ; bson_print( &rule ) ; bson_init ( &condition ) ; bson_append_start_object ( &condition, "dev_id.id_s" ) ; bson_append_int ( &condition, "year", 2007 ) ; bson_append_int ( &condition, "mon", 11 ) ; bson_append_int ( &condition, "day", 10 ) ; bson_append_int ( &condition, "num", 1024 ) ; bson_append_finish_object ( &condition ) ; rc = bson_finish ( &condition ) ; CHECK_RC ( rc, "Failed to build bson" ) ; rc = sdbUpsert( collection, &rule, &condition, NULL ) ; if( rc!=SDB_OK ) { printf("Failed to update the record, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } bson_destroy(&rule); printf("Success to update!" OSS_NEWLINE ) ; done: // drop collection space rc = sdbDropCollectionSpace( connection, COLLECTION_SPACE_NAME ) ; if ( rc != SDB_OK ) { printf("Failed to drop the specified collection,\ rc = %d" OSS_NEWLINE, rc ) ; }
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; }
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; }
/* * Add key-value to a bson document */ int db_mongodb_bson_add(bson_t *doc, const db_key_t _k, const db_val_t *_v, int idx) { int vtype; vtype = VAL_TYPE(_v); if(VAL_NULL(_v)) { if(!bson_append_null(doc, _k->s, _k->len)) { LM_ERR("failed to append int to bson doc %.*s = %d [%d]\n", _k->len, _k->s, VAL_INT(_v), idx); goto error; } goto done; } switch(vtype) { case DB1_INT: if(!bson_append_int32(doc, _k->s, _k->len, VAL_INT(_v))) { LM_ERR("failed to append int to bson doc %.*s = %d [%d]\n", _k->len, _k->s, VAL_INT(_v), idx); goto error; } break; case DB1_BIGINT: if(!bson_append_int64(doc, _k->s, _k->len, VAL_BIGINT(_v ))) { LM_ERR("failed to append bigint to bson doc %.*s = %lld [%d]\n", _k->len, _k->s, VAL_BIGINT(_v), idx); goto error; } return -1; case DB1_DOUBLE: if(!bson_append_double(doc, _k->s, _k->len, VAL_DOUBLE(_v))) { LM_ERR("failed to append double to bson doc %.*s = %f [%d]\n", _k->len, _k->s, VAL_DOUBLE(_v), idx); goto error; } break; case DB1_STRING: if(!bson_append_utf8(doc, _k->s, _k->len, VAL_STRING(_v), strlen(VAL_STRING(_v))) ) { LM_ERR("failed to append string to bson doc %.*s = %s [%d]\n", _k->len, _k->s, VAL_STRING(_v), idx); goto error; } break; case DB1_STR: if(!bson_append_utf8(doc, _k->s, _k->len, VAL_STR(_v).s, VAL_STR(_v).len) ) { LM_ERR("failed to append str to bson doc %.*s = %.*s [%d]\n", _k->len, _k->s, VAL_STR(_v).len, VAL_STR(_v).s, idx); goto error; } break; case DB1_DATETIME: if(!bson_append_time_t(doc, _k->s, _k->len, VAL_TIME(_v))) { LM_ERR("failed to append time to bson doc %.*s = %ld [%d]\n", _k->len, _k->s, VAL_TIME(_v), idx); goto error; } break; case DB1_BLOB: if(!bson_append_binary(doc, _k->s, _k->len, BSON_SUBTYPE_BINARY, (const uint8_t *)VAL_BLOB(_v).s, VAL_BLOB(_v).len) ) { LM_ERR("failed to append blob to bson doc %.*s = [bin] [%d]\n", _k->len, _k->s, idx); goto error; } break; case DB1_BITMAP: if(!bson_append_int32(doc, _k->s, _k->len, VAL_INT(_v))) { LM_ERR("failed to append bitmap to bson doc %.*s = %d [%d]\n", _k->len, _k->s, VAL_INT(_v), idx); goto error; } break; default: LM_ERR("val type [%d] not supported\n", vtype); return -1; } done: return 0; error: return -1; }
static void lua_val_to_bson(lua_State *L, const char *key, int vpos, bson *bs, int tref) { int vtype = lua_type(L, vpos); char nbuf[TCNUMBUFSIZ]; if (key == NULL && vtype != LUA_TTABLE) { luaL_error(L, "lua_val_to_bson: Table must be on top of lua stack"); return; } switch (vtype) { case LUA_TTABLE: { if (vpos < 0) { vpos = lua_gettop(L) + vpos + 1; } lua_checkstack(L, 3); int bsontype_found = luaL_getmetafield(L, vpos, "__bsontype"); if (!bsontype_found) { lua_rawgeti(L, LUA_REGISTRYINDEX, tref); //+ reg table lua_pushvalue(L, vpos); //+ val lua_rawget(L, -2); //-val +reg table val if (lua_toboolean(L, -1)) { //already traversed lua_pop(L, 2); break; } lua_pop(L, 1); //-reg table val lua_pushvalue(L, vpos); lua_pushboolean(L, 1); lua_rawset(L, -3); lua_pop(L, 1); //-reg table int len = 0; bool query = false; bool array = true; if (luaL_getmetafield(L, vpos, "__query")) { lua_pop(L, 1); query = true; array = false; } if (array) { for (lua_pushnil(L); lua_next(L, vpos); lua_pop(L, 1)) { ++len; if ((lua_type(L, -2) != LUA_TNUMBER) || (lua_tointeger(L, -2) != len)) { lua_pop(L, 2); array = false; break; } } } if (array) { if (key) bson_append_start_array(bs, key); int i; for (i = 1; i <= len; ++i, lua_pop(L, 1)) { lua_rawgeti(L, vpos, i); bson_numstrn(nbuf, TCNUMBUFSIZ, (int64_t) i); lua_val_to_bson(L, nbuf, -1, bs, tref); } if (key) bson_append_finish_array(bs); } else if (query) { //special query builder case //oarr format: //{ {fname1, v1, v2...}, {fname2, v21, v22,..}, ... } //where: vN: {op, val} OR {val} with '__bval' metafield //Eg: {fname : {$inc : {...}, $dec : {...}}} -> {fname, {$inc, {}}, {$dec, {}}} lua_getfield(L, vpos, "_oarr"); //+oarr if (!lua_istable(L, -1)) { //it is not array lua_pop(L, 1); break; } if (key) bson_append_start_object(bs, key); //iterate over _oarr int ipos = lua_gettop(L); size_t ilen = lua_objlen(L, ipos); lua_checkstack(L, 2); size_t i; for (i = 1; i <= ilen; ++i, lua_pop(L, 1)) { lua_rawgeti(L, ipos, i); //gettop == 3 if (!lua_istable(L, -1)) continue; char *fname = NULL; int jpos = lua_gettop(L); size_t jlen = lua_objlen(L, jpos); lua_checkstack(L, 3); bool wrapped = false; size_t j; for (j = 1; j <= jlen; ++j, lua_pop(L, 1)) { lua_rawgeti(L, jpos, j); if (j == 1) { fname = strdup(lua_tostring(L, -1)); continue; } if (!fname || !lua_istable(L, -1)) { //invalid state lua_pop(L, 1); //pop val break; } int vblkpos = lua_gettop(L); if (j == 2 && luaL_getmetafield(L, -1, "__bval")) { //{val} single value +metafield lua_pop(L, 1); //-metafield lua_rawgeti(L, vblkpos, 1); //+val lua_val_to_bson(L, fname, lua_gettop(L), bs, tref); lua_pop(L, 2); //-val -lua_rawgeti break; //Terminate due single val } else { //{op, val} value if (!wrapped) { bson_append_start_object(bs, fname); wrapped = true; } lua_rawgeti(L, vblkpos, 1); //+op const char *op = lua_tostring(L, -1); if (op) { lua_rawgeti(L, vblkpos, 2); //+val lua_val_to_bson(L, op, lua_gettop(L), bs, tref); lua_pop(L, 1); //-val } lua_pop(L, 1); //-op } } if (wrapped) { bson_append_finish_object(bs); } if (fname) { free(fname); fname = NULL; } } if (key) bson_append_finish_object(bs); lua_pop(L, 1); //-oarr } else { if (key) bson_append_start_object(bs, key); TCLIST *keys = tclistnew(); //we need to sort keys due to unordered nature of lua tables for (lua_pushnil(L); lua_next(L, vpos);) { lua_pop(L, 1); //-val size_t ksize = 0; int ktype = lua_type(L, -1); if (ktype == LUA_TSTRING) { //accept only string keys const char* key = lua_tolstring(L, -1, &ksize); tclistpush(keys, key, ksize); } } tclistsort(keys); int i; for (i = 0; i < TCLISTNUM(keys); ++i) { int vkeysz = TCLISTVALSIZ(keys, i); const char *vkey = TCLISTVALPTR(keys, i); lua_pushlstring(L, vkey, vkeysz); lua_rawget(L, vpos); //+val if (key == NULL && lua_type(L, -1) == LUA_TSTRING && vkeysz == JDBIDKEYNAMEL && !strcmp(JDBIDKEYNAME, vkey)) { //root level OID as string //pack OID as type table lua_push_bsontype_table(L, BSON_OID); //+type table lua_pushvalue(L, -2); //dup oid(val) on stack lua_rawseti(L, -2, 1); //pop oid val if (ejdbisvalidoidstr(lua_tostring(L, -2))) { lua_val_to_bson(L, vkey, lua_gettop(L), bs, tref); } else { luaL_error(L, "OID _id='%s' is not valid", lua_tostring(L, -2)); } lua_pop(L, 1); //-type table } else { lua_val_to_bson(L, vkey, lua_gettop(L), bs, tref); } lua_pop(L, 1); //-val } tclistdel(keys); if (key) bson_append_finish_object(bs); } } else { //metafield __bsontype on top int bson_type = lua_tointeger(L, -1); if (!key && bson_type != BSON_OBJECT && bson_type != BSON_ARRAY) { lua_pop(L, 1); luaL_error(L, "Invalid object structure"); } lua_pop(L, 1); //-metafield __bsontype lua_rawgeti(L, -1, 1); //get first value switch (bson_type) { case BSON_OID: { const char* boid = lua_tostring(L, -1); if (boid && strlen(boid) == 24) { bson_oid_t oid; bson_oid_from_string(&oid, boid); bson_append_oid(bs, key, &oid); } break; } case BSON_DATE: bson_append_date(bs, key, (bson_date_t) lua_tonumber(L, -1)); break; case BSON_REGEX: { const char* regex = lua_tostring(L, -1); lua_rawgeti(L, -2, 2); // re opts const char* options = lua_tostring(L, -1); if (regex && options) { bson_append_regex(bs, key, regex, options); } lua_pop(L, 1); break; } case BSON_BINDATA: { size_t len; const char* cbuf = lua_tolstring(L, -1, &len); bson_append_binary(bs, key, BSON_BIN_BINARY, cbuf, len); break; } case BSON_NULL: bson_append_null(bs, key); break; case BSON_UNDEFINED: bson_append_undefined(bs, key); break; case BSON_OBJECT: if (key) bson_append_start_object(bs, key); lua_val_to_bson(L, NULL, vpos, bs, tref); if (key) bson_append_finish_object(bs); break; case BSON_ARRAY: if (key) bson_append_start_array(bs, key); lua_val_to_bson(L, NULL, vpos, bs, tref); if (key) bson_append_finish_array(bs); break; case BSON_DOUBLE: bson_append_double(bs, key, (double) lua_tonumber(L, -1)); break; case BSON_INT: bson_append_int(bs, key, (int32_t) lua_tonumber(L, -1)); break; case BSON_LONG: bson_append_long(bs, key, (int64_t) lua_tonumber(L, -1)); break; case BSON_BOOL: bson_append_bool(bs, key, lua_toboolean(L, -1)); break; default: break; } lua_pop(L, 1); //-1 first value } break; } case LUA_TNIL: bson_append_null(bs, key); break; case LUA_TNUMBER: { lua_Number numval = lua_tonumber(L, vpos); if (numval == floor(numval)) { int64_t iv = (int64_t) numval; if (-(1LL << 31) <= iv && iv <= (1LL << 31)) { bson_append_int(bs, key, iv); } else { bson_append_long(bs, key, iv); } } else { bson_append_double(bs, key, numval); } break; } case LUA_TBOOLEAN: bson_append_bool(bs, key, lua_toboolean(L, vpos)); break; case LUA_TSTRING: bson_append_string(bs, key, lua_tostring(L, vpos)); break; } }
EXPORT int mongo_bson_buffer_append_binary(struct bson_buffer* b, char* name, int type, void* value, int len) { return (bson_append_binary((bson*) b, name, type, (const char*) value, len) == BSON_OK); }