Skip to content

Commit d4c1abc

Browse files
author
Christian Hergert
committed
collection: wrap mongoc_client_command() and mongoc_client_command_simple().
This simplifies our command handling so that it is all done in mongoc_client_t. However, the error unwrapping is part of mongoc_cursor_t, which is tied in with regular query failure handling.
1 parent 02c16ac commit d4c1abc

File tree

1 file changed

+21
-70
lines changed

1 file changed

+21
-70
lines changed

mongoc/mongoc-collection.c

Lines changed: 21 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ mongoc_collection_find (mongoc_collection_t *collection, /* IN */
343343
* Executes a command on a cluster node matching @read_prefs. If
344344
* @read_prefs is not provided, it will be run on the primary node.
345345
*
346-
* This function will always return a mongoc_cursor_t with the exception
347-
* of invalid API use.
346+
* This function will always return a mongoc_cursor_t.
348347
*
349348
* Parameters:
350349
* @collection: A mongoc_collection_t.
@@ -365,28 +364,23 @@ mongoc_collection_find (mongoc_collection_t *collection, /* IN */
365364
*/
366365

367366
mongoc_cursor_t *
368-
mongoc_collection_command (mongoc_collection_t *collection, /* IN */
369-
mongoc_query_flags_t flags, /* IN */
370-
bson_uint32_t skip, /* IN */
371-
bson_uint32_t n_return, /* IN */
372-
const bson_t *query, /* IN */
373-
const bson_t *fields, /* IN */
374-
const mongoc_read_prefs_t *read_prefs) /* IN */
367+
mongoc_collection_command (mongoc_collection_t *collection,
368+
mongoc_query_flags_t flags,
369+
bson_uint32_t skip,
370+
bson_uint32_t n_return,
371+
const bson_t *query,
372+
const bson_t *fields,
373+
const mongoc_read_prefs_t *read_prefs)
375374
{
376-
char ns[MONGOC_NAMESPACE_MAX+4];
377-
378-
bson_return_val_if_fail(collection, NULL);
379-
bson_return_val_if_fail(query, NULL);
380-
381-
snprintf(ns, sizeof ns, "%s.$cmd", collection->db);
382-
ns[sizeof ns - 1] = '\0';
375+
BSON_ASSERT (collection);
376+
BSON_ASSERT (query);
383377

384378
if (!read_prefs) {
385379
read_prefs = collection->read_prefs;
386380
}
387381

388-
return _mongoc_cursor_new(collection->client, ns, flags, skip,
389-
n_return, 0, TRUE, query, fields, read_prefs);
382+
return mongoc_client_command (collection->client, collection->db, flags,
383+
skip, n_return, query, fields, read_prefs);
390384
}
391385

392386

@@ -417,60 +411,17 @@ mongoc_collection_command (mongoc_collection_t *collection, /* IN */
417411
*/
418412

419413
bson_bool_t
420-
mongoc_collection_command_simple (
421-
mongoc_collection_t *collection, /* IN */
422-
const bson_t *command, /* IN */
423-
const mongoc_read_prefs_t *read_prefs, /* IN */
424-
bson_t *reply, /* OUT */
425-
bson_error_t *error) /* OUT */
414+
mongoc_collection_command_simple (mongoc_collection_t *collection,
415+
const bson_t *command,
416+
const mongoc_read_prefs_t *read_prefs,
417+
bson_t *reply,
418+
bson_error_t *error)
426419
{
427-
mongoc_cursor_t *cursor;
428-
const bson_t *b;
429-
bson_bool_t ret = FALSE;
430-
bson_iter_t iter;
431-
const char *errmsg = NULL;
432-
int code = 0;
433-
434-
bson_return_val_if_fail(collection, FALSE);
435-
bson_return_val_if_fail(command, FALSE);
420+
BSON_ASSERT (collection);
421+
BSON_ASSERT (command);
436422

437-
cursor = mongoc_collection_command(collection, MONGOC_QUERY_NONE, 0,
438-
1, command, NULL, read_prefs);
439-
440-
if (!mongoc_cursor_next(cursor, &b)) {
441-
mongoc_cursor_error(cursor, error);
442-
mongoc_cursor_destroy(cursor);
443-
if (reply) {
444-
bson_init(reply);
445-
}
446-
return FALSE;
447-
}
448-
449-
if (reply) {
450-
bson_copy_to(b, reply);
451-
}
452-
453-
if (!bson_iter_init_find(&iter, b, "ok") || !bson_iter_as_bool(&iter)) {
454-
if (bson_iter_init_find(&iter, b, "code") &&
455-
BSON_ITER_HOLDS_INT32(&iter)) {
456-
code = bson_iter_int32(&iter);
457-
}
458-
if (bson_iter_init_find(&iter, b, "errmsg") &&
459-
BSON_ITER_HOLDS_UTF8(&iter)) {
460-
errmsg = bson_iter_utf8(&iter, NULL);
461-
}
462-
bson_set_error(error,
463-
MONGOC_ERROR_QUERY,
464-
code,
465-
"%s", errmsg ? errmsg : "Unknown command failure");
466-
ret = FALSE;
467-
} else {
468-
ret = TRUE;
469-
}
470-
471-
mongoc_cursor_destroy(cursor);
472-
473-
return ret;
423+
return mongoc_client_command_simple (collection->client, collection->db,
424+
command, read_prefs, reply, error);
474425
}
475426

476427

0 commit comments

Comments
 (0)