Skip to content

Commit fa45d53

Browse files
author
Christian Hergert
committed
collection: add mongoc_collection_stats().
1 parent 459368e commit fa45d53

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

build/cmake/libmongoc-ssl.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mongoc_collection_rename
4040
mongoc_collection_save
4141
mongoc_collection_set_read_prefs
4242
mongoc_collection_set_write_concern
43+
mongoc_collection_stats
4344
mongoc_collection_update
4445
mongoc_collection_validate
4546
mongoc_cursor_clone

build/cmake/libmongoc.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mongoc_collection_rename
4141
mongoc_collection_save
4242
mongoc_collection_set_read_prefs
4343
mongoc_collection_set_write_concern
44+
mongoc_collection_stats
4445
mongoc_collection_update
4546
mongoc_collection_validate
4647
mongoc_cursor_clone

src/libmongoc.symbols

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mongoc_collection_rename
4242
mongoc_collection_save
4343
mongoc_collection_set_read_prefs
4444
mongoc_collection_set_write_concern
45+
mongoc_collection_stats
4546
mongoc_collection_update
4647
mongoc_collection_validate
4748
mongoc_cursor_clone

src/mongoc/mongoc-collection.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,3 +1465,61 @@ mongoc_collection_rename (mongoc_collection_t *collection,
14651465

14661466
return ret;
14671467
}
1468+
1469+
1470+
/*
1471+
*--------------------------------------------------------------------------
1472+
*
1473+
* mongoc_collection_stats --
1474+
*
1475+
* Fetches statistics about the collection.
1476+
*
1477+
* The result is stored in @stats, which should NOT be an initialized
1478+
* bson_t or a leak will occur.
1479+
*
1480+
* @stats, @options, and @error are optional.
1481+
*
1482+
* Returns:
1483+
* true on success and @stats is set.
1484+
* false on failure and @error is set.
1485+
*
1486+
* Side effects:
1487+
* @stats and @error.
1488+
*
1489+
*--------------------------------------------------------------------------
1490+
*/
1491+
1492+
bool
1493+
mongoc_collection_stats (mongoc_collection_t *collection,
1494+
const bson_t *options,
1495+
bson_t *stats,
1496+
bson_error_t *error)
1497+
{
1498+
bson_iter_t iter;
1499+
bson_t cmd = BSON_INITIALIZER;
1500+
bool ret;
1501+
1502+
bson_return_val_if_fail (collection, false);
1503+
1504+
if (options &&
1505+
bson_iter_init_find (&iter, options, "scale") &&
1506+
!BSON_ITER_HOLDS_INT32 (&iter)) {
1507+
bson_set_error (error,
1508+
MONGOC_ERROR_BSON,
1509+
MONGOC_ERROR_BSON_INVALID,
1510+
"'sacle' must be an int32 value.");
1511+
return false;
1512+
}
1513+
1514+
BSON_APPEND_UTF8 (&cmd, "collStats", collection->collection);
1515+
1516+
if (options) {
1517+
bson_concat (&cmd, options);
1518+
}
1519+
1520+
ret = mongoc_collection_command_simple (collection, &cmd, NULL, stats, error);
1521+
1522+
bson_destroy (&cmd);
1523+
1524+
return ret;
1525+
}

src/mongoc/mongoc-collection.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ bool mongoc_collection_rename (mongoc_col
112112
const char *new_name,
113113
bool drop_target_before_rename,
114114
bson_error_t *error);
115+
bool mongoc_collection_stats (mongoc_collection_t *collection,
116+
const bson_t *options,
117+
bson_t *reply,
118+
bson_error_t *error);
115119
const mongoc_read_prefs_t *mongoc_collection_get_read_prefs (const mongoc_collection_t *collection);
116120
void mongoc_collection_set_read_prefs (mongoc_collection_t *collection,
117121
const mongoc_read_prefs_t *read_prefs);

tests/test-mongoc-collection.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,45 @@ test_rename (void)
707707
}
708708

709709

710+
static void
711+
test_stats (void)
712+
{
713+
mongoc_collection_t *collection;
714+
mongoc_client_t *client;
715+
bson_error_t error;
716+
bson_iter_t iter;
717+
bson_t stats;
718+
bson_t doc = BSON_INITIALIZER;
719+
bool r;
720+
721+
client = mongoc_client_new (gTestUri);
722+
ASSERT (client);
723+
724+
collection = get_test_collection (client, "test_stats");
725+
ASSERT (collection);
726+
727+
r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, &error);
728+
assert (r);
729+
730+
r = mongoc_collection_stats (collection, NULL, &stats, &error);
731+
assert (r);
732+
733+
assert (bson_iter_init_find (&iter, &stats, "ns"));
734+
735+
assert (bson_iter_init_find (&iter, &stats, "count"));
736+
assert (bson_iter_as_int64 (&iter) >= 1);
737+
738+
bson_destroy (&stats);
739+
740+
r = mongoc_collection_drop (collection, &error);
741+
assert (r);
742+
743+
mongoc_collection_destroy (collection);
744+
mongoc_client_destroy (client);
745+
bson_destroy (&doc);
746+
}
747+
748+
710749
static void
711750
cleanup_globals (void)
712751
{
@@ -731,6 +770,7 @@ test_collection_install (TestSuite *suite)
731770
TestSuite_Add (suite, "/Collection/aggregate", test_aggregate);
732771
TestSuite_Add (suite, "/Collection/validate", test_validate);
733772
TestSuite_Add (suite, "/Collection/rename", test_rename);
773+
TestSuite_Add (suite, "/Collection/stats", test_stats);
734774

735775
atexit (cleanup_globals);
736776
}

0 commit comments

Comments
 (0)