static void test_regex (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error = { 0 }; int64_t count; bson_t q = BSON_INITIALIZER; client = mongoc_client_new (gTestUri); ASSERT (client); collection = mongoc_client_get_collection (client, "test", "test"); ASSERT (collection); bson_append_regex (&q, "hello", -1, "^/wo", NULL); count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, &q, 0, 0, NULL, &error); ASSERT (count > 0); ASSERT (!error.domain); bson_destroy (&q); mongoc_collection_destroy (collection); mongoc_client_destroy (client); }
static void test_find_and_modify_opts (void) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; bson_error_t error; mongoc_find_and_modify_opts_t *opts; future_t *future; request_t *request; server = mock_server_with_autoismaster (0); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); opts = mongoc_find_and_modify_opts_new (); assert (mongoc_find_and_modify_opts_set_max_time_ms (opts, 42)); assert (mongoc_find_and_modify_opts_append (opts, tmp_bson ("{'foo': 1}"))); future = future_collection_find_and_modify_with_opts ( collection, tmp_bson ("{}"), opts, NULL, &error); request = mock_server_receives_command ( server, "db", MONGOC_QUERY_NONE, "{'findAndModify': 'collection', 'maxTimeMS': 42, 'foo': 1}"); mock_server_replies_ok_and_destroys (request); ASSERT_OR_PRINT (future_get_bool (future), error); future_destroy (future); mongoc_find_and_modify_opts_destroy (opts); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
bool mongoc_database_remove_all_users (mongoc_database_t *database, bson_error_t *error) { mongoc_collection_t *col; bson_error_t lerror; bson_t cmd; bool ret; ENTRY; bson_return_val_if_fail (database, false); bson_init (&cmd); BSON_APPEND_INT32 (&cmd, "dropAllUsersFromDatabase", 1); ret = mongoc_database_command_simple (database, &cmd, NULL, NULL, &lerror); bson_destroy (&cmd); if (!ret && (lerror.code == MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND)) { bson_init (&cmd); col = mongoc_client_get_collection (database->client, database->name, "system.users"); BSON_ASSERT (col); ret = mongoc_collection_remove (col, MONGOC_REMOVE_NONE, &cmd, NULL, error); bson_destroy (&cmd); mongoc_collection_destroy (col); } RETURN (ret); }
bool TMongoDriver::updateMulti(const QString &collection, const QVariantMap &criteria, const QVariantMap &object) { if (!isOpen()) { return false; } errorCode = 0; errorString.clear(); bson_error_t error; mongoc_collection_t *col = mongoc_client_get_collection(mongoClient, qPrintable(dbName), qPrintable(collection)); bool res = mongoc_collection_update(col, MONGOC_UPDATE_MULTI_UPDATE, (bson_t *)TBson::toBson(criteria).data(), (bson_t *)TBson::toBson(object).data(), nullptr, &error); setLastCommandStatus(mongoc_collection_get_last_error(col)); mongoc_collection_destroy(col); if (!res) { tSystemError("MongoDB UpdateMulti Error: %s", error.message); errorCode = error.code; errorString = QLatin1String(error.message); } return res; }
bool TMongoDriver::find(const QString &collection, const QVariantMap &criteria, const QVariantMap &orderBy, const QStringList &fields, int limit, int skip, int ) { if (!isOpen()) { return false; } errorCode = 0; errorString.clear(); mongoc_collection_t *col = mongoc_client_get_collection(mongoClient, qPrintable(dbName), qPrintable(collection)); mongoc_cursor_t *cursor = mongoc_collection_find(col, MONGOC_QUERY_NONE, skip, limit, 0, (bson_t *)TBson::toBson(criteria, orderBy).data(), (bson_t *)TBson::toBson(fields).data(), nullptr); /* Read Prefs, nullptr for default */ setLastCommandStatus(mongoc_collection_get_last_error(col)); mongoc_collection_destroy(col); mongoCursor->setCursor(cursor); if (cursor) { bson_error_t error; if (mongoc_cursor_error(cursor, &error)) { errorCode = error.code; errorString = QLatin1String(error.message); } } else { tSystemError("MongoDB Cursor Error"); } return (bool)cursor; }
int main (int argc,char* argv[]) { mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; const bson_t *doc; bson_t *query; char *str; mongoc_init (); client = mongoc_client_new ("mongodb://*****:*****@192.168.0.112:27017/"); collection = mongoc_client_get_collection (client, "clown", "testCollection"); query = bson_new (); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL); while (mongoc_cursor_next (cursor, &doc)) { str = bson_as_json (doc, NULL); printf ("%s\n", str); bson_free (str); } bson_destroy (query); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); return 0; }
static void test_count (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error; int64_t count; bson_t b; client = mongoc_client_new(gTestUri); ASSERT (client); collection = mongoc_client_get_collection(client, "test", "test"); ASSERT (collection); bson_init(&b); count = mongoc_collection_count(collection, MONGOC_QUERY_NONE, &b, 0, 0, NULL, &error); bson_destroy(&b); if (count == -1) { MONGOC_WARNING("%s\n", error.message); } ASSERT (count != -1); mongoc_collection_destroy(collection); mongoc_client_destroy(client); }
int main (int argc, char *argv[]) { mongoc_collection_t *collection; mongoc_client_t *client; if (argc != 2) { fprintf(stderr, "usage: %s MONGO_URI\n", argv[0]); return EXIT_FAILURE; } mongoc_init(); client = mongoc_client_new(argv[1]); if (!client) { fprintf(stderr, "Invalid URI: \"%s\"\n", argv[1]); return EXIT_FAILURE; } collection = mongoc_client_get_collection(client, "local", "oplog.rs"); tail_collection(collection); mongoc_collection_destroy(collection); mongoc_client_destroy(client); return 0; }
static void _test_read_concern_wire_version (bool allow, bool explicit) { mongoc_read_concern_t *rc; bson_t opts = BSON_INITIALIZER; mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; const bson_t *doc; future_t *future; request_t *request; bson_error_t error; rc = mongoc_read_concern_new (); mongoc_read_concern_set_level (rc, "foo"); server = mock_server_with_autoismaster ( allow ? WIRE_VERSION_READ_CONCERN : WIRE_VERSION_READ_CONCERN - 1); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); if (explicit) { mongoc_read_concern_append (rc, &opts); } else {
static void test_mongoc_client_authenticate_failure (void) { mongoc_collection_t *collection; mongoc_cursor_t *cursor; mongoc_client_t *client; const bson_t *doc; bson_error_t error; bool r; bson_t q; /* * Try authenticating with that user. */ bson_init(&q); client = mongoc_client_new(gTestUriWithBadPassword); collection = mongoc_client_get_collection(client, "test", "test"); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0, &q, NULL, NULL); r = mongoc_cursor_next(cursor, &doc); assert(!r); r = mongoc_cursor_error(cursor, &error); assert(r); assert(error.domain == MONGOC_ERROR_CLIENT); assert(error.code == MONGOC_ERROR_CLIENT_AUTHENTICATE); mongoc_cursor_destroy(cursor); mongoc_collection_destroy(collection); mongoc_client_destroy(client); }
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); }
int mongodb_module_init(struct state_conf *conf, UNUSED char **fields, UNUSED int fieldlens) { char *uri_str = NULL; buffer_fill = 0; const char *db; if (conf->output_args) { uri_str = conf->output_args; } mongoc_init(); mongoc_log_set_handler(mongodb_module_log, NULL); mongoc_uri_t *uri = mongoc_uri_new(uri_str); if (uri == NULL) { log_fatal("mongodb-module", "URI %s not valid!", uri_str); } client = mongoc_client_new_from_uri(uri); db = mongoc_uri_get_database(uri); collection = mongoc_client_get_collection(client, db ? db : strdup("zmap_output"), conf->output_filename ? conf->output_filename : strdup("zmap_output")); bulk = mongoc_collection_create_bulk_operation(collection,false,NULL); return EXIT_SUCCESS; }
SEXP R_mongo_collection_new(SEXP ptr_client, SEXP collection, SEXP db) { mongoc_client_t *client = r2client(ptr_client); mongoc_collection_t *col = mongoc_client_get_collection (client, translateCharUTF8(asChar(db)), translateCharUTF8(asChar(collection))); SEXP out = PROTECT(col2r(col)); setAttrib(out, install("client"), ptr_client); UNPROTECT(1); return out; }
static void _test_find_command (const mongoc_uri_t *uri, mock_server_t *server, const char *query_in, mongoc_read_prefs_t *read_prefs, mongoc_query_flags_t expected_find_cmd_query_flags, const char *expected_find_cmd) { mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; const bson_t *doc; bson_t b = BSON_INITIALIZER; future_t *future; request_t *request; client = mongoc_client_new_from_uri (uri); collection = mongoc_client_get_collection (client, "test", "test"); mongoc_collection_set_read_prefs (collection, read_prefs); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 1, 0, tmp_bson (query_in), NULL, read_prefs); future = future_cursor_next (cursor, &doc); request = mock_server_receives_command ( server, "test", expected_find_cmd_query_flags, expected_find_cmd); mock_server_replies (request, MONGOC_REPLY_NONE, /* flags */ 0, /* cursorId */ 0, /* startingFrom */ 1, /* numberReturned */ "{'ok': 1," " 'cursor': {" " 'id': 0," " 'ns': 'db.collection'," " 'firstBatch': [{'a': 1}]}}"); /* mongoc_cursor_next returned true */ assert (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&b); }
/* direct connection to a secondary requires read pref primaryPreferred to * avoid "not master" error from server */ static void _test_op_msg_direct_connection (bool is_mongos, test_op_msg_direct_fn_t fn, const char *expected_cmd) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_read_prefs_t *prefs = NULL; mongoc_cursor_t *cursor; const bson_t *doc; bson_t *cmd; future_t *future; request_t *request; const char *reply; int i; if (is_mongos) { server = mock_mongos_new (WIRE_VERSION_OP_MSG); } else { server = mock_server_with_autoismaster (WIRE_VERSION_OP_MSG); } mock_server_auto_endsessions (server); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); for (i = 0; i < 2; i++) { if (i == 1) { /* user-supplied read preference primary makes no difference */ prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY); } cursor = fn (collection, prefs); future = future_cursor_next (cursor, &doc); cmd = tmp_bson (expected_cmd); request = mock_server_receives_msg (server, 0, cmd); reply = "{'ok': 1," " 'cursor': {" " 'id': 0," " 'ns': 'db.collection'," " 'firstBatch': [{'a': 1}]}}"; mock_server_replies_simple (request, reply); BSON_ASSERT (future_get_bool (future)); future_destroy (future); request_destroy (request); mongoc_cursor_destroy (cursor); mongoc_read_prefs_destroy (prefs); /* null ok */ } mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
int main(int argc, char *argv[]) { mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; const bson_t *doc; bson_t *query; char *str; mongoc_init(); client = mongoc_client_new("mongodb://localhost:27017/"); collection = mongoc_client_get_collection(client, "stockopedia", "instruments"); query = bson_new(); bson_t *fields = bson_new(); BSON_APPEND_INT32(fields, "RIC", 1); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, query, fields, NULL); bson_iter_t iter; const bson_value_t *value; while (mongoc_cursor_next(cursor, &doc)) { str = bson_as_json(doc, NULL); printf("%s\n", str); if (bson_iter_init(&iter, doc)) { while (bson_iter_next(&iter)) { printf("Found a field named: %s\n", bson_iter_key(&iter)); value = bson_iter_value(&iter); if (value->value_type == BSON_TYPE_UTF8) { printf("It's a UTF8 : '%s'\n", value->value.v_utf8.str); } } } //printf("Found element key : '%s'\n", bson_iter_key(&iter)); // if (bson_iter_init(&iter, doc)) { // // } bson_free(str); } //Now fetch quotes for each RIC bson_destroy(query); mongoc_cursor_destroy(cursor); mongoc_collection_destroy(collection); mongoc_client_destroy(client); return 0; }
mongoc_collection_t * mongoc_database_get_collection (mongoc_database_t *database, const char *collection) { bson_return_val_if_fail (database, NULL); bson_return_val_if_fail (collection, NULL); return mongoc_client_get_collection (database->client, database->name, collection); }
mongoc_collection_t * mongoc_database_get_collection (mongoc_database_t *database, const char *collection) { BSON_ASSERT (database); BSON_ASSERT (collection); return mongoc_client_get_collection (database->client, database->name, collection); }
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[]) { mongoc_client_t *client; mongoc_apm_callbacks_t *callbacks; stats_t stats = { 0 }; mongoc_collection_t *collection; const char *uristr = "mongodb://127.0.0.1/?appname=cmd-monitoring-example"; const char *collection_name = "test"; bson_t doc; mongoc_init (); if (argc > 1) { uristr = argv [1]; } client = mongoc_client_new (uristr); if (!client) { fprintf (stderr, "Failed to parse URI.\n"); return EXIT_FAILURE; } mongoc_client_set_error_api (client, 2); callbacks = mongoc_apm_callbacks_new (); mongoc_apm_set_command_started_cb (callbacks, command_started); mongoc_apm_set_command_succeeded_cb (callbacks, command_succeeded ); mongoc_apm_set_command_failed_cb (callbacks, command_failed); mongoc_client_set_apm_callbacks (client, callbacks, (void *) &stats /* context pointer */); bson_init (&doc); BSON_APPEND_INT32 (&doc, "_id", 1); collection = mongoc_client_get_collection (client, "test", collection_name); mongoc_collection_drop (collection, NULL); mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, NULL); /* duplicate key error on the second insert */ mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, NULL); printf ("started: %d\nsucceeded: %d\nfailed: %d\n", stats.started, stats.succeeded, stats.failed); bson_destroy (&doc); mongoc_collection_destroy (collection); mongoc_apm_callbacks_destroy (callbacks); mongoc_client_destroy (client); mongoc_cleanup (); return EXIT_SUCCESS; }
char *be_mongo_getuser(void *handle, const char *username) { struct mongo_backend *conf = (struct mongo_backend *)handle; mongoc_collection_t *collection; mongoc_cursor_t *cursor; bson_error_t error; const bson_t *doc; const char *collection_name = "passport"; bson_t query; char *str = NULL; char *result = malloc(33); memset(result, 0, 33); bson_init (&query); bson_append_utf8 (&query, "username", -1, username, -1); collection = mongoc_client_get_collection (conf->client, "cas", collection_name); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, NULL, /* Fields, NULL for all. */ NULL); /* Read Prefs, NULL for default */ bson_iter_t iter; while (!mongoc_cursor_error (cursor, &error) && mongoc_cursor_more (cursor)) { if (mongoc_cursor_next (cursor, &doc)) { bson_iter_init(&iter, doc); bson_iter_find(&iter, "pwd"); //fprintf (stdout, "%s\n", bson_iter_utf8(&iter, NULL)); str = bson_as_json (doc, NULL); //fprintf (stdout, "%s\n", str); bson_free (str); char *src = (char *)bson_iter_utf8(&iter, NULL); memcpy(result, src, strlen(src)); } } if (mongoc_cursor_error (cursor, &error)) { fprintf (stderr, "Cursor Failure: %s\n", error.message); return result; } bson_destroy (&query); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); return result; }
static mongoc_collection_t *get_collection(Object obj) { mongoc_collection_t *collection; auto db = obj->o_realProp("db", ObjectData::RealPropUnchecked, "MongoCollection")->toObject(); auto client = db->o_realProp("client", ObjectData::RealPropUnchecked, "MongoDB")->toObject(); String db_name = db->o_realProp("db_name", ObjectData::RealPropUnchecked, "MongoDB")->toString(); String collection_name = obj->o_realProp("name", ObjectData::RealPropUnchecked, "MongoCollection")->toString(); collection = mongoc_client_get_collection(get_client(client)->get(), db_name.c_str(), collection_name.c_str()); return collection; }
int telemetry_mongo_insert(centernode_t *cn, cnaccess_t* ca, cdnmsg_t *req) { mongoc_collection_t *collection; bson_error_t error; bson_t *doc; char docbuf[256] = {0}; #pragma pack(1) struct Info_s { uint16_t addr; uint32_t status; } *pInfo; #pragma pack(4) int size = (ntohl(req->pdu.len)-23)/6; uint8_t mytime[7]; memcpy ((void*)mytime, ((char*)&req->pdu + ntohl(req->pdu.len)-7), 7); pInfo = (struct Info_s*)&req->pdu.body.telemetry.addr; int i = 0; for (i=0; i<size; i++ ) { sprintf (docbuf, "{\"type\":%d,\"nodeid\":%d,\"addr\":%d,\"data\":%d,\"time\":\"20%02d-%02d-%02d %02d:%02d:%02d.%03d\"}", ntohs(req->pdu.cmd), ntohs(req->pdu.body.login.nodeid), ntohs(pInfo->addr), ntohl(pInfo->status), mytime[0], mytime[1], mytime[2], mytime[3], mytime[4], mytime[5], mytime[6]); doc = bson_new_from_json((uint8_t*)docbuf, strlen(docbuf), &error); if (strcmp (error.message, "") != 0) { fprintf (stderr, "%s\n", error.message); return 0; } // collection = getValidColl (); char szaddr[128] = {0}; sprintf (szaddr, "%x_%x_%x", ntohs(req->pdu.body.login.nodeid),ntohs(pInfo->addr),ntohs(req->pdu.cmd)); pthread_mutex_lock (&cn->Mongo[0].MongoLock); // collection = cn->Mongo[0].MongoCollection; collection = mongoc_client_get_collection (cn->Mongo[0].MongoClient, "mydb", szaddr); if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) { fprintf (stderr, "%s\n", error.message); } mongoc_collection_destroy (collection); pthread_mutex_unlock (&cn->Mongo[0].MongoLock); bson_destroy (doc); pInfo++; } return 0; }
/** * mongoc_database_has_collection: * @database: (in): A #mongoc_database_t. * @name: (in): The name of the collection to check for. * @error: (out) (allow-none): A location for a #bson_error_t, or %NULL. * * Checks to see if a collection exists within the database on the MongoDB * server. * * This will return %false if their was an error communicating with the * server, or if the collection does not exist. * * If @error is provided, it will first be zeroed. Upon error, error.domain * will be set. * * Returns: %true if @name exists, otherwise %false. @error may be set. */ bool mongoc_database_has_collection (mongoc_database_t *database, const char *name, bson_error_t *error) { mongoc_collection_t *collection; mongoc_read_prefs_t *read_prefs; mongoc_cursor_t *cursor; const bson_t *doc; bson_iter_t iter; bool ret = false; const char *cur_name; bson_t q = BSON_INITIALIZER; char ns[140]; ENTRY; BSON_ASSERT (database); BSON_ASSERT (name); if (error) { memset (error, 0, sizeof *error); } bson_snprintf (ns, sizeof ns, "%s.%s", database->name, name); ns[sizeof ns - 1] = '\0'; read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY); collection = mongoc_client_get_collection (database->client, database->name, "system.namespaces"); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, &q, NULL, read_prefs); while (!mongoc_cursor_error (cursor, error) && mongoc_cursor_more (cursor)) { while (mongoc_cursor_next (cursor, &doc) && bson_iter_init_find (&iter, doc, "name") && BSON_ITER_HOLDS_UTF8 (&iter)) { cur_name = bson_iter_utf8(&iter, NULL); if (!strcmp(cur_name, ns)) { ret = true; GOTO(cleanup); } } } cleanup: mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_read_prefs_destroy (read_prefs); RETURN(ret); }
void mongo_node_init() { mongoc_client_t *client = mongo_getClient(); nodeCollection = mongoc_client_get_collection(client, "filesystem", "node"); // Create index to avoid duplicate nodes. /* const bson_t *indexKeys = BCON_NEW("name", BCON_INT32(1)); mongo_createIndexIfAbsent(nodeCollection, "name_1", indexKeys, 1); bson_destroy((bson_t *) indexKeys); */ }
/* test that we add readConcern only inside $query, not outside it too */ static void test_mongos_read_concern (void) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_read_prefs_t *prefs; mongoc_cursor_t *cursor; const bson_t *doc; future_t *future; request_t *request; server = mock_mongos_new (WIRE_VERSION_READ_CONCERN); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "test", "test"); prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY); cursor = mongoc_collection_find_with_opts ( collection, tmp_bson ("{'a': 1}"), tmp_bson ("{'readConcern': {'level': 'foo'}}"), prefs); future = future_cursor_next (cursor, &doc); request = mock_server_receives_command ( server, "test", MONGOC_QUERY_SLAVE_OK, "{" " '$query': {" " 'find': 'test', 'filter': {}, 'readConcern': {'level': 'foo'}" " }," " '$readPreference': {" " 'mode': 'secondary'" " }," " 'readConcern': {'$exists': false}" "}"); mock_server_replies_to_find ( request, MONGOC_QUERY_SLAVE_OK, 0, 1, "db.collection", "{}", true); /* mongoc_cursor_next returned true */ BSON_ASSERT (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_read_prefs_destroy (prefs); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
static void ha_config_add_shard (ha_node_t *node, ha_replica_set_t *replica_set) { mongoc_collection_t *collection; mongoc_client_t *client; bson_string_t *shardstr; bson_error_t error; bson_bool_t r; bson_t reply; bson_t cmd = BSON_INITIALIZER; char *uristr; uristr = bson_strdup_printf ("mongodb://127.0.0.1:%hu/", node->port); client = mongoc_client_new (uristr); collection = mongoc_client_get_collection (client, "admin", "fake"); shardstr = bson_string_new (NULL); bson_string_append_printf (shardstr, "%s/127.0.0.1:%hu", replica_set->name, replica_set->nodes->port); bson_append_utf8 (&cmd, "addShard", -1, shardstr->str, shardstr->len); bson_string_free (shardstr, TRUE); again: sleep (1); r = mongoc_collection_command_simple (collection, &cmd, NULL, &reply, &error); if (!r) { fprintf (stderr, "%s\n", error.message); goto again; } #if 1 { char *str; str = bson_as_json (&reply, NULL); printf ("%s\n", str); bson_free (str); } #endif bson_destroy (&reply); bson_destroy (&cmd); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_free (uristr); }
int database_find_blockchain_transaction(struct database* db, unsigned char* hash, size_t max_height, struct transaction** tx, size_t* height) { mongoc_collection_t* collection = mongoc_client_get_collection(db->client, database_name(db), "transactions"); // Build a query doc bson_t* query = bson_new(); // Set the hash BSON_APPEND_BINARY(query, "hash", BSON_SUBTYPE_BINARY, (uint8_t*)hash, 32); // Force the height to be valid (on the main chain) bson_t* height_doc = bson_new(); BSON_APPEND_DOCUMENT_BEGIN(query, "height", height_doc); BSON_APPEND_INT32(height_doc, "$lte", (int)max_height); BSON_APPEND_INT32(height_doc, "$gte", 0); bson_append_document_end(query, height_doc); // Perform find mongoc_cursor_t* cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL); bson_error_t error; if(cursor == NULL || mongoc_cursor_error(cursor, &error)) { printf("MongoDB error: %s\n", (cursor == NULL) ? "NULL cursor" : error.message); return -1; } bson_t const* doc; int found = 0; while(mongoc_cursor_next(cursor, &doc) != 0) { if(height != NULL) { bson_iter_t iter; if(!bson_iter_init_find(&iter, doc, "height") || !BSON_ITER_HOLDS_INT32(&iter)) { printf("MongoDB error: tx doesn't have height!\n"); return -1; } *height = (size_t)bson_iter_int32(&iter); } if(tx != NULL) { *tx = transaction_from_bson(doc); } found = 1; break; } mongoc_cursor_destroy(cursor); bson_destroy(height_doc); bson_destroy(query); mongoc_collection_destroy(collection); return found; }
int mongo_add_document(mongoc_client_t *client, char *database, char *collection_name, bson_t *document) { mongoc_collection_t *collection; bson_error_t error; collection = mongoc_client_get_collection(client, database, collection_name); if (!mongoc_collection_insert(collection, MONGOC_INSERT_NONE, document, NULL, &error)) { printf("%s\n", error.message); return -1; } mongoc_collection_destroy(collection); return 0; }
static void test_secondary (mongoc_client_t *client) { mongoc_collection_t *col; col = mongoc_client_get_collection(client, "test", "test"); while (!gShutdown) { query_collection(col); } mongoc_collection_destroy(col); }