本文整理汇总了C++中bson_size函数的典型用法代码示例。如果您正苦于以下问题:C++ bson_size函数的具体用法?C++ bson_size怎么用?C++ bson_size使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了bson_size函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: bson_append_code_w_scope
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;
}
示例2: test_bson_build_full
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");
}
示例3: mongo_find
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;
}
示例4: test_bson_string
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);
}
示例5: bson_append_code_w_scope_n
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;
}
示例6: _bson_append_document_element
/** @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;
}
示例7: f_map_bson_buf
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 );
}
示例8: mongo_cursor_op_query
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;
}
示例9: mongo_find
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;
}
示例10: test_bson_cursor_get_javascript_w_scope
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);
}
示例11: test_mongo_wire_generate_reply
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;
}
示例12: bson_find
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;
}
示例13: mongo_insert
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);
}
示例14: test_bson_binary
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);
}
示例15: uvmongo_message_serialize_delete
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;
}