Skip to content

Commit 30fdc1c

Browse files
authored
Kyle/snapshot example 2 (#1156)
CDRIVER-4295 Implement snapshot query example number two for the manual. Related: DRIVERS-2181
1 parent dcf9027 commit 30fdc1c

File tree

1 file changed

+155
-4
lines changed

1 file changed

+155
-4
lines changed

src/libmongoc/tests/test-mongoc-sample-commands.c

Lines changed: 155 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,7 +2526,6 @@ pet_setup (mongoc_collection_t *cats_collection,
25262526
return ok;
25272527
}
25282528

2529-
25302529
/*
25312530
* Increment 'accumulator' by the amount of adoptable pets in the given
25322531
* collection.
@@ -2603,17 +2602,17 @@ accumulate_adoptable_count (const mongoc_client_session_t *cs,
26032602
static void
26042603
test_example_59 (mongoc_database_t *db)
26052604
{
2606-
/* Start Snapshot Query Example 1 */
26072605
mongoc_client_t *client = NULL;
2606+
client = test_framework_new_default_client ();
2607+
2608+
/* Start Snapshot Query Example 1 */
26082609
mongoc_client_session_t *cs = NULL;
26092610
mongoc_collection_t *cats_collection = NULL;
26102611
mongoc_collection_t *dogs_collection = NULL;
26112612
int64_t adoptable_pets_count = 0;
26122613
bson_error_t error;
26132614
mongoc_session_opt_t *session_opts;
26142615

2615-
client = test_framework_new_default_client ();
2616-
26172616
cats_collection = mongoc_client_get_collection (client, "pets", "cats");
26182617
dogs_collection = mongoc_client_get_collection (client, "pets", "dogs");
26192618

@@ -2676,6 +2675,157 @@ test_example_59 (mongoc_database_t *db)
26762675
/* End Snapshot Query Example 1 Post */
26772676
}
26782677

