void _aggregate_recurse_fill(bson_iter_t *iter, bson_t* new_doc, bson_t* existing_aggregate_doc, bson_t* merged_aggregate_doc, const char *key) { bson_iter_t child_iter; bson_t child_doc; while (bson_iter_next (iter)) { int new_key_length = strlen(bson_iter_key(iter)); if (strcmp("", key) != 0) { new_key_length += strlen(key) + 1; } char new_key[new_key_length]; if (strcmp("", key) == 0) { strcpy(new_key, bson_iter_key(iter)); } else { strcpy(new_key, key); strcat(new_key, "."); strcat(new_key, bson_iter_key(iter)); } if (strcmp("_id", new_key) == 0) { bson_value_t *existing_id = _aggregate_get_value_at_key(existing_aggregate_doc, "_id"); bson_append_value(merged_aggregate_doc, "_id", -1, existing_id); continue; } if (BSON_ITER_HOLDS_DOCUMENT (iter)) { const char *agg_key = NULL; const bson_value_t *agg_field = NULL; if (bson_iter_recurse (iter, &child_iter)) { if (bson_iter_next (&child_iter) && _aggregate_is_agg_operator(bson_iter_key(&child_iter))) { agg_key = bson_iter_key(&child_iter); agg_field = bson_iter_value(&child_iter); } if (agg_key && !bson_iter_next (&child_iter)) { bson_value_t *existing_value = _aggregate_get_value_at_key(existing_aggregate_doc, new_key); bson_value_t *new_doc_value = _aggregate_get_value_at_key(new_doc, (*agg_field).value.v_utf8.str + 1); bson_value_t * agg_result = _aggregate(existing_value, new_doc_value, agg_key); bson_append_value(merged_aggregate_doc, bson_iter_key(iter), -1, agg_result); continue; } } bson_append_document_begin (merged_aggregate_doc, bson_iter_key(iter), -1, &child_doc); if (bson_iter_recurse (iter, &child_iter)) { _aggregate_recurse_fill (&child_iter, new_doc, existing_aggregate_doc, &child_doc, new_key); } bson_append_document_end (merged_aggregate_doc, &child_doc); } else { bson_append_value(merged_aggregate_doc, bson_iter_key(iter), -1, bson_iter_value(iter)); } } }
bool _aggregate_have_same_id(bson_t *doc, bson_value_t *id_value) {; bson_value_t *doc_id_value = _aggregate_get_value_at_key(doc, "_id"); bson_t new_doc1; bson_init (&new_doc1); bson_append_value(&new_doc1, "_id", -1, doc_id_value); bson_t new_doc2; bson_init (&new_doc2); bson_append_value(&new_doc2, "_id", -1, id_value); bool are_equal = bson_equal(&new_doc1, &new_doc2); bson_value_destroy(doc_id_value); return are_equal; }
/* Construct the aggregate command in cmd: * { aggregate: collname, pipeline: [], cursor: { batchSize: x } } */ static void _make_command (mongoc_change_stream_t *stream, bson_t *command) { bson_iter_t iter; bson_t change_stream_stage; /* { $changeStream: <change_stream_doc> } */ bson_t change_stream_doc; bson_t pipeline; bson_t cursor_doc; bson_init (command); bson_append_utf8 (command, "aggregate", 9, stream->coll->collection, stream->coll->collectionlen); bson_append_array_begin (command, "pipeline", 8, &pipeline); /* Append the $changeStream stage */ bson_append_document_begin (&pipeline, "0", 1, &change_stream_stage); bson_append_document_begin ( &change_stream_stage, "$changeStream", 13, &change_stream_doc); bson_concat (&change_stream_doc, &stream->full_document); if (!bson_empty (&stream->resume_token)) { bson_concat (&change_stream_doc, &stream->resume_token); } bson_append_document_end (&change_stream_stage, &change_stream_doc); bson_append_document_end (&pipeline, &change_stream_stage); /* Append user pipeline if it exists */ if (bson_iter_init_find (&iter, &stream->pipeline_to_append, "pipeline") && BSON_ITER_HOLDS_ARRAY (&iter)) { bson_iter_t child_iter; uint32_t key_int = 1; char buf[16]; const char *key_str; BSON_ASSERT (bson_iter_recurse (&iter, &child_iter)); while (bson_iter_next (&child_iter)) { /* The user pipeline may consist of invalid stages or non-documents. * Append anyway, and rely on the server error. */ size_t keyLen = bson_uint32_to_string (key_int, &key_str, buf, sizeof (buf)); bson_append_value ( &pipeline, key_str, (int) keyLen, bson_iter_value (&child_iter)); ++key_int; } } bson_append_array_end (command, &pipeline); /* Add batch size if needed */ bson_append_document_begin (command, "cursor", 6, &cursor_doc); if (stream->batch_size > 0) { bson_append_int32 (&cursor_doc, "batchSize", 9, stream->batch_size); } bson_append_document_end (command, &cursor_doc); }
/** * _mongoc_gridfs_file_flush_page: * * Unconditionally flushes the file's current page to the database. * The page to flush is determined by page->n. * * Side Effects: * * On success, file->page is properly destroyed and set to NULL. * * Returns: * * True on success; false otherwise. */ static bool _mongoc_gridfs_file_flush_page (mongoc_gridfs_file_t *file) { bson_t *selector, *update; bool r; const uint8_t *buf; uint32_t len; ENTRY; BSON_ASSERT (file); BSON_ASSERT (file->page); buf = _mongoc_gridfs_file_page_get_data (file->page); len = _mongoc_gridfs_file_page_get_len (file->page); selector = bson_new (); bson_append_value (selector, "files_id", -1, &file->files_id); bson_append_int32 (selector, "n", -1, file->n); update = bson_sized_new (file->chunk_size + 100); bson_append_value (update, "files_id", -1, &file->files_id); bson_append_int32 (update, "n", -1, file->n); bson_append_binary (update, "data", -1, BSON_SUBTYPE_BINARY, buf, len); r = mongoc_collection_update (file->gridfs->chunks, MONGOC_UPDATE_UPSERT, selector, update, NULL, &file->error); bson_destroy (selector); bson_destroy (update); if (r) { _mongoc_gridfs_file_page_destroy (file->page); file->page = NULL; r = mongoc_gridfs_file_save (file); } RETURN (r); }
static void append_upserted (bson_t *doc, const bson_value_t *upserted_id) { bson_t array = BSON_INITIALIZER; bson_t child; /* append upserted: [{index: 0, _id: upserted_id}]*/ bson_append_document_begin (&array, "0", 1, &child); bson_append_int32 (&child, "index", 5, 0); bson_append_value (&child, "_id", 3, upserted_id); bson_append_document_end (&array, &child); bson_append_array (doc, "upserted", 8, &array); bson_destroy (&array); }
static void test_value_decimal128 (void) { const bson_value_t *value; bson_value_t copy; bson_iter_t iter; bson_decimal128_t dec; bson_t other = BSON_INITIALIZER; bson_t *doc; assert (bson_decimal128_from_string ("123.5", &dec)); doc = BCON_NEW ("decimal128", BCON_DECIMAL128 (&dec)); assert (bson_iter_init (&iter, doc) && bson_iter_next (&iter)); assert (value = bson_iter_value (&iter)); bson_value_copy (value, ©); assert (bson_append_value (&other, bson_iter_key (&iter), -1, ©)); bson_value_destroy (©); bson_destroy (doc); bson_destroy (&other); }
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); }
/** save a gridfs file */ bool mongoc_gridfs_file_save (mongoc_gridfs_file_t *file) { bson_t *selector, *update, child; const char *md5; const char *filename; const char *content_type; const bson_t *aliases; const bson_t *metadata; bool r; ENTRY; if (!file->is_dirty) { return 1; } if (file->page && _mongoc_gridfs_file_page_is_dirty (file->page)) { _mongoc_gridfs_file_flush_page (file); } md5 = mongoc_gridfs_file_get_md5 (file); filename = mongoc_gridfs_file_get_filename (file); content_type = mongoc_gridfs_file_get_content_type (file); aliases = mongoc_gridfs_file_get_aliases (file); metadata = mongoc_gridfs_file_get_metadata (file); selector = bson_new (); bson_append_value (selector, "_id", -1, &file->files_id); update = bson_new (); bson_append_document_begin (update, "$set", -1, &child); bson_append_int64 (&child, "length", -1, file->length); bson_append_int32 (&child, "chunkSize", -1, file->chunk_size); bson_append_date_time (&child, "uploadDate", -1, file->upload_date); if (md5) { bson_append_utf8 (&child, "md5", -1, md5, -1); } if (filename) { bson_append_utf8 (&child, "filename", -1, filename, -1); } if (content_type) { bson_append_utf8 (&child, "contentType", -1, content_type, -1); } if (aliases) { bson_append_array (&child, "aliases", -1, aliases); } if (metadata) { bson_append_document (&child, "metadata", -1, metadata); } bson_append_document_end (update, &child); r = mongoc_collection_update (file->gridfs->files, MONGOC_UPDATE_UPSERT, selector, update, NULL, &file->error); file->failed = !r; bson_destroy (selector); bson_destroy (update); file->is_dirty = 0; RETURN (r); }
/** refresh a gridfs file's underlying page * * This unconditionally fetches the current page, even if the current page * covers the same theoretical chunk. */ static bool _mongoc_gridfs_file_refresh_page (mongoc_gridfs_file_t *file) { bson_t *query, *fields, child, child2; const bson_t *chunk; const char *key; bson_iter_t iter; uint32_t n; const uint8_t *data; uint32_t len; ENTRY; BSON_ASSERT (file); n = (uint32_t)(file->pos / file->chunk_size); if (file->page) { _mongoc_gridfs_file_page_destroy (file->page); file->page = NULL; } /* if the file pointer is past the end of the current file (I.e. pointing to * a new chunk) and we're on a chunk boundary, we'll pass the page * constructor a new empty page */ if ((int64_t)file->pos >= file->length && !(file->pos % file->chunk_size)) { data = (uint8_t *)""; len = 0; } else { /* if we have a cursor, but the cursor doesn't have the chunk we're going * to need, destroy it (we'll grab a new one immediately there after) */ if (file->cursor && !(file->cursor_range[0] >= n && file->cursor_range[1] <= n)) { mongoc_cursor_destroy (file->cursor); file->cursor = NULL; } if (!file->cursor) { query = bson_new (); bson_append_document_begin(query, "$query", -1, &child); bson_append_value (&child, "files_id", -1, &file->files_id); bson_append_document_begin (&child, "n", -1, &child2); bson_append_int32 (&child2, "$gte", -1, (int32_t)(file->pos / file->chunk_size)); bson_append_document_end (&child, &child2); bson_append_document_end(query, &child); bson_append_document_begin(query, "$orderby", -1, &child); bson_append_int32 (&child, "n", -1, 1); bson_append_document_end(query, &child); fields = bson_new (); bson_append_int32 (fields, "n", -1, 1); bson_append_int32 (fields, "data", -1, 1); bson_append_int32 (fields, "_id", -1, 0); /* find all chunks greater than or equal to our current file pos */ file->cursor = mongoc_collection_find (file->gridfs->chunks, MONGOC_QUERY_NONE, 0, 0, 0, query, fields, NULL); file->cursor_range[0] = n; file->cursor_range[1] = (uint32_t)(file->length / file->chunk_size); bson_destroy (query); bson_destroy (fields); BSON_ASSERT (file->cursor); } /* we might have had a cursor before, then seeked ahead past a chunk. * iterate until we're on the right chunk */ while (file->cursor_range[0] <= n) { if (!mongoc_cursor_next (file->cursor, &chunk)) { if (file->cursor->failed) { memcpy (&(file->error), &(file->cursor->error), sizeof (bson_error_t)); file->failed = true; } RETURN (0); } file->cursor_range[0]++; } bson_iter_init (&iter, chunk); /* grab out what we need from the chunk */ while (bson_iter_next (&iter)) { key = bson_iter_key (&iter); if (strcmp (key, "n") == 0) { n = bson_iter_int32 (&iter); } else if (strcmp (key, "data") == 0) { bson_iter_binary (&iter, NULL, &len, &data); } else { RETURN (0); } } /* we're on the wrong chunk somehow... probably because our gridfs is * missing chunks. * * TODO: maybe we should make more noise here? */ if (!(n == file->pos / file->chunk_size)) { return 0; } } file->page = _mongoc_gridfs_file_page_new (data, len, file->chunk_size); /* seek in the page towards wherever we're supposed to be */ RETURN (_mongoc_gridfs_file_page_seek (file->page, file->pos % file->chunk_size)); }
void aggregate_group(aggregation_state_t *state, char *document_json) { bson_t *group_document; bson_error_t error; group_document = bson_new_from_json (document_json, -1, &error); if (!group_document) { //TODO: Handle error return; } // Step 1: Find the _specification of the _id from the group documnent bson_value_t *id_spec = _aggregate_get_value_at_key(group_document, "_id"); bson_t *new_docs[(*state).docs_len]; int new_docs_len = 0; // Step 2: Aggregate each document in the state size_t index; for(index = 0; index < (*state).docs_len; index++) { // Step 2a: Create a copy of the _id spec, with replacements from the source documnent bson_t orig_doc; orig_doc = (*state).docs[index]; bson_value_t *id_value = _aggregate_group_id_replace(id_spec, &orig_doc); // // Step 2b: Figure of if this _id already exists in the result set and create a new one // // if it doesn't bson_t *aggregate_doc = NULL; size_t aggregate_doc_index = 0; size_t new_docs_index; for(new_docs_index = 0; new_docs_index < new_docs_len; new_docs_index++) { bson_t *new_doc = new_docs[new_docs_index]; if (_aggregate_have_same_id(new_doc, id_value)) { aggregate_doc = new_doc; aggregate_doc_index = new_docs_index; break; } } if (aggregate_doc == NULL) { bson_t *doc = bson_new(); bson_append_value(doc, "_id", -1, id_value); aggregate_doc = doc; aggregate_doc_index = new_docs_len; new_docs[new_docs_len++] = aggregate_doc; } // // Step 2c: Build rest of the result document using the current result and // // group document bson_iter_t iter; if (!bson_iter_init (&iter, group_document)) { //TODO: Handle error continue; } bson_t *merged_aggregate_doc = bson_new(); _aggregate_recurse_fill(&iter, &orig_doc, aggregate_doc, merged_aggregate_doc , ""); new_docs[aggregate_doc_index] = merged_aggregate_doc; } size_t result_index = 0; size_t new_docs_index = 0; bson_t *result_docs = malloc(new_docs_len * sizeof(bson_t)); for(new_docs_index = 0; new_docs_index < new_docs_len; new_docs_index++) { bson_t *new_doc = new_docs[new_docs_index]; result_docs[result_index] = *new_doc; result_index++; } (*state).docs = result_docs; (*state).docs_len = result_index; }
/* construct the aggregate command in cmd. looks like one of the following: * for a collection change stream: * { aggregate: collname, pipeline: [], cursor: { batchSize: x } } * for a database change stream: * { aggregate: 1, pipeline: [], cursor: { batchSize: x } } * for a client change stream: * { aggregate: 1, pipeline: [{$changeStream: {allChangesForCluster: true}}], * cursor: { batchSize: x } } */ static void _make_command (mongoc_change_stream_t *stream, bson_t *command) { bson_iter_t iter; bson_t change_stream_stage; /* { $changeStream: <change_stream_doc> } */ bson_t change_stream_doc; bson_t pipeline; bson_t cursor_doc; bson_init (command); if (stream->change_stream_type == MONGOC_CHANGE_STREAM_COLLECTION) { bson_append_utf8 ( command, "aggregate", 9, stream->coll, (int) strlen (stream->coll)); } else { bson_append_int32 (command, "aggregate", 9, 1); } bson_append_array_begin (command, "pipeline", 8, &pipeline); /* append the $changeStream stage. */ bson_append_document_begin (&pipeline, "0", 1, &change_stream_stage); bson_append_document_begin ( &change_stream_stage, "$changeStream", 13, &change_stream_doc); bson_concat (&change_stream_doc, stream->full_document); if (!bson_empty (&stream->resume_token)) { bson_concat (&change_stream_doc, &stream->resume_token); } /* Change streams spec: "startAtOperationTime and resumeAfter are mutually * exclusive; if both startAtOperationTime and resumeAfter are set, the * server will return an error. Drivers MUST NOT throw a custom error, and * MUST defer to the server error." */ if (!_mongoc_timestamp_empty (&stream->operation_time)) { _mongoc_timestamp_append ( &stream->operation_time, &change_stream_doc, "startAtOperationTime"); } if (stream->change_stream_type == MONGOC_CHANGE_STREAM_CLIENT) { bson_append_bool (&change_stream_doc, "allChangesForCluster", 20, true); } bson_append_document_end (&change_stream_stage, &change_stream_doc); bson_append_document_end (&pipeline, &change_stream_stage); /* Append user pipeline if it exists */ if (bson_iter_init_find (&iter, &stream->pipeline_to_append, "pipeline") && BSON_ITER_HOLDS_ARRAY (&iter)) { bson_iter_t child_iter; uint32_t key_int = 1; char buf[16]; const char *key_str; BSON_ASSERT (bson_iter_recurse (&iter, &child_iter)); while (bson_iter_next (&child_iter)) { /* the user pipeline may consist of invalid stages or non-documents. * append anyway, and rely on the server error. */ size_t keyLen = bson_uint32_to_string (key_int, &key_str, buf, sizeof (buf)); bson_append_value ( &pipeline, key_str, (int) keyLen, bson_iter_value (&child_iter)); ++key_int; } } bson_append_array_end (command, &pipeline); /* Add batch size if needed */ bson_append_document_begin (command, "cursor", 6, &cursor_doc); if (stream->batch_size > 0) { bson_append_int32 (&cursor_doc, "batchSize", 9, stream->batch_size); } bson_append_document_end (command, &cursor_doc); }