Esempio n. 1
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;
}
Esempio n. 2
0
gboolean
bson_cursor_get_javascript_w_scope (const bson_cursor *c,
                                    const gchar **js,
                                    bson **scope)
{
  bson *b;
  gint32 size, docpos;

  if (!js || !scope)
    return FALSE;

  BSON_CURSOR_CHECK_TYPE (c, BSON_TYPE_JS_CODE_W_SCOPE);

  docpos = bson_stream_doc_size (bson_data (c->obj),
                                  c->value_pos + sizeof (gint32)) +
    sizeof (gint32) * 2;
  size = bson_stream_doc_size (bson_data (c->obj), c->value_pos + docpos) -
    sizeof (gint32) - 1;
  b = bson_new_sized (size);
  b->data = g_byte_array_append (b->data,
                                 bson_data (c->obj) + c->value_pos + docpos +
                                 sizeof (gint32), size);
  bson_finish (b);

  *scope = b;
  *js = (gchar *)(bson_data (c->obj) + c->value_pos + sizeof (gint32) * 2);

  return TRUE;
}
Esempio n. 3
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;
}
Esempio n. 4
0
   INT32 RecordSharding::getGroupByRecord(bson* record,
                                          string& collection,
                                          UINT32& groupId)
   {
      INT32 rc = SDB_OK;

      SDB_ASSERT(_inited, "must be inited");
      SDB_ASSERT(NULL != record, "record can't be NULL");

      if (_groupNum > 1)
      {
         if (_isMainCL)
         {
            map<string, CataInfo>::iterator it;

            rc = _cataInfo.getSubCLNameByRecord(bson_data(record), collection);
            if (SDB_OK != rc)
            {
               PD_LOG(PDERROR, "failed to get subCL by record, rc=%d", rc);
               goto error;
            }

            it = _subCataInfo.find(collection);
            if (it == _subCataInfo.end())
            {
               rc = SDB_SYS;
               PD_LOG(PDERROR, "failed to get CataInfo by subCL, subCL=%s, rc=%d",
                      collection.c_str(), rc);
               goto error;
            }

            rc = (it->second).getGroupByRecord(bson_data(record), groupId);
            if (SDB_OK != rc)
            {
               PD_LOG(PDERROR, "failed to get group of subCL[%s] by record, rc=%d",
                      collection.c_str(), rc);
               goto error;
            }
         }
         else
         {
            rc = _cataInfo.getGroupByRecord(bson_data(record), groupId);
            if (SDB_OK != rc)
            {
               PD_LOG(PDERROR, "failed to get group by record, rc=%d", rc);
               goto error;
            }
            collection = _collectionName;
         }
      }
      else
      {
         groupId = 0;
      }

   done:
      return rc;
   error:
      goto done;
   }
Esempio n. 5
0
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");

}
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);
}
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);
}
Esempio n. 8
0
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;
}
Esempio n. 9
0
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);
}
Esempio n. 10
0
 nlohmann::json collection::convert_to_json(std::shared_ptr<bson> const& value)
 {
     char* buffer;
     int size = 0;
     bson2json(bson_data(value.get()), &buffer, &size);
     return nlohmann::json::parse(std::string(buffer, size));
 }
