Skip to content

Commit 16815f8

Browse files
committed
CDRIVER-1000 upgrade $or, etc. from OP_QUERY to find cmd
1 parent ef9978d commit 16815f8

File tree

3 files changed

+93
-50
lines changed

3 files changed

+93
-50
lines changed

src/mongoc/mongoc-cursor-private.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ struct _mongoc_cursor_t
6161
unsigned end_of_event : 1;
6262
unsigned has_fields : 1;
6363
unsigned in_exhaust : 1;
64-
unsigned has_dollar : 1;
6564

6665
bson_t query;
6766
bson_t fields;

src/mongoc/mongoc-cursor.c

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ _mongoc_cursor_new (mongoc_client_t *client,
188188
}
189189
}
190190

191-
cursor->has_dollar = found_dollar ? 1 : 0;
192-
193191
if (found_dollar && found_non_dollar) {
194192
bson_set_error (&cursor->error,
195193
MONGOC_ERROR_CURSOR,
@@ -564,15 +562,15 @@ _invalid_field (const char *query_field,
564562
}
565563

566564

567-
static void
568-
_query_to_cmd_field (const char *query_field,
569-
const char **cmd_field,
570-
int *len)
565+
static bool
566+
_translate_query_opt (const char *query_field,
567+
const char **cmd_field,
568+
int *len)
571569
{
572570
if (query_field[0] != '$') {
573571
*cmd_field = query_field;
574572
*len = -1;
575-
return;
573+
return true;
576574
}
577575

578576
/* strip the leading '$' */
@@ -587,11 +585,36 @@ _query_to_cmd_field (const char *query_field,
587585
} else if (!strcmp ("showDiskLoc", query_field)) { /* <= MongoDb 3.0 */
588586
*cmd_field = "showRecordId";
589587
*len = 12;
588+
} else if (!strcmp("hint", query_field)) {
589+
*cmd_field = "hint";
590+
*len = 4;
591+
} else if (!strcmp("comment", query_field)) {
592+
*cmd_field = "comment";
593+
*len = 7;
594+
} else if (!strcmp("maxScan", query_field)) {
595+
*cmd_field = "maxScan";
596+
*len = 7;
597+
} else if (!strcmp("maxTimeMS", query_field)) {
598+
*cmd_field = "maxTimeMS";
599+
*len = 9;
600+
} else if (!strcmp("max", query_field)) {
601+
*cmd_field = "max";
602+
*len = 3;
603+
} else if (!strcmp("min", query_field)) {
604+
*cmd_field = "min";
605+
*len = 3;
606+
} else if (!strcmp("returnKey", query_field)) {
607+
*cmd_field = "returnKey";
608+
*len = 9;
609+
} else if (!strcmp("snapshot", query_field)) {
610+
*cmd_field = "snapshot";
611+
*len = 8;
590612
} else {
591-
/* just use the field name, minus the '$' */
592-
*cmd_field = query_field;
593-
*len = -1;
613+
/* not a special command field, must be a query operator like $or */
614+
return false;
594615
}
616+
617+
return true;
595618
}
596619

597620

@@ -657,16 +680,21 @@ _mongoc_cursor_prepare_find_command (mongoc_cursor_t *cursor,
657680
*/
658681
bson_t empty = BSON_INITIALIZER;
659682
bson_append_document (command, "filter", 6, &empty);
660-
} else if (cursor->has_dollar) {
683+
} else if (bson_has_field (&cursor->query, "$query")) {
661684
bson_iter_init (&iter, &cursor->query);
662685
while (bson_iter_next (&iter)) {
663686
if (_invalid_field (bson_iter_key (&iter), cursor)) {
664687
return false;
665688
}
666689

667-
_query_to_cmd_field (bson_iter_key (&iter), &command_field, &len);
668690
value = bson_iter_value (&iter);
669-
bson_append_value (command, command_field, len, value);
691+
if (_translate_query_opt (bson_iter_key (&iter),
692+
&command_field,
693+
&len)) {
694+
bson_append_value (command, command_field, len, value);
695+
} else {
696+
bson_append_value (command, bson_iter_key (&iter), -1, value);
697+
}
670698
}
671699
} else if (bson_has_field (&cursor->query, "filter")) {
672700
bson_concat (command, &cursor->query);

tests/test-mongoc-collection-find.c

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ _test_collection_op_query_or_find_command (
176176
mongoc_client_t *client;
177177
mongoc_collection_t *collection;
178178
mongoc_cursor_t *cursor;
179+
bson_error_t error;
179180
future_t *future;
180181
request_t *request;
181182
const bson_t *doc;
@@ -198,6 +199,7 @@ _test_collection_op_query_or_find_command (
198199
test_data->fields_bson,
199200
test_data->read_prefs);
200201

202+
ASSERT_OR_PRINT (!mongoc_cursor_error (cursor, &error), error);
201203
future = future_cursor_next (cursor, &doc);
202204
request = check_request_fn (server, test_data);
203205
ASSERT (request);
@@ -388,6 +390,22 @@ test_dollar_query (void)
388390
}
389391

390392

393+
static void
394+
test_dollar_or (void)
395+
{
396+
test_collection_find_t test_data = TEST_COLLECTION_FIND_INIT;
397+
398+
test_data.docs = "[{'_id': 1}, {'_id': 2}, {'_id': 3}]";
399+
test_data.query_input = "{'$or': [{'_id': 1}, {'_id': 3}]}";
400+
test_data.expected_op_query = test_data.query_input;
401+
test_data.expected_find_command =
402+
"{'find': 'collection', 'filter': {'$or': [{'_id': 1}, {'_id': 3}]}}";
403+
404+
test_data.expected_result = "[{'_id': 1}, {'_id': 3}]";
405+
_test_collection_find (&test_data);
406+
}
407+
408+
391409
/* test that we can query for a document by a key named "filter" */
392410
static void
393411
test_key_named_filter (void)
@@ -526,40 +544,6 @@ test_int_modifiers (void)
526544
}
527545

528546

529-
static void
530-
test_bool_modifiers (void)
531-
{
532-
const char *modifiers[] = {
533-
"snapshot",
534-
"showRecordId",
535-
};
536-
537-
const char *mod;
538-
size_t i;
539-
char *query;
540-
char *find_command;
541-
test_collection_find_t test_data = TEST_COLLECTION_FIND_INIT;
542-
543-
test_data.expected_result = test_data.docs = "[{'_id': 1}]";
544-
545-
for (i = 0; i < sizeof (modifiers) / sizeof (const char *); i++) {
546-
mod = modifiers[i];
547-
query = bson_strdup_printf ("{'$query': {}, '$%s': true}", mod);
548-
549-
/* find command has same modifier, without the $-prefix */
550-
find_command = bson_strdup_printf (
551-
"{'find': 'collection', 'filter': {}, '%s': true}", mod);
552-
553-
test_data.expected_op_query = test_data.query_input = query;
554-
test_data.expected_find_command = find_command;
555-
_test_collection_find (&test_data);
556-
557-
bson_free (query);
558-
bson_free (find_command);
559-
}
560-
}
561-
562-
563547
static void
564548
test_index_spec_modifiers (void)
565549
{
@@ -621,6 +605,19 @@ test_max (void)
621605
}
622606

623607

608+
static void
609+
test_snapshot (void)
610+
{
611+
test_collection_find_t test_data = TEST_COLLECTION_FIND_INIT;
612+
test_data.docs = "[{'_id': 1}]";
613+
test_data.query_input = "{'$query': {}, '$snapshot': true}";
614+
test_data.expected_op_query = test_data.query_input;
615+
test_data.expected_find_command = "{'find': 'collection', 'filter': {}, 'snapshot': true}";
616+
test_data.expected_result = "[{'_id': 1}]";
617+
_test_collection_find (&test_data);
618+
}
619+
620+
624621
/* $showDiskLoc becomes showRecordId */
625622
static void
626623
test_diskloc (void)
@@ -690,6 +687,21 @@ test_limit (void)
690687
}
691688

692689

690+
static void
691+
test_unrecognized_dollar_option (void)
692+
{
693+
test_collection_find_t test_data = TEST_COLLECTION_FIND_INIT;
694+
695+
test_data.query_input = "{'$query': {'a': 1}, '$dumb': 1}";
696+
test_data.expected_find_command =
697+
"{'find': 'collection', 'filter': {'a': 1}, '$dumb': 1}";
698+
699+
test_data.requires_wire_version_4 = true;
700+
test_data.do_live = false;
701+
_test_collection_find (&test_data);
702+
}
703+
704+
693705
static void
694706
test_query_flags (void)
695707
{
@@ -968,6 +980,8 @@ test_collection_find_install (TestSuite *suite)
968980
{
969981
TestSuite_Add (suite, "/Collection/find/dollar_query",
970982
test_dollar_query);
983+
TestSuite_Add (suite, "/Collection/find/dollar_or",
984+
test_dollar_or);
971985
TestSuite_Add (suite, "/Collection/find/key_named_filter",
972986
test_key_named_filter);
973987
TestSuite_Add (suite, "/Collection/find/cmd/subdoc_named_filter",
@@ -984,14 +998,14 @@ test_collection_find_install (TestSuite *suite)
984998
test_fields);
985999
TestSuite_Add (suite, "/Collection/find/modifiers/integer",
9861000
test_int_modifiers);
987-
TestSuite_Add (suite, "/Collection/find/modifiers/bool",
988-
test_bool_modifiers);
9891001
TestSuite_Add (suite, "/Collection/find/modifiers/index_spec",
9901002
test_index_spec_modifiers);
9911003
TestSuite_Add (suite, "/Collection/find/comment",
9921004
test_comment);
9931005
TestSuite_Add (suite, "/Collection/find/max",
9941006
test_max);
1007+
TestSuite_Add (suite, "/Collection/find/modifiers/bool",
1008+
test_snapshot);
9951009
TestSuite_Add (suite, "/Collection/find/showdiskloc",
9961010
test_diskloc);
9971011
TestSuite_Add (suite, "/Collection/find/returnkey",
@@ -1002,6 +1016,8 @@ test_collection_find_install (TestSuite *suite)
10021016
test_batch_size);
10031017
TestSuite_Add (suite, "/Collection/find/limit",
10041018
test_limit);
1019+
TestSuite_Add (suite, "/Collection/find/unrecognized",
1020+
test_unrecognized_dollar_option);
10051021
TestSuite_Add (suite, "/Collection/find/flags",
10061022
test_query_flags);
10071023

0 commit comments

Comments
 (0)