/* 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; }
static void _test_topology_events (bool pooled) { mongoc_client_t *client; mongoc_client_pool_t *pool = NULL; context_t context; bool r; bson_error_t error; bson_iter_t events_iter; bson_iter_t event_iter; uint32_t i; context_init (&context); if (pooled) { pool = test_framework_client_pool_new (); pool_set_topology_event_callbacks (pool, &context); client = mongoc_client_pool_pop (pool); } else { client = test_framework_client_new (); client_set_topology_event_callbacks (client, &context); } r = mongoc_client_command_simple (client, "admin", tmp_bson ("{'ping': 1}"), NULL, NULL, &error); ASSERT_OR_PRINT (r, error); if (pooled) { mongoc_client_pool_push (pool, client); mongoc_client_pool_destroy (pool); } else { mongoc_client_destroy (client); } /* first event is topology opening */ bson_iter_init (&events_iter, &context.events); bson_iter_next (&events_iter); ASSERT (bson_iter_recurse (&events_iter, &event_iter)); ASSERT (bson_iter_find (&event_iter, "topology_opening_event")); /* last event is topology closed */ for (i = 1; i < context.n_events; i++) { ASSERT (bson_iter_next (&events_iter)); } ASSERT (bson_iter_recurse (&events_iter, &event_iter)); ASSERT (bson_iter_find (&event_iter, "topology_closed_event")); /* no more events */ ASSERT (!bson_iter_next (&events_iter)); context_destroy (&context); }
void call_ismaster (bson_t *reply) { bson_t ismaster = BSON_INITIALIZER; mongoc_client_t *client; bool r; BSON_APPEND_INT32 (&ismaster, "isMaster", 1); client = test_framework_client_new (NULL); r = mongoc_client_command_simple (client, "admin", &ismaster, NULL, reply, NULL); assert (r); }
bool mongoc_database_command_simple (mongoc_database_t *database, const bson_t *command, const mongoc_read_prefs_t *read_prefs, bson_t *reply, bson_error_t *error) { BSON_ASSERT (database); BSON_ASSERT (command); if (!read_prefs) { read_prefs = database->read_prefs; } return mongoc_client_command_simple (database->client, database->name, command, read_prefs, reply, error); }
static void test_topology_events_disabled (void) { mongoc_client_t *client; context_t context; bool r; bson_error_t error; bson_iter_t events_iter; bson_iter_t event_iter; uint32_t i; context_init (&context); client = test_framework_client_new (); client_set_topology_event_callbacks (client, &context); r = mongoc_client_command_simple ( client, "admin", tmp_bson ("{'ping': 1}"), NULL, NULL, &error); ASSERT_OR_PRINT (r, error); /* disable callbacks before destroying so we don't see a topology closed * event */ mongoc_client_set_apm_callbacks (client, NULL, NULL); mongoc_client_destroy (client); /* first event is topology opening */ bson_iter_init (&events_iter, &context.events); bson_iter_next (&events_iter); ASSERT (bson_iter_recurse (&events_iter, &event_iter)); ASSERT (bson_iter_find (&event_iter, "topology_opening_event")); /* move forward to the last event */ for (i = 1; i < context.n_events; i++) { ASSERT (bson_iter_next (&events_iter)); } /* verify we didn't receive a topology closed event */ ASSERT (bson_iter_recurse (&events_iter, &event_iter)); ASSERT (!bson_iter_find (&event_iter, "topology_closed_event")); /* no more events */ ASSERT (!bson_iter_next (&events_iter)); context_destroy (&context); }
bool mongoc_client_get_server_status (mongoc_client_t *client, /* IN */ mongoc_read_prefs_t *read_prefs, /* IN */ bson_t *reply, /* OUT */ bson_error_t *error) /* OUT */ { bson_t cmd = BSON_INITIALIZER; bool ret = false; bson_return_val_if_fail (client, false); BSON_APPEND_INT32 (&cmd, "serverStatus", 1); ret = mongoc_client_command_simple (client, "admin", &cmd, read_prefs, reply, error); bson_destroy (&cmd); return ret; }
bool mongoc_database_command_simple (mongoc_database_t *database, const bson_t *command, const mongoc_read_prefs_t *read_prefs, bson_t *reply, bson_error_t *error) { BSON_ASSERT (database); BSON_ASSERT (command); /* Server Selection Spec: "The generic command method has a default read * preference of mode 'primary'. The generic command method MUST ignore any * default read preference from client, database or collection * configuration. The generic command method SHOULD allow an optional read * preference argument." */ return mongoc_client_command_simple (database->client, database->name, command, read_prefs, reply, error); }
static void * background_mongoc_client_command_simple (void *data) { future_t *future = (future_t *) data; future_value_t return_value; return_value.type = future_value_bool_type; future_value_set_bool ( &return_value, mongoc_client_command_simple ( future_value_get_mongoc_client_ptr (future_get_param (future, 0)), future_value_get_const_char_ptr (future_get_param (future, 1)), future_value_get_const_bson_ptr (future_get_param (future, 2)), future_value_get_const_mongoc_read_prefs_ptr (future_get_param (future, 3)), future_value_get_bson_ptr (future_get_param (future, 4)), future_value_get_bson_error_ptr (future_get_param (future, 5)) )); future_resolve (future, return_value); return NULL; }
void connect_mongo_db(const char *host, const char *name) { bson_t *command, reply; bson_error_t error; char *str; bool retval; mongoc_init(); client = mongoc_client_new(host); //连接失败 if(client == NULL){ printf("connect mongodb fail db_host: %s" , host); return; } command = BCON_NEW ("ping", BCON_INT32 (1)); retval = mongoc_client_command_simple (client, "admin", command, NULL, &reply, &error); if (!retval) { fprintf (stderr, "%s\n", error.message); bson_destroy (&reply); return; } str = bson_as_json (&reply, NULL); printf ("%s\n", str); bson_destroy (&reply); bson_destroy (command); bson_free (str); database = mongoc_client_get_database(client, name); mongoc_cleanup(); return; };
int main (int argc, char *argv[]) { const char *uri_str = "mongodb://localhost:27017"; mongoc_client_t *client; mongoc_database_t *database; mongoc_collection_t *collection; bson_t *command, reply, *insert; bson_error_t error; char *str; bool retval; /* * Required to initialize libmongoc's internals */ mongoc_init (); /* * Optionally get MongoDB URI from command line */ if (argc > 1) { uri_str = argv[1]; } /* * Create a new client instance */ client = mongoc_client_new (uri_str); /* * Register the application name so we can track it in the profile logs * on the server. This can also be done from the URI (see other examples). */ mongoc_client_set_appname (client, "connect-example"); /* * Get a handle on the database "db_name" and collection "coll_name" */ database = mongoc_client_get_database (client, "db_name"); collection = mongoc_client_get_collection (client, "db_name", "coll_name"); /* * Do work. This example pings the database, prints the result as JSON and * performs an insert */ command = BCON_NEW ("ping", BCON_INT32 (1)); retval = mongoc_client_command_simple ( client, "admin", command, NULL, &reply, &error); if (!retval) { fprintf (stderr, "%s\n", error.message); return EXIT_FAILURE; } str = bson_as_json (&reply, NULL); printf ("%s\n", str); insert = BCON_NEW ("hello", BCON_UTF8 ("world")); if (!mongoc_collection_insert ( collection, MONGOC_INSERT_NONE, insert, NULL, &error)) { fprintf (stderr, "%s\n", error.message); } bson_destroy (insert); bson_destroy (&reply); bson_destroy (command); bson_free (str); /* * Release our handles and clean up libmongoc */ mongoc_collection_destroy (collection); mongoc_database_destroy (database); mongoc_client_destroy (client); mongoc_cleanup (); return 0; }