Skip to content

Commit a6224a3

Browse files
amidvidyhanumantmk
authored andcommitted
CDRIVER-442 reimplement mongoc_database_has_collection using mongoc_database_get_collection_info
1 parent bf90e37 commit a6224a3

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

src/mongoc/mongoc-database.c

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -586,15 +586,13 @@ mongoc_database_has_collection (mongoc_database_t *database,
586586
const char *name,
587587
bson_error_t *error)
588588
{
589-
mongoc_collection_t *collection;
590-
mongoc_read_prefs_t *read_prefs;
591-
mongoc_cursor_t *cursor;
592-
const bson_t *doc;
593-
bson_iter_t iter;
589+
bson_t *infos = NULL;
590+
bson_iter_t info_iter;
591+
bson_iter_t col_array_iter;
592+
bson_iter_t col_iter;
594593
bool ret = false;
595594
const char *cur_name;
596-
bson_t q = BSON_INITIALIZER;
597-
char ns[140];
595+
bson_t filter = BSON_INITIALIZER;
598596

599597
ENTRY;
600598

@@ -605,34 +603,38 @@ mongoc_database_has_collection (mongoc_database_t *database,
605603
memset (error, 0, sizeof *error);
606604
}
607605

608-
bson_snprintf (ns, sizeof ns, "%s.%s", database->name, name);
606+
BSON_APPEND_UTF8 (&filter, "name", name);
609607

610-
read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY);
611-
collection = mongoc_client_get_collection (database->client,
612-
database->name,
613-
"system.namespaces");
614-
cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, &q,
615-
NULL, read_prefs);
616-
617-
while (!mongoc_cursor_error (cursor, error) &&
618-
mongoc_cursor_more (cursor)) {
619-
while (mongoc_cursor_next (cursor, &doc) &&
620-
bson_iter_init_find (&iter, doc, "name") &&
621-
BSON_ITER_HOLDS_UTF8 (&iter)) {
622-
cur_name = bson_iter_utf8(&iter, NULL);
623-
if (!strcmp(cur_name, ns)) {
624-
ret = true;
625-
GOTO(cleanup);
608+
infos = mongoc_database_get_collection_info (database, &filter, error);
609+
610+
if (!infos ||
611+
(error &&
612+
((error->domain != 0) ||
613+
(error->code != 0)))) {
614+
return ret;
615+
}
616+
617+
if (bson_iter_init_find (&info_iter, infos, "collections") &&
618+
BSON_ITER_HOLDS_ARRAY (&info_iter) &&
619+
bson_iter_recurse (&info_iter, &col_array_iter)) {
620+
while (bson_iter_next (&col_array_iter)) {
621+
if (BSON_ITER_HOLDS_DOCUMENT (&col_array_iter) &&
622+
bson_iter_recurse (&col_array_iter, &col_iter) &&
623+
bson_iter_find (&col_iter, "name") &&
624+
BSON_ITER_HOLDS_UTF8 (&col_iter) &&
625+
(cur_name = bson_iter_utf8 (&col_iter, NULL))) {
626+
if (!strcmp (cur_name, name)) {
627+
ret = true;
628+
GOTO (cleanup);
629+
}
626630
}
627631
}
628632
}
629633

630634
cleanup:
631-
mongoc_cursor_destroy (cursor);
632-
mongoc_collection_destroy (collection);
633-
mongoc_read_prefs_destroy (read_prefs);
635+
bson_free (infos);
634636

635-
RETURN(ret);
637+
RETURN (ret);
636638
}
637639

638640
/* Uses old way of querying system.namespaces. */

0 commit comments

Comments
 (0)