Esempio n. 11
0
v8::Local<v8::Object> decodeObject(const bson *bb)
{
    bson_iterator it;
    bson_iterator_from_buffer(&it, bson_data(bb));

    return decodeObject(&it, false);
}
void
test_bson_timestamp (void)
{
  bson *b;
  gint64 l = 9876543210;

  b = bson_new ();
  ok (bson_append_timestamp (b, "ts", l), "bson_append_timestamp() works");
  bson_finish (b);

  cmp_ok (bson_size (b), "==", 17, "BSON timestamp element size check");
  ok (memcmp (bson_data (b),
              "\021\000\000\000\021\164\163\000\352\026\260\114\002\000\000"
              "\000\000",
              bson_size (b)) == 0,
      "BSON timestamp element contents check");

  bson_free (b);

  b = bson_new ();
  ok (bson_append_timestamp (b, NULL, l) == FALSE,
      "bson_append_timestamp() with a NULL key should fail");
  ok (bson_append_timestamp (NULL, "ts", l) == FALSE,
      "bson_append_timestamp() without a BSON object should fail");
  bson_finish (b);
  cmp_ok (bson_size (b), "==", 5,
          "BSON object should be empty");

  ok (bson_append_timestamp (b, "ts", l) == FALSE,
      "Appending to a finished element should fail");

  bson_free (b);
}
void
test_bson_utc_datetime (void)
{
  bson *b;

  b = bson_new ();
  ok (bson_append_utc_datetime (b, "date", 1294860709000),
      "bson_append_utc_datetime() works");
  bson_finish (b);

  cmp_ok (bson_size (b), "==", 19, "BSON UTC datetime element size check");
  ok (memcmp (bson_data (b),
              "\023\000\000\000\011\144\141\164\145\000\210\154\266\173\055"
              "\001\000\000\000",
              bson_size (b)) == 0,
      "BSON UTC datetime element contents check");

  bson_free (b);

  b = bson_new ();
  ok (bson_append_utc_datetime (b, NULL, 1294860709000) == FALSE,
      "bson_append_utc_datetime() with a NULL key should fail");
  ok (bson_append_utc_datetime (NULL, "date", 1294860709000) == FALSE,
      "bson_append_utc_datetime() without a BSON object should fail");
  bson_finish (b);
  cmp_ok (bson_size (b), "==", 5,
          "BSON object should be empty");

  ok (bson_append_utc_datetime (b, "date", 1294860709000) == FALSE,
      "Appending to a finished element should fail");

  bson_free (b);
}
Esempio n. 14
0
void
test_bson_null (void)
{
  bson *b;

  b = bson_new ();
  ok (bson_append_null (b, "null"),
      "bson_append_null() works");
  bson_finish (b);

  cmp_ok (bson_size (b), "==", 11, "BSON NULL element size check");
  ok (memcmp (bson_data (b),
	      "\013\000\000\000\012\156\165\154\154\000\000",
	      bson_size (b)) == 0,
      "BSON NULL element contents check");

  bson_free (b);

  b = bson_new ();
  ok (bson_append_null (b, NULL) == FALSE,
      "bson_append_null() should fail without a key name");
  ok (bson_append_null (NULL, "null") == FALSE,
      "bson_append_null() should fail without a BSON object");
  bson_finish (b);
  cmp_ok (bson_size (b), "==", 5,
	  "BSON object should be empty");

  ok (bson_append_null (b, "null") == FALSE,
      "Appending to a finished element should fail");

  bson_free (b);
}
Esempio n. 15
0
File: log.c Progetto: soumy/monitor
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);
}
Esempio n. 16
0
gboolean
bson_append_javascript_w_scope (bson *b, const gchar *name,
                                const gchar *js, gint32 len,
                                const bson *scope)
{
  gint size;
  size_t length;

  if (!js || !scope || bson_size (scope) < 0 || len < -1)
    return FALSE;

  if (!_bson_append_element_header (b, BSON_TYPE_JS_CODE_W_SCOPE, name))
    return FALSE;

  length = (len != -1) ? (size_t)len + 1: strlen (js) + 1;

  size = length + sizeof (gint32) + sizeof (gint32) + bson_size (scope);

  _bson_append_int32 (b, GINT32_TO_LE (size));

  /* Append the JS code */
  _bson_append_int32 (b, GINT32_TO_LE (length));
  b->data = g_byte_array_append (b->data, (const guint8 *)js, length - 1);
  _bson_append_byte (b, 0);

  /* Append the scope */
  b->data = g_byte_array_append (b->data, bson_data (scope),
                                 bson_size (scope));

  return TRUE;
}
Esempio n. 17
0
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;
}
Esempio n. 18
0
void
test_bson_document (void)
{
  bson *b, *e1, *e2;

  e1 = bson_new ();
  bson_append_int32 (e1, "i32", 1984);
  bson_append_string (e1, "str", "hello world", -1);
  bson_finish (e1);

  e2 = bson_new ();
  bson_append_string (e2, "foo", "bar", -1);
  ok (bson_append_document (e2, "subd", e1),
      "bson_append_document() works");
  bson_finish (e2);
  bson_free (e1);

  b = bson_new ();
  ok (bson_append_document (b, "doc", e2),
      "bson_append_document() works still");
  bson_finish (b);
  bson_free (e2);

  cmp_ok (bson_size (b), "==", 69, "BSON document element size check");
  ok (memcmp (bson_data (b),
	      "\105\000\000\000\003\144\157\143\000\073\000\000\000\002\146"
	      "\157\157\000\004\000\000\000\142\141\162\000\003\163\165\142"
	      "\144\000\043\000\000\000\020\151\063\062\000\300\007\000\000"
	      "\002\163\164\162\000\014\000\000\000\150\145\154\154\157\040"
	      "\167\157\162\154\144\000\000\000\000",
	      bson_size (b)) == 0,
      "BSON document element contents check");

  bson_free (b);

  e1 = bson_new ();
  bson_append_int32 (e1, "foo", 42);
  b = bson_new ();

  ok (bson_append_document (b, "doc", e1) == FALSE,
      "bson_append_document() with an unfinished document should fail");
  bson_finish (e1);
  ok (bson_append_document (b, NULL, e1) == FALSE,
      "bson_append_document() with a NULL key should fail");
  ok (bson_append_document (b, "doc", NULL) == FALSE,
      "bson_append_document() with a NULL document should fail");
  ok (bson_append_document (NULL, "doc", e1) == FALSE,
      "bson_append_document() without a BSON object should fail");
  bson_finish (b);

  cmp_ok (bson_size (b), "==", 5,
	  "BSON object should be empty");

  ok (bson_append_document (b, "doc", e1) == FALSE,
      "Appending to a finished element should fail");

  bson_free (e1);
  bson_free (b);
}
Esempio n. 19
0
gboolean
bson_cursor_get_binary (const bson_cursor *c,
                        bson_binary_subtype *subtype,
                        const guint8 **data, gint32 *size)
{
  if (!subtype || !size || !data)
    return FALSE;

  BSON_CURSOR_CHECK_TYPE (c, BSON_TYPE_BINARY);

  *size = bson_stream_doc_size (bson_data(c->obj), c->value_pos);
  *subtype = (bson_binary_subtype)(bson_data (c->obj)[c->value_pos +
                                                      sizeof (gint32)]);
  *data = (guint8 *)(bson_data (c->obj) + c->value_pos + sizeof (gint32) + 1);

  return TRUE;
}
Esempio n. 20
0
bson_type
bson_cursor_type (const bson_cursor *c)
{
  if (!c || c->pos < sizeof (gint32))
    return BSON_TYPE_NONE;

  return (bson_type)(bson_data (c->obj)[c->pos]);
}
Esempio n. 21
0
void
test_bson_array (void)
{
  bson *b, *e1, *e2;

  e1 = bson_new ();
  bson_append_int32 (e1, "0", 1984);
  bson_append_string (e1, "1", "hello world", -1);
  bson_finish (e1);

  e2 = bson_new ();
  bson_append_string (e2, "0", "bar", -1);
  ok (bson_append_array (e2, "1", e1),
      "bson_append_array() works");
  bson_finish (e2);
  bson_free (e1);

  b = bson_new ();
  ok (bson_append_array (b, "0", e2),
      "bson_append_array() works still");
  bson_finish (b);
  bson_free (e2);

  cmp_ok (bson_size (b), "==", 58, "BSON array element size check");
  ok (memcmp (bson_data (b),
	      "\072\000\000\000\004\060\000\062\000\000\000\002\060\000\004"
	      "\000\000\000\142\141\162\000\004\061\000\037\000\000\000\020"
	      "\060\000\300\007\000\000\002\061\000\014\000\000\000\150\145"
	      "\154\154\157\040\167\157\162\154\144\000\000\000\000",
	      bson_size (b)) == 0,
      "BSON array element contents check");

  bson_free (b);

  e1 = bson_new ();
  bson_append_int32 (e1, "0", 1984);
  b = bson_new ();

  ok (bson_append_array (b, "array", e1) == FALSE,
      "bson_append_array() with an unfinished array should fail");
  bson_finish (e1);
  ok (bson_append_array (b, NULL, e1) == FALSE,
      "bson_append_array() with a NULL name should fail");
  ok (bson_append_array (b, "foo", NULL) == FALSE,
      "bson_append_array() with a NULL array should fail");
  ok (bson_append_array (NULL, "foo", e1) == FALSE,
      "bson_append_array() with a NULL BSON should fail");
  bson_finish (b);
  cmp_ok (bson_size (b), "==", 5,
	  "BSON object should be empty");

  ok (bson_append_array (b, "array", e1) == FALSE,
      "Appending to a finished element should fail");

  bson_free (e1);
  bson_free (b);
}
Esempio n. 22
0
static void debug_message(const char *msg) {
    bson b[1];
    bson_init( b );
    bson_append_string( b, "type", "debug" );
    bson_append_string( b, "msg", msg );
    bson_finish( b );
    log_raw_direct(bson_data( b ), bson_size( b ));
    bson_destroy( b );
    // log_flush();
}
Esempio n. 23
0
gboolean
bson_cursor_get_symbol (const bson_cursor *c, const gchar **dest)
{
  if (!dest)
    return FALSE;

  BSON_CURSOR_CHECK_TYPE (c, BSON_TYPE_SYMBOL);

  *dest = (gchar *)(bson_data (c->obj) + c->value_pos + sizeof (gint32));

  return TRUE;
}
Esempio n. 24
0
gboolean
bson_cursor_get_oid (const bson_cursor *c, const guint8 **dest)
{
  if (!dest)
    return FALSE;

  BSON_CURSOR_CHECK_TYPE (c, BSON_TYPE_OID);

  *dest = (guint8 *)(bson_data (c->obj) + c->value_pos);

  return TRUE;
}
Esempio n. 25
0
gboolean
bson_cursor_get_boolean (const bson_cursor *c, gboolean *dest)
{
  if (!dest)
    return FALSE;

  BSON_CURSOR_CHECK_TYPE (c, BSON_TYPE_BOOLEAN);

  *dest = (gboolean)(bson_data (c->obj) + c->value_pos)[0];

  return TRUE;
}
Esempio n. 26
0
gboolean
bson_cursor_get_double (const bson_cursor *c, gdouble *dest)
{
  if (!dest)
    return FALSE;

  BSON_CURSOR_CHECK_TYPE (c, BSON_TYPE_DOUBLE);

  memcpy (dest, bson_data (c->obj) + c->value_pos, sizeof (gdouble));
  *dest = GDOUBLE_FROM_LE (*dest);

  return TRUE;
}
Esempio n. 27
0
/** @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;
}
Esempio n. 28
0
gboolean
bson_cursor_get_int64 (const bson_cursor *c, gint64 *dest)
{
  if (!dest)
    return FALSE;

  BSON_CURSOR_CHECK_TYPE (c, BSON_TYPE_INT64);

  memcpy (dest, bson_data (c->obj) + c->value_pos, sizeof (gint64));
  *dest = GINT64_FROM_LE (*dest);

  return TRUE;
}
Esempio n. 29
0
gboolean
bson_cursor_get_array (const bson_cursor *c, bson **dest)
{
  bson *b;
  gint32 size;

  if (!dest)
    return FALSE;

  BSON_CURSOR_CHECK_TYPE (c, BSON_TYPE_ARRAY);

  size = bson_stream_doc_size (bson_data(c->obj), c->value_pos) -
    sizeof (gint32) - 1;
  b = bson_new_sized (size);
  b->data = g_byte_array_append (b->data,
                                 bson_data (c->obj) + c->value_pos +
                                 sizeof (gint32), size);
  bson_finish (b);

  *dest = b;

  return TRUE;
}
Esempio n. 30
0
gboolean
bson_cursor_get_regex (const bson_cursor *c, const gchar **regex,
                       const gchar **options)
{
  if (!regex || !options)
    return FALSE;

  BSON_CURSOR_CHECK_TYPE (c, BSON_TYPE_REGEXP);

  *regex = (gchar *)(bson_data (c->obj) + c->value_pos);
  *options = (gchar *)(*regex + strlen(*regex) + 1);

  return TRUE;
}