void test_bson_cursor_get_string (void) { bson *b; bson_cursor *c; const gchar *s = "deadbeef"; ok (bson_cursor_get_string (NULL, &s) == FALSE, "bson_cursor_get_string() with a NULL cursor fails"); b = test_bson_generate_full (); c = bson_cursor_new (b); ok (bson_cursor_get_string (c, NULL) == FALSE, "bson_cursor_get_string() with a NULL destination fails"); ok (bson_cursor_get_string (c, &s) == FALSE, "bson_cursor_get_string() at the initial position fails"); is (s, "deadbeef", "destination remains unchanged after failed cursor operations"); bson_cursor_free (c); c = bson_find (b, "str"); ok (bson_cursor_get_string (c, &s), "bson_cursor_get_string() works"); is (s, "hello world", "bson_cursor_get_string() returns the correct result"); bson_cursor_next (c); ok (bson_cursor_get_string (c, &s) == FALSE, "bson_cursor_get_string() should fail when the cursor points to " "non-string data"); bson_cursor_free (c); bson_free (b); }
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); }
void test_bson_cursor_get_int32 (void) { bson *b; bson_cursor *c; gint d = 12345; ok (bson_cursor_get_int32 (NULL, &d) == FALSE, "bson_cursor_get_int32() with a NULL cursor fails"); b = test_bson_generate_full (); c = bson_cursor_new (b); ok (bson_cursor_get_int32 (c, NULL) == FALSE, "bson_cursor_get_int32() with a NULL destination fails"); ok (bson_cursor_get_int32 (c, &d) == FALSE, "bson_cursor_get_int32() at the initial position fails"); cmp_ok (d, "==", 12345, "destination remains unchanged after failed cursor operations"); bson_cursor_free (c); c = bson_find (b, "int32"); ok (bson_cursor_get_int32 (c, &d), "bson_cursor_get_int32() works"); cmp_ok (d, "==", 32, "bson_cursor_get_int32() returns the correct result"); bson_cursor_next (c); ok (bson_cursor_get_int32 (c, &d) == FALSE, "bson_cursor_get_int32() should fail when the cursor points to " "non-int32 data"); bson_cursor_free (c); bson_free (b); }
void test_bson_cursor_get_timestamp (void) { bson *b; bson_cursor *c; gint64 d = (gint64)987654; ok (bson_cursor_get_timestamp (NULL, &d) == FALSE, "bson_cursor_get_timestamp() with a NULL cursor fails"); b = test_bson_generate_full (); c = bson_cursor_new (b); ok (bson_cursor_get_timestamp (c, NULL) == FALSE, "bson_cursor_get_timestamp() with a NULL destination fails"); ok (bson_cursor_get_timestamp (c, &d) == FALSE, "bson_cursor_get_timestamp() at the initial position fails"); cmp_ok (d, "==", 987654, "destination remains unchanged after failed cursor operations"); bson_cursor_free (c); c = bson_find (b, "ts"); ok (bson_cursor_get_timestamp (c, &d), "bson_cursor_get_timestamp() works"); ok (d == 1294860709000, "bson_cursor_get_timestamp() returns the correct result"); bson_cursor_next (c); ok (bson_cursor_get_timestamp (c, &d) == FALSE, "bson_cursor_get_timestamp() should fail when the cursor points to " "non-timestamp data"); bson_cursor_free (c); bson_free (b); }
void test_bson_cursor_get_binary (void) { bson *b; bson_cursor *c; const guint8 *d = (guint8 *)"deadbeef"; bson_binary_subtype t = 0xff; gint32 s = -1; ok (bson_cursor_get_binary (NULL, &t, &d, &s) == FALSE, "bson_cursor_get_binary() with a NULL cursor fails"); b = test_bson_generate_full (); c = bson_cursor_new (b); ok (bson_cursor_get_binary (c, NULL, NULL, NULL) == FALSE, "bson_cursor_get_binary() with NULL destinations fails"); ok (bson_cursor_get_binary (c, NULL, &d, &s) == FALSE, "bson_cursor_get_binary() with a NULL subtype destination fails"); ok (bson_cursor_get_binary (c, &t, NULL, &s) == FALSE, "bson_cursor_get_binary() with a NULL binary destination fails"); ok (bson_cursor_get_binary (c, &t, &d, NULL) == FALSE, "bson_cursor_get_binary() with a NULL size destination fails"); ok (bson_cursor_get_binary (c, &t, &d, &s) == FALSE, "bson_cursor_get_binary() at the initial position fails"); ok (memcmp (d, "deadbeef", sizeof ("deadbeef")) == 0, "binary destination remains unchanged after failed cursor operations"); cmp_ok (t, "==", 0xff, "subtype destination remains unchanged after failed cursor " "operations"); cmp_ok (s, "==", -1, "size destination remains unchanged after failed cursor operations"); bson_cursor_free (c); c = bson_find (b, "binary0"); ok (bson_cursor_get_binary (c, &t, &d, &s), "bson_cursor_get_binary() works"); cmp_ok (s, "==", 7, "bson_cursor_get_binary() returns the correct result"); ok (memcmp (d, "foo\0bar", s) == 0, "bson_cursor_get_binary() returns the correct result"); cmp_ok (t, "==", BSON_BINARY_SUBTYPE_GENERIC, "bson_cursor_get_binary() returns the correct result"); bson_cursor_next (c); ok (bson_cursor_get_binary (c, &t, &d, &s) == FALSE, "bson_cursor_get_binary() should fail when the cursor points to " "non-binary data"); bson_cursor_free (c); bson_free (b); }
struct ofields* get_data(struct results *res) { struct ofields *fields; const char *msg; const char *prog; const char *syslog_tag; gint64 date_r; bson_cursor *c; fields = malloc(sizeof(struct ofields)); c = bson_find (res->result, "msg"); if (!bson_cursor_get_string (c, &msg)) { perror ("bson_cursor_get_string()"); exit (1); } bson_cursor_free (c); c = bson_find (res->result, "sys"); if (!bson_cursor_get_string (c, &prog)) { perror ("bson_cursor_get_string()"); exit (1); } bson_cursor_free (c); c = bson_find (res->result, "syslog_tag"); if (!bson_cursor_get_string (c, &syslog_tag)) { perror ("bson_cursor_get_string()"); exit (1); } bson_cursor_free (c); c = bson_find (res->result, "time_rcvd"); if (!bson_cursor_get_utc_datetime (c, &date_r)) { perror ("bson_cursor_get_utc_datetime()"); exit (1); } bson_cursor_free (c); fields->msg = msg; fields->prog = prog; fields->syslog_tag = syslog_tag; fields->date_r = date_r; return fields; }
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); }
void test_bson_find (void) { bson *b; bson_cursor *c; ok (bson_find (NULL, NULL) == NULL, "bson_find() with NULL parameters should fail"); ok (bson_find (NULL, "key") == NULL, "bson_find() with a NULL BSON object should fail"); b = bson_new (); ok (bson_find (b, "key") == NULL, "bson_find() with an unfinished BSON object should fail"); bson_free (b); b = test_bson_generate_full (); ok (bson_find (b, NULL) == FALSE, "bson_find() with a NULL key should fail"); ok (bson_find (b, "__invalid__") == FALSE, "bson_find() with a non-existent key should fail"); ok ((c = bson_find (b, "alert")) != NULL, "bson_find() works"); bson_cursor_free (c); bson_free (b); }
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"); }
void tut_sync_query_simple (void) { mongo_sync_connection *conn; mongo_packet *p; mongo_sync_cursor *cursor; bson *query; gint i = 0; conn = mongo_sync_connect ("localhost", 27017, FALSE); if (!conn) { perror ("mongo_sync_connect()"); exit (1); } query = bson_new (); bson_finish (query); p = mongo_sync_cmd_query (conn, "tutorial.docs", 0, 0, 10, query, NULL); if (!p) { perror ("mongo_sync_cmd_query()"); exit (1); } bson_free (query); cursor = mongo_sync_cursor_new (conn, "tutorial.docs", p); if (!cursor) { perror ("mongo_sync_cursor_new()"); exit (1); } while (mongo_sync_cursor_next (cursor)) { bson *result = mongo_sync_cursor_get_data (cursor); bson_cursor *c; if (!result) { perror ("mongo_sync_cursor_get_data()"); exit (1); } printf ("Keys in document #%d:\n", i); c = bson_cursor_new (result); while (bson_cursor_next (c)) printf ("\t%s\n", bson_cursor_key (c)); i++; bson_cursor_free (c); bson_free (result); } mongo_sync_cursor_free (cursor); mongo_sync_disconnect (conn); }
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); }
static void print_coll_info (bson *info) { bson_cursor *c = NULL; bson *options = NULL; const gchar *name; gboolean capped = FALSE; gint64 size = -1; gint64 max = -1; c = bson_find (info, "name"); bson_cursor_get_string (c, &name); bson_cursor_find (c, "options"); bson_cursor_get_document (c, &options); printf ("Options for %s:\n", name); bson_cursor_free (c); bson_free (info); c = bson_find (options, "capped"); bson_cursor_get_boolean (c, &capped); bson_cursor_free (c); c = bson_find (options, "size"); bson_cursor_get_int64 (c, &size); bson_cursor_free (c); c = bson_find (options, "max"); bson_cursor_get_int64 (c, &max); bson_cursor_free (c); bson_free (options); printf ("\tCapped: %s\n", (capped) ? "yes" : "no"); if (size > 0) printf ("\tSize : %lu\n", size); if (max > 0) printf ("\tMax : %lu\n", max); printf ("\n"); }
static void do_query (mongo_sync_connection *conn) { mongo_sync_cursor *c; bson *query; gchar *error = NULL; query = bson_build (BSON_TYPE_TIMESTAMP, "processed", 1294860709000, BSON_TYPE_NONE); bson_finish (query); c = mongo_sync_cursor_new (conn, "blahblah.plurals", mongo_sync_cmd_query (conn, "blahblah.plurals", 0, 0, 10, query, NULL)); if (!c) { fprintf (stderr, "Error creating the query cursor: %s\n", strerror (errno)); exit (1); } bson_free (query); while (mongo_sync_cursor_next (c)) { bson *b = mongo_sync_cursor_get_data (c); bson_cursor *bc; gint32 w_t; gint64 w_i; if (!b) { int e = errno; mongo_sync_cmd_get_last_error (conn, "blahblah.plurals", &error); fprintf (stderr, "Error retrieving cursor data: %s\n", (error) ? error : strerror (e)); exit (1); } bc = bson_find (b, "word_type"); bson_cursor_get_int32 (bc, &w_t); printf ("\rWord type: %d\n", w_t); bc = bson_find (b, "word_id"); bson_cursor_get_int64 (bc, &w_i); printf ("\rWord id: %d\n", (int)w_i); bson_cursor_free (bc); bson_free (b); } printf ("\n"); mongo_sync_cursor_free (c); }
static void do_query (mongo_sync_connection *conn) { mongo_sync_cursor *c; bson *query; gchar *error = NULL; query = bson_build (BSON_TYPE_STRING, "tutorial-program", "tut_hl_client.c", -1, BSON_TYPE_NONE); bson_finish (query); c = mongo_sync_cursor_new (conn, "lmc.tutorial", mongo_sync_cmd_query (conn, "lmc.tutorial", 0, 0, 10, query, NULL)); if (!c) { fprintf (stderr, "Error creating the query cursor: %s\n", strerror (errno)); exit (1); } bson_free (query); while (mongo_sync_cursor_next (c)) { bson *b = mongo_sync_cursor_get_data (c); bson_cursor *bc; gint32 cnt; if (!b) { int e = errno; mongo_sync_cmd_get_last_error (conn, "lmc", &error); fprintf (stderr, "Error retrieving cursor data: %s\n", (error) ? error : strerror (e)); exit (1); } bc = bson_find (b, "counter"); bson_cursor_get_int32 (bc, &cnt); printf ("\rCounter: %d", cnt); bson_cursor_free (bc); bson_free (b); } printf ("\n"); mongo_sync_cursor_free (c); }
struct team_warning * team_warning_bson_parse(bson *b) { struct team_warning *res = NULL; bson_cursor *bc = NULL; if (!b) return NULL; XCALLOC(res, 1); bc = bson_cursor_new(b); while (bson_cursor_next(bc)) { const unsigned char *key = bson_cursor_key(bc); if (!strcmp(key, "date")) { if (ej_bson_parse_utc_datetime(bc, "date", &res->date) < 0) goto fail; } else if (!strcmp(key, "issuer_id")) { if (ej_bson_parse_int(bc, "issuer_id", &res->issuer_id, 1, 1, 0, 0) < 0) goto fail; } else if (!strcmp(key, "issuer_ip")) { if (ej_bson_parse_ip(bc, "issuer_ip", &res->issuer_ip) < 0) goto fail; } else if (!strcmp(key, "text")) { if (ej_bson_parse_string(bc, "text", &res->text) < 0) goto fail; } else if (!strcmp(key, "comment")) { if (ej_bson_parse_string(bc, "comment", &res->comment) < 0) goto fail; } } bson_cursor_free(bc); return res; fail: if (res) { xfree(res->text); xfree(res->comment); xfree(res); } if (bc) bson_cursor_free(bc); return NULL; }
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 test_func_mongo_sync_oidtest (void) { mongo_sync_connection *conn; bson *boid, *reply = NULL; bson_cursor *c; mongo_packet *p; guint8 *oid; const guint8 *noid; mongo_util_oid_init (0); oid = mongo_util_oid_new (1); boid = bson_new (); bson_append_oid (boid, "driverOIDTest", oid); bson_finish (boid); conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE); p = mongo_sync_cmd_custom (conn, config.db, boid); ok (p != NULL, "driverOIDTest(OID) custom command works"); mongo_wire_reply_packet_get_nth_document (p, 1, &reply); bson_finish (reply); c = bson_find (reply, "oid"); bson_cursor_get_oid (c, &noid); ok (memcmp (oid, noid, 12) == 0, "driverOIDTest(OID) returns the same OID"); bson_cursor_free (c); mongo_sync_disconnect (conn); mongo_wire_packet_free (p); bson_free (boid); bson_free (reply); }
void tut_sync_query_complex (void) { mongo_sync_connection *conn; mongo_packet *p; mongo_sync_cursor *cursor; bson *query, *select; gint i = 0; conn = mongo_sync_connect ("localhost", 27017, FALSE); if (!conn) { perror ("mongo_sync_connect()"); exit (1); } query = bson_build_full (BSON_TYPE_DOCUMENT, "$query", TRUE, bson_build (BSON_TYPE_BOOLEAN, "yes?", FALSE, BSON_TYPE_NONE), BSON_TYPE_DOCUMENT, "$orderby", TRUE, bson_build (BSON_TYPE_INT32, "n", 1, BSON_TYPE_NONE), BSON_TYPE_NONE); bson_finish (query); select = bson_build (BSON_TYPE_INT32, "hello", 1, BSON_TYPE_INT32, "n", 1, BSON_TYPE_INT32, "yes?", 1, BSON_TYPE_NONE); bson_finish (select); p = mongo_sync_cmd_query (conn, "tutorial.docs", 0, 0, 10, query, select); if (!p) { perror ("mongo_sync_cmd_query()"); exit (1); } bson_free (query); bson_free (select); cursor = mongo_sync_cursor_new (conn, "tutorial.docs", p); if (!cursor) { perror ("mongo_sync_cursor_new()"); exit (1); } while (mongo_sync_cursor_next (cursor)) { const char *hello; gint32 n; gboolean yes; bson *result; bson_cursor *c; result = mongo_sync_cursor_get_data (cursor); if (!result) { perror ("mongo_sync_cursor_get_data()"); exit (1); } c = bson_find (result, "hello"); if (!bson_cursor_get_string (c, &hello)) { perror ("bson_cursor_get_string()"); exit (1); } bson_cursor_free (c); c = bson_find (result, "n"); if (!bson_cursor_get_int32 (c, &n)) { perror ("bson_cursor_get_int32()"); exit (1); } bson_cursor_free (c); c = bson_find (result, "yes?"); if (!bson_cursor_get_boolean (c, &yes)) { perror ("bson_cursor_get_boolean()"); exit (1); } bson_cursor_free (c); printf ("Document #%d: hello=%s; n=%d; yes?=%s\n", i, hello, n, (yes) ? "TRUE" : "FALSE"); bson_free (result); i++; } mongo_sync_cursor_free (cursor); mongo_sync_disconnect (conn); }
void test_mongo_wire_cmd_insert_n (void) { bson *ins, *tmp; const bson *docs[10]; mongo_packet *p; mongo_packet_header hdr; const guint8 *data; gint32 data_size; bson_cursor *c; gint32 pos; ins = test_bson_generate_full (); tmp = bson_new (); docs[0] = ins; docs[1] = tmp; docs[2] = ins; docs[3] = ins; docs[4] = NULL; docs[5] = ins; ok (mongo_wire_cmd_insert_n (1, NULL, 1, docs) == NULL, "mongo_wire_cmd_insert_n() fails with a NULL namespace"); ok (mongo_wire_cmd_insert_n (1, "test.ns", 1, NULL) == NULL, "mongo_wire_cmd_insert_n() fails with no documents"); ok (mongo_wire_cmd_insert_n (1, "test.ns", 0, docs) == NULL, "mongo_wire_cmd_insert_n() fails with no documents"); ok (mongo_wire_cmd_insert_n (1, "test.ns", 2, docs) == NULL, "mongo_wire_cmd_insert_n() fails with an unfinished document"); bson_finish (tmp); ok (mongo_wire_cmd_insert_n (1, "test.ns", 5, docs) == NULL, "mongo_wire_cmd_insert_n() fails with a NULL document in the array"); ok ((p = mongo_wire_cmd_insert_n (1, "test.ns", 3, docs)) != NULL, "mongo_wire_cmd_insert() works"); bson_free (ins); bson_free (tmp); /* Test basic header data */ mongo_wire_packet_get_header (p, &hdr); cmp_ok ((data_size = mongo_wire_packet_get_data (p, &data)), "!=", -1, "Packet data size appears fine"); cmp_ok (hdr.length, "==", sizeof (mongo_packet_header) + data_size, "Packet header length is correct"); cmp_ok (hdr.id, "==", 1, "Header ID is ok"); cmp_ok (hdr.resp_to, "==", 0, "Response ID is ok"); /* * Test the first document */ /* pos = zero + collection_name + NULL */ pos = sizeof (gint32) + strlen ("test.ns") + 1; ok ((ins = bson_new_from_data (data + pos, _DOC_SIZE (data, pos) - 1)) != NULL, "First document is included"); bson_finish (ins); ok ((c = bson_find (ins, "int32")) != NULL, "BSON contains 'int32'"); cmp_ok (bson_cursor_type (c), "==", BSON_TYPE_INT32, "int32 has correct type"); bson_cursor_next (c); cmp_ok (bson_cursor_type (c), "==", BSON_TYPE_INT64, "next element has correct type too"); ok (bson_cursor_next (c) == FALSE, "No more data after the update BSON object"); bson_cursor_free (c); /* * Test the second document */ pos += bson_size (ins); ok ((tmp = bson_new_from_data (data + pos, _DOC_SIZE (data, pos) - 1)) != NULL, "Second document is included"); bson_finish (tmp); cmp_ok (bson_size (tmp), "==", 5, "Second document is empty"); bson_free (ins); bson_free (tmp); mongo_wire_packet_free (p); }
void mongo_gridfs_list (config_t *config) { mongo_sync_cursor *cursor; mongo_sync_gridfs *gfs; gfs = mongo_gridfs_connect (config); cursor = mongo_sync_gridfs_list (gfs, NULL); while (mongo_sync_cursor_next (cursor)) { bson *meta = mongo_sync_cursor_get_data (cursor); bson_cursor *c; const guint8 oid[12]; gint32 i32, chunk_size; gint64 length, date; const gchar *md5, *filename = NULL; gchar *oid_s; c = bson_find (meta, "_id"); if (!bson_cursor_get_oid (c, (const guint8 **)&oid)) mongo_gridfs_error (errno); bson_cursor_find (c, "length"); if (!bson_cursor_get_int32 (c, &i32)) { if (!bson_cursor_get_int64 (c, &length)) mongo_gridfs_error (errno); } else length = i32; bson_cursor_find (c, "chunkSize"); if (!bson_cursor_get_int32 (c, &chunk_size)) mongo_gridfs_error (errno); bson_cursor_find (c, "uploadDate"); if (!bson_cursor_get_utc_datetime (c, &date)) mongo_gridfs_error (errno); bson_cursor_find (c, "md5"); if (!bson_cursor_get_string (c, &md5)) mongo_gridfs_error (errno); bson_cursor_find (c, "filename"); bson_cursor_get_string (c, &filename); bson_cursor_free (c); oid_s = mongo_util_oid_as_string (oid); printf ("{ _id: ObjectID(\"%s\"), length: %" G_GINT64_FORMAT ", chunkSize: %i, uploadDate: %" G_GINT64_FORMAT ", md5: \"%s\"", oid_s, length, chunk_size, date, md5); g_free (oid_s); if (filename) printf (", filename: \"%s\"", filename); printf (" }\n"); if (config->verbose) { c = bson_cursor_new (meta); printf ("\tExtra metadata: [ "); while (bson_cursor_next (c)) { if (strcmp (bson_cursor_key (c), "_id") && strcmp (bson_cursor_key (c), "length") && strcmp (bson_cursor_key (c), "chunkSize") && strcmp (bson_cursor_key (c), "uploadDate") && strcmp (bson_cursor_key (c), "md5") && strcmp (bson_cursor_key (c), "filename")) { printf ("%s (%s), ", bson_cursor_key (c), bson_cursor_type_as_string (c)); } } bson_cursor_free (c); printf ("]\n"); } } mongo_sync_gridfs_free (gfs, TRUE); }
void test_mongo_wire_cmd_update (void) { bson *sel, *upd, *tmp; mongo_packet *p; mongo_packet_header hdr; const guint8 *data; gint32 data_size; bson_cursor *c; gint32 pos; sel = bson_new (); bson_append_null (sel, "_id"); bson_finish (sel); upd = test_bson_generate_full (); ok (mongo_wire_cmd_update (1, NULL, 0, sel, upd) == NULL, "mongo_wire_cmd_update() with a NULL namespace should fail"); ok (mongo_wire_cmd_update (1, "test.ns", 0, NULL, upd) == NULL, "mongo_wire_cmd_update() with a NULL selector should fail"); ok (mongo_wire_cmd_update (1, "test.ns", 0, sel, NULL) == NULL, "mongo_wire_cmd_update() with a NULL update should fail"); tmp = bson_new (); ok (mongo_wire_cmd_update (1, "test.ns", 0, tmp, upd) == NULL, "mongo_wire_cmd_update() fails with an unfinished selector"); ok (mongo_wire_cmd_update (1, "test.ns", 0, sel, tmp) == NULL, "mongo_wire_cmd_update() fails with an unfinished update"); bson_free (tmp); ok ((p = mongo_wire_cmd_update (1, "test.ns", 0, sel, upd)) != NULL, "mongo_wire_cmd_update() works"); bson_free (sel); 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"); /* * Verify the selector object. */ /* pos = zero + collection_name + NULL + flags */ pos = sizeof (gint32) + strlen ("test.ns") + 1 + sizeof (gint32); ok ((sel = bson_new_from_data (data + pos, (gint32)data[pos] - 1)) != NULL, "Packet contains a valid BSON selector document"); bson_finish (sel); ok ((c = bson_find (sel, "_id")) != NULL, "BSON contains an _id"); cmp_ok (bson_cursor_type (c), "==", BSON_TYPE_NULL, "_id has correct type"); bson_cursor_free (c); bson_free (sel); /* * Verify the update object */ pos += (gint32)data[pos]; ok ((tmp = bson_new_from_data (data + pos, bson_stream_doc_size (data, pos) - 1)) != NULL, "Packet contains a valid BSON update document"); bson_finish (tmp); cmp_ok (bson_size (upd), "==", bson_size (tmp), "Packet's update document has the correct size"); ok ((c = bson_find (tmp, "int32")) != NULL, "BSON contains 'int32'"); cmp_ok (bson_cursor_type (c), "==", BSON_TYPE_INT32, "int32 has correct type"); bson_cursor_next (c); cmp_ok (bson_cursor_type (c), "==", BSON_TYPE_INT64, "next element has correct type too"); ok (bson_cursor_next (c) == FALSE, "No more data after the update BSON object"); bson_cursor_free (c); bson_free (tmp); bson_free (upd); mongo_wire_packet_free (p); }
void ej_bson_unparse( FILE *out, const struct _bson *b, int is_array) { if (!b) { fprintf(out, "NULL"); return; } if (is_array) { fprintf(out, "[ "); } else { fprintf(out, "{ "); } bson_cursor *cursor = bson_cursor_new(b); int first = 1; while (bson_cursor_next(cursor)) { if (!first) fprintf(out, ", "); if (!is_array) { fprintf(out, "%s : ", bson_cursor_key(cursor)); } bson_type t = bson_cursor_type(cursor); switch (t) { case BSON_TYPE_DOUBLE: break; case BSON_TYPE_STRING: { const char *value = NULL; if (bson_cursor_get_string(cursor, &value)) { fprintf(out, "\"%s\"", value); } } break; case BSON_TYPE_DOCUMENT: { bson *doc = NULL; if (bson_cursor_get_document(cursor, &doc)) { ej_bson_unparse(out, doc, 0); bson_free(doc); } } break; case BSON_TYPE_ARRAY: { bson *doc = NULL; if (bson_cursor_get_array(cursor, &doc)) { ej_bson_unparse(out, doc, 1); bson_free(doc); } } break; case BSON_TYPE_BINARY: { bson_binary_subtype bt = 0; const unsigned char *bd = NULL; int bz = 0; if (bson_cursor_get_binary(cursor, &bt, &bd, &bz) && bt == BSON_BINARY_SUBTYPE_UUID && bz == sizeof(ej_uuid_t)) { ej_uuid_t value; memcpy(&value, bd, sizeof(value)); fprintf(out, "\"%s\"", ej_uuid_unparse(&value, NULL)); } } break; case BSON_TYPE_OID: case BSON_TYPE_BOOLEAN: break; case BSON_TYPE_UTC_DATETIME: { gint64 ts = 0; if (bson_cursor_get_utc_datetime(cursor, &ts)) { time_t tt = (time_t) (ts / 1000); int ms = (int) (ts % 1000); struct tm *ptm = gmtime(&tt); fprintf(out, "\"%d/%02d/%02d %02d:%02d:%02d.%04d\"", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, ms); } } break; case BSON_TYPE_NULL: break; case BSON_TYPE_INT32: { int value = 0; if (bson_cursor_get_int32(cursor, &value)) { fprintf(out, "%d", value); } } break; case BSON_TYPE_INT64: { gint64 value = 0; if (bson_cursor_get_int64(cursor, &value)) { fprintf(out, "%lld", (long long) value); } } break; default: break; } first = 0; } bson_cursor_free(cursor); cursor = NULL; if (is_array) { fprintf(out, " ]"); } else { fprintf(out, " }"); } }
void test_mongo_wire_cmd_delete (void) { mongo_packet *p; bson *s, *tmp; mongo_packet_header hdr; const guint8 *data; gint32 data_size; gint32 pos; bson_cursor *c; s = test_bson_generate_full (); tmp = bson_new (); ok (mongo_wire_cmd_delete (1, NULL, 0, s) == NULL, "mongo_wire_cmd_delete() fails with a NULL namespace"); ok (mongo_wire_cmd_delete (1, "test.ns", 0, NULL) == NULL, "mongo_wire_cmd_delete() fails with a NULL selector"); ok (mongo_wire_cmd_delete (1, "test.ns", 0, tmp) == NULL, "mongo_wire_cmd_delete() fails with an unfinished selector"); bson_free (tmp); ok ((p = mongo_wire_cmd_delete (1, "test.ns", 0, s)) != NULL, "mongo_wire_cmd_delete() works"); bson_free (s); /* Test basic header data */ mongo_wire_packet_get_header (p, &hdr); cmp_ok ((data_size = mongo_wire_packet_get_data (p, &data)), "!=", -1, "Packet data size appears fine"); cmp_ok (hdr.length, "==", sizeof (mongo_packet_header) + data_size, "Packet header length is correct"); cmp_ok (hdr.id, "==", 1, "Header ID is ok"); cmp_ok (hdr.resp_to, "==", 0, "Response ID is ok"); /* * Test the constructed request */ /* pos = zero + ns + NULL + flags */ pos = sizeof (gint32) + strlen ("test.ns") + 1 + sizeof (gint32); ok ((s = bson_new_from_data (data + pos, _DOC_SIZE (data, pos) - 1)) != NULL, "Packet contains a valid BSON update document"); bson_finish (s); ok ((c = bson_find (s, "int32")) != NULL, "BSON contains 'int32'"); cmp_ok (bson_cursor_type (c), "==", BSON_TYPE_INT32, "int32 has correct type"); bson_cursor_next (c); cmp_ok (bson_cursor_type (c), "==", BSON_TYPE_INT64, "next element has correct type too"); ok (bson_cursor_next (c) == FALSE, "No more data after the update BSON object"); bson_cursor_free (c); bson_free (s); mongo_wire_packet_free (p); }
struct team_extra * team_extra_bson_parse(bson *b) { bson_cursor *bc = NULL; bson_cursor *bc2 = NULL; struct team_extra *res = NULL; bson *arr = NULL; bson *doc = NULL; struct team_warning *tw = NULL; if (!b) return NULL; XCALLOC(res, 1); bc = bson_cursor_new(b); while (bson_cursor_next(bc)) { const unsigned char *key = bson_cursor_key(bc); if (!strcmp(key, "_id")) { if (ej_bson_parse_uuid(bc, "_id", &res->uuid) < 0) goto fail; } else if (!strcmp(key, "contest_id")) { if (ej_bson_parse_int(bc, "contest_id", &res->contest_id, 1, 1, 0, 0) < 0) goto fail; } else if (!strcmp(key, "user_id")) { if (ej_bson_parse_int(bc, "user_id", &res->user_id, 1, 1, 0, 0) < 0) goto fail; } else if (!strcmp(key, "viewed_clars")) { if (ej_bson_parse_array(bc, "viewed_clars", &arr) < 0) goto fail; bc2 = bson_cursor_new(arr); while (bson_cursor_next(bc2)) { int clar_id = 0; if (ej_bson_parse_int(bc2, "viewed_clars/clar_id", &clar_id, 1, 0, 0, 0) < 0) goto fail; if (clar_id >= res->clar_map_size) team_extra_extend_clar_map(res, clar_id); res->clar_map[clar_id / BPE] |= (1UL << clar_id % BPE); } bson_cursor_free(bc2); bc2 = NULL; bson_free(arr); arr = NULL; } else if (!strcmp(key, "clar_uuids")) { if (ej_bson_parse_array(bc, "clar_uuids", &arr) < 0) goto fail; bc2 = bson_cursor_new(arr); while (bson_cursor_next(bc2)) { ej_uuid_t uuid; if (ej_bson_parse_uuid(bc2, "clar_uuids/uuid", &uuid) < 0) goto fail; team_extra_add_clar_uuid(res, &uuid); } bson_cursor_free(bc2); bc2 = NULL; bson_free(arr); arr = NULL; } else if (!strcmp(key, "disq_comment")) { if (ej_bson_parse_string(bc, "disq_comment", &res->disq_comment) < 0) goto fail; } else if (!strcmp(key, "warnings")) { if (ej_bson_parse_array(bc, "warnings", &arr) < 0) goto fail; bc2 = bson_cursor_new(arr); while (bson_cursor_next(bc2)) { if (ej_bson_parse_document(bc2, "warnings/warning", &doc) < 0) goto fail; if (!(tw = team_warning_bson_parse(doc))) goto fail; if (res->warn_u == res->warn_a) { if (!(res->warn_a *= 2)) res->warn_a = 16; XREALLOC(res->warns, res->warn_a); } res->warns[res->warn_u++] = tw; tw = NULL; bson_free(doc); doc = NULL; } bson_cursor_free(bc2); bc2 = NULL; bson_free(arr); arr = NULL; } else if (!strcmp(key, "status")) { if (ej_bson_parse_int(bc, "status", &res->status, 1, 0, 0, 0) < 0) goto fail; } else if (!strcmp(key, "run_fields")) { if (ej_bson_parse_int(bc, "run_fields", &res->run_fields, 1, 0, 0, 0) < 0) goto fail; } } bson_cursor_free(bc); return res; fail: team_extra_free(res); if (doc) bson_free(doc); if (arr) bson_free(arr); if (bc2) bson_cursor_free(bc2); if (bc) bson_cursor_free(bc); return NULL; }