Skip to content

Commit 578b378

Browse files
committed
CDRIVER-837: apply readConcern to mongoc_collection_find() calls
1 parent 665bb4c commit 578b378

File tree

6 files changed

+49
-12
lines changed

6 files changed

+49
-12
lines changed

src/mongoc/mongoc-client.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ mongoc_client_command (mongoc_client_t *client,
11751175
}
11761176

11771177
return _mongoc_cursor_new (client, db_name, flags, skip, limit, batch_size,
1178-
true, query, fields, read_prefs);
1178+
true, query, fields, read_prefs, NULL);
11791179
}
11801180

11811181

@@ -1445,7 +1445,7 @@ mongoc_client_find_databases (mongoc_client_t *client,
14451445
BSON_APPEND_INT32 (&cmd, "listDatabases", 1);
14461446

14471447
cursor = _mongoc_cursor_new (client, "admin", MONGOC_QUERY_SLAVE_OK,
1448-
0, 0, 0, true, NULL, NULL, NULL);
1448+
0, 0, 0, true, NULL, NULL, NULL, NULL);
14491449

14501450
_mongoc_cursor_array_init (cursor, &cmd, "databases");
14511451

src/mongoc/mongoc-collection.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ _mongoc_collection_cursor_new (mongoc_collection_t *collection,
100100
false, /* is_command */
101101
NULL, /* query */
102102
NULL, /* fields */
103-
NULL); /* read prefs */
103+
NULL, /* read prefs */
104+
NULL); /* read concern */
104105
}
105106

106107
static void
@@ -469,7 +470,8 @@ mongoc_collection_find (mongoc_collection_t *collection, /* IN */
469470
}
470471

471472
return _mongoc_cursor_new (collection->client, collection->ns, flags, skip,
472-
limit, batch_size, false, query, fields, read_prefs);
473+
limit, batch_size, false, query, fields, read_prefs,
474+
collection->read_concern);
473475
}
474476

475477

src/mongoc/mongoc-cursor-private.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct _mongoc_cursor_t
6565
bson_t query;
6666
bson_t fields;
6767

68+
mongoc_read_concern_t *read_concern;
6869
mongoc_read_prefs_t *read_prefs;
6970

