static void example (void) { bson_t *query; bson_t *doc; doc = BCON_NEW ("hello", "[", "{", "foo", BCON_UTF8 ("bar"), "}", "]"); query = BCON_NEW ("hello.0.foo", BCON_UTF8 ("bar")); log_query (doc, query); check_match (doc, query); bson_destroy (doc); bson_destroy (query); /* i is > 1 or i < -1. */ query = BCON_NEW ("$or", "[", "{", "i", "{", "$gt", BCON_INT32 (1), "}", "}", "{", "i", "{", "$lt", BCON_INT32 (-1), "}", "}", "]"); doc = BCON_NEW ("i", BCON_INT32 (2)); log_query (doc, query); check_match (doc, query); bson_destroy (doc); doc = BCON_NEW ("i", BCON_INT32 (0)); log_query (doc, query); check_match (doc, query); bson_destroy (doc); bson_destroy (query); }
static void test_mongoc_matcher_compare (void) { mongoc_matcher_t *matcher; compare_check checks[] = { { "$gt", 2, 2, false }, { "$gte", 2, 2, true}, { "$lt", 2, 2, false}, { "$lte", 2, 2, true}, { "$ne", 2, 2, false}, { NULL } }; bson_t *doc; bson_t *q; int i; for (i = 0; checks [i].op; i++) { doc = BCON_NEW ("a", BCON_INT32 (checks[i].doc)); q = BCON_NEW ("a", "{", checks[i].op, BCON_INT32 (checks[i].query), "}"); matcher = mongoc_matcher_new (q, NULL); assert (matcher); assert (mongoc_matcher_match (matcher, doc) == checks[i].expected); bson_destroy (q); bson_destroy (doc); mongoc_matcher_destroy (matcher); } }
static void test_bson_iter_find_descendant (void) { bson_iter_t iter; bson_iter_t desc; bson_t *b; b = get_bson(BINARY_DIR"/dotkey.bson"); assert(bson_iter_init(&iter, b)); assert(bson_iter_find_descendant(&iter, "a.b.c.0", &desc)); assert(BSON_ITER_HOLDS_INT32(&desc)); assert(bson_iter_int32(&desc) == 1); bson_destroy(b); b = BCON_NEW ("foo", "{", "bar", "[", "{", "baz", BCON_INT32 (1), "}", "]", "}"); assert (bson_iter_init (&iter, b)); assert (bson_iter_find_descendant (&iter, "foo.bar.0.baz", &desc)); assert (BSON_ITER_HOLDS_INT32 (&desc)); assert (bson_iter_int32 (&desc) == 1); bson_destroy (b); b = BCON_NEW ("nModified", BCON_INT32 (1), "n", BCON_INT32 (2)); assert (bson_iter_init (&iter, b)); assert (bson_iter_find_descendant (&iter, "n", &desc)); assert (!strcmp (bson_iter_key (&desc), "n")); bson_destroy (b); b = BCON_NEW ("", BCON_INT32 (1), "n", BCON_INT32 (2)); assert (bson_iter_init (&iter, b)); assert (bson_iter_find_descendant (&iter, "n", &desc)); assert (!strcmp (bson_iter_key (&desc), "n")); bson_destroy (b); }
static void test_extract_ctx (void) { int32_t a, b, c; bson_t *bson = BCON_NEW ("a", BCON_INT32 (1), "b", BCON_INT32 (2), "c", BCON_INT32 (3)); test_extract_ctx_helper (bson, 3, "a", BCONE_INT32 (a), NULL, "b", BCONE_INT32 (b), NULL, "c", BCONE_INT32 (c), NULL); assert (a == 1); assert (b == 2); assert (c == 3); bson_destroy (bson); }
static void test_mongoc_matcher_eq_doc (void) { bson_t *spec; bson_t *doc; bson_error_t error; mongoc_matcher_t *matcher; /* {doc: {a: 1}} matches itself */ spec = BCON_NEW ("doc", "{", "a", BCON_INT32 (1), "}"); matcher = mongoc_matcher_new (spec, &error); BSON_ASSERT (matcher); BSON_ASSERT (mongoc_matcher_match (matcher, spec)); /* {doc: {a: 1}} matches {doc: {a: 1}, foo: "whatever"} */ doc = BCON_NEW ("doc", "{", "a", BCON_INT32 (1), "}", "foo", BCON_UTF8 ("whatever")); BSON_ASSERT (mongoc_matcher_match (matcher, doc)); bson_destroy (doc); /* {doc: {a: 1}} doesn't match {doc: 1} */ doc = BCON_NEW ("doc", BCON_INT32 (1)); BSON_ASSERT (!mongoc_matcher_match (matcher, doc)); bson_destroy (doc); /* {doc: {a: 1}} doesn't match {doc: {}} */ doc = BCON_NEW ("doc", "{", "}"); BSON_ASSERT (!mongoc_matcher_match (matcher, doc)); bson_destroy (doc); /* {doc: {a: 1}} doesn't match {doc: {a: 2}} */ doc = BCON_NEW ("doc", "{", "a", BCON_INT32 (2), "}"); BSON_ASSERT (!mongoc_matcher_match (matcher, doc)); bson_destroy (doc); /* {doc: {a: 1}} doesn't match {doc: {b: 1}} */ doc = BCON_NEW ("doc", "{", "b", BCON_INT32 (1), "}"); BSON_ASSERT (!mongoc_matcher_match (matcher, doc)); bson_destroy (doc); /* {doc: {a: 1}} doesn't match {doc: {a: 1, b: 1}} */ doc = BCON_NEW ("doc", "{", "a", BCON_INT32 (1), "b", BCON_INT32 (1), "}"); BSON_ASSERT (!mongoc_matcher_match (matcher, doc)); bson_destroy (doc); /* {doc: {a: 1, b:1}} matches itself */ bson_destroy (spec); mongoc_matcher_destroy (matcher); spec = BCON_NEW ("doc", "{", "a", BCON_INT32 (1), "b", BCON_INT32 (1), "}"); matcher = mongoc_matcher_new (spec, &error); BSON_ASSERT (matcher); BSON_ASSERT (mongoc_matcher_match (matcher, spec)); /* {doc: {a: 1, b:1}} doesn't match {doc: {a: 1}} */ doc = BCON_NEW ("doc", "{", "a", BCON_INT32 (1), "}"); BSON_ASSERT (!mongoc_matcher_match (matcher, doc)); bson_destroy (spec); bson_destroy (doc); mongoc_matcher_destroy (matcher); }
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; }
int main (int argc, char *argv[]) { mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error; bson_t *query; bson_t *update; bson_t reply; char *str; mongoc_init (); client = mongoc_client_new ("mongodb://127.0.0.1:27017/"); collection = mongoc_client_get_collection (client, "test", "test"); /* * Build our query, {"cmpxchg": 1} */ query = BCON_NEW ("cmpxchg", BCON_INT32 (1)); /* * Build our update. {"$set": {"cmpxchg": 2}} */ update = BCON_NEW ("$set", "{", "cmpxchg", BCON_INT32 (2), "}"); /* * Submit the findAndModify. */ if (!mongoc_collection_find_and_modify (collection, query, NULL, update, NULL, false, false, true, &reply, &error)) { fprintf (stderr, "find_and_modify() failure: %s\n", error.message); return 1; } /* * Print the result as JSON. */ str = bson_as_json (&reply, NULL); printf ("%s\n", str); bson_free (str); /* * Cleanup. */ bson_destroy (query); bson_destroy (update); bson_destroy (&reply); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mongoc_cleanup (); return 0; }
int main (int argc, char *argv[]) { bson_t * query; /* time_t oid_thinks_time; //what time does the OID think it is bson_oid_t oid; bson_oid_t *oid_pointer = &oid; bson_oid_init (&oid, NULL); // get a standard ObjectId oid_thinks_time = bson_oid_get_time_t (&oid); //It was just made printf ("The OID was generated at %u\n", (unsigned) oid_thinks_time); //prove it time_t ts = time(NULL); //make a new time struct tm * timeinfo = localtime(&ts); timeinfo->tm_year = 2014-1900; //-1900 because time.h timeinfo->tm_mon = 12 - 1; // time.h off by one (starts at 0) timeinfo->tm_mday = 25; ts = mktime(timeinfo); // create the time u_int32_t ts_uint = (uint32_t)ts; ts_uint = BSON_UINT32_TO_BE (ts_uint); //BSON wants big endian time memcpy (&oid_pointer->bytes[0], &ts_uint, sizeof (ts_uint)); //overwrite the first 4 bytes with user selected time oid_thinks_time = bson_oid_get_time_t (&oid); printf ("The OID was fixed to time %u\n", (unsigned) oid_thinks_time);//prove it */ query = BCON_NEW ("ping", BCON_INT32 (1)); query = BCON_NEW("$and","[", "{", "a", BCON_INT32(1), "}", "{", "yyyyyyy", "{", "$ne", BCON_UTF8("xxxxxxxxxx"), "}", "}","]"); bson_t *doc; query = BCON_NEW ( "$and", "[", "{", "_id", BCON_INT32(1), "}", "{", "yyyyyy", "{", "$ne", BCON_UTF8 ("xxxxxxx"), "}", "}","]" ); query = BCON_NEW("$and","[", "{", "timestamp", "{", "$eq", BCON_INT64(1111111111112), "}", "}", "{", "timestamp", "{", "$gte", BCON_INT64(1111111111111), "}", "}", "{", "timestamp", "{", "$lt", BCON_INT64(1111111111110), "}", "}","]"); //prove it looks right size_t s; char * as_json; as_json = bson_as_json(query, &s); printf("%s\n", as_json); return 0; }
static void bulk3 (mongoc_collection_t *collection) { mongoc_bulk_operation_t *bulk; bson_error_t error; bson_t *query; bson_t *doc; bson_t reply; char *str; bool ret; /* false indicates unordered */ bulk = mongoc_collection_create_bulk_operation (collection, false, NULL); /* Add a document */ doc = BCON_NEW ("_id", BCON_INT32 (1)); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); /* remove {_id: 2} */ query = BCON_NEW ("_id", BCON_INT32 (2)); mongoc_bulk_operation_remove_one (bulk, query); bson_destroy (query); /* insert {_id: 3} */ doc = BCON_NEW ("_id", BCON_INT32 (3)); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); /* replace {_id:4} {'i': 1} */ query = BCON_NEW ("_id", BCON_INT32 (4)); doc = BCON_NEW ("i", BCON_INT32 (1)); mongoc_bulk_operation_replace_one (bulk, query, doc, false); bson_destroy (query); bson_destroy (doc); ret = mongoc_bulk_operation_execute (bulk, &reply, &error); str = bson_as_json (&reply, NULL); printf ("%s\n", str); bson_free (str); if (!ret) { printf ("Error: %s\n", error.message); } bson_destroy (&reply); mongoc_bulk_operation_destroy (bulk); }
static void test_inline_array (void) { int32_t a, b; bson_t *bcon = BCON_NEW ("foo", "[", BCON_INT32 (1), BCON_INT32 (2), "]"); assert ( BCON_EXTRACT (bcon, "foo", "[", BCONE_INT32 (a), BCONE_INT32 (b), "]")); assert (a == 1); assert (b == 2); bson_destroy (bcon); }
int main(void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; bson_error_t error; bson_t *options; client = mongoc_client_new ("mongodb://localhost:27017/admin"); database = mongoc_client_get_database (client, "databaseName"); options = BCON_NEW ( "validator", "{", "age", "{", "$lte", BCON_INT32 (34), "}", "}", "validationAction", BCON_UTF8 ("error"), "validationLevel", BCON_UTF8 ("moderate")); collection = mongoc_database_create_collection (database, "collectionName", options, &error); if (!collection) { fprintf(stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__); return 1; } fam_flags (collection); fam_bypass (collection); fam_update (collection); fam_fields (collection); fam_sort (collection); mongoc_collection_drop (collection, NULL); bson_destroy (options); mongoc_database_destroy (database); mongoc_collection_destroy (collection); mongoc_client_destroy (client); return 0; }
void test_suite (mongoc_database_t *db, mongoc_collection_t *collection) { bson_error_t error; bson_t query = BSON_INITIALIZER; int64_t count; bson_t *options, *pipeline; double start_time, end_time, delta_time; mongoc_cursor_t *cursor; count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, &query, 0, 0, NULL, &error); printf ("mongoc_collection_count count: %"PRId64"\n", count); options = BCON_NEW ("cursor", "{", "}", "allowDiskUse", BCON_BOOL (1)); pipeline = BCON_NEW ( "pipeline", "[", "{", "$match", "{", "}", "}", "{", "$project", "{", "text", BCON_INT32 (1), "}", "}", "]" ); start_time = dtimeofday (); cursor = mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, pipeline, options, NULL); count = mongoc_cursor_dump (cursor); end_time = dtimeofday (); delta_time = end_time - start_time + 0.0000001; printf ("mongoc_cursor_dump: secs: %.2f, count: %"PRId64", %.2f docs/sec\n", delta_time, count, count/delta_time); }
static void bulk1 (mongoc_collection_t *collection) { mongoc_bulk_operation_t *bulk; bson_error_t error; bson_t *doc; bson_t reply; char *str; bool ret; int i; bulk = mongoc_collection_create_bulk_operation (collection, true, NULL); for (i = 0; i < 10000; i++) { doc = BCON_NEW ("i", BCON_INT32 (i)); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); } ret = mongoc_bulk_operation_execute (bulk, &reply, &error); str = bson_as_json (&reply, NULL); printf ("%s\n", str); bson_free (str); if (!ret) { fprintf (stderr, "Error: %s\n", error.message); } bson_destroy (&reply); mongoc_bulk_operation_destroy (bulk); }
int main (int argc, char *argv[]) { bson_t *options; bson_error_t error; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_database_t *database; mongoc_init (); client = mongoc_client_new ("mongodb://localhost/"); database = mongoc_client_get_database (client, "testasdf"); /* Create schema validator */ options = BCON_NEW ("validator", "{", "number", "{", "$gte", BCON_INT32 (5), "}", "}"); collection = mongoc_database_create_collection (database, "collname", options, &error); if (collection) { bulk5_fail (collection); bulk5_success (collection); mongoc_collection_destroy (collection); } else { fprintf(stderr, "Couldn't create collection: '%s'\n", error.message); } bson_free (options); mongoc_database_destroy (database); mongoc_client_destroy (client); mongoc_cleanup (); return 0; }
static void print_pipeline (mongoc_collection_t *collection) { mongoc_cursor_t *cursor; bson_error_t error; const bson_t *doc; bson_t *pipeline; char *str; pipeline = BCON_NEW ("pipeline", "[", "{", "$group", "{", "_id", "$state", "total_pop", "{", "$sum", "$pop", "}", "}", "}", "{", "$match", "{", "total_pop", "{", "$gte", BCON_INT32 (10000000), "}", "}", "}", "]"); cursor = mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL); while (mongoc_cursor_next (cursor, &doc)) { str = bson_as_json (doc, NULL); printf ("%s\n", str); bson_free (str); } if (mongoc_cursor_error (cursor, &error)) { fprintf (stderr, "Cursor Failure: %s\n", error.message); } mongoc_cursor_destroy (cursor); bson_destroy (pipeline); }
static void test_skip (void) { bson_t *bcon = BCON_NEW ( "hello", "world", "foo", "{", "bar", BCON_INT32 (10), "}" ); assert (BCON_EXTRACT (bcon, "hello", BCONE_SKIP (BSON_TYPE_UTF8), "foo", "{", "bar", BCONE_SKIP (BSON_TYPE_INT32), "}" )); assert (!BCON_EXTRACT (bcon, "hello", BCONE_SKIP (BSON_TYPE_UTF8), "foo", "{", "bar", BCONE_SKIP (BSON_TYPE_INT64), "}" )); bson_destroy (bcon); }
static void test_inline_doc (void) { int32_t a, b; bson_t *bcon = BCON_NEW ("foo", "{", "b", BCON_INT32 (2), "a", BCON_INT32 (1), "}"); BSON_ASSERT (BCON_EXTRACT ( bcon, "foo", "{", "a", BCONE_INT32 (a), "b", BCONE_INT32 (b), "}")); BSON_ASSERT (a == 1); BSON_ASSERT (b == 2); bson_destroy (bcon); }
static void test_nested (void) { const char *utf8; int i32; bson_t *bcon = BCON_NEW ( "hello", "world", "foo", "{", "bar", BCON_INT32 (10), "}" ); assert (BCON_EXTRACT (bcon, "hello", BCONE_UTF8 (utf8), "foo", "{", "bar", BCONE_INT32 (i32), "}" )); assert (strcmp ("world", utf8) == 0); assert (i32 == 10); bson_destroy (bcon); }
static void test_mongoc_matcher_in_basic (void) { mongoc_matcher_t *matcher; bson_error_t error; bool r; bson_t *spec; bson_t doc = BSON_INITIALIZER; spec = BCON_NEW ("key", "{", "$in", "[", BCON_INT32 (1), BCON_INT32 (2), BCON_INT32 (3), "]", "}"); matcher = mongoc_matcher_new (spec, &error); r = mongoc_matcher_match (matcher, &doc); ASSERT (!r); bson_reinit (&doc); bson_append_int32 (&doc, "key", 3, 1); r = mongoc_matcher_match (matcher, &doc); ASSERT (r); bson_reinit (&doc); bson_append_int32 (&doc, "key", 3, 2); r = mongoc_matcher_match (matcher, &doc); ASSERT (r); bson_reinit (&doc); bson_append_int32 (&doc, "key", 3, 3); r = mongoc_matcher_match (matcher, &doc); ASSERT (r); bson_reinit (&doc); bson_append_int32 (&doc, "key", 3, 4); r = mongoc_matcher_match (matcher, &doc); ASSERT (!r); bson_destroy (&doc); bson_destroy (spec); mongoc_matcher_destroy (matcher); }
static void test_invalid_write_concern (void) { mongoc_bulk_write_flags_t write_flags = MONGOC_BULK_WRITE_FLAGS_INIT; mongoc_write_command_t command; mongoc_write_result_t result; mongoc_collection_t *collection; mongoc_client_t *client; mongoc_write_concern_t *write_concern; mongoc_server_stream_t *server_stream; bson_t *doc; bson_t reply = BSON_INITIALIZER; bson_error_t error; bool r; client = test_framework_client_new (); assert(client); collection = get_test_collection(client, "test_invalid_write_concern"); assert(collection); write_concern = mongoc_write_concern_new(); assert(write_concern); mongoc_write_concern_set_w(write_concern, 0); mongoc_write_concern_set_journal(write_concern, true); assert(!mongoc_write_concern_is_valid (write_concern)); doc = BCON_NEW("_id", BCON_INT32(0)); _mongoc_write_command_init_insert(&command, doc, write_flags, ++client->cluster.operation_id, true); _mongoc_write_result_init (&result); server_stream = mongoc_cluster_stream_for_writes (&client->cluster, &error); ASSERT_OR_PRINT (server_stream, error); _mongoc_write_command_execute (&command, client, server_stream, collection->db, collection->collection, write_concern, 0, &result); r = _mongoc_write_result_complete (&result, 2, collection->write_concern, &reply, &error); assert(!r); ASSERT_CMPINT(error.domain, ==, MONGOC_ERROR_COMMAND); ASSERT_CMPINT(error.code, ==, MONGOC_ERROR_COMMAND_INVALID_ARG); _mongoc_write_command_destroy (&command); _mongoc_write_result_destroy (&result); bson_destroy(doc); mongoc_server_stream_cleanup (server_stream); mongoc_collection_destroy(collection); mongoc_client_destroy(client); mongoc_write_concern_destroy(write_concern); }
static void test_inline_nested (void) { bson_t bcon, expected, foo, bar, third; bson_init (&bcon); bson_init (&expected); bson_init (&foo); bson_init (&bar); bson_init (&third); bson_append_utf8 (&third, "hello", -1, "world", -1); bson_append_int32 (&bar, "0", -1, 1); bson_append_int32 (&bar, "1", -1, 2); bson_append_document (&bar, "2", -1, &third); bson_append_array (&foo, "bar", -1, &bar); bson_append_document (&expected, "foo", -1, &foo); BCON_APPEND (&bcon, "foo", "{", "bar", "[", BCON_INT32 (1), BCON_INT32 (2), "{", "hello", "world", "}", "]", "}"); bson_eq_bson (&bcon, &expected); bson_destroy (&bcon); bson_destroy (&expected); bson_destroy (&foo); bson_destroy (&bar); bson_destroy (&third); }
static void bulk4 (mongoc_collection_t *collection) { mongoc_write_concern_t *wc; mongoc_bulk_operation_t *bulk; bson_error_t error; bson_t *doc; bson_t reply; char *str; bool ret; wc = mongoc_write_concern_new (); mongoc_write_concern_set_w (wc, 4); mongoc_write_concern_set_wtimeout (wc, 100); /* milliseconds */ bulk = mongoc_collection_create_bulk_operation (collection, true, wc); /* Two inserts */ doc = BCON_NEW ("_id", BCON_INT32 (10)); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); doc = BCON_NEW ("_id", BCON_INT32 (11)); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); ret = mongoc_bulk_operation_execute (bulk, &reply, &error); str = bson_as_json (&reply, NULL); printf ("%s\n", str); bson_free (str); if (!ret) { printf ("Error: %s\n", error.message); } bson_destroy (&reply); mongoc_bulk_operation_destroy (bulk); mongoc_write_concern_destroy (wc); }
static void test_int32 (void) { int32_t i32; bson_t *bcon = BCON_NEW ("foo", BCON_INT32 (10)); assert (BCON_EXTRACT (bcon, "foo", BCONE_INT32 (i32))); assert (i32 == 10); bson_destroy (bcon); }
static void bulk5_success (mongoc_collection_t *collection) { mongoc_bulk_operation_t *bulk; bson_error_t error; bson_t *doc; bson_t reply; char *str; bool ret; bulk = mongoc_collection_create_bulk_operation (collection, true, NULL); /* Allow this document to bypass document validation. * NOTE: When authentication is enabled, the authenticated user must have * either the "dbadmin" or "restore" roles to bypass document validation */ mongoc_bulk_operation_set_bypass_document_validation (bulk, true); /* Two inserts */ doc = BCON_NEW ("_id", BCON_INT32 (31)); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); doc = BCON_NEW ("_id", BCON_INT32 (32)); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); ret = mongoc_bulk_operation_execute (bulk, &reply, &error); str = bson_as_json (&reply, NULL); printf ("%s\n", str); bson_free (str); if (!ret) { printf ("Error: %s\n", error.message); } bson_destroy (&reply); mongoc_bulk_operation_destroy (bulk); }
static void test_invalid_write_concern (void) { mongoc_write_command_t command; mongoc_write_result_t result; mongoc_collection_t *collection; mongoc_client_t *client; mongoc_write_concern_t *write_concern; bson_t **docs; bson_t reply = BSON_INITIALIZER; bson_error_t error; bool r; client = test_framework_client_new (NULL); assert(client); collection = get_test_collection(client, "test_invalid_write_concern"); assert(collection); write_concern = mongoc_write_concern_new(); assert(write_concern); mongoc_write_concern_set_w(write_concern, 0); mongoc_write_concern_set_journal(write_concern, true); assert(!_mongoc_write_concern_is_valid(write_concern)); docs = bson_malloc(sizeof(bson_t*)); docs[0] = BCON_NEW("_id", BCON_INT32(0)); _mongoc_write_command_init_insert(&command, (const bson_t * const *)docs, 1, true, true); _mongoc_write_result_init (&result); _mongoc_write_command_execute (&command, client, 0, collection->db, collection->collection, write_concern, 0, &result); r = _mongoc_write_result_complete (&result, &reply, &error); assert(!r); assert(error.domain = MONGOC_ERROR_COMMAND); assert(error.code = MONGOC_ERROR_COMMAND_INVALID_ARG); _mongoc_write_command_destroy (&command); _mongoc_write_result_destroy (&result); bson_destroy(docs[0]); bson_free(docs); mongoc_collection_destroy(collection); mongoc_client_destroy(client); mongoc_write_concern_destroy(write_concern); }
static void test_mongoc_matcher_eq_int32 (void) { bson_t *spec; bson_t *doc; bson_error_t error; mongoc_matcher_t *matcher; bool r; spec = BCON_NEW ("hello", BCON_INT32 (1234)); matcher = mongoc_matcher_new (spec, &error); BSON_ASSERT (matcher); r = mongoc_matcher_match (matcher, spec); BSON_ASSERT (r); bson_destroy (spec); mongoc_matcher_destroy (matcher); spec = BCON_NEW ("hello", BCON_INT32 (1234)); doc = BCON_NEW ("hello", BCON_INT64 (1234)); matcher = mongoc_matcher_new (spec, &error); BSON_ASSERT (matcher); r = mongoc_matcher_match (matcher, doc); BSON_ASSERT (r); bson_destroy (spec); bson_destroy (doc); mongoc_matcher_destroy (matcher); spec = BCON_NEW ("hello", BCON_INT32 (1234)); doc = BCON_NEW ("hello", BCON_INT64 (4321)); matcher = mongoc_matcher_new (spec, &error); BSON_ASSERT (matcher); r = mongoc_matcher_match (matcher, doc); BSON_ASSERT (!r); bson_destroy (spec); bson_destroy (doc); mongoc_matcher_destroy (matcher); }
static void bulk5_fail (mongoc_collection_t *collection) { mongoc_bulk_operation_t *bulk; bson_error_t error; bson_t *doc; bson_t reply; char *str; bool ret; bulk = mongoc_collection_create_bulk_operation (collection, true, NULL); /* Two inserts */ doc = BCON_NEW ("_id", BCON_INT32 (31)); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); doc = BCON_NEW ("_id", BCON_INT32 (32)); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); /* The above documents do not comply to the schema validation rules * we created previously, so this will result in an error */ ret = mongoc_bulk_operation_execute (bulk, &reply, &error); str = bson_as_json (&reply, NULL); printf ("%s\n", str); bson_free (str); if (!ret) { printf ("Error: %s\n", error.message); } bson_destroy (&reply); mongoc_bulk_operation_destroy (bulk); }
bson_t * col_view_create (const char *stub, ...) { bson_t *bson; va_list ap; ele_t type; int keep_going = 1; bcon_append_ctx_t ctx; bcon_append_ctx_init (&ctx); va_start (ap, stub); bson = bson_new (); while (keep_going) { type = va_arg (ap, ele_t); switch (type) { case ELE_SORT: BCON_APPEND_CTX (bson, &ctx, "sort", "{"); bcon_append_ctx_va (bson, &ctx, &ap); BCON_APPEND_CTX (bson, &ctx, "}"); break; case ELE_LIMIT: { int i = va_arg (ap, int); BCON_APPEND_CTX (bson, &ctx, "limit", BCON_INT32 (i)); break; } case ELE_QUERY: BCON_APPEND_CTX (bson, &ctx, "query", "{"); bcon_append_ctx_va (bson, &ctx, &ap); BCON_APPEND_CTX (bson, &ctx, "}"); break; case ELE_END: keep_going = 0; break; default: BSON_ASSERT (0); break; } } va_end (ap); return bson; }
static void test_int32 (void) { bson_t bcon, expected; bson_init (&bcon); bson_init (&expected); bson_append_int32 (&expected, "foo", -1, 100); BCON_APPEND (&bcon, "foo", BCON_INT32 (100)); bson_eq_bson (&bcon, &expected); bson_destroy (&bcon); bson_destroy (&expected); }
int main (int argc, char *argv[]) { bson_t *bson; char *json; bson = COL_VIEW_CREATE ( SORT ("a", BCON_INT32 (1)), QUERY ("hello", "world"), LIMIT (10)); json = bson_as_canonical_extended_json (bson, NULL); printf ("%s\n", json); bson_free (json); bson_destroy (bson); return 0; }