Skip to content

Commit 723d5b6

Browse files
committed
CDRIVER-1137 use read pref for count and collStats
mongoc_collection_count and mongoc_collection_stats now default to the collection's read preference.
1 parent 032b66f commit 723d5b6

File tree

8 files changed

+285
-41
lines changed

8 files changed

+285
-41
lines changed

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ Server Selection Spec. These are the affected functions:
1717
* mongoc_collection_command
1818
* mongoc_collection_command_simple
1919

20+
On the other hand, the following command-specific helper functions now use the
21+
collection's read preference:
22+
23+
* mongoc_collection_count
24+
* mongoc_collection_stats
25+
2026
New functions to send maxTimeMS or any arbitrary options with findAndModify:
2127

2228
* mongoc_find_and_modify_opts_set_max_time_ms

build/generate-future-functions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@
139139
param("const_mongoc_read_prefs_ptr", "read_prefs"),
140140
param("bson_error_ptr", "error")]),
141141

142+
future_function("int64_t",
143+
"mongoc_collection_count_with_opts",
144+
[param("mongoc_collection_ptr", "collection"),
145+
param("mongoc_query_flags_t", "flags"),
146+
param("const_bson_ptr", "query"),
147+
param("int64_t", "skip"),
148+
param("int64_t", "limit"),
149+
param("const_bson_ptr", "opts"),
150+
param("const_mongoc_read_prefs_ptr", "read_prefs"),
151+
param("bson_error_ptr", "error")]),
152+
142153
future_function("bool",
143154
"mongoc_collection_find_and_modify_with_opts",
144155
[param("mongoc_collection_ptr", "collection"),
@@ -160,6 +171,13 @@
160171
param("bson_ptr", "reply"),
161172
param("bson_error_ptr", "error")]),
162173

