-
Notifications
You must be signed in to change notification settings - Fork 446
/
mongoc-cursor.c
1697 lines (1390 loc) · 49.9 KB
/
mongoc-cursor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2013 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mongoc/mongoc-cursor.h"
#include "mongoc/mongoc-cursor-private.h"
#include "mongoc/mongoc-client-private.h"
#include "mongoc/mongoc-client-session-private.h"
#include "mongoc/mongoc-counters-private.h"
#include "mongoc/mongoc-error.h"
#include "mongoc/mongoc-log.h"
#include "mongoc/mongoc-trace-private.h"
#include "mongoc/mongoc-read-concern-private.h"
#include "mongoc/mongoc-util-private.h"
#include "mongoc/mongoc-write-concern-private.h"
#include "mongoc/mongoc-read-prefs-private.h"
#undef MONGOC_LOG_DOMAIN
#define MONGOC_LOG_DOMAIN "cursor"
#define CURSOR_FAILED(cursor_) ((cursor_)->error.domain != 0)
static bool
_translate_query_opt (const char *query_field,
const char **cmd_field,
int *len);
bool
_mongoc_cursor_set_opt_int64 (mongoc_cursor_t *cursor,
const char *option,
int64_t value)
{
bson_iter_t iter;
if (bson_iter_init_find (&iter, &cursor->opts, option)) {
if (!BSON_ITER_HOLDS_INT64 (&iter)) {
return false;
}
bson_iter_overwrite_int64 (&iter, value);
return true;
}
return BSON_APPEND_INT64 (&cursor->opts, option, value);
}
static int64_t
_mongoc_cursor_get_opt_int64 (const mongoc_cursor_t *cursor,
const char *option,
int64_t default_value)
{
bson_iter_t iter;
if (bson_iter_init_find (&iter, &cursor->opts, option)) {
return bson_iter_as_int64 (&iter);
}
return default_value;
}
static bool
_mongoc_cursor_set_opt_bool (mongoc_cursor_t *cursor,
const char *option,
bool value)
{
bson_iter_t iter;
if (bson_iter_init_find (&iter, &cursor->opts, option)) {
if (!BSON_ITER_HOLDS_BOOL (&iter)) {
return false;
}
bson_iter_overwrite_bool (&iter, value);
return true;
}
return BSON_APPEND_BOOL (&cursor->opts, option, value);
}
bool
_mongoc_cursor_get_opt_bool (const mongoc_cursor_t *cursor, const char *option)
{
bson_iter_t iter;
if (bson_iter_init_find (&iter, &cursor->opts, option)) {
return bson_iter_as_bool (&iter);
}
return false;
}
int32_t
_mongoc_n_return (mongoc_cursor_t *cursor)
{
int64_t limit;
int64_t batch_size;
int64_t n_return;
/* calculate numberToReturn according to:
* https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst#combining-limit-and-batch-size-for-the-wire-protocol
*/
limit = mongoc_cursor_get_limit (cursor);
batch_size = mongoc_cursor_get_batch_size (cursor);
if (limit < 0) {
n_return = limit;
} else if (limit == 0) {
n_return = batch_size;
} else if (batch_size == 0) {
n_return = limit;
} else if (limit < batch_size) {
n_return = limit;
} else {
n_return = batch_size;
}
/* if a specified limit exists, account for documents already returned. */
if (limit > 0 && cursor->count) {
int64_t remaining = limit - cursor->count;
/* remaining can be 0 if we have retrieved "limit" documents, but still
* have a cursor id: SERVER-21086. use nonzero batchSize to fetch final
* empty batch and trigger server to close cursor. */
if (remaining <= 0) {
return 1;
}
n_return = BSON_MIN (n_return, remaining);
}
/* check boundary conditions */
if (n_return < INT32_MIN) {
return INT32_MIN;
} else if (n_return > INT32_MAX) {
return INT32_MAX;
} else {
return (int32_t) n_return;
}
}
void
_mongoc_set_cursor_ns (mongoc_cursor_t *cursor, const char *ns, uint32_t nslen)
{
const char *dot;
bson_strncpy (cursor->ns, ns, sizeof cursor->ns);
cursor->nslen = BSON_MIN (nslen, sizeof cursor->ns);
dot = strstr (cursor->ns, ".");
if (dot) {
cursor->dblen = (uint32_t) (dot - cursor->ns);
} else {
/* a database name with no collection name */
cursor->dblen = cursor->nslen;
}
}
/* return first key beginning with $, or NULL. precondition: bson is valid. */
static const char *
_first_dollar_field (const bson_t *bson)
{
bson_iter_t iter;
const char *key;
BSON_ASSERT (bson_iter_init (&iter, bson));
while (bson_iter_next (&iter)) {
key = bson_iter_key (&iter);
if (key[0] == '$') {
return key;
}
}
return NULL;
}
/* if src is non-NULL, it is validated and copied to dst. returns false and
* sets the cursor error if validation fails. */
bool
_mongoc_cursor_check_and_copy_to (mongoc_cursor_t *cursor,
const char *err_prefix,
const bson_t *src,
bson_t *dst)
{
bson_error_t validate_err;
bson_init (dst);
if (src) {
if (!bson_validate_with_error (
src, BSON_VALIDATE_EMPTY_KEYS, &validate_err)) {
bson_set_error (&cursor->error,
MONGOC_ERROR_CURSOR,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
"Invalid %s: %s",
err_prefix,
validate_err.message);
return false;
}
bson_destroy (dst);
bson_copy_to (src, dst);
}
return true;
}
mongoc_cursor_t *
_mongoc_cursor_new_with_opts (mongoc_client_t *client,
const char *db_and_collection,
const bson_t *opts,
const mongoc_read_prefs_t *user_prefs,
const mongoc_read_prefs_t *default_prefs,
const mongoc_read_concern_t *read_concern)
{
mongoc_cursor_t *cursor;
mongoc_topology_description_type_t td_type;
uint32_t server_id;
mongoc_read_concern_t *read_concern_local = NULL;
bson_error_t validate_err;
const char *dollar_field;
bson_iter_t iter;
ENTRY;
BSON_ASSERT (client);
cursor = (mongoc_cursor_t *) bson_malloc0 (sizeof *cursor);
cursor->client = client;
cursor->state = UNPRIMED;
cursor->client_generation = client->generation;
bson_init (&cursor->opts);
bson_init (&cursor->error_doc);
if (opts) {
if (!bson_validate_with_error (
opts, BSON_VALIDATE_EMPTY_KEYS, &validate_err)) {
bson_set_error (&cursor->error,
MONGOC_ERROR_CURSOR,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
"Invalid opts: %s",
validate_err.message);
GOTO (finish);
}
dollar_field = _first_dollar_field (opts);
if (dollar_field) {
bson_set_error (&cursor->error,
MONGOC_ERROR_CURSOR,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
"Cannot use $-modifiers in opts: \"%s\"",
dollar_field);
GOTO (finish);
}
if (bson_iter_init_find (&iter, opts, "sessionId")) {
if (!_mongoc_client_session_from_iter (
client, &iter, &cursor->client_session, &cursor->error)) {
GOTO (finish);
}
cursor->explicit_session = true;
}
if (bson_iter_init_find (&iter, opts, "readConcern")) {
read_concern_local =
_mongoc_read_concern_new_from_iter (&iter, &cursor->error);
if (!read_concern_local) {
/* invalid read concern */
GOTO (finish);
}
read_concern = read_concern_local;
}
/* true if there's a valid serverId or no serverId, false on err */
if (!_mongoc_get_server_id_from_opts (opts,
MONGOC_ERROR_CURSOR,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
&server_id,
&cursor->error)) {
GOTO (finish);
}
if (server_id) {
(void) mongoc_cursor_set_hint (cursor, server_id);
}
bson_copy_to_excluding_noinit (opts,
&cursor->opts,
"serverId",
"sessionId",
"bypassDocumentValidation",
NULL);
/* only include bypassDocumentValidation if it's true */
if (bson_iter_init_find (&iter, opts, "bypassDocumentValidation") &&
bson_iter_as_bool (&iter)) {
BSON_APPEND_BOOL (&cursor->opts, "bypassDocumentValidation", true);
}
}
if (_mongoc_client_session_in_txn (cursor->client_session)) {
if (!IS_PREF_PRIMARY (user_prefs)) {
bson_set_error (&cursor->error,
MONGOC_ERROR_CURSOR,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
"Read preference in a transaction must be primary");
GOTO (finish);
}
cursor->read_prefs =
mongoc_read_prefs_copy (cursor->client_session->txn.opts.read_prefs);
if (bson_has_field (opts, "readConcern")) {
bson_set_error (&cursor->error,
MONGOC_ERROR_CURSOR,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
"Cannot set read concern after starting transaction");
GOTO (finish);
}
} else if (user_prefs) {
cursor->read_prefs = mongoc_read_prefs_copy (user_prefs);
} else if (default_prefs) {
cursor->read_prefs = mongoc_read_prefs_copy (default_prefs);
} else {
cursor->read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY);
}
cursor->read_concern = read_concern ? mongoc_read_concern_copy (read_concern)
: mongoc_read_concern_new ();
if (db_and_collection) {
_mongoc_set_cursor_ns (
cursor, db_and_collection, (uint32_t) strlen (db_and_collection));
}
if (_mongoc_cursor_get_opt_bool (cursor, MONGOC_CURSOR_EXHAUST)) {
if (_mongoc_cursor_get_opt_int64 (cursor, MONGOC_CURSOR_LIMIT, 0)) {
bson_set_error (&cursor->error,
MONGOC_ERROR_CURSOR,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
"Cannot specify both 'exhaust' and 'limit'.");
GOTO (finish);
}
td_type = _mongoc_topology_get_type (client->topology);
if (td_type == MONGOC_TOPOLOGY_SHARDED) {
bson_set_error (&cursor->error,
MONGOC_ERROR_CURSOR,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
"Cannot use exhaust cursor with sharded cluster.");
GOTO (finish);
}
}
(void) _mongoc_read_prefs_validate (cursor->read_prefs, &cursor->error);
finish:
mongoc_read_concern_destroy (read_concern_local);
mongoc_counter_cursors_active_inc ();
RETURN (cursor);
}
static bool
_translate_query_opt (const char *query_field, const char **cmd_field, int *len)
{
if (query_field[0] != '$') {
*cmd_field = query_field;
*len = -1;
return true;
}
/* strip the leading '$' */
query_field++;
if (!strcmp (MONGOC_CURSOR_ORDERBY, query_field)) {
*cmd_field = MONGOC_CURSOR_SORT;
*len = MONGOC_CURSOR_SORT_LEN;
} else if (!strcmp (MONGOC_CURSOR_SHOW_DISK_LOC,
query_field)) { /* <= MongoDb 3.0 */
*cmd_field = MONGOC_CURSOR_SHOW_RECORD_ID;
*len = MONGOC_CURSOR_SHOW_RECORD_ID_LEN;
} else if (!strcmp (MONGOC_CURSOR_HINT, query_field)) {
*cmd_field = MONGOC_CURSOR_HINT;
*len = MONGOC_CURSOR_HINT_LEN;
} else if (!strcmp (MONGOC_CURSOR_COMMENT, query_field)) {
*cmd_field = MONGOC_CURSOR_COMMENT;
*len = MONGOC_CURSOR_COMMENT_LEN;
} else if (!strcmp (MONGOC_CURSOR_MAX_SCAN, query_field)) {
*cmd_field = MONGOC_CURSOR_MAX_SCAN;
*len = MONGOC_CURSOR_MAX_SCAN_LEN;
} else if (!strcmp (MONGOC_CURSOR_MAX_TIME_MS, query_field)) {
*cmd_field = MONGOC_CURSOR_MAX_TIME_MS;
*len = MONGOC_CURSOR_MAX_TIME_MS_LEN;
} else if (!strcmp (MONGOC_CURSOR_MAX, query_field)) {
*cmd_field = MONGOC_CURSOR_MAX;
*len = MONGOC_CURSOR_MAX_LEN;
} else if (!strcmp (MONGOC_CURSOR_MIN, query_field)) {
*cmd_field = MONGOC_CURSOR_MIN;
*len = MONGOC_CURSOR_MIN_LEN;
} else if (!strcmp (MONGOC_CURSOR_RETURN_KEY, query_field)) {
*cmd_field = MONGOC_CURSOR_RETURN_KEY;
*len = MONGOC_CURSOR_RETURN_KEY_LEN;
} else if (!strcmp (MONGOC_CURSOR_SNAPSHOT, query_field)) {
*cmd_field = MONGOC_CURSOR_SNAPSHOT;
*len = MONGOC_CURSOR_SNAPSHOT_LEN;
} else {
/* not a special command field, must be a query operator like $or */
return false;
}
return true;
}
/* set up a new opt bson from older ways of specifying options.
* slave_ok may be NULL.
* error may be NULL.
*/
void
_mongoc_cursor_flags_to_opts (mongoc_query_flags_t qflags,
bson_t *opts, /* IN/OUT */
bool *slave_ok /* OUT */)
{
ENTRY;
BSON_ASSERT (opts);
if (slave_ok) {
*slave_ok = !!(qflags & MONGOC_QUERY_SLAVE_OK);
}
if (qflags & MONGOC_QUERY_TAILABLE_CURSOR) {
bson_append_bool (
opts, MONGOC_CURSOR_TAILABLE, MONGOC_CURSOR_TAILABLE_LEN, true);
}
if (qflags & MONGOC_QUERY_OPLOG_REPLAY) {
bson_append_bool (opts,
MONGOC_CURSOR_OPLOG_REPLAY,
MONGOC_CURSOR_OPLOG_REPLAY_LEN,
true);
}
if (qflags & MONGOC_QUERY_NO_CURSOR_TIMEOUT) {
bson_append_bool (opts,
MONGOC_CURSOR_NO_CURSOR_TIMEOUT,
MONGOC_CURSOR_NO_CURSOR_TIMEOUT_LEN,
true);
}
if (qflags & MONGOC_QUERY_AWAIT_DATA) {
bson_append_bool (
opts, MONGOC_CURSOR_AWAIT_DATA, MONGOC_CURSOR_AWAIT_DATA_LEN, true);
}
if (qflags & MONGOC_QUERY_EXHAUST) {
bson_append_bool (
opts, MONGOC_CURSOR_EXHAUST, MONGOC_CURSOR_EXHAUST_LEN, true);
}
if (qflags & MONGOC_QUERY_PARTIAL) {
bson_append_bool (opts,
MONGOC_CURSOR_ALLOW_PARTIAL_RESULTS,
MONGOC_CURSOR_ALLOW_PARTIAL_RESULTS_LEN,
true);
}
}
/* Checks if the passed query was wrapped in a $query, and if so, parses the
* query modifiers:
* https://docs.mongodb.com/manual/reference/operator/query-modifier/
* and translates them to find command options:
* https://docs.mongodb.com/manual/reference/command/find/
* opts must be initialized, and may already have options set.
* unwrapped must be uninitialized, and will be initialized at return.
* Returns true if query was unwrapped. */
bool
_mongoc_cursor_translate_dollar_query_opts (const bson_t *query,
bson_t *opts,
bson_t *unwrapped,
bson_error_t *error)
{
bool has_filter = false;
const char *key;
bson_iter_t iter;
const char *opt_key;
int len;
uint32_t data_len;
const uint8_t *data;
bson_error_t error_local = {0};
ENTRY;
BSON_ASSERT (query);
BSON_ASSERT (opts);
/* If the query is explicitly specified wrapped in $query, unwrap it and
* translate the options to new options. */
if (bson_has_field (query, "$query")) {
/* like "{$query: {a: 1}, $orderby: {b: 1}, $otherModifier: true}" */
if (!bson_iter_init (&iter, query)) {
bson_set_error (&error_local,
MONGOC_ERROR_BSON,
MONGOC_ERROR_BSON_INVALID,
"Invalid BSON in query document");
GOTO (done);
}
while (bson_iter_next (&iter)) {
key = bson_iter_key (&iter);
if (key[0] != '$') {
bson_set_error (&error_local,
MONGOC_ERROR_CURSOR,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
"Cannot mix $query with non-dollar field '%s'",
key);
GOTO (done);
}
if (!strcmp (key, "$query")) {
/* set "filter" to the incoming document's "$query" */
bson_iter_document (&iter, &data_len, &data);
if (!bson_init_static (unwrapped, data, (size_t) data_len)) {
bson_set_error (&error_local,
MONGOC_ERROR_BSON,
MONGOC_ERROR_BSON_INVALID,
"Invalid BSON in $query subdocument");
GOTO (done);
}
has_filter = true;
} else if (_translate_query_opt (key, &opt_key, &len)) {
/* "$orderby" becomes "sort", etc., "$unknown" -> "unknown" */
if (!bson_append_iter (opts, opt_key, len, &iter)) {
bson_set_error (&error_local,
MONGOC_ERROR_BSON,
MONGOC_ERROR_BSON_INVALID,
"Error adding \"%s\" to query",
opt_key);
}
} else {
/* strip leading "$" */
if (!bson_append_iter (opts, key + 1, -1, &iter)) {
bson_set_error (&error_local,
MONGOC_ERROR_BSON,
MONGOC_ERROR_BSON_INVALID,
"Error adding \"%s\" to query",
key);
}
}
}
}
done:
if (error) {
memcpy (error, &error_local, sizeof (bson_error_t));
}
if (!has_filter) {
bson_init (unwrapped);
}
RETURN (has_filter);
}
void
mongoc_cursor_destroy (mongoc_cursor_t *cursor)
{
char db[MONGOC_NAMESPACE_MAX];
ENTRY;
if (!cursor) {
EXIT;
}
if (cursor->impl.destroy) {
cursor->impl.destroy (&cursor->impl);
}
if (cursor->client_generation == cursor->client->generation) {
if (cursor->in_exhaust) {
cursor->client->in_exhaust = false;
if (cursor->state != DONE) {
/* The only way to stop an exhaust cursor is to kill the connection
*/
mongoc_cluster_disconnect_node (
&cursor->client->cluster, cursor->server_id, false, NULL);
}
} else if (cursor->cursor_id) {
bson_strncpy (db, cursor->ns, cursor->dblen + 1);
_mongoc_client_kill_cursor (cursor->client,
cursor->server_id,
cursor->cursor_id,
cursor->operation_id,
db,
cursor->ns + cursor->dblen + 1,
cursor->client_session);
}
}
if (cursor->client_session && !cursor->explicit_session) {
mongoc_client_session_destroy (cursor->client_session);
}
mongoc_read_prefs_destroy (cursor->read_prefs);
mongoc_read_concern_destroy (cursor->read_concern);
mongoc_write_concern_destroy (cursor->write_concern);
bson_destroy (&cursor->opts);
bson_destroy (&cursor->error_doc);
bson_free (cursor);
mongoc_counter_cursors_active_dec ();
mongoc_counter_cursors_disposed_inc ();
EXIT;
}
mongoc_server_stream_t *
_mongoc_cursor_fetch_stream (mongoc_cursor_t *cursor)
{
mongoc_server_stream_t *server_stream;
bson_t reply;
ENTRY;
if (cursor->server_id) {
server_stream =
mongoc_cluster_stream_for_server (&cursor->client->cluster,
cursor->server_id,
true /* reconnect_ok */,
cursor->client_session,
&reply,
&cursor->error);
} else {
server_stream = mongoc_cluster_stream_for_reads (&cursor->client->cluster,
cursor->read_prefs,
cursor->client_session,
&reply,
&cursor->error);
if (server_stream) {
cursor->server_id = server_stream->sd->id;
}
}
if (!server_stream) {
bson_destroy (&cursor->error_doc);
bson_copy_to (&reply, &cursor->error_doc);
bson_destroy (&reply);
}
RETURN (server_stream);
}
bool
_mongoc_cursor_monitor_command (mongoc_cursor_t *cursor,
mongoc_server_stream_t *server_stream,
const bson_t *cmd,
const char *cmd_name)
{
mongoc_client_t *client;
mongoc_apm_command_started_t event;
char db[MONGOC_NAMESPACE_MAX];
ENTRY;
client = cursor->client;
if (!client->apm_callbacks.started) {
/* successful */
RETURN (true);
}
bson_strncpy (db, cursor->ns, cursor->dblen + 1);
mongoc_apm_command_started_init (&event,
cmd,
db,
cmd_name,
client->cluster.request_id,
cursor->operation_id,
&server_stream->sd->host,
server_stream->sd->id,
client->apm_context);
client->apm_callbacks.started (&event);
mongoc_apm_command_started_cleanup (&event);
RETURN (true);
}
/* append array of docs from current cursor batch */
static void
_mongoc_cursor_append_docs_array (mongoc_cursor_t *cursor,
bson_t *docs,
mongoc_cursor_response_legacy_t *response)
{
bool eof = false;
char str[16];
const char *key;
uint32_t i = 0;
size_t keylen;
const bson_t *doc;
while ((doc = bson_reader_read (response->reader, &eof))) {
keylen = bson_uint32_to_string (i, &key, str, sizeof str);
bson_append_document (docs, key, (int) keylen, doc);
}
bson_reader_reset (response->reader);
}
void
_mongoc_cursor_monitor_succeeded (mongoc_cursor_t *cursor,
mongoc_cursor_response_legacy_t *response,
int64_t duration,
bool first_batch,
mongoc_server_stream_t *stream,
const char *cmd_name)
{
bson_t docs_array;
mongoc_apm_command_succeeded_t event;
mongoc_client_t *client;
bson_t reply;
bson_t reply_cursor;
ENTRY;
client = cursor->client;
if (!client->apm_callbacks.succeeded) {
EXIT;
}
/* we sent OP_QUERY/OP_GETMORE, fake a reply to find/getMore command:
* {ok: 1, cursor: {id: 17, ns: "...", first/nextBatch: [ ... docs ... ]}}
*/
bson_init (&docs_array);
_mongoc_cursor_append_docs_array (cursor, &docs_array, response);
bson_init (&reply);
bson_append_int32 (&reply, "ok", 2, 1);
bson_append_document_begin (&reply, "cursor", 6, &reply_cursor);
bson_append_int64 (&reply_cursor, "id", 2, mongoc_cursor_get_id (cursor));
bson_append_utf8 (&reply_cursor, "ns", 2, cursor->ns, cursor->nslen);
bson_append_array (&reply_cursor,
first_batch ? "firstBatch" : "nextBatch",
first_batch ? 10 : 9,
&docs_array);
bson_append_document_end (&reply, &reply_cursor);
bson_destroy (&docs_array);
mongoc_apm_command_succeeded_init (&event,
duration,
&reply,
cmd_name,
client->cluster.request_id,
cursor->operation_id,
&stream->sd->host,
stream->sd->id,
client->apm_context);
client->apm_callbacks.succeeded (&event);
mongoc_apm_command_succeeded_cleanup (&event);
bson_destroy (&reply);
EXIT;
}
void
_mongoc_cursor_monitor_failed (mongoc_cursor_t *cursor,
int64_t duration,
mongoc_server_stream_t *stream,
const char *cmd_name)
{
mongoc_apm_command_failed_t event;
mongoc_client_t *client;
bson_t reply;
ENTRY;
client = cursor->client;
if (!client->apm_callbacks.failed) {
EXIT;
}
/* we sent OP_QUERY/OP_GETMORE, fake a reply to find/getMore command:
* {ok: 0}
*/
bson_init (&reply);
bson_append_int32 (&reply, "ok", 2, 0);
mongoc_apm_command_failed_init (&event,
duration,
cmd_name,
&cursor->error,
&reply,
client->cluster.request_id,
cursor->operation_id,
&stream->sd->host,
stream->sd->id,
client->apm_context);
client->apm_callbacks.failed (&event);
mongoc_apm_command_failed_cleanup (&event);
bson_destroy (&reply);
EXIT;
}
#define ADD_FLAG(_flags, _value) \
do { \
if (!BSON_ITER_HOLDS_BOOL (&iter)) { \
bson_set_error (&cursor->error, \
MONGOC_ERROR_COMMAND, \
MONGOC_ERROR_COMMAND_INVALID_ARG, \
"invalid option %s, should be type bool", \
key); \
return false; \
} \
if (bson_iter_as_bool (&iter)) { \
*_flags |= _value; \
} \
} while (false);
bool
_mongoc_cursor_opts_to_flags (mongoc_cursor_t *cursor,
mongoc_server_stream_t *stream,
mongoc_query_flags_t *flags /* OUT */)
{
bson_iter_t iter;
const char *key;
*flags = MONGOC_QUERY_NONE;
if (!bson_iter_init (&iter, &cursor->opts)) {
bson_set_error (&cursor->error,
MONGOC_ERROR_BSON,
MONGOC_ERROR_BSON_INVALID,
"Invalid 'opts' parameter.");
return false;
}
while (bson_iter_next (&iter)) {
key = bson_iter_key (&iter);
if (!strcmp (key, MONGOC_CURSOR_ALLOW_PARTIAL_RESULTS)) {
ADD_FLAG (flags, MONGOC_QUERY_PARTIAL);
} else if (!strcmp (key, MONGOC_CURSOR_AWAIT_DATA)) {
ADD_FLAG (flags, MONGOC_QUERY_AWAIT_DATA);
} else if (!strcmp (key, MONGOC_CURSOR_EXHAUST)) {
ADD_FLAG (flags, MONGOC_QUERY_EXHAUST);
} else if (!strcmp (key, MONGOC_CURSOR_NO_CURSOR_TIMEOUT)) {
ADD_FLAG (flags, MONGOC_QUERY_NO_CURSOR_TIMEOUT);
} else if (!strcmp (key, MONGOC_CURSOR_OPLOG_REPLAY)) {
ADD_FLAG (flags, MONGOC_QUERY_OPLOG_REPLAY);
} else if (!strcmp (key, MONGOC_CURSOR_TAILABLE)) {
ADD_FLAG (flags, MONGOC_QUERY_TAILABLE_CURSOR);
}
}
if (cursor->slave_ok) {
*flags |= MONGOC_QUERY_SLAVE_OK;
} else if (cursor->server_id &&
(stream->topology_type == MONGOC_TOPOLOGY_RS_WITH_PRIMARY ||
stream->topology_type == MONGOC_TOPOLOGY_RS_NO_PRIMARY) &&
stream->sd->type != MONGOC_SERVER_RS_PRIMARY) {
*flags |= MONGOC_QUERY_SLAVE_OK;
}
return true;
}
bool
_mongoc_cursor_run_command (mongoc_cursor_t *cursor,
const bson_t *command,
const bson_t *opts,
bson_t *reply)
{
mongoc_cluster_t *cluster;
mongoc_server_stream_t *server_stream;
bson_iter_t iter;
mongoc_cmd_parts_t parts;
const char *cmd_name;
bool is_primary;
mongoc_read_prefs_t *prefs = NULL;
char db[MONGOC_NAMESPACE_MAX];
mongoc_session_opt_t *session_opts;
bool ret = false;
ENTRY;
cluster = &cursor->client->cluster;
mongoc_cmd_parts_init (
&parts, cursor->client, db, MONGOC_QUERY_NONE, command);
parts.is_read_command = true;
parts.read_prefs = cursor->read_prefs;
parts.assembled.operation_id = cursor->operation_id;
server_stream = _mongoc_cursor_fetch_stream (cursor);
if (!server_stream) {
_mongoc_bson_init_if_set (reply);
GOTO (done);
}
if (opts) {
if (!bson_iter_init (&iter, opts)) {
_mongoc_bson_init_if_set (reply);
bson_set_error (&cursor->error,
MONGOC_ERROR_BSON,
MONGOC_ERROR_BSON_INVALID,
"Invalid BSON in opts document");
GOTO (done);
}
if (!mongoc_cmd_parts_append_opts (&parts,
&iter,
server_stream->sd->max_wire_version,
&cursor->error)) {
_mongoc_bson_init_if_set (reply);
GOTO (done);
}
}
if (parts.assembled.session) {
/* initial query/aggregate/etc, and opts contains "sessionId" */
BSON_ASSERT (!cursor->client_session);
BSON_ASSERT (!cursor->explicit_session);
cursor->client_session = parts.assembled.session;
cursor->explicit_session = true;
} else if (cursor->client_session) {
/* a getMore with implicit or explicit session already acquired */
mongoc_cmd_parts_set_session (&parts, cursor->client_session);
} else {
/* try to create an implicit session. not causally consistent. we keep
* the session but leave cursor->explicit_session as 0, so we use the
* same lsid for getMores but destroy the session when the cursor dies.
*/
session_opts = mongoc_session_opts_new ();
mongoc_session_opts_set_causal_consistency (session_opts, false);
/* returns NULL if sessions aren't supported. ignore errors. */
cursor->client_session =
mongoc_client_start_session (cursor->client, session_opts, NULL);
mongoc_cmd_parts_set_session (&parts, cursor->client_session);
mongoc_session_opts_destroy (session_opts);
}
if (!mongoc_cmd_parts_set_read_concern (&parts,
cursor->read_concern,
server_stream->sd->max_wire_version,
&cursor->error)) {
_mongoc_bson_init_if_set (reply);
GOTO (done);
}
bson_strncpy (db, cursor->ns, cursor->dblen + 1);
parts.assembled.db_name = db;
if (!_mongoc_cursor_opts_to_flags (
cursor, server_stream, &parts.user_query_flags)) {
_mongoc_bson_init_if_set (reply);
GOTO (done);
}
/* we might use mongoc_cursor_set_hint to target a secondary but have no
* read preference, so the secondary rejects the read. same if we have a
* direct connection to a secondary (topology type "single"). with
* OP_QUERY we handle this by setting slaveOk. here we use $readPreference.
*/
cmd_name = _mongoc_get_command_name (command);
is_primary =