7071
mongoc_query_flags_t flags;
@@ -100,7 +101,8 @@ mongoc_cursor_t * _mongoc_cursor_new (mongoc_client_t
100101
bool is_command,
101102
const bson_t *query,
102103
const bson_t *fields,
103-
const mongoc_read_prefs_t *read_prefs);
104+
const mongoc_read_prefs_t *read_prefs,
105+
const mongoc_read_concern_t *read_concern);
104106
mongoc_cursor_t *_mongoc_cursor_clone (const mongoc_cursor_t *cursor);
105107
void _mongoc_cursor_destroy (mongoc_cursor_t *cursor);
106108
bool _mongoc_read_from_buffer (mongoc_cursor_t *cursor,

src/mongoc/mongoc-cursor.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "mongoc-log.h"
2424
#include "mongoc-trace.h"
2525
#include "mongoc-cursor-cursorid-private.h"
26+
#include "mongoc-read-concern-private.h"
2627
#include "mongoc-util-private.h"
2728

2829

@@ -71,7 +72,8 @@ _mongoc_cursor_new (mongoc_client_t *client,
7172
bool is_command,
7273
const bson_t *query,
7374
const bson_t *fields,
74-
const mongoc_read_prefs_t *read_prefs)
75+
const mongoc_read_prefs_t *read_prefs,
76+
const mongoc_read_concern_t *read_concern)
7577
{
7678
mongoc_cursor_t *cursor;
7779
bson_iter_t iter;
@@ -83,6 +85,9 @@ _mongoc_cursor_new (mongoc_client_t *client,
8385
BSON_ASSERT (client);
8486
BSON_ASSERT (db_and_collection);
8587

88+
if (!read_concern) {
89+
read_concern = client->read_concern;
90+
}
8691
if (!read_prefs) {
8792
read_prefs = client->read_prefs;
8893
}
@@ -216,6 +221,10 @@ _mongoc_cursor_new (mongoc_client_t *client,
216221
cursor->read_prefs = mongoc_read_prefs_copy (read_prefs);
217222
}
218223

224+
if (read_concern) {
225+
cursor->read_concern = mongoc_read_concern_copy (read_concern);
226+
}
227+
219228
_mongoc_buffer_init(&cursor->buffer, NULL, 0, NULL, NULL);
220229

221230
finish:
@@ -344,7 +353,19 @@ _mongoc_cursor_initial_query (mongoc_cursor_t *cursor)
344353
if (_use_find_command (cursor, server_stream)) {
345354
b = _mongoc_cursor_find_command (cursor);
346355
} else {
347-
b = _mongoc_cursor_op_query (cursor, server_stream);
356+
/* When the user explicitly provides a readConcern -- but the server
357+
* doesn't support readConcern, we must error:
358+
* https://github.com/mongodb/specifications/blob/master/source/read-write-concern/read-write-concern.rst#errors-1
359+
*/
360+
if (cursor->read_concern->level != NULL
361+
&& server_stream->sd->max_wire_version < WIRE_VERSION_READ_CONCERN) {
362+
bson_set_error (&cursor->error,
363+
MONGOC_ERROR_COMMAND,
364+
MONGOC_ERROR_PROTOCOL_BAD_WIRE_VERSION,
365+
"The selected server does not support readConcern");
366+
} else {
367+
b = _mongoc_cursor_op_query (cursor, server_stream);
368+
}
348369
}
349370

350371
done:
@@ -719,6 +740,13 @@ _mongoc_cursor_prepare_find_command (mongoc_cursor_t *cursor,
719740
bson_append_int32 (command, "batchSize", 9, cursor->batch_size);
720741
}
721742

743+
if (cursor->read_concern->level != NULL) {
744+
const bson_t *read_concern_bson;
745+
746+
read_concern_bson = _mongoc_read_concern_get_bson (cursor->read_concern);
747+
BSON_APPEND_DOCUMENT (command, "readConcern", read_concern_bson);
748+
}
749+
722750
_mongoc_cursor_prepare_find_command_flags (cursor, command);
723751

724752
return true;
@@ -1163,6 +1191,11 @@ _mongoc_cursor_clone (const mongoc_cursor_t *cursor)
11631191
_clone->read_prefs = mongoc_read_prefs_copy (cursor->read_prefs);
11641192
}
11651193

1194+
if (cursor->read_concern) {
1195+
_clone->read_concern = mongoc_read_concern_copy (cursor->read_concern);
1196+
}
1197+
1198+
11661199
bson_copy_to (&cursor->query, &_clone->query);
11671200
bson_copy_to (&cursor->fields, &_clone->fields);
11681201

src/mongoc/mongoc-database.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ mongoc_database_find_collections (mongoc_database_t *database,
847847

848848
cursor = _mongoc_cursor_new (database->client, database->name,
849849
MONGOC_QUERY_SLAVE_OK, 0, 0, 0, true,
850-
NULL, NULL, NULL);
850+
NULL, NULL, NULL, NULL);
851851

852852
_mongoc_cursor_cursorid_init (cursor, &cmd);
853853

tests/test-mongoc-cursor.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ test_get_host (void)
4545

4646
client = test_framework_client_new ();
4747
cursor = _mongoc_cursor_new(client, "test.test", MONGOC_QUERY_NONE, 0, 1, 1,
48-
false, &q, NULL, NULL);
48+
false, &q, NULL, NULL, NULL);
4949
r = mongoc_cursor_next(cursor, &doc);
5050
if (!r && mongoc_cursor_error(cursor, &error)) {
5151
MONGOC_ERROR("%s", error.message);
@@ -94,7 +94,7 @@ test_clone (void)
9494
}
9595

9696
cursor = _mongoc_cursor_new(client, "test.test", MONGOC_QUERY_NONE, 0, 1, 1,
97-
false, &q, NULL, NULL);
97+
false, &q, NULL, NULL, NULL);
9898
ASSERT(cursor);
9999

100100
r = mongoc_cursor_next(cursor, &doc);
@@ -136,7 +136,7 @@ test_invalid_query (void)
136136
q = BCON_NEW ("foo", BCON_INT32 (1), "$orderby", "{", "}");
137137

138138
cursor = _mongoc_cursor_new (client, "test.test", MONGOC_QUERY_NONE, 0, 1, 1,
139-
false, q, NULL, NULL);
139+
false, q, NULL, NULL, NULL);
140140
assert (!mongoc_cursor_is_alive (cursor));
141141
r = mongoc_cursor_next (cursor, &doc);
142142
assert (!r);
@@ -192,7 +192,7 @@ test_kill_cursor_live (void)
192192

193193
cursor = _mongoc_cursor_new (client, collection->ns, MONGOC_QUERY_NONE,
194194
0, 0, 0, false /* is_command */,
195-
b, NULL, NULL);
195+
b, NULL, NULL, NULL);
196196

197197
cursor->rpc.reply.cursor_id = cursor_id;
198198
cursor->sent = true;

0 commit comments

Comments
 (0)