2678+
static bool
2679+
retail_setup (mongoc_collection_t *sales_collection)
2680+
{
2681+
bool ok = true;
2682+
bson_t *doc = NULL;
2683+
bson_error_t error;
2684+
struct timeval tv;
2685+
int64_t unix_time_now = 0;
2686+
2687+
if (bson_gettimeofday (&tv)) {
2688+
MONGOC_ERROR ("could not get time of day");
2689+
goto cleanup;
2690+
}
2691+
unix_time_now = 1000 * tv.tv_sec;
2692+
2693+
mongoc_collection_drop (sales_collection, NULL);
2694+
2695+
doc = BCON_NEW ("shoeType",
2696+
BCON_UTF8 ("boot"),
2697+
"price",
2698+
BCON_INT64 (30),
2699+
"saleDate",
2700+
BCON_DATE_TIME (unix_time_now));
2701+
2702+
ok =
2703+
mongoc_collection_insert_one (sales_collection, doc, NULL, NULL, &error);
2704+
if (!ok) {
2705+
MONGOC_ERROR ("insert into retail.sales failed: %s", error.message);
2706+
goto cleanup;
2707+
}
2708+
2709+
cleanup:
2710+
bson_destroy (doc);
2711+
return ok;
2712+
}
2713+
2714+
2715+
static void
2716+
test_example_60 (mongoc_database_t *db)
2717+
{
2718+
mongoc_client_t *client = NULL;
2719+
client = test_framework_new_default_client ();
2720+
2721+
/* Start Snapshot Query Example 2 */
2722+
mongoc_client_session_t *cs = NULL;
2723+
mongoc_collection_t *sales_collection = NULL;
2724+
bson_error_t error;
2725+
mongoc_session_opt_t *session_opts;
2726+
bson_t *pipeline = NULL;
2727+
bson_t opts = BSON_INITIALIZER;
2728+
mongoc_cursor_t *cursor = NULL;
2729+
const bson_t *doc = NULL;
2730+
bool ok = true;
2731+
bson_iter_t iter;
2732+
int64_t total_sales = 0;
2733+
2734+
sales_collection = mongoc_client_get_collection (client, "retail", "sales");
2735+
2736+
/* seed 'retail.sales' with example data */
2737+
if (!retail_setup (sales_collection)) {
2738+
goto cleanup;
2739+
}
2740+
2741+
/* start a snapshot session */
2742+
session_opts = mongoc_session_opts_new ();
2743+
mongoc_session_opts_set_snapshot (session_opts, true);
2744+
cs = mongoc_client_start_session (client, session_opts, &error);
2745+
mongoc_session_opts_destroy (session_opts);
2746+
if (!cs) {
2747+
MONGOC_ERROR ("Could not start session: %s", error.message);
2748+
goto cleanup;
2749+
}
2750+
2751+
if (!mongoc_client_session_append (cs, &opts, &error)) {
2752+
MONGOC_ERROR ("could not apply session options: %s", error.message);
2753+
goto cleanup;
2754+
}
2755+
2756+
pipeline = BCON_NEW ("pipeline",
2757+
"[",
2758+
"{",
2759+
"$match",
2760+
"{",
2761+
"$expr",
2762+
"{",
2763+
"$gt",
2764+
"[",
2765+
"$saleDate",
2766+
"{",
2767+
"$dateSubtract",
2768+
"{",
2769+
"startDate",
2770+
"$$NOW",
2771+
"unit",
2772+
BCON_UTF8 ("day"),
2773+
"amount",
2774+
BCON_INT64 (1),
2775+
"}",
2776+
"}",
2777+
"]",
2778+
"}",
2779+
"}",
2780+
"}",
2781+
"{",
2782+
"$count",
2783+
BCON_UTF8 ("totalDailySales"),
2784+
"}",
2785+
"]");
2786+
2787+
cursor = mongoc_collection_aggregate (
2788+
sales_collection, MONGOC_QUERY_NONE, pipeline, &opts, NULL);
2789+
bson_destroy (&opts);
2790+
2791+
ok = mongoc_cursor_next (cursor, &doc);
2792+
2793+
if (mongoc_cursor_error (cursor, &error)) {
2794+
MONGOC_ERROR ("could not get totalDailySales: %s", error.message);
2795+
ok = false;
2796+
goto cleanup;
2797+
}
2798+
2799+
if (!ok) {
2800+
MONGOC_ERROR ("%s", "cursor has no results");
2801+
goto cleanup;
2802+
}
2803+
2804+
ok = bson_iter_init_find (&iter, doc, "totalDailySales");
2805+
if (ok) {
2806+
total_sales = bson_iter_as_int64 (&iter);
2807+
} else {
2808+
MONGOC_ERROR ("%s", "missing key: 'totalDailySales'");
2809+
goto cleanup;
2810+
}
2811+
2812+
/* End Snapshot Query Example 2 */
2813+
2814+
if (total_sales != 1) {
2815+
MONGOC_ERROR ("there should be exactly 1 total_sales, found: %" PRId64,
2816+
total_sales);
2817+
}
2818+
2819+
/* Start Snapshot Query Example 2 Post */
2820+
cleanup:
2821+
mongoc_collection_destroy (sales_collection);
2822+
mongoc_client_session_destroy (cs);
2823+
mongoc_cursor_destroy (cursor);
2824+
bson_destroy (pipeline);
2825+
mongoc_client_destroy (client);
2826+
/* End Snapshot Query Example 2 Post */
2827+
}
2828+
26792829
/* clang-format off */
26802830

26812831
typedef struct {
@@ -4205,6 +4355,7 @@ test_sample_commands (void)
42054355
test_sample_command (test_example_58, 58, db, collection, false);
42064356
test_sample_command (test_example_56, 56, db, collection, true);
42074357
test_sample_command (test_example_59, 59, db, collection, true);
4358+
test_sample_command (test_example_60, 60, db, collection, true);
42084359
test_sample_change_stream_command (test_example_change_stream, db);
42094360
test_sample_causal_consistency (client);
42104361
test_sample_aggregation (db);

0 commit comments

Comments
 (0)