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 bool get_upserted_id (const bson_t *update, bson_value_t *upserted_id) { bson_iter_t iter; bson_iter_t id_iter; /* Versions of MongoDB before 2.6 don't return the _id for an upsert if _id * is not an ObjectId, so find it in the update document's query "q" or * update "u". It must be in one or both: if it were in neither the _id * would be server-generated, therefore an ObjectId, therefore returned and * we wouldn't call this function. If _id is in both the update document * *and* the query spec the update document _id takes precedence. */ bson_iter_init (&iter, update); if (bson_iter_find_descendant (&iter, "u._id", &id_iter)) { bson_value_copy (bson_iter_value (&id_iter), upserted_id); return true; } else { bson_iter_init (&iter, update); if (bson_iter_find_descendant (&iter, "q._id", &id_iter)) { bson_value_copy (bson_iter_value (&id_iter), upserted_id); return true; } } /* server bug? */ return false; }
/* *----------------------------------------------------------------------- * * Run the JSON tests from the SDAM Monitoring spec. * *----------------------------------------------------------------------- */ static void test_sdam_monitoring_cb (bson_t *test) { mongoc_client_t *client; mongoc_topology_t *topology; bson_t phase; bson_t phases; bson_t outcome; bson_iter_t phase_iter; bson_iter_t phase_field_iter; bson_iter_t outcome_iter; bson_iter_t iter; bson_t events_expected; context_t context; /* parse out the uri and use it to create a client */ BSON_ASSERT (bson_iter_init_find (&iter, test, "uri")); client = mongoc_client_new (bson_iter_utf8 (&iter, NULL)); topology = client->topology; context_init (&context); client_set_topology_event_callbacks (client, &context); /* for each phase, parse and validate */ BSON_ASSERT (bson_iter_init_find (&iter, test, "phases")); bson_iter_bson (&iter, &phases); bson_iter_init (&phase_iter, &phases); while (bson_iter_next (&phase_iter)) { bson_iter_bson (&phase_iter, &phase); /* this test doesn't exercise this code path naturally, see below in * _test_topology_events for a non-hacky test of this event */ _mongoc_topology_description_monitor_opening (&topology->description); process_sdam_test_ismaster_responses (&phase, &client->topology->description); /* parse out "outcome" and validate */ BSON_ASSERT (bson_iter_init_find (&phase_field_iter, &phase, "outcome")); bson_iter_bson (&phase_field_iter, &outcome); bson_iter_init (&outcome_iter, &outcome); while (bson_iter_next (&outcome_iter)) { if (strcmp ("events", bson_iter_key (&outcome_iter)) == 0) { bson_iter_bson (&outcome_iter, &events_expected); check_json_apm_events (&context.events, &events_expected); } else { fprintf (stderr, "ERROR: unparsed test field %s\n", bson_iter_key (&outcome_iter)); BSON_ASSERT (false); } } } mongoc_client_destroy (client); context_destroy (&context); }
static int mongo_get_oauth_key(const u08bits *kid, oauth_key_data_raw *key) { mongoc_collection_t * collection = mongo_get_collection("oauth_key"); if (!collection) return -1; bson_t query; bson_init(&query); BSON_APPEND_UTF8(&query, "kid", (const char *)kid); bson_t fields; bson_init(&fields); BSON_APPEND_INT32(&fields, "lifetime", 1); BSON_APPEND_INT32(&fields, "timestamp", 1); BSON_APPEND_INT32(&fields, "as_rs_alg", 1); BSON_APPEND_INT32(&fields, "ikm_key", 1); mongoc_cursor_t * cursor; cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0, &query, &fields, NULL); int ret = -1; ns_bzero(key,sizeof(oauth_key_data_raw)); STRCPY(key->kid,kid); if (!cursor) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error querying MongoDB collection 'oauth_key'\n"); } else { const bson_t * item; uint32_t length; bson_iter_t iter; if (mongoc_cursor_next(cursor, &item)) { if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "as_rs_alg") && BSON_ITER_HOLDS_UTF8(&iter)) { STRCPY(key->as_rs_alg,bson_iter_utf8(&iter, &length)); } if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "ikm_key") && BSON_ITER_HOLDS_UTF8(&iter)) { STRCPY(key->ikm_key,bson_iter_utf8(&iter, &length)); } if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "timestamp") && BSON_ITER_HOLDS_INT64(&iter)) { key->timestamp = (u64bits)bson_iter_int64(&iter); } if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "lifetime") && BSON_ITER_HOLDS_INT32(&iter)) { key->lifetime = (u32bits)bson_iter_int32(&iter); } ret = 0; } mongoc_cursor_destroy(cursor); } mongoc_collection_destroy(collection); bson_destroy(&query); bson_destroy(&fields); return ret; }
static void test_bson_iter_find_case (void) { bson_t b; bson_iter_t iter; bson_init(&b); assert(bson_append_utf8(&b, "key", -1, "value", -1)); assert(bson_iter_init(&iter, &b)); assert(bson_iter_find_case(&iter, "KEY")); assert(bson_iter_init(&iter, &b)); assert(!bson_iter_find(&iter, "KEY")); bson_destroy(&b); }
static int _score_tags (const bson_t *read_tags, const bson_t *node_tags) { uint32_t len; bson_iter_t iter; const char *key; const char *str; int count; int i; bson_return_val_if_fail(read_tags, -1); bson_return_val_if_fail(node_tags, -1); count = bson_count_keys(read_tags); if (!bson_empty(read_tags) && bson_iter_init(&iter, read_tags)) { for (i = count; bson_iter_next(&iter); i--) { if (BSON_ITER_HOLDS_UTF8(&iter)) { key = bson_iter_key(&iter); str = bson_iter_utf8(&iter, &len); if (_contains_tag(node_tags, key, str, len)) { return count; } } } return -1; } return 0; }
/* *-------------------------------------------------------------------------- * * mongoc_server_description_has_rs_member -- * * Return true if this address is included in server's list of rs * members, false otherwise. * * Returns: * true, false * * Side effects: * None * *-------------------------------------------------------------------------- */ bool mongoc_server_description_has_rs_member(mongoc_server_description_t *server, const char *address) { bson_iter_t member_iter; const bson_t *rs_members[3]; int i; if (server->type != MONGOC_SERVER_UNKNOWN) { rs_members[0] = &server->hosts; rs_members[1] = &server->arbiters; rs_members[2] = &server->passives; for (i = 0; i < 3; i++) { bson_iter_init (&member_iter, rs_members[i]); while (bson_iter_next (&member_iter)) { if (strcasecmp (address, bson_iter_utf8 (&member_iter, NULL)) == 0) { return true; } } } } return false; }
/************************************************************************************************************************* F_SECUNDARIA: Obteniendo el VALOR dlong int IP, puerto o numBloque para una copiaX de un bloque X TESTEADO =) ****************************/ long int getValorDeUnaCopia_ArchivoDB (const bson_t * documento_X, int numBloque, int numDeCopia, char * claveRequerida) { bson_iter_t punteroDeBusqueda; bson_iter_t valorEncontrado; long int valorRequerido; // GENERO LA CLAVE DE BUSQUEDA //Ejemplo "Bloqlong int.IP" Bloques.numBloque.ArrayDeCopias.numDeCopia.claveRequerida (clavlong int IP o puerto o numDeBloque) char *claveGenerada = string_new(); string_append(&claveGenerada, "Bloques"); string_append(&claveGenerada, "."); string_append(&claveGenerada, string_itoa(numBloque)); string_append(&claveGenerada, ".copias."); string_append(&claveGenerada, string_itoa(numDeCopia-1)); string_append(&claveGenerada, "."); string_append(&claveGenerada, claveRequerida); if (documento_X != NULL && bson_iter_init (&punteroDeBusqueda, documento_X) && bson_iter_find_descendant (&punteroDeBusqueda, claveGenerada, &valorEncontrado) && BSON_ITER_HOLDS_INT32 (&valorEncontrado)) { valorRequerido = bson_iter_int32 (&valorEncontrado); } free(claveGenerada); return valorRequerido; }
//Devuelve un num positivo o Devuelve 0 si hay error int getCantidadDeCopiasDe1Bloque_ArchivoDB(const bson_t * documento_X, int numBloque) { bson_iter_t punteroDeBusqueda; bson_iter_t valorEncontrado; int CantidadDeCopias = 0; char *claveGenerada = string_new(); string_append(&claveGenerada, "Bloques"); string_append(&claveGenerada, "."); string_append(&claveGenerada, string_itoa(numBloque)); string_append(&claveGenerada, "."); string_append(&claveGenerada, "cantidadDeCopias"); if (documento_X != NULL && bson_iter_init (&punteroDeBusqueda, documento_X) && bson_iter_find_descendant (&punteroDeBusqueda, claveGenerada, &valorEncontrado) && BSON_ITER_HOLDS_INT32 (&valorEncontrado)) { CantidadDeCopias = bson_iter_int32 (&valorEncontrado); } free(claveGenerada); return CantidadDeCopias; }
static void test_bson_iter_as_bool (void) { bson_iter_t iter; bson_t b; bson_init(&b); bson_append_int32(&b, "int32[true]", -1, 1); bson_append_int32(&b, "int32[false]", -1, 0); bson_append_int64(&b, "int64[true]", -1, 1); bson_append_int64(&b, "int64[false]", -1, 0); bson_append_double(&b, "int64[true]", -1, 1.0); bson_append_double(&b, "int64[false]", -1, 0.0); bson_iter_init(&iter, &b); bson_iter_next(&iter); assert_cmpint(true, ==, bson_iter_as_bool(&iter)); bson_iter_next(&iter); assert_cmpint(false, ==, bson_iter_as_bool(&iter)); bson_iter_next(&iter); assert_cmpint(true, ==, bson_iter_as_bool(&iter)); bson_iter_next(&iter); assert_cmpint(false, ==, bson_iter_as_bool(&iter)); bson_iter_next(&iter); assert_cmpint(true, ==, bson_iter_as_bool(&iter)); bson_iter_next(&iter); assert_cmpint(false, ==, bson_iter_as_bool(&iter)); bson_destroy(&b); }
void mongoc_bulk_operation_update_one (mongoc_bulk_operation_t *bulk, const bson_t *selector, const bson_t *document, bool upsert) { mongoc_write_command_t command = { 0 }; bson_iter_t iter; bson_return_if_fail (bulk); bson_return_if_fail (selector); bson_return_if_fail (document); ENTRY; if (bson_iter_init (&iter, document)) { while (bson_iter_next (&iter)) { if (!strchr (bson_iter_key (&iter), '$')) { MONGOC_WARNING ("%s(): update_one only works with $ operators.", __FUNCTION__); EXIT; } } } _mongoc_write_command_init_update (&command, selector, document, upsert, false, bulk->ordered); _mongoc_array_append_val (&bulk->commands, command); EXIT; }
/* Check a correct CONNECT message */ static gboolean sim_parser_connect_test1 (void) { bson_t *bson_connect = bson_new (); bson_t child; uint8_t uuid[]={0x07,0x92,0xd6,0x72,0xf4,0xce,0x11,0xe4,0x9d,0xe2,0x00,0x0c,0x29,0xd9,0x46,0xde}; SimParser *parser = NULL; SimCommand *cmd = NULL; gboolean result = FALSE; bson_append_document_begin (bson_connect,"connect", -1, &child); BSON_APPEND_INT32 (&child, "id", 10); BSON_APPEND_INT32 (&child, "type", SIM_SESSION_TYPE_WEB); BSON_APPEND_UTF8 (&child, "version", "5.0.1"); if (bson_append_binary (&child, "sensor_id", -1, BSON_SUBTYPE_UUID, uuid, 16) == FALSE) return FALSE; bson_append_document_end (bson_connect, &child); /* Check */ bson_iter_t iter; bson_iter_init (&iter, bson_connect); do{ if ((parser = sim_parser_new()) == NULL) { result = FALSE; break; } if ((cmd = sim_parser_bson (parser, bson_get_data (bson_connect), bson_connect->len)) == NULL) { result = FALSE; break; } if (cmd->type != SIM_COMMAND_TYPE_CONNECT) { result = FALSE; break; } if (cmd->data.connect.sensor_ver->major != 5 || cmd->data.connect.sensor_ver->minor != 0 || cmd->data.connect.sensor_ver->micro != 1) { result = FALSE; break; } if (cmd->data.connect.sensor_id == NULL) { result = FALSE; break; } /* Check uuid */ SimUuid * uuidbin = sim_uuid_new_from_bin (uuid); gboolean test = sim_uuid_equal (uuidbin, cmd->data.connect.sensor_id); g_object_unref (uuidbin); if (!test) { result = FALSE; break; } result = TRUE; } while (0); bson_destroy (bson_connect); g_object_unref (parser); return result; }
static void mongoc_uri_bson_append_or_replace_key (bson_t *options, const char *option, const char *value) { bson_iter_t iter; bool found = false; if (bson_iter_init (&iter, options)) { bson_t tmp = BSON_INITIALIZER; while (bson_iter_next (&iter)) { const bson_value_t *bvalue; if (!strcasecmp(bson_iter_key (&iter), option)) { bson_append_utf8(&tmp, option, -1, value, -1); found = true; continue; } bvalue = bson_iter_value (&iter); BSON_APPEND_VALUE (&tmp, bson_iter_key (&iter), bvalue); } if (! found) { bson_append_utf8(&tmp, option, -1, value, -1); } bson_destroy (options); bson_copy_to (&tmp, options); bson_destroy (&tmp); } }
mongoc_matcher_t * mongoc_matcher_new (const bson_t *query, /* IN */ bson_error_t *error) /* OUT */ { mongoc_matcher_op_t *op; mongoc_matcher_t *matcher; bson_iter_t iter; BSON_ASSERT (query); matcher = bson_malloc0 (sizeof *matcher); bson_copy_to (query, &matcher->query); if (!bson_iter_init (&iter, &matcher->query)) { goto failure; } if (!(op = _mongoc_matcher_parse_logical (MONGOC_MATCHER_OPCODE_AND, &iter, true, error))) { goto failure; } matcher->optree = op; return matcher; failure: bson_destroy (&matcher->query); bson_free (matcher); return NULL; }
static void test_reader_tell (void) { bson_reader_t *reader; const bson_t *b; bson_uint32_t i; bson_iter_t iter; bson_bool_t eof; int fd; fd = open("tests/binary/stream.bson", O_RDONLY); assert(fd >= 0); reader = bson_reader_new_from_fd(fd, TRUE); for (i = 0; i < 1000; i++) { if (i) { assert_cmpint(5 * i, ==, bson_reader_tell(reader)); } else { assert_cmpint(0, ==, bson_reader_tell(reader)); } eof = FALSE; b = bson_reader_read(reader, &eof); assert(b); assert(bson_iter_init(&iter, b)); assert(!bson_iter_next(&iter)); }
static void test_reader_from_fd (void) { bson_reader_t *reader; const bson_t *b; bson_uint32_t i; bson_iter_t iter; bson_bool_t eof; int fd; fd = bson_open("tests/binary/stream.bson", BSON_O_RDONLY); assert(fd >= 0); reader = bson_reader_new_from_fd(fd, TRUE); for (i = 0; i < 1000; i++) { eof = FALSE; b = bson_reader_read(reader, &eof); assert(b); assert(bson_iter_init(&iter, b)); assert(!bson_iter_next(&iter)); } assert_cmpint(eof, ==, FALSE); b = bson_reader_read(reader, &eof); assert(!b); assert_cmpint(eof, ==, TRUE); bson_reader_destroy(reader); }
view::const_iterator view::find(stdx::string_view key) const { bson_t b; bson_iter_t iter; if (!bson_init_static(&b, _data, _length)) { return cend(); } if (!bson_iter_init(&iter, &b)) { return cend(); } if (key.empty()) { return cend(); } while (bson_iter_next(&iter)) { const char* ikey = bson_iter_key(&iter); if (0 == strncmp(key.data(), ikey, key.size())) { return const_iterator(element(iter.raw, iter.len, iter.off)); } } return cend(); }
int iter_bson(const bson_t *bson, ERL_NIF_TERM *out, decode_state *ds) { vec_term_t vec; bson_iter_t iter; int ret = 0; vec_init(&vec); ds->vec = &vec; if(!bson_iter_init(&iter, bson)) { goto finish; } if(bson_iter_visit_all(&iter, &decode_visitors, ds) || iter.err_off) { goto finish; } if(!make_document(ds->env, ds->vec, out, ds->return_maps)) { goto finish; } ret = 1; finish: vec_deinit(&vec); ds->vec = NULL; return ret; }
static void test_reader_tell (void) { bson_reader_t *reader; const bson_t *b; uint32_t i; bson_iter_t iter; bool eof; int fd; fd = bson_open(BINARY_DIR"/stream.bson", O_RDONLY); assert(-1 != fd); reader = bson_reader_new_from_handle ((void *)&fd, &test_reader_from_handle_read, &test_reader_from_handle_destroy); for (i = 0; i < 1000; i++) { if (i) { assert_cmpint(5 * i, ==, bson_reader_tell(reader)); } else { assert_cmpint(0, ==, bson_reader_tell(reader)); } eof = false; b = bson_reader_read(reader, &eof); assert(b); assert(bson_iter_init(&iter, b)); assert(!bson_iter_next(&iter)); }
dir_t* dir_getDirFromBSON(const bson_t *doc) { bson_iter_t iter; const bson_value_t *value; const char *key; dir_t *dir = dir_create(); if (bson_iter_init(&iter, doc)) { while (bson_iter_next(&iter)) { key = bson_iter_key(&iter); value = bson_iter_value(&iter); if (strcmp(key, "_id") == 0) { strcpy(dir->id, value->value.v_utf8.str); } else if (strcmp(key, "name") == 0) { dir->name = strdup(value->value.v_utf8.str); } else if (strcmp(key, "parentId") == 0) { strcpy(dir->parentId, value->value.v_utf8.str); } /* if (bson_iter_find(&iter, "_id")) strcpy(dir->id, bson_iter_utf8(&iter, NULL)); if (bson_iter_find(&iter, "name")) strcpy(dir->name, bson_iter_utf8(&iter, NULL)); if (bson_iter_find(&iter, "parentId")) strcpy(dir->parentId, bson_iter_utf8(&iter, NULL)); */ } } return dir; }
static void test_bson_iter_utf8 (void) { uint32_t len = 0; bson_iter_t iter; bson_t *b; char *s; b = bson_new(); assert(bson_append_utf8(b, "foo", -1, "bar", -1)); assert(bson_append_utf8(b, "bar", -1, "baz", -1)); assert(bson_iter_init(&iter, b)); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_UTF8(&iter)); assert(!strcmp(bson_iter_key(&iter), "foo")); assert(!strcmp(bson_iter_utf8(&iter, NULL), "bar")); s = bson_iter_dup_utf8(&iter, &len); assert_cmpstr("bar", s); assert_cmpint(len, ==, 3); bson_free(s); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_UTF8(&iter)); assert(!strcmp(bson_iter_key(&iter), "bar")); assert(!strcmp(bson_iter_utf8(&iter, NULL), "baz")); assert(!bson_iter_next(&iter)); bson_destroy(b); }
/* * If error is not set, set code from first document in array like * [{"code": 64, "errmsg": "duplicate"}, ...]. Format the error message * from all errors in array. */ static void _set_error_from_response (bson_t *bson_array, mongoc_error_domain_t domain, const char *error_type, bson_error_t *error /* OUT */) { bson_iter_t array_iter; bson_iter_t doc_iter; bson_string_t *compound_err; const char *errmsg = NULL; int32_t code = 0; uint32_t n_keys, i; compound_err = bson_string_new (NULL); n_keys = bson_count_keys (bson_array); if (n_keys > 1) { bson_string_append_printf ( compound_err, "Multiple %s errors: ", error_type); } if (!bson_empty0 (bson_array) && bson_iter_init (&array_iter, bson_array)) { /* get first code and all error messages */ i = 0; while (bson_iter_next (&array_iter)) { if (BSON_ITER_HOLDS_DOCUMENT (&array_iter) && bson_iter_recurse (&array_iter, &doc_iter)) { /* parse doc, which is like {"code": 64, "errmsg": "duplicate"} */ while (bson_iter_next (&doc_iter)) { /* use the first error code we find */ if (BSON_ITER_IS_KEY (&doc_iter, "code") && code == 0) { code = bson_iter_int32 (&doc_iter); } else if (BSON_ITER_IS_KEY (&doc_iter, "errmsg")) { errmsg = bson_iter_utf8 (&doc_iter, NULL); /* build message like 'Multiple write errors: "foo", "bar"' */ if (n_keys > 1) { bson_string_append_printf (compound_err, "\"%s\"", errmsg); if (i < n_keys - 1) { bson_string_append (compound_err, ", "); } } else { /* single error message */ bson_string_append (compound_err, errmsg); } } } i++; } } if (code && compound_err->len) { bson_set_error ( error, domain, (uint32_t) code, "%s", compound_err->str); } } bson_string_free (compound_err, true); }
static int mongo_get_admin_user(const u08bits *usname, u08bits *realm, password_t pwd) { mongoc_collection_t * collection = mongo_get_collection("admin_user"); if(!collection) return -1; realm[0]=0; pwd[0]=0; bson_t query; bson_init(&query); BSON_APPEND_UTF8(&query, "name", (const char *)usname); bson_t fields; bson_init(&fields); BSON_APPEND_INT32(&fields, "realm", 1); BSON_APPEND_INT32(&fields, "password", 1); mongoc_cursor_t * cursor; cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0, &query, &fields, NULL); int ret = -1; if (!cursor) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error querying MongoDB collection 'admin_user'\n"); } else { const bson_t * item; uint32_t length; bson_iter_t iter; if (mongoc_cursor_next(cursor, &item)) { if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "realm") && BSON_ITER_HOLDS_UTF8(&iter)) { strncpy((char*)realm,bson_iter_utf8(&iter, &length),STUN_MAX_REALM_SIZE); ret = 0; } if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "password") && BSON_ITER_HOLDS_UTF8(&iter)) { strncpy((char*)pwd,bson_iter_utf8(&iter, &length),STUN_MAX_PWD_SIZE); ret = 0; } } mongoc_cursor_destroy(cursor); } mongoc_collection_destroy(collection); bson_destroy(&query); bson_destroy(&fields); return ret; }
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; }
/* {{{ proto void BulkWrite::update(array|object $query, array|object $newObj[, array $updateOptions = array()]) Adds an update operation to bulk */ PHP_METHOD(BulkWrite, update) { php_phongo_bulkwrite_t *intern; zval *query; zval *newObj; zval *updateOptions = NULL; mongoc_update_flags_t flags = MONGOC_UPDATE_NONE; bson_t *bquery; bson_t *bupdate; SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(return_value) SUPPRESS_UNUSED_WARNING(return_value_used) intern = Z_BULKWRITE_OBJ_P(getThis()); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "AA|a!", &query, &newObj, &updateOptions) == FAILURE) { return; } bquery = bson_new(); bupdate = bson_new(); phongo_zval_to_bson(query, PHONGO_BSON_NONE, bquery, NULL TSRMLS_CC); phongo_zval_to_bson(newObj, PHONGO_BSON_NONE, bupdate, NULL TSRMLS_CC); if (updateOptions) { flags |= php_array_fetch_bool(updateOptions, "multi") ? MONGOC_UPDATE_MULTI_UPDATE : 0; flags |= php_array_fetch_bool(updateOptions, "upsert") ? MONGOC_UPDATE_UPSERT : 0; } if (flags & MONGOC_UPDATE_MULTI_UPDATE) { mongoc_bulk_operation_update(intern->bulk, bquery, bupdate, !!(flags & MONGOC_UPDATE_UPSERT)); } else { bson_iter_t iter; zend_bool replaced = 0; if (bson_iter_init(&iter, bupdate)) { while (bson_iter_next (&iter)) { if (!strchr (bson_iter_key (&iter), '$')) { mongoc_bulk_operation_replace_one(intern->bulk, bquery, bupdate, !!(flags & MONGOC_UPDATE_UPSERT)); replaced = 1; break; } } } if (!replaced) { mongoc_bulk_operation_update_one(intern->bulk, bquery, bupdate, !!(flags & MONGOC_UPDATE_UPSERT)); } } intern->num_ops++; bson_clear(&bquery); bson_clear(&bupdate); }
view::iterator view::begin() const { bson_t b; bson_iter_t iter; bson_init_static(&b, _data, _length); bson_iter_init(&iter, &b); bson_iter_next(&iter); return iterator(element{iter.raw, iter.len, iter.off}); }
static int _score_tags (const bson_t *read_tags, const bson_t *node_tags) { uint32_t len; bson_iter_t iter; bson_iter_t sub_iter; const char *key; const char *str; int count; bool node_matches_set; bson_return_val_if_fail(read_tags, -1); bson_return_val_if_fail(node_tags, -1); count = bson_count_keys(read_tags); /* Execute this block if read tags were provided, else bail and return 0 (all nodes equal) */ if (!bson_empty(read_tags) && bson_iter_init(&iter, read_tags)) { /* * Iterate over array of read tag sets provided (each element is a tag set) * Tag sets are provided in order of preference so return the count of the * first set that matches the node or -1 if no set matched the node. */ while (count && bson_iter_next(&iter)) { if (BSON_ITER_HOLDS_DOCUMENT(&iter) && bson_iter_recurse(&iter, &sub_iter)) { node_matches_set = true; /* Iterate over the key/value pairs (tags) in the current set */ while (bson_iter_next(&sub_iter) && BSON_ITER_HOLDS_UTF8(&sub_iter)) { key = bson_iter_key(&sub_iter); str = bson_iter_utf8(&sub_iter, &len); /* If any of the tags do not match, this node cannot satisfy this tag set. */ if (!_contains_tag(node_tags, key, str, len)) { node_matches_set = false; break; } } /* This set matched, return the count as the score */ if (node_matches_set) { return count; } /* Decrement the score and try to match the next set. */ count--; } } return -1; } return 0; }
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 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); }
bson_value_t* _aggregate_get_value_at_key(bson_t *doc, char *key) { bson_iter_t iter; bson_iter_t child_iter; if (!bson_iter_init (&iter, doc) || !bson_iter_find_descendant (&iter, key, &child_iter)) { return NULL; } bson_value_t *result = bson_malloc(sizeof(bson_value_t)); bson_value_copy(bson_iter_value(&child_iter), result); return result; }