174+
future_function("bool",
175+
"mongoc_collection_stats",
176+
[param("mongoc_collection_ptr", "collection"),
177+
param("const_bson_ptr", "options"),
178+
param("bson_ptr", "stats"),
179+
param("bson_error_ptr", "error")]),
180+
163181
future_function("bool",
164182
"mongoc_collection_insert",
165183
[param("mongoc_collection_ptr", "collection"),

doc/mongoc_collection_count.page

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ mongoc_collection_count (mongoc_collection_t *collection,
4343
<title>Description</title>
4444
<p>This function shall execute a count query on the underlying 'collection'. The bson 'query' is not validated, simply passed along as appropriate to the server. As such, compatibility and errors should be validated in the appropriate server documentation.</p>
4545
<p>For more information, see the <link href="http://docs.mongodb.org/manual/reference/operator/query/">query reference</link> at the MongoDB website.</p>
46-
<note style="tip"><p>The <code xref="mongoc_read_concern_t">mongoc_read_concern_t</code> specified on the <code xref="mongoc_collection_t">mongoc_collection_t</code> will be used, if any.</p></note>
46+
<p>The <code xref="mongoc_read_concern_t">mongoc_read_concern_t</code> specified on the <code xref="mongoc_collection_t">mongoc_collection_t</code> will be used, if any. If <code>read_prefs</code> is NULL, the collection's read preferences are used.</p>
4747
</section>
4848

4949
<section id="errors">

doc/mongoc_collection_stats.page

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ mongoc_collection_stats (mongoc_collection_t *collection,
2525
<table>
2626
<tr><td><p>collection</p></td><td><p>A <code xref="mongoc_collection_t">mongoc_collection_t</code>.</p></td></tr>
2727
<tr><td><p>options</p></td><td><p>An optional <code xref="bson:bson_t">bson_t</code> containing extra options to pass to the <code>collStats</code> command.</p></td></tr>
28-
<tr><td><p>reply</p></td><td><p>An optional location for a <code xref="bson:bson_t">bson_t</code> to store the result.</p></td></tr>
28+
<tr><td><p>reply</p></td><td><p>An uninitialized <code xref="bson:bson_t">bson_t</code> to store the result.</p></td></tr>
2929
<tr><td><p>error</p></td><td><p>An optional location for a <code xref="bson:bson_error_t">bson_error_t</code> or <code>NULL</code>.</p></td></tr>
3030
</table>
3131
</section>
3232

3333
<section id="description">
3434
<title>Description</title>
35-
<p>This function is a helper to retrieve statistics about the collection.</p>
35+
<p>Run the <code>collStats</code> command to retrieve statistics about the collection.</p>
36+
<p>The command uses the <code xref="mongoc_read_prefs_t">mongoc_read_prefs_t</code> set on <code>collection</code>.</p>
3637
</section>
3738

3839
<section id="errors">
@@ -42,8 +43,8 @@ mongoc_collection_stats (mongoc_collection_t *collection,
4243

4344
<section id="return">
4445
<title>Returns</title>
45-
<p>true if successful</p>
46-
<p><code>reply</code> is always initialized to simplfiy memory handling.</p>
46+
<p>True if successful.</p>
47+
<p><code>reply</code> is always initialized and must be freed with <code xref="bson:bson_destroy">bson_destroy</code>.</p>
4748
</section>
4849

4950
</page>

src/mongoc/mongoc-collection.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -630,14 +630,17 @@ mongoc_collection_count (mongoc_collection_t *collection, /* IN */
630630
const mongoc_read_prefs_t *read_prefs, /* IN */
631631
bson_error_t *error) /* OUT */
632632
{
633+
/* Server Selection Spec: "may-use-secondary" commands SHOULD take a read
634+
* preference argument and otherwise MUST use the default read preference
635+
* from client, database or collection configuration. */
633636
return mongoc_collection_count_with_opts (
634637
collection,
635638
flags,
636639
query,
637640
skip,
638641
limit,
639642
NULL,
640-
read_prefs,
643+
read_prefs ? read_prefs : collection->read_prefs,
641644
error);
642645
}
643646

@@ -654,25 +657,24 @@ mongoc_collection_count_with_opts (mongoc_collection_t *collection, /* IN
654657
{
655658
mongoc_server_stream_t *server_stream;
656659
mongoc_cluster_t *cluster;
660+
mongoc_apply_read_prefs_result_t read_prefs_result = READ_PREFS_RESULT_INIT;
657661
bson_iter_t iter;
658662
int64_t ret = -1;
659663
bool success;
660664
bson_t reply;
661-
bson_t cmd;
665+
bson_t cmd = BSON_INITIALIZER;
662666
bson_t q;
663667

664668
ENTRY;
665669

666-
667670
cluster = &collection->client->cluster;
668671
server_stream = mongoc_cluster_stream_for_writes (cluster, error);
669672
if (!server_stream) {
670-
RETURN (-1);
673+
GOTO (done);
671674
}
672675

673676
BSON_ASSERT (collection);
674677

675-
bson_init(&cmd);
676678
bson_append_utf8(&cmd, "count", 5, collection->collection,
677679
collection->collectionlen);
678680
if (query) {
@@ -696,9 +698,7 @@ mongoc_collection_count_with_opts (mongoc_collection_t *collection, /* IN
696698
MONGOC_ERROR_COMMAND,
697699
MONGOC_ERROR_PROTOCOL_BAD_WIRE_VERSION,
698700
"The selected server does not support readConcern");
699-
bson_destroy (&cmd);
700-
mongoc_server_stream_cleanup (server_stream);
701-
RETURN (-1);
701+
GOTO (done);
702702
}
703703

704704
read_concern_bson = _mongoc_read_concern_get_bson (collection->read_concern);
@@ -708,18 +708,23 @@ mongoc_collection_count_with_opts (mongoc_collection_t *collection, /* IN
708708
bson_concat(&cmd, opts);
709709
}
710710

711-
success = mongoc_cluster_run_command_monitored (cluster,
712-
server_stream,
713-
MONGOC_QUERY_SLAVE_OK,
714-
collection->db,
715-
&cmd, &reply, error);
711+
apply_read_preferences (read_prefs, server_stream,
712+
&cmd, flags, &read_prefs_result);
713+
714+
success = mongoc_cluster_run_command_monitored (
715+
cluster, server_stream, read_prefs_result.flags, collection->db,
716+
read_prefs_result.query_with_read_prefs, &reply, error);
716717

717718
if (success && bson_iter_init_find(&iter, &reply, "n")) {
718719
ret = bson_iter_as_int64(&iter);
719720
}
721+
720722
bson_destroy (&reply);
721-
bson_destroy (&cmd);
723+
724+
done:
725+
apply_read_prefs_result_cleanup (&read_prefs_result);
722726
mongoc_server_stream_cleanup (server_stream);
727+
bson_destroy (&cmd);
723728

724729
RETURN (ret);
725730
}
@@ -2053,7 +2058,12 @@ mongoc_collection_stats (mongoc_collection_t *collection,
20532058
bson_concat (&cmd, options);
20542059
}
20552060

2056-
ret = mongoc_collection_command_simple (collection, &cmd, NULL, stats, error);
2061+
/* Server Selection Spec: "may-use-secondary" commands SHOULD take a read
2062+
* preference argument and otherwise MUST use the default read preference
2063+
* from client, database or collection configuration. */
2064+
ret = mongoc_collection_command_simple (collection, &cmd,
2065+
collection->read_prefs,
2066+
stats, error);
20572067

20582068
bson_destroy (&cmd);
20592069

tests/mock_server/future-functions.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,32 @@ background_mongoc_collection_count (void *data)
135135
return NULL;
136136
}
137137

138+
static void *
139+
background_mongoc_collection_count_with_opts (void *data)
140+
{
141+
future_t *future = (future_t *) data;
142+
future_value_t return_value;
143+
144+
return_value.type = future_value_int64_t_type;
145+
146+
future_value_set_int64_t (
147+
&return_value,
148+
mongoc_collection_count_with_opts (
149+
future_value_get_mongoc_collection_ptr (future_get_param (future, 0)),
150+
future_value_get_mongoc_query_flags_t (future_get_param (future, 1)),
151+
future_value_get_const_bson_ptr (future_get_param (future, 2)),
152+
future_value_get_int64_t (future_get_param (future, 3)),
153+
future_value_get_int64_t (future_get_param (future, 4)),
154+
future_value_get_const_bson_ptr (future_get_param (future, 5)),
155+
future_value_get_const_mongoc_read_prefs_ptr (future_get_param (future, 6)),
156+
future_value_get_bson_error_ptr (future_get_param (future, 7))
157+
));
158+
159+
future_resolve (future, return_value);
160+
161+
return NULL;
162+
}
163+
138164
static void *
139165
background_mongoc_collection_find_and_modify_with_opts (void *data)
140166
{
@@ -186,6 +212,28 @@ background_mongoc_collection_find_and_modify (void *data)
186212
return NULL;
187213
}
188214

215+
static void *
216+
background_mongoc_collection_stats (void *data)
217+
{
218+
future_t *future = (future_t *) data;
219+
future_value_t return_value;
220+
221+
return_value.type = future_value_bool_type;
222+
223+
future_value_set_bool (
224+
&return_value,
225+
mongoc_collection_stats (
226+
future_value_get_mongoc_collection_ptr (future_get_param (future, 0)),
227+
future_value_get_const_bson_ptr (future_get_param (future, 1)),
228+
future_value_get_bson_ptr (future_get_param (future, 2)),
229+
future_value_get_bson_error_ptr (future_get_param (future, 3))
230+
));
231+
232+
future_resolve (future, return_value);
233+
234+
return NULL;
235+
}
236+
189237
static void *
190238
background_mongoc_collection_insert (void *data)
191239
{
@@ -627,6 +675,48 @@ future_collection_count (
627675
return future;
628676
}
629677

678+
future_t *
679+
future_collection_count_with_opts (
680+
mongoc_collection_ptr collection,
681+
mongoc_query_flags_t flags,
682+
const_bson_ptr query,
683+
int64_t skip,
684+
int64_t limit,
685+
const_bson_ptr opts,
686+
const_mongoc_read_prefs_ptr read_prefs,
687+
bson_error_ptr error)
688+
{
689+
future_t *future = future_new (future_value_int64_t_type,
690+
8);
691+
692+
future_value_set_mongoc_collection_ptr (
693+
future_get_param (future, 0), collection);
694+
695+
future_value_set_mongoc_query_flags_t (
696+
future_get_param (future, 1), flags);
697+
698+
future_value_set_const_bson_ptr (
699+
future_get_param (future, 2), query);
700+
701+
future_value_set_int64_t (
702+
future_get_param (future, 3), skip);
703+
704+
future_value_set_int64_t (
705+
future_get_param (future, 4), limit);
706+
707+
future_value_set_const_bson_ptr (
708+
future_get_param (future, 5), opts);
709+
710+
future_value_set_const_mongoc_read_prefs_ptr (
711+
future_get_param (future, 6), read_prefs);
712+
713+
future_value_set_bson_error_ptr (
714+
future_get_param (future, 7), error);
715+
716+
future_start (future, background_mongoc_collection_count_with_opts);
717+
return future;
718+
}
719+
630720
future_t *
631721
future_collection_find_and_modify_with_opts (
632722
mongoc_collection_ptr collection,
@@ -707,6 +797,32 @@ future_collection_find_and_modify (
707797
return future;
708798
}
709799

800+
future_t *
801+
future_collection_stats (
802+
mongoc_collection_ptr collection,
803+
const_bson_ptr options,
804+
bson_ptr stats,
805+
bson_error_ptr error)
806+
{
807+
future_t *future = future_new (future_value_bool_type,
808+
4);
809+
810+
future_value_set_mongoc_collection_ptr (
811+
future_get_param (future, 0), collection);
812+
813+
future_value_set_const_bson_ptr (
814+
future_get_param (future, 1), options);
815+
816+
future_value_set_bson_ptr (
817+
future_get_param (future, 2), stats);
818+
819+
future_value_set_bson_error_ptr (
820+
future_get_param (future, 3), error);
821+
822+
future_start (future, background_mongoc_collection_stats);
823+
return future;
824+
}
825+
710826
future_t *
711827
future_collection_insert (
712828
mongoc_collection_ptr collection,

tests/mock_server/future-functions.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ future_collection_count (
6767
);
6868

6969

70+
future_t *
71+
future_collection_count_with_opts (
72+
73+
mongoc_collection_ptr collection,
74+
mongoc_query_flags_t flags,
75+
const_bson_ptr query,
76+
int64_t skip,
77+
int64_t limit,
78+
const_bson_ptr opts,
79+
const_mongoc_read_prefs_ptr read_prefs,
80+
bson_error_ptr error
81+
);
82+
83+
7084
future_t *
7185
future_collection_find_and_modify_with_opts (
7286

@@ -94,6 +108,16 @@ future_collection_find_and_modify (
94108
);
95109

96110

111+
future_t *
112+
future_collection_stats (
113+
114+
mongoc_collection_ptr collection,
115+
const_bson_ptr options,
116+
bson_ptr stats,
117+
bson_error_ptr error
118+
);
119+
120+
97121
future_t *
98122
future_collection_insert (
99123

0 commit comments

Comments
 (0)