Skip to content

Commit 15fd079

Browse files
committed
CDRIVER-837: add readConcern support for aggregate
1 parent ec5a55a commit 15fd079

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

src/mongoc/mongoc-collection.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,21 @@ mongoc_collection_aggregate (mongoc_collection_t *collection, /* IN */
386386
}
387387
}
388388

389+
if (collection->read_concern->level != NULL) {
390+
const bson_t *read_concern_bson;
391+
392+
if (selected_server->max_wire_version < WIRE_VERSION_READ_CONCERN) {
393+
bson_set_error (&cursor->error,
394+
MONGOC_ERROR_COMMAND,
395+
MONGOC_ERROR_PROTOCOL_BAD_WIRE_VERSION,
396+
"The selected server does not support readConcern");
397+
GOTO (done);
398+
}
399+
400+
read_concern_bson = _mongoc_read_concern_get_bson (collection->read_concern);
401+
BSON_APPEND_DOCUMENT (&command, "readConcern", read_concern_bson);
402+
}
403+
389404
if (use_cursor) {
390405
_mongoc_cursor_cursorid_init (cursor, &command);
391406
} else {

tests/test-mongoc-collection.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,6 +2738,113 @@ test_find_read_concern (void)
27382738
}
27392739

27402740

2741+
static void
2742+
test_aggregate_read_concern (void)
2743+
{
2744+
mock_server_t *server;
2745+
mongoc_client_t *client;
2746+
mongoc_collection_t *collection;
2747+
mongoc_read_concern_t *rc;
2748+
future_t *future;
2749+
request_t *request;
2750+
mongoc_cursor_t *cursor;
2751+
const bson_t *doc;
2752+
2753+
server = mock_server_with_autoismaster (WIRE_VERSION_READ_CONCERN);
2754+
mock_server_run (server);
2755+
client = mongoc_client_new_from_uri (mock_server_get_uri (server));
2756+
collection = mongoc_client_get_collection (client, "db", "collection");
2757+
2758+
/* No readConcern */
2759+
cursor = mongoc_collection_aggregate (
2760+
collection,
2761+
MONGOC_QUERY_NONE,
2762+
tmp_bson ("[{'a': 1}]"),
2763+
NULL,
2764+
NULL);
2765+
2766+
ASSERT (cursor);
2767+
future = future_cursor_next (cursor, &doc);
2768+
2769+
request = mock_server_receives_command (
2770+
server, "db", MONGOC_QUERY_SLAVE_OK,
2771+
"{"
2772+
" 'aggregate' : 'collection',"
2773+
" 'pipeline' : [{"
2774+
" 'a' : 1"
2775+
" }],"
2776+
" 'cursor' : { },"
2777+
" 'readConcern': { '$exists': false }"
2778+
"}"
2779+
);
2780+
2781+
mock_server_replies_simple (request,
2782+
"{'ok': 1,"
2783+
" 'cursor': {"
2784+
" 'id': 0,"
2785+
" 'ns': 'db.collection',"
2786+
" 'firstBatch': [{'_id': 123}]"
2787+
"}}");
2788+
2789+
ASSERT (future_get_bool (future));
2790+
ASSERT_MATCH (doc, "{'_id': 123}");
2791+
2792+
/* cursor is completed */
2793+
assert (!mongoc_cursor_next (cursor, &doc));
2794+
mongoc_cursor_destroy (cursor);
2795+
request_destroy (request);
2796+
future_destroy (future);
2797+
2798+
/* readConcern: majority */
2799+
rc = mongoc_read_concern_new ();
2800+
mongoc_read_concern_set_level (rc, MONGOC_READ_CONCERN_LEVEL_MAJORITY);
2801+
mongoc_collection_set_read_concern (collection, rc);
2802+
cursor = mongoc_collection_aggregate (
2803+
collection,
2804+
MONGOC_QUERY_NONE,
2805+
tmp_bson ("[{'a': 1}]"),
2806+
NULL,
2807+
NULL);
2808+
2809+
ASSERT (cursor);
2810+
future = future_cursor_next (cursor, &doc);
2811+
2812+
request = mock_server_receives_command (
2813+
server, "db", MONGOC_QUERY_SLAVE_OK,
2814+
"{"
2815+
" 'aggregate' : 'collection',"
2816+
" 'pipeline' : [{"
2817+
" 'a' : 1"
2818+
" }],"
2819+
" 'cursor' : { },"
2820+
" 'readConcern': { 'level': 'majority'}"
2821+
"}"
2822+
);
2823+
2824+
mock_server_replies_simple (request,
2825+
"{'ok': 1,"
2826+
" 'cursor': {"
2827+
" 'id': 0,"
2828+
" 'ns': 'db.collection',"
2829+
" 'firstBatch': [{'_id': 123}]"
2830+
"}}");
2831+
2832+
ASSERT (future_get_bool (future));
2833+
ASSERT_MATCH (doc, "{'_id': 123}");
2834+
2835+
/* cursor is completed */
2836+
assert (!mongoc_cursor_next (cursor, &doc));
2837+
mongoc_cursor_destroy (cursor);
2838+
request_destroy (request);
2839+
future_destroy (future);
2840+
2841+
2842+
mongoc_collection_destroy (collection);
2843+
mongoc_client_destroy (client);
2844+
mock_server_destroy (server);
2845+
}
2846+
2847+
27412848

27422849
void
27432850
test_collection_install (TestSuite *suite)
@@ -2787,6 +2894,7 @@ test_collection_install (TestSuite *suite)
27872894
TestSuite_Add (suite, "/Collection/drop", test_drop);
27882895
TestSuite_Add (suite, "/Collection/aggregate", test_aggregate);
27892896
TestSuite_Add (suite, "/Collection/aggregate/large", test_aggregate_large);
2897+
TestSuite_Add (suite, "/Collection/aggregate/read_concern", test_aggregate_read_concern);
27902898
TestSuite_AddFull (suite, "/Collection/aggregate/bypass_document_validation", test_aggregate_bypass, NULL, NULL, test_framework_skip_if_max_version_version_less_than_4);
27912899
TestSuite_Add (suite, "/Collection/validate", test_validate);
27922900
TestSuite_Add (suite, "/Collection/rename", test_rename);

0 commit comments

Comments
 (0)