static void topology_changed (const mongoc_apm_topology_changed_t *event) { context_t *ctx; bson_oid_t topology_id; bson_t prev_td; bson_t new_td; ctx = (context_t *) mongoc_apm_topology_changed_get_context (event); mongoc_apm_topology_changed_get_topology_id (event, &topology_id); ASSERT (bson_oid_equal (&topology_id, &ctx->topology_id)); td_to_bson (mongoc_apm_topology_changed_get_previous_description (event), &prev_td); td_to_bson (mongoc_apm_topology_changed_get_new_description (event), &new_td); context_append (ctx, BCON_NEW ("topology_description_changed_event", "{", "newDescription", BCON_DOCUMENT (&new_td), "previousDescription", BCON_DOCUMENT (&prev_td), "topologyId", BCON_UTF8 ("42"), "}")); bson_destroy (&prev_td); bson_destroy (&new_td); }
static void server_changed (const mongoc_apm_server_changed_t *event) { context_t *ctx; bson_oid_t topology_id; const char *host_and_port; bson_t prev_sd; bson_t new_sd; ctx = (context_t *) mongoc_apm_server_changed_get_context (event); /* check topology id is consistent */ mongoc_apm_server_changed_get_topology_id (event, &topology_id); ASSERT (bson_oid_equal (&topology_id, &ctx->topology_id)); host_and_port = mongoc_apm_server_changed_get_host (event)->host_and_port; sd_to_bson (mongoc_apm_server_changed_get_previous_description (event), &prev_sd); sd_to_bson (mongoc_apm_server_changed_get_new_description (event), &new_sd); context_append (ctx, BCON_NEW ("server_description_changed_event", "{", "topologyId", BCON_UTF8 ("42"), "address", BCON_UTF8 (host_and_port), "previousDescription", BCON_DOCUMENT (&prev_sd), "newDescription", BCON_DOCUMENT (&new_sd), "}")); bson_destroy (&prev_sd); bson_destroy (&new_sd); }
static void test_bson_document (void) { bson_t ochild; bson_t *child = BCON_NEW ("bar", "baz"); bson_t *bcon = BCON_NEW ("foo", BCON_DOCUMENT (child)); assert (BCON_EXTRACT (bcon, "foo", BCONE_DOCUMENT (ochild))); bson_eq_bson (&ochild, child); bson_destroy (&ochild); bson_destroy (child); bson_destroy (bcon); }
SEXP R_mongo_collection_create_index(SEXP ptr_col, SEXP ptr_bson) { mongoc_collection_t *col = r2col(ptr_col); bson_t *keys = r2bson(ptr_bson); const char * collection_name = mongoc_collection_get_name(col); char * index_name = mongoc_collection_keys_to_index_string (keys); bson_error_t err; //From: https://s3.amazonaws.com/mciuploads/mongo-c-driver/docs/latest/create-indexes.html bson_t * command = BCON_NEW ("createIndexes", BCON_UTF8 (collection_name), "indexes", "[", "{", "key", BCON_DOCUMENT (keys), "name", BCON_UTF8 (index_name), "}", "]"); if(!mongoc_collection_write_command_with_opts(col, command, NULL, NULL, &err)) stop(err.message); return Rf_ScalarLogical(1); }
static void test_bson_document (void) { bson_t bcon, expected, child; bson_init (&bcon); bson_init (&expected); bson_init (&child); bson_append_utf8 (&child, "bar", -1, "baz", -1); bson_append_document (&expected, "foo", -1, &child); BCON_APPEND (&bcon, "foo", BCON_DOCUMENT (&child)); bson_eq_bson (&bcon, &expected); bson_destroy (&bcon); bson_destroy (&expected); bson_destroy (&child); }
static void test_value_basic (void) { static const uint8_t raw[16] = { 0 }; const bson_value_t *value; bson_value_t copy; bson_iter_t iter; bson_oid_t oid; bson_t other = BSON_INITIALIZER; bson_t *doc; bson_t sub = BSON_INITIALIZER; bool r; int i; bson_oid_init (&oid, NULL); doc = BCON_NEW ("double", BCON_DOUBLE (123.4), "utf8", "this is my string", "document", BCON_DOCUMENT (&sub), "array", BCON_DOCUMENT (&sub), "binary", BCON_BIN (BSON_SUBTYPE_BINARY, raw, sizeof raw), "undefined", BCON_UNDEFINED, "oid", BCON_OID (&oid), "bool", BCON_BOOL (true), "datetime", BCON_DATE_TIME (12345678), "null", BCON_NULL, "regex", BCON_REGEX ("^hello", "i"), "dbpointer", BCON_DBPOINTER ("test.test", &oid), "code", BCON_CODE ("var a = function() {}"), "symbol", BCON_SYMBOL ("my_symbol"), "codewscope", BCON_CODEWSCOPE ("var a = 1;", &sub), "int32", BCON_INT32 (1234), "timestamp", BCON_TIMESTAMP (1234, 4567), "int64", BCON_INT32 (4321), "maxkey", BCON_MAXKEY, "minkey", BCON_MINKEY); r = bson_iter_init (&iter, doc); assert (r); for (i = 0; i < 20; i++) { r = bson_iter_next (&iter); assert (r); value = bson_iter_value (&iter); assert (value); bson_value_copy (value, ©); r = bson_append_value (&other, bson_iter_key (&iter), -1, ©); assert (r); bson_value_destroy (©); } r = bson_iter_next (&iter); assert (!r); bson_destroy (doc); bson_destroy (&other); }