コード例 #1
0
ファイル: test-bson.c プロジェクト: dgoon/libbson
static void
test_bson_copy_to_excluding (void)
{
   bson_iter_t iter;
   bson_bool_t r;
   bson_t b;
   bson_t c;
   int i;

   bson_init(&b);
   bson_append_int32(&b, "a", 1, 1);
   bson_append_int32(&b, "b", 1, 2);

   bson_copy_to_excluding(&b, &c, "b", NULL);
   r = bson_iter_init_find(&iter, &c, "a");
   assert(r);
   r = bson_iter_init_find(&iter, &c, "b");
   assert(!r);

   i = bson_count_keys(&b);
   assert_cmpint(i, ==, 2);

   i = bson_count_keys(&c);
   assert_cmpint(i, ==, 1);

   bson_destroy(&b);
   bson_destroy(&c);
}
コード例 #2
0
static void
append_write_err (bson_t *doc,
                  uint32_t code,
                  const char *errmsg,
                  size_t errmsg_len,
                  const bson_t *errinfo)
{
   bson_t array = BSON_INITIALIZER;
   bson_t child;

   BSON_ASSERT (errmsg);

   /* writeErrors: [{index: 0, code: code, errmsg: errmsg, errInfo: {...}}] */
   bson_append_document_begin (&array, "0", 1, &child);
   bson_append_int32 (&child, "index", 5, 0);
   bson_append_int32 (&child, "code", 4, (int32_t) code);
   bson_append_utf8 (&child, "errmsg", 6, errmsg, (int) errmsg_len);
   if (errinfo) {
      bson_append_document (&child, "errInfo", 7, errinfo);
   }

   bson_append_document_end (&array, &child);
   bson_append_array (doc, "writeErrors", 11, &array);

   bson_destroy (&array);
}
コード例 #3
0
static bool
handle_ismaster (mock_server_t   *server,
                 mongoc_stream_t *client,
                 mongoc_rpc_t    *rpc,
                 const bson_t    *doc)
{
   bson_t reply_doc = BSON_INITIALIZER;
   time_t now = time (NULL);

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

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

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

   bson_destroy (&reply_doc);

   return true;
}
コード例 #4
0
ファイル: test-bson.c プロジェクト: dgoon/libbson
static void
test_bson_copy_to (void)
{
   bson_t b;
   bson_t c;
   int i;

   /*
    * Test inline structure copy.
    */
   bson_init(&b);
   assert(bson_append_int32(&b, "foobar", -1, 1234));
   bson_copy_to(&b, &c);
   assert_bson_equal(&b, &c);
   bson_destroy(&c);
   bson_destroy(&b);

   /*
    * Test malloced copy.
    */
   bson_init(&b);
   for (i = 0; i < 1000; i++) {
      assert(bson_append_int32(&b, "foobar", -1, 1234));
   }
   bson_copy_to(&b, &c);
   assert_bson_equal(&b, &c);
   bson_destroy(&c);
   bson_destroy(&b);
}
コード例 #5
0
ファイル: test-iter.c プロジェクト: Cabriter/abelkhan
static void
test_bson_iter_as_bool (void)
{
   bson_iter_t iter;
   bson_t b;

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

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

   bson_destroy(&b);
}
コード例 #6
0
ファイル: queries.c プロジェクト: ablawat/technologie-nosql
bson_t * bson_pipeline_query1_create()
{
    bson_t *bson_pipeline_query = bson_new();
    bson_t *bson1, *bson2, *bson3, *bson4;
    
    bson1 = bson_new();
    
    bson2 = bson_new();
    bson3 = bson_new();
    
    bson_append_utf8(bson3, "_id", -1, "$action", -1);
    
    bson4 = bson_new();
    bson_append_int32(bson4, "$sum", -1, 1);
    bson_append_document(bson3, "count", -1, bson4);
    bson_append_document(bson2, "$group", -1, bson3);
    bson_append_document(bson1, "0", -1, bson2);
    
    
    bson2 = bson_new();
    bson3 = bson_new();
    
    bson_append_int32(bson3, "count", -1, -1);
    bson_append_document(bson2, "$sort", -1, bson3);
    bson_append_document(bson1, "1", -1, bson2);
    
    
    bson_append_array(bson_pipeline_query, "pipeline", -1, bson1);
    
    return bson_pipeline_query;
}
コード例 #7
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);
}
コード例 #8
0
ファイル: bcon-speed.c プロジェクト: rcsanchez97/libbson
int
main (int argc, char *argv[])
{
   int i;
   int n;
   int bcon;
   bson_t bson, foo, bar, baz;
   bson_init (&bson);

   if (argc != 3) {
      fprintf (stderr,
               "usage: bcon-speed NUM_ITERATIONS [y|n]\n"
               "\n"
               "  y = perform speed tests with bcon\n"
               "  n = perform speed tests with bson_append\n"
               "\n");
      return EXIT_FAILURE;
   }

   assert (argc == 3);

   n = atoi (argv[1]);
   bcon = (argv[2][0] == 'y') ? 1 : 0;

   for (i = 0; i < n; i++) {
      if (bcon) {
         BCON_APPEND (&bson,
                      "foo",
                      "{",
                      "bar",
                      "{",
                      "baz",
                      "[",
                      BCON_INT32 (1),
                      BCON_INT32 (2),
                      BCON_INT32 (3),
                      "]",
                      "}",
                      "}");
      } else {
         bson_append_document_begin (&bson, "foo", -1, &foo);
         bson_append_document_begin (&foo, "bar", -1, &bar);
         bson_append_array_begin (&bar, "baz", -1, &baz);
         bson_append_int32 (&baz, "0", -1, 1);
         bson_append_int32 (&baz, "1", -1, 2);
         bson_append_int32 (&baz, "2", -1, 3);
         bson_append_array_end (&bar, &baz);
         bson_append_document_end (&foo, &bar);
         bson_append_document_end (&bson, &foo);
      }

      bson_reinit (&bson);
   }

   bson_destroy (&bson);

   return 0;
}
コード例 #9
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);
}
コード例 #10
0
ファイル: test-json.c プロジェクト: Cabriter/abelkhan
static void
test_bson_as_json (void)
{
   bson_oid_t oid;
   bson_decimal128_t decimal128;
   bson_t *b;
   bson_t *b2;
   char *str;
   size_t len;
   int i;

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

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

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

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

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

   bson_destroy(b);
   bson_destroy(b2);
}
コード例 #11
0
ファイル: test-bson.c プロジェクト: dgoon/libbson
static void
test_bson_count_keys (void)
{
   bson_t b;

   bson_init(&b);
   assert(bson_append_int32(&b, "0", -1, 0));
   assert(bson_append_int32(&b, "1", -1, 1));
   assert(bson_append_int32(&b, "2", -1, 2));
   assert_cmpint(bson_count_keys(&b), ==, 3);
   bson_destroy(&b);
}
コード例 #12
0
static void
test_command (void)
{
   mongoc_database_t *database;
   mongoc_client_t *client;
   mongoc_cursor_t *cursor;
   bson_error_t error;
   const bson_t *doc;
   bool r;
   bson_t cmd = BSON_INITIALIZER;
   bson_t reply;

   client = mongoc_client_new (gTestUri);
   assert (client);

   database = mongoc_client_get_database (client, "admin");

   /*
    * Test a known working command, "ping".
    */
   bson_append_int32 (&cmd, "ping", 4, 1);

   cursor = mongoc_database_command (database, MONGOC_QUERY_NONE, 0, 1, 0, &cmd, NULL, NULL);
   assert (cursor);

   r = mongoc_cursor_next (cursor, &doc);
   assert (r);
   assert (doc);

   r = mongoc_cursor_next (cursor, &doc);
   assert (!r);
   assert (!doc);

   mongoc_cursor_destroy (cursor);


   /*
    * Test a non-existing command to ensure we get the failure.
    */
   bson_reinit (&cmd);
   bson_append_int32 (&cmd, "a_non_existing_command", -1, 1);

   r = mongoc_database_command_simple (database, &cmd, NULL, &reply, &error);
   assert (!r);
   assert (error.domain == MONGOC_ERROR_QUERY);
   assert (error.code == MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND);
   assert (!strcmp ("no such cmd: a_non_existing_command", error.message));

   mongoc_database_destroy (database);
   mongoc_client_destroy (client);
   bson_destroy (&cmd);
}
コード例 #13
0
ファイル: test-bson.c プロジェクト: dgoon/libbson
static void
test_bson_append_iter (void)
{
   bson_iter_t iter;
   bson_bool_t r;
   bson_t b;
   bson_t c;

   bson_init(&b);
   bson_append_int32(&b, "a", 1, 1);
   bson_append_int32(&b, "b", 1, 2);
   bson_append_int32(&b, "c", 1, 3);
   bson_append_utf8(&b, "d", 1, "hello", 5);

   bson_init(&c);

   r = bson_iter_init_find(&iter, &b, "a");
   assert(r);
   r = bson_append_iter(&c, NULL, 0, &iter);
   assert(r);

   r = bson_iter_init_find(&iter, &b, "c");
   assert(r);
   r = bson_append_iter(&c, NULL, 0, &iter);
   assert(r);

   r = bson_iter_init_find(&iter, &b, "d");
   assert(r);
   r = bson_append_iter(&c, "world", -1, &iter);
   assert(r);

   bson_iter_init(&iter, &c);
   r = bson_iter_next(&iter);
   assert(r);
   assert_cmpstr("a", bson_iter_key(&iter));
   assert_cmpint(BSON_TYPE_INT32, ==, bson_iter_type(&iter));
   assert_cmpint(1, ==, bson_iter_int32(&iter));
   r = bson_iter_next(&iter);
   assert(r);
   assert_cmpstr("c", bson_iter_key(&iter));
   assert_cmpint(BSON_TYPE_INT32, ==, bson_iter_type(&iter));
   assert_cmpint(3, ==, bson_iter_int32(&iter));
   r = bson_iter_next(&iter);
   assert(r);
   assert_cmpint(BSON_TYPE_UTF8, ==, bson_iter_type(&iter));
   assert_cmpstr("world", bson_iter_key(&iter));
   assert_cmpstr("hello", bson_iter_utf8(&iter, NULL));

   bson_destroy(&b);
   bson_destroy(&c);
}
コード例 #14
0
ファイル: queries.c プロジェクト: ablawat/technologie-nosql
bson_t * bson_pipeline_query4_create()
{
    bson_t *bson_pipeline_query = bson_new();
    bson_t *bson1, *bson2, *bson3, *bson4;
    
    bson1 = bson_new();
    
    bson2 = bson_new();
    bson3 = bson_new();
    
    bson_append_utf8(bson3, "modelName", -1, "movies", -1);
    bson_append_document(bson2, "$match", -1, bson3);
    bson_append_document(bson1, "0", -1, bson2);
    
    
    bson2 = bson_new();
    bson3 = bson_new();
    
    bson_append_utf8(bson3, "action", -1, "Liked", -1);
    bson_append_document(bson2, "$match", -1, bson3);
    bson_append_document(bson1, "1", -1, bson2);
    bson2 = bson_new();
    bson3 = bson_new();
    
    bson_append_utf8(bson3, "_id", -1, "$title", -1);
    
    bson4 = bson_new();
    bson_append_int32(bson4, "$sum", -1, 1);
    bson_append_document(bson3, "count", -1, bson4);
    bson_append_document(bson2, "$group", -1, bson3);
    bson_append_document(bson1, "2", -1, bson2);
    
    
    bson2 = bson_new();
    bson3 = bson_new();
    
    bson_append_int32(bson3, "count", -1, -1);
    bson_append_document(bson2, "$sort", -1, bson3);
    bson_append_document(bson1, "3", -1, bson2);
    
    
    bson2 = bson_new();
    bson_append_int32(bson2, "$limit", -1, 15);
    
    bson_append_document(bson1, "4", -1, bson2);
    
    
    bson_append_array(bson_pipeline_query, "pipeline", -1, bson1);
    
    return bson_pipeline_query;
}
コード例 #15
0
ファイル: test-bson.c プロジェクト: dgoon/libbson
static void
test_bson_append_int32 (void)
{
   bson_t *b;
   bson_t *b2;

   b = bson_new();
   assert(bson_append_int32(b, "a", -1, -123));
   assert(bson_append_int32(b, "c", -1, 0));
   assert(bson_append_int32(b, "b", -1, 123));
   b2 = get_bson("test33.bson");
   assert_bson_equal(b, b2);
   bson_destroy(b);
   bson_destroy(b2);
}
コード例 #16
0
static bson_bool_t
mongoc_uri_parse_option (mongoc_uri_t *uri,
                         const char   *str)
{
   bson_int32_t v_int;
   const char *end_key;
   char *key;
   char *value;

   if (!(key = scan_to_unichar(str, '=', &end_key))) {
      return FALSE;
   }

   value = strdup(end_key + 1);
   mongoc_uri_do_unescape(&value);

   if (!strcasecmp(key, "connecttimeoutms") ||
       !strcasecmp(key, "sockettimeoutms") ||
       !strcasecmp(key, "maxpoolsize") ||
       !strcasecmp(key, "minpoolsize") ||
       !strcasecmp(key, "maxidletimems") ||
       !strcasecmp(key, "waitqueuemultiple") ||
       !strcasecmp(key, "waitqueuetimeoutms") ||
       !strcasecmp(key, "wtimeoutms")) {
      v_int = strtol(value, NULL, 10);
      bson_append_int32(&uri->options, key, -1, v_int);
   } else if (!strcasecmp(key, "w")) {
      if (*value == '-' || isdigit(*value)) {
         v_int = strtol(value, NULL, 10);
         bson_append_int32(&uri->options, key, -1, v_int);
      } else {
         bson_append_utf8(&uri->options, key, -1, value, -1);
      }
   } else if (!strcasecmp(key, "journal") ||
              !strcasecmp(key, "slaveok") ||
              !strcasecmp(key, "ssl")) {
      bson_append_bool(&uri->options, key, -1, !strcmp(value, "true"));
   } else if (!strcasecmp(key, "readpreferencetags")) {
      mongoc_uri_parse_read_prefs(uri, value);
   } else {
      bson_append_utf8(&uri->options, key, -1, value, -1);
   }

   bson_free(key);
   bson_free(value);

   return TRUE;
}
コード例 #17
0
static void
test_index (void)
{
   mongoc_collection_t *collection;
   mongoc_client_t *client;
   mongoc_index_opt_t opt;
   bson_error_t error;
   bool r;
   bson_t keys;

   mongoc_index_opt_init(&opt);

   client = mongoc_client_new(gTestUri);
   ASSERT (client);

   collection = mongoc_client_get_collection(client, "test", "test");
   ASSERT (collection);

   bson_init(&keys);
   bson_append_int32(&keys, "hello", -1, 1);
   r = mongoc_collection_ensure_index(collection, &keys, &opt, &error);
   ASSERT (r);

   r = mongoc_collection_ensure_index(collection, &keys, &opt, &error);
   ASSERT (r);

   r = mongoc_collection_drop_index(collection, "hello_1", &error);
   ASSERT (r);

   bson_destroy(&keys);

   mongoc_collection_destroy(collection);
   mongoc_client_destroy(client);
}
コード例 #18
0
void
test_func_mongo_sync_max_insert_size (void)
{
  mongo_sync_connection *conn;
  const bson *docs[10];
  bson *b1, *b2, *b3;

  b1 = bson_new ();
  bson_append_string (b1, "func_mongo_sync_max_insert_size", "works", -1);

  bson_finish (b1);
  b2 = bson_new ();
  bson_append_int32 (b2, "int32", 1984);
  bson_finish (b2);
  b3 = bson_new ();
  bson_finish (b3);

  conn = mongo_sync_connect (config.primary_host, config.primary_port,
			     FALSE);

  /*
   * cmd_insert_n()
   */
  mongo_sync_conn_set_max_insert_size (conn, bson_size (b1) +
				       bson_size (b3) + 1);

  docs[0] = b1;
  docs[1] = b2;
  docs[2] = b3;

  ok (mongo_sync_cmd_insert_n (conn, config.ns, 3, docs) == TRUE,
      "mongo_sync_cmd_insert_n() works with a small max_insert_size");

  mongo_sync_conn_set_max_insert_size (conn, 1);
  errno = 0;
  ok (mongo_sync_cmd_insert_n (conn, config.ns, 3, docs) == FALSE,
      "mongo_sync_cmd_insert_n() should fail if any one document is too big");
  cmp_ok (errno, "==", EMSGSIZE,
	  "errno is set to EMSGSIZE");

  /*
   * cmd_insert()
   */
  mongo_sync_conn_set_max_insert_size (conn, bson_size (b1) +
				       bson_size (b3) + 1);
  ok (mongo_sync_cmd_insert (conn, config.ns, b1, b2, b3, NULL) == TRUE,
      "mongo_sync_cmd_insert() works with a small max_insert_size");

  mongo_sync_conn_set_max_insert_size (conn, 1);
  errno = 0;
  ok (mongo_sync_cmd_insert (conn, config.ns, b1, b2, b3, NULL) == FALSE,
      "mongo_sync_cmd_insert() should fail if any one document is too big");
  cmp_ok (errno, "==", EMSGSIZE,
	  "errno is set to EMSGSIZE");

  mongo_sync_disconnect (conn);
  bson_free (b1);
  bson_free (b2);
  bson_free (b3);
}
コード例 #19
0
/* Test method that returns the server's version string */
static String HHVM_METHOD(MongoClient, getServerVersion) {
  bool result;
  bson_t buildInfo, reply;
  bson_error_t error;
  bson_iter_t iter;
  String retval;

  auto client = get_client(this_);

  bson_init(&buildInfo);
  bson_append_int32(&buildInfo, "buildInfo", 9, 1);

  result = mongoc_client_command_simple(client->get(), "test", &buildInfo, nullptr, &reply, &error);

  bson_destroy(&buildInfo);

  if ( ! result) {
    throw Exception("Command error: %s", error.message);
  }

  if (bson_iter_init_find(&iter, &reply, "version")) {
    retval = String(bson_iter_utf8(&iter, nullptr), CopyString);
  }

  bson_destroy(&reply);

  return retval;
}
コード例 #20
0
bool
mongoc_uri_set_option_as_int32 (mongoc_uri_t *uri,
                                const char   *option,
                                int32_t       value)
{
   const bson_t *options;
   bson_iter_t iter;

   BSON_ASSERT (option);

   if (!mongoc_uri_option_is_int32 (option)) {
      return false;
   }

   if ((options = mongoc_uri_get_options (uri)) &&
         bson_iter_init_find_case (&iter, options, option)) {
      if (BSON_ITER_HOLDS_INT32 (&iter)) {
         bson_iter_overwrite_int32 (&iter, value);
         return true;
      } else {
         return false;
      }
   }

   bson_append_int32(&uri->options, option, -1, value);
   return true;
}
コード例 #21
0
void
test_mongo_sync_cmd_custom (void)
{
  mongo_sync_connection *c;
  bson *cmd;

  c = test_make_fake_sync_conn (-1, FALSE);
  cmd = bson_new ();
  bson_append_int32 (cmd, "getnonce", 1);
  bson_finish (cmd);

  ok (mongo_sync_cmd_custom (NULL, "test", cmd) == NULL,
      "mongo_sync_cmd_custom() fails with a NULL connection");
  ok (mongo_sync_cmd_custom (c, NULL, cmd) == NULL,
      "mongo_sync_cmd_custom() fails with a NULL namespace");

  ok (mongo_sync_cmd_custom (c, "test", cmd) == NULL,
      "mongo_sync_cmd_custom() fails with a bogus FD");
  mongo_sync_conn_set_slaveok (c, TRUE);
  ok (mongo_sync_cmd_custom (c, "test", cmd) == NULL,
      "mongo_sync_cmd_custom() fails with a bogus FD");

  bson_free (cmd);
  mongo_sync_disconnect (c);

  test_mongo_sync_cmd_custom_net ();
}
コード例 #22
0
void
test_mongo_sync_cmd_kill_cursors_net (void)
{
  mongo_packet *p;
  mongo_sync_connection *conn;
  bson *b;
  gint i;
  mongo_reply_packet_header rh;
  gint64 cid;

  begin_network_tests (3);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, TRUE);
  mongo_sync_conn_set_auto_reconnect (conn, TRUE);

  b = bson_new ();
  for (i = 0; i < 40; i++)
    {
      bson_reset (b);
      bson_append_string (b, "test-name", __FILE__, -1);
      bson_append_int32 (b, "seq", i);
      bson_finish (b);

      mongo_sync_cmd_insert (conn, config.ns, b, NULL);
    }
  bson_free (b);

  b = bson_new ();
  bson_append_string (b, "test-name", __FILE__, -1);
  bson_finish (b);

  p = mongo_sync_cmd_query (conn, config.ns,
			    MONGO_WIRE_FLAG_QUERY_NO_CURSOR_TIMEOUT,
			    0, 2, b, NULL);
  mongo_wire_reply_packet_get_header (p, &rh);
  cid = rh.cursor_id;
  mongo_wire_packet_free (p);

  ok (mongo_sync_cmd_kill_cursors (conn, 1, cid) == TRUE,
      "mongo_sync_kill_cursors() works");

  p = mongo_sync_cmd_query (conn, config.ns,
			    MONGO_WIRE_FLAG_QUERY_NO_CURSOR_TIMEOUT,
			    0, 2, b, NULL);
  bson_free (b);
  mongo_wire_reply_packet_get_header (p, &rh);
  cid = rh.cursor_id;
  mongo_wire_packet_free (p);
  shutdown (conn->super.fd, SHUT_RDWR);
  sleep (3);

  ok (mongo_sync_cmd_kill_cursors (conn, 1, cid) == TRUE,
      "mongo_sync_cmd_kill_cursors() automatically reconnects");

  mongo_sync_disconnect (conn);

  test_mongo_sync_cmd_kill_cursors_net_secondary ();

  end_network_tests ();
}
コード例 #23
0
bool
_mongoc_cursor_prepare_getmore_command (mongoc_cursor_t *cursor,
                                        bson_t          *command)
{
   const char *collection;
   int collection_len;

   ENTRY;

   _mongoc_cursor_collection (cursor, &collection, &collection_len);

   bson_init (command);
   bson_append_int64 (command, "getMore", 7, mongoc_cursor_get_id (cursor));
   bson_append_utf8 (command, "collection", 10, collection, collection_len);

   if (cursor->batch_size) {
      bson_append_int64 (command, "batchSize", 9, cursor->batch_size);
   }

   /* Find, getMore And killCursors Commands Spec: "In the case of a tailable
      cursor with awaitData == true the driver MUST provide a Cursor level
      option named maxAwaitTimeMS (See CRUD specification for details). The
      maxTimeMS option on the getMore command MUST be set to the value of the
      option maxAwaitTimeMS. If no maxAwaitTimeMS is specified, the driver
      SHOULD not set maxTimeMS on the getMore command."
    */
   if (cursor->flags & MONGOC_QUERY_TAILABLE_CURSOR &&
       cursor->flags & MONGOC_QUERY_AWAIT_DATA &&
       cursor->max_await_time_ms) {
      bson_append_int32 (command, "maxTimeMS", 9, cursor->max_await_time_ms);
   }

   RETURN (true);
}
コード例 #24
0
ファイル: test-writer.c プロジェクト: 3rduncle/libbson
static void
test_bson_writer_null_realloc_2 (void)
{
   const uint8_t testdata[] = { 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0 };
   bson_writer_t *writer;
   uint8_t *buf = bson_malloc0(32);
   size_t buflen = 32;
   bson_t *b;
   int r;
   int i;

   writer = bson_writer_new(&buf, &buflen, 0, NULL, NULL);
   for (i=0; i<5; i++) {
      assert(bson_writer_begin(writer, &b));
      bson_writer_end(writer);
   }

   assert(bson_writer_begin(writer, &b));
   assert(!bson_append_int32(b, "a", -1, 123));
   bson_writer_end(writer);

   r = memcmp(buf, testdata, 32);
   assert(r == 0);
   bson_writer_destroy(writer);

   bson_free (buf);
}
コード例 #25
0
static void
test_mongoc_client_command (void)
{
    mongoc_client_t *client;
    mongoc_cursor_t *cursor;
    const bson_t *doc;
    bool r;
    bson_t cmd = BSON_INITIALIZER;

    client = mongoc_client_new (gTestUri);
    assert (client);

    bson_append_int32 (&cmd, "ping", 4, 1);

    cursor = mongoc_client_command (client, "admin", MONGOC_QUERY_NONE, 0, 1, 0, &cmd, NULL, NULL);
    assert (!cursor->redir_primary);

    r = mongoc_cursor_next (cursor, &doc);
    assert (r);
    assert (doc);

    r = mongoc_cursor_next (cursor, &doc);
    assert (!r);
    assert (!doc);

    mongoc_cursor_destroy (cursor);
    mongoc_client_destroy (client);
    bson_destroy (&cmd);
}
コード例 #26
0
void
test_p_bson_find (void)
{
    bson *b;
    bson_cursor *c;
    gint i;
    gchar **keys;
    gboolean ret = TRUE;

    keys = g_new(gchar *, MAX_KEYS);

    b = bson_new ();
    for (i = 0; i < MAX_KEYS; i++)
    {
        keys[i] = g_strdup_printf ("tmp_key_%d", i);
        bson_append_int32 (b, keys[i], i);
    }
    bson_finish (b);

    for (i = 1; i <= MAX_KEYS; i++)
    {
        c = bson_find (b, keys[i - 1]);
        if (!c)
            ret = FALSE;
        bson_cursor_free (c);
        g_free (keys[i - 1]);
    }

    bson_free (b);
    g_free (keys);

    ok (ret == TRUE,
        "bson_find() performance test ok");
}
コード例 #27
0
ファイル: tut_hl_client.c プロジェクト: 3d0c/libmongo-client
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);
}
コード例 #28
0
ファイル: xuser_mongo.c プロジェクト: NUOG/ejudge
static struct team_extra *
do_get_entry(
        struct xuser_mongo_cnts_state *state,
        int user_id)
{
    struct team_extra *extra = NULL;
    bson *query = NULL;
    int pos = 0, count = 0;
    bson **results = NULL;

    if (user_id <= 0) return NULL;

    if ((extra = find_entry(state, user_id, &pos)))
        return extra;

    query = bson_new();
    bson_append_int32(query, "contest_id", state->contest_id);
    bson_append_int32(query, "user_id", user_id);
    bson_finish(query);
    count = state->plugin_state->common->i->query(state->plugin_state->common, "xuser", 0, 1, query, NULL, &results);
    if (count < 0) goto done;
    if (count > 1) {
        err("do_get_entry: multiple entries returned: %d", count);
        goto done;
    }
    if (count == 1) {
        if (!(extra = team_extra_bson_parse(results[0]))) {
            goto done;
        }
    }
    if (!extra) {
        XCALLOC(extra, 1);
        extra->user_id = user_id;
        extra->contest_id = state->contest_id;
    }
    insert_entry(state, user_id, extra, pos);

done:
    if (query) bson_free(query);
    if (results) {
        for (int i = 0; i < count; ++i) {
            bson_free(results[i]);
        }
        xfree(results);
    }
    return extra;
}
コード例 #29
0
ファイル: cmd_custom.c プロジェクト: sazzer/libmongo-client
void
test_mongo_wire_cmd_custom (void)
{
  bson *cmd;
  mongo_packet *p;

  mongo_packet_header hdr;
  const guint8 *data;
  gint32 data_size;

  bson_cursor *c;
  gint32 pos;

  cmd = bson_new ();
  bson_append_int32 (cmd, "getnonce", 1);

  ok (mongo_wire_cmd_custom (1, "test", 0, NULL) == NULL,
      "mongo_wire_cmd_custom() fails with a NULL command");
  ok (mongo_wire_cmd_custom (1, "test", 0, cmd) == NULL,
      "mongo_wire_cmd_custom() fails with an unfinished command");
  bson_finish (cmd);
  ok (mongo_wire_cmd_custom (1, NULL, 0, cmd) == NULL,
      "mongo_wire_cmd_custom() fails with a NULL db");

  ok ((p = mongo_wire_cmd_custom (1, "test", 0, cmd)) != NULL,
      "mongo_wire_cmd_custom() works");
  bson_free (cmd);

  /* Verify the header */
  mongo_wire_packet_get_header (p, &hdr);
  cmp_ok ((data_size = mongo_wire_packet_get_data (p, &data)), "!=", -1,
	  "Packet data size looks fine");
  cmp_ok (hdr.length, "==", sizeof (mongo_packet_header) + data_size,
	  "Packet header length is OK");
  cmp_ok (hdr.id, "==", 1, "Packet request ID is ok");
  cmp_ok (hdr.resp_to, "==", 0, "Packet reply ID is ok");

  /*
   * Test the created request
   */

  /* pos = zero + collection_name + NULL + skip + ret */
  pos = sizeof (gint32) + strlen ("test.$cmd") + 1 + sizeof (gint32) * 2;
  ok ((cmd = bson_new_from_data (data + pos,
				 _DOC_SIZE (data, pos) - 1)) != NULL,
      "Packet contains a BSON document");
  bson_finish (cmd);

  ok ((c = bson_find (cmd, "getnonce")) != NULL,
      "BSON object contains a 'getnonce' key");
  cmp_ok (bson_cursor_type (c), "==", BSON_TYPE_INT32,
	  "'getnonce' key has the correct type");
  ok (bson_cursor_next (c) == FALSE,
      "'getnonce' key is the last in the object");

  bson_cursor_free (c);
  bson_free (cmd);
  mongo_wire_packet_free (p);
}
コード例 #30
0
ファイル: test.c プロジェクト: chrisrob/libmongo-client
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;
}