bson_buffer * bson_append_code_w_scope( bson_buffer * b , const char * name , const char * code , const bson * scope){ int sl = strlen(code) + 1; int size = 4 + 4 + sl + bson_size(scope); if (!bson_append_estart(b, bson_codewscope, name, size)) return 0; bson_append32(b, &size); bson_append32(b, &sl); bson_append(b, code, sl); bson_append(b, scope->data, bson_size(scope)); return b; }
void test_bson_build_full (void) { bson *b, *o; b = bson_build_full (BSON_TYPE_DOUBLE, "double", FALSE, 3.14, BSON_TYPE_STRING, "str", FALSE, "hello world", -1, BSON_TYPE_DOCUMENT, "doc", TRUE, bson_build (BSON_TYPE_STRING, "name", "sub-document", -1, BSON_TYPE_INT32, "answer", 42, BSON_TYPE_NONE), BSON_TYPE_ARRAY, "array", TRUE, bson_build (BSON_TYPE_INT32, "0", 32, BSON_TYPE_INT64, "1", (gint64)-42, BSON_TYPE_NONE), BSON_TYPE_BINARY, "binary0", FALSE, BSON_BINARY_SUBTYPE_GENERIC, "foo\0bar", 7, BSON_TYPE_OID, "_id", FALSE, "1234567890ab", BSON_TYPE_BOOLEAN, "TRUE", FALSE, FALSE, BSON_TYPE_UTC_DATETIME, "date", FALSE, 1294860709000, BSON_TYPE_TIMESTAMP, "ts", FALSE, 1294860709000, BSON_TYPE_NULL, "null", FALSE, BSON_TYPE_REGEXP, "foobar", FALSE, "s/foo.*bar/", "i", BSON_TYPE_JS_CODE, "alert", FALSE, "alert (\"hello world!\");", -1, BSON_TYPE_SYMBOL, "sex", FALSE, "Marilyn Monroe", -1, BSON_TYPE_JS_CODE_W_SCOPE, "print", TRUE, "alert (v);", -1, bson_build (BSON_TYPE_STRING, "v", "hello world", -1, BSON_TYPE_NONE), BSON_TYPE_INT32, "int32", FALSE, 32, BSON_TYPE_INT64, "int64", FALSE, (gint64)-42, BSON_TYPE_NONE); bson_finish (b); o = test_bson_generate_full (); cmp_ok (bson_size (b), "==", bson_size (o), "bson_build_full() and hand crafted BSON object sizes match"); ok (memcmp (bson_data (b), bson_data (o), bson_size (b)) == 0, "bson_build_full() and hand crafted BSON objects match"); bson_free (b); bson_free (o); b = bson_build_full (BSON_TYPE_UNDEFINED, "undef", FALSE, BSON_TYPE_NONE); ok (b == NULL, "bson_build_full() should fail with an unsupported element type"); b = bson_build_full (BSON_TYPE_STRING, "str", FALSE, "hello", -1, BSON_TYPE_UNDEFINED, "undef", FALSE, BSON_TYPE_NONE); ok (b == NULL, "bson_build_full() should fail with an unsupported element type"); }
mongo_cursor* mongo_find(mongo_connection* conn, const char* ns, bson* query, bson* fields, int nToReturn, int nToSkip, int options) { int sl; int res; mongo_cursor * cursor; char * data; mongo_message * mm = mongo_message_create( 16 + /* header */ 4 + /* options */ strlen( ns ) + 1 + /* ns */ 4 + 4 + /* skip,return */ bson_size( query ) + bson_size( fields ) , 0 , 0 , MONGO_OP_QUERY ); data = &mm->data; data = mongo_data_append32( data , &options ); data = mongo_data_append( data , ns , strlen( ns ) + 1 ); data = mongo_data_append32( data , &nToSkip ); data = mongo_data_append32( data , &nToReturn ); data = mongo_data_append( data , query->data , bson_size( query ) ); if ( fields ) data = mongo_data_append( data , fields->data , bson_size( fields ) ); bson_fatal_msg( (data == ((char*)mm) + mm->head.len), "query building fail!" ); res = mongo_message_send( conn , mm ); if(res != MONGO_OK){ return NULL; } cursor = (mongo_cursor*)bson_malloc(sizeof(mongo_cursor)); res = mongo_read_response( conn, (mongo_reply **)&(cursor->reply) ); if( res != MONGO_OK ) { free( cursor ); return NULL; } sl = strlen(ns)+1; cursor->ns = bson_malloc(sl); if (!cursor->ns){ free(cursor->reply); free( cursor ); return NULL; } memcpy( (void*)cursor->ns, ns, sl ); cursor->conn = conn; cursor->current.data = NULL; cursor->options = options; return (mongo_cursor*)cursor; }
void test_bson_string (void) { bson *b; /* Test #1: A single string element, with default size. */ b = bson_new (); ok (bson_append_string (b, "hello", "world", -1), "bson_append_string() works"); bson_finish (b); cmp_ok (bson_size (b), "==", 22, "BSON string element size check"); ok (memcmp (bson_data (b), "\026\000\000\000\002\150\145\154\154\157\000\006\000\000\000" "\167\157\162\154\144\000\000", bson_size (b)) == 0, "BSON string element contents check"); bson_free (b); /* Test #2: A single string element, with explicit length. */ b = bson_new (); ok (bson_append_string (b, "goodbye", "cruel world, this garbage is gone.", strlen ("cruel world")), "bson_append_string() with explicit length works"); bson_finish (b); cmp_ok (bson_size (b), "==", 30, "BSON string element size check, #2"); ok (memcmp (bson_data (b), "\036\000\000\000\002\147\157\157\144\142\171\145\000\014\000" "\000\000\143\162\165\145\154\040\167\157\162\154\144\000\000", bson_size (b)) == 0, "BSON string element contents check, #2"); bson_free (b); /* Test #3: Negative test, passing invalid arguments. */ b = bson_new (); ok (bson_append_string (b, "hello", "world", -42) == FALSE, "bson_append_string() should fail with invalid length"); ok (bson_append_string (b, "hello", NULL, -1) == FALSE, "bson_append_string() should fail without a string"); ok (bson_append_string (b, NULL, "world", -1) == FALSE, "bson_append_string() should fail without a key name"); ok (bson_append_string (NULL, "hello", "world", -1) == FALSE, "bson_append_string() should fail without a BSON object"); bson_finish (b); cmp_ok (bson_size (b), "==", 5, "BSON object should be empty"); ok (bson_append_string (b, "hello", "world", -1) == FALSE, "Appending to a finished element should fail"); bson_free (b); }
int bson_append_code_w_scope_n( bson *b, const char *name, const char *code, int len, const bson *scope ) { int sl = len + 1; int size = 4 + 4 + sl + bson_size( scope ); if ( bson_append_estart( b, BSON_CODEWSCOPE, name, size ) == BSON_ERROR ) return BSON_ERROR; bson_append32( b, &size ); bson_append32( b, &sl ); bson_append( b, code, sl ); bson_append( b, scope->data, bson_size( scope ) ); return BSON_OK; }
/** @internal Append a document-like element to a BSON object. * * Arrays and documents are both similar, and differ very little: * different type, and arrays have restrictions on key names (which * are not enforced by this library). * * This convenience function can append both types. * * @param b is the BSON object to append to. * @param type is the document-like type to append. * @param name is the key name. * @param doc is the document-like object to append. * * @note The @a doc must be a finished BSON object. * * @returns TRUE on success, FALSE otherwise. */ static gboolean _bson_append_document_element (bson *b, bson_type type, const gchar *name, const bson *doc) { if (bson_size (doc) < 0) return FALSE; if (!_bson_append_element_header (b, type, name)) return FALSE; b->data = g_byte_array_append (b->data, bson_data (doc), bson_size (doc)); return TRUE; }
void f_map_bson_buf (void){ buffer_t *buf; bson b[1]; bson_init_empty(b); map_to_bson(sp, b); buf = allocate_buffer(bson_size(b)); memcpy(buf->item, b->data, bson_size(b)); pop_stack(); push_refed_buffer(buf); bson_destroy( b ); }
static int mongo_cursor_op_query( mongo_cursor *cursor ) { int res; bson empty; char *data; mongo_message *mm; /* Set up default values for query and fields, if necessary. */ if( ! cursor->query ) cursor->query = bson_empty( &empty ); else if( mongo_cursor_bson_valid( cursor, cursor->query ) != MONGO_OK ) return MONGO_ERROR; if( ! cursor->fields ) cursor->fields = bson_empty( &empty ); else if( mongo_cursor_bson_valid( cursor, cursor->fields ) != MONGO_OK ) return MONGO_ERROR; mm = mongo_message_create( 16 + /* header */ 4 + /* options */ strlen( cursor->ns ) + 1 + /* ns */ 4 + 4 + /* skip,return */ bson_size( cursor->query ) + bson_size( cursor->fields ) , 0 , 0 , MONGO_OP_QUERY ); data = &mm->data; data = mongo_data_append32( data , &cursor->options ); data = mongo_data_append( data , cursor->ns , strlen( cursor->ns ) + 1 ); data = mongo_data_append32( data , &cursor->skip ); data = mongo_data_append32( data , &cursor->limit ); data = mongo_data_append( data , cursor->query->data , bson_size( cursor->query ) ); if ( cursor->fields ) data = mongo_data_append( data , cursor->fields->data , bson_size( cursor->fields ) ); bson_fatal_msg( ( data == ( ( char * )mm ) + mm->head.len ), "query building fail!" ); res = mongo_message_send( cursor->conn , mm ); if( res != MONGO_OK ) { return MONGO_ERROR; } res = mongo_read_response( cursor->conn, ( mongo_reply ** )&( cursor->reply ) ); if( res != MONGO_OK ) { return MONGO_ERROR; } cursor->seen += cursor->reply->fields.num; cursor->flags |= MONGO_CURSOR_QUERY_SENT; return MONGO_OK; }
mongo_cursor* mongo_find(mongo_connection* conn, const char* ns, bson* query, bson* fields, int nToReturn, int nToSkip, int options){ int sl; mongo_cursor * cursor; char * data; mongo_message * mm = mongo_message_create(conn->p, 16 + /* header */ 4 + /* options */ strlen( ns ) + 1 + /* ns */ 4 + 4 + /* skip,return */ bson_size( query ) + bson_size( fields ) , 0 , 0 , mongo_op_query ); data = &mm->data; data = mongo_data_append32( data , &options ); data = mongo_data_append( data , ns , strlen( ns ) + 1 ); data = mongo_data_append32( data , &nToSkip ); data = mongo_data_append32( data , &nToReturn ); data = mongo_data_append( data , query->data , bson_size( query ) ); if ( fields ) data = mongo_data_append( data , fields->data , bson_size( fields ) ); bson_fatal_msg( (data == ((char*)mm) + mm->head.len), "query building fail!" ); mongo_message_send( conn , mm ); cursor = (mongo_cursor*)apr_palloc(conn->p, sizeof(mongo_cursor)); MONGO_TRY{ cursor->mm = mongo_read_response(conn); }MONGO_CATCH{ //free(cursor); MONGO_RETHROW(); } sl = strlen(ns)+1; cursor->ns = apr_palloc(conn->p, sl); if (!cursor->ns){ /* //free(cursor->mm); //free(cursor); */ return 0; } memcpy((void*)cursor->ns, ns, sl); /* cast needed to silence GCC warning */ cursor->conn = conn; cursor->current.data = NULL; return cursor; }
void test_bson_cursor_get_javascript_w_scope (void) { bson *b, *scope = NULL, *valid; bson_cursor *c; const gchar *s = "deadbeef"; ok (bson_cursor_get_javascript_w_scope (NULL, &s, &scope) == FALSE, "bson_cursor_get_javascript_w_scope() with a NULL cursor fails"); b = test_bson_generate_full (); c = bson_cursor_new (b); ok (bson_cursor_get_javascript_w_scope (c, NULL, &scope) == FALSE, "bson_cursor_get_javascript_w_scope() with a NULL js destination fails"); ok (bson_cursor_get_javascript_w_scope (c, &s, NULL) == FALSE, "bson_cursor_get_javascript_w_scope() with a NULL scope destinatin fails"); ok (bson_cursor_get_javascript_w_scope (c, &s, &scope) == FALSE, "bson_cursor_get_javascript_w_scope() at the initial position fails"); is (s, "deadbeef", "destination remains unchanged after failed cursor operations"); bson_cursor_free (c); c = bson_find (b, "print"); ok (bson_cursor_get_javascript_w_scope (c, &s, &scope), "bson_cursor_get_javascript_w_scope() works"); is (s, "alert (v);", "bson_cursor_get_javascript_w_scope() returns the correct result"); valid = bson_new (); bson_append_string (valid, "v", "hello world", -1); bson_finish (valid); cmp_ok (bson_size (scope), "==", bson_size (valid), "The returned scope's length is correct"); ok (memcmp (bson_data (scope), bson_data (valid), bson_size (scope)) == 0, "The returned scope is correct"); bson_free (valid); bson_cursor_next (c); ok (bson_cursor_get_javascript_w_scope (c, &s, &scope) == FALSE, "bson_cursor_get_javascript_w_scope() should fail when the cursor " "points to non-javascript data"); bson_cursor_free (c); bson_free (b); bson_free (scope); }
mongo_packet * test_mongo_wire_generate_reply (gboolean valid, gint32 nreturn, gboolean with_docs) { mongo_reply_packet_header rh; mongo_packet_header h; mongo_packet *p; guint8 *data; gint data_size = sizeof (mongo_reply_packet_header); bson *b1 = NULL, *b2 = NULL; p = mongo_wire_packet_new (); h.opcode = (valid) ? GINT32_TO_LE (1) : GINT32_TO_LE (42); h.id = GINT32_TO_LE (1984); h.resp_to = GINT32_TO_LE (42); if (with_docs) { b1 = test_bson_generate_full (); b2 = test_bson_generate_full (); data_size += bson_size (b1) + bson_size (b2); } h.length = GINT32_TO_LE (sizeof (mongo_packet_header) + data_size); mongo_wire_packet_set_header (p, &h); data = g_try_malloc (data_size); rh.flags = 0; rh.cursor_id = GINT64_TO_LE ((gint64)12345); rh.start = 0; rh.returned = GINT32_TO_LE (nreturn); memcpy (data, &rh, sizeof (mongo_reply_packet_header)); if (with_docs) { memcpy (data + sizeof (mongo_reply_packet_header), bson_data (b1), bson_size (b1)); memcpy (data + sizeof (mongo_reply_packet_header) + bson_size (b1), bson_data (b2), bson_size (b2)); } mongo_wire_packet_set_data (p, data, data_size); g_free (data); bson_free (b1); bson_free (b2); return p; }
bson_cursor * bson_find (const bson *b, const gchar *name) { bson_cursor *c; if (bson_size (b) == -1 || !name) return NULL; c = bson_cursor_new (b); if (_bson_cursor_find (b, name, sizeof (gint32), bson_size (c->obj) - 1, FALSE, c)) return c; bson_cursor_free (c); return NULL; }
void mongo_insert( mongo_connection * conn , const char * ns , bson * bson ){ char * data; mongo_message * mm = mongo_message_create( 16 /* header */ + 4 /* ZERO */ + strlen(ns) + 1 + bson_size(bson) , 0, 0, mongo_op_insert); data = &mm->data; data = mongo_data_append32(data, &zero); data = mongo_data_append(data, ns, strlen(ns) + 1); data = mongo_data_append(data, bson->data, bson_size(bson)); mongo_message_send(conn, mm); }
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); }
uvmongo_message_t * uvmongo_message_serialize_delete(buffer_t * fullname, bson * selector) { char * data; char * ns = buffer_string(fullname); size_t nslen = strlen(ns) + 1; size_t bslen = bson_size(selector); size_t msglen = 16 + 4 + nslen + 4 + bslen; uvmongo_message_t * msg = uvmongo_message_new(msglen, 0, 0, OP_DELETE); data = &msg->data; data = uvmongo_message_append32(data, &ZERO); data = uvmongo_message_append(data, ns, nslen); data = uvmongo_message_append32(data, &ONE); data = uvmongo_message_append(data, selector->data, bson_size(selector)); return msg; }
void f_bson_buf_find(void){ int size; bson b[1]; bson sub; svalue_t v; bson_iterator it; size = (sp-1)->u.buf->size; bson_init_empty(b); bson_init_size( b, size ); memcpy(b->data, (sp-1)->u.buf->item, size); b->finished = 1; /* buff不合法 */ if(bson_size(b) != size){ pop_n_elems(st_num_arg); push_number(0); goto free_bson; } /* 找不到数据 */ if(!bson_find( &it, b, sp->u.string )){ pop_n_elems(st_num_arg); push_number(0); goto free_bson; } bson_to_v(&it, &v); free_buffer((sp-1)->u.buf); pop_stack(); *sp = v; free_bson: bson_destroy( b ); }
gboolean bson_cursor_next (bson_cursor *c) { const guint8 *d; gint32 pos, bs; if (!c) return FALSE; d = bson_data (c->obj); if (c->pos == 0) pos = sizeof (guint32); else { bs = _bson_get_block_size (bson_cursor_type (c), d + c->value_pos); if (bs == -1) return FALSE; pos = c->value_pos + bs; } if (pos >= bson_size (c->obj) - 1) return FALSE; c->pos = pos; c->key = (gchar *) &d[c->pos + 1]; c->value_pos = c->pos + strlen (c->key) + 2; return TRUE; }
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; } }
static duo_code_t _duo_bson_response(struct duo_ctx *ctx, bson *resp) { bson obj; bson_iterator it; duo_code_t ret; const char *p; int code; bson_init(&obj, (char *)ctx->body, 0); ret = DUO_SERVER_ERROR; if (ctx->body_len <= 0 || bson_size(&obj) > ctx->body_len) { _duo_seterr(ctx, "invalid BSON response"); return (ret); } _BSON_FIND(ctx, &it, &obj, "stat", bson_string); p = bson_iterator_string(&it); if (strcasecmp(p, "OK") == 0) { _BSON_FIND(ctx, &it, &obj, "response", bson_object); if (resp) bson_iterator_subobject(&it, resp); ret = DUO_OK; } else if (strcasecmp(p, "FAIL") == 0) { _BSON_FIND(ctx, &it, &obj, "code", bson_int); code = bson_iterator_int(&it); _BSON_FIND(ctx, &it, &obj, "message", bson_string); _duo_seterr(ctx, "%d: %s", code, bson_iterator_string(&it)); ret = DUO_FAIL; } return (ret); }
void test_bson_cursor_get_document (void) { bson *b, *d = NULL; bson_cursor *c; ok (bson_cursor_get_document (NULL, &d) == FALSE, "bson_cursor_get_document() with a NULL cursor fails"); b = test_bson_generate_full (); c = bson_cursor_new (b); ok (bson_cursor_get_document (c, NULL) == FALSE, "bson_cursor_get_document() with a NULL destination fails"); ok (bson_cursor_get_document (c, &d) == FALSE, "bson_cursor_get_document() at the initial position fails"); ok (d == NULL, "destination remains unchanged after failed cursor operations"); bson_cursor_free (c); c = bson_find (b, "doc"); ok (bson_cursor_get_document (c, &d), "bson_cursor_get_document() works"); cmp_ok (bson_size (d), ">", 0, "the returned document is finished"); bson_free (d); bson_cursor_next (c); ok (bson_cursor_get_document (c, &d) == FALSE, "bson_cursor_get_document() fails if the cursor points to " "non-document data"); bson_cursor_free (c); bson_free (b); }
SEXP rmongo_find(SEXP mongo_conn, SEXP ns, SEXP query, SEXP sort, SEXP fields, SEXP limit, SEXP skip, SEXP options) { mongo* conn = _checkMongo(mongo_conn); const char* _ns = CHAR(STRING_ELT(ns, 0)); bson* _query = _checkBSON(query); bson* _sort = _checkBSON(sort); bson* q = _query; bson sorted_query; if (_sort != NULL && bson_size(_sort) > 5) { q = &sorted_query; bson_init(q); bson_append_bson(q, "$query", _query); bson_append_bson(q, "$orderby", _sort); bson_finish(q); } bson* _fields = _checkBSON(fields); int _limit = asInteger(limit); int _skip = asInteger(skip); int _options = 0; int i; int len = LENGTH(options); for (i = 0; i < len; i++) _options |= INTEGER(options)[i]; mongo_cursor* cursor = mongo_find(conn, _ns, q, _fields, _limit, _skip, _options); if (q == &sorted_query) bson_destroy(&sorted_query); return _mongo_cursor_create(cursor); }
static void do_inserts (mongo_sync_connection *conn) { bson *base; gint i; base = bson_build (BSON_TYPE_STRING, "tutorial-program", "tut_hl_client.c", -1, BSON_TYPE_INT32, "the answer to life, the universe and everything", 42, BSON_TYPE_NONE); bson_finish (base); for (i = 0; i < 1000; i++) { bson *n; n = bson_new_from_data (bson_data (base), bson_size (base) - 1); bson_append_int32 (n, "counter", i); bson_finish (n); if (!mongo_sync_cmd_insert (conn, "lmc.tutorial", n, NULL)) { fprintf (stderr, "Error inserting document %d: %s\n", i, strerror (errno)); exit (1); } bson_free (n); } bson_free (base); }
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); }
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; }
int test_insert_limits( const char *set_name ) { char version[10]; mongo conn[1]; mongo_write_concern wc[1]; int i; char key[10]; int res = 0; bson b[1], b2[1]; bson *objs[2]; mongo_write_concern_init( wc ); wc->w = 1; mongo_write_concern_finish( wc ); /* We'll perform the full test if we're running v2.0 or later. */ if( mongo_get_server_version( version ) != -1 && version[0] <= '1' ) return 0; mongo_replset_init( conn, set_name ); mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 ); mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT ); res = mongo_replset_connect( conn ); if( res != MONGO_OK ) { res = conn->err; return res; } ASSERT( conn->max_bson_size > MONGO_DEFAULT_MAX_BSON_SIZE ); bson_init( b ); for(i=0; i<1200000; i++) { sprintf( key, "%d", i + 10000000 ); bson_append_int( b, key, i ); } bson_finish( b ); ASSERT( bson_size( b ) > conn->max_bson_size ); ASSERT( mongo_insert( conn, "test.foo", b, wc ) == MONGO_ERROR ); ASSERT( conn->err == MONGO_BSON_TOO_LARGE ); mongo_clear_errors( conn ); ASSERT( conn->err == 0 ); bson_init( b2 ); bson_append_int( b2, "foo", 1 ); bson_finish( b2 ); objs[0] = b; objs[1] = b2; ASSERT( mongo_insert_batch( conn, "test.foo", (const bson**)objs, 2, wc, 0 ) == MONGO_ERROR ); ASSERT( conn->err == MONGO_BSON_TOO_LARGE ); mongo_write_concern_destroy( wc ); return 0; }
void mongo_remove(mongo_connection* conn, const char* ns, const bson* cond){ char * data; mongo_message * mm = mongo_message_create( 16 /* header */ + 4 /* ZERO */ + strlen(ns) + 1 + 4 /* ZERO */ + bson_size(cond) , 0 , 0 , mongo_op_delete ); data = &mm->data; data = mongo_data_append32(data, &zero); data = mongo_data_append(data, ns, strlen(ns) + 1); data = mongo_data_append32(data, &zero); data = mongo_data_append(data, cond->data, bson_size(cond)); mongo_message_send(conn, mm); }
SEXP mongo_gridfile_get_descriptor(SEXP gfile) { gridfile* _gfile = _checkGridfile(gfile); if (bson_size(_gfile->meta) <= 5) return R_NilValue; SEXP ret = _mongo_bson_create(_gfile->meta); UNPROTECT(3); return ret; }
int mongo_remove(mongo_connection* conn, const char* ns, const bson* cond){ char * data; mongo_message * mm = mongo_message_create( 16 /* header */ + 4 /* ZERO */ + strlen(ns) + 1 + 4 /* ZERO */ + bson_size(cond) , 0 , 0 , MONGO_OP_DELETE ); data = &mm->data; data = mongo_data_append32(data, &ZERO); data = mongo_data_append(data, ns, strlen(ns) + 1); data = mongo_data_append32(data, &ZERO); data = mongo_data_append(data, cond->data, bson_size(cond)); return mongo_message_send(conn, mm); }
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; }
gboolean bson_cursor_find_next (bson_cursor *c, const gchar *name) { if (!c || !name) return FALSE; return _bson_cursor_find (c->obj, name, c->pos, bson_size (c->obj) - 1, FALSE, c); }