Skip to content

Commit 084132e

Browse files
Maga Napangachergert
authored andcommitted
added support for accessing getLastError document.
new: mongoc_collection_get_last_error(). Documentation added. Related to: https://jira.mongodb.org/browse/CDRIVER-295
1 parent 0dafc6d commit 084132e

10 files changed

+122
-8
lines changed

build/cmake/libmongoc-ssl.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mongoc_collection_drop
2929
mongoc_collection_drop_index
3030
mongoc_collection_ensure_index
3131
mongoc_collection_find
32+
mongoc_collection_get_last_error
3233
mongoc_collection_get_name
3334
mongoc_collection_get_read_prefs
3435
mongoc_collection_get_write_concern

build/cmake/libmongoc.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mongoc_collection_drop
2828
mongoc_collection_drop_index
2929
mongoc_collection_ensure_index
3030
mongoc_collection_find
31+
mongoc_collection_get_last_error
3132
mongoc_collection_get_name
3233
mongoc_collection_get_read_prefs
3334
mongoc_collection_get_write_concern
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
mongoc_collection_get_last_error(3)
2+
=============================
3+
4+
5+
NAME
6+
----
7+
mongoc_collection_get_last_error - Returns getLastError document.
8+
9+
10+
SYNOPSIS
11+
--------
12+
[source,c]
13+
-----------------------
14+
bson_t *
15+
mongoc_collection_get_last_error (const mongoc_collection_t *collection);
16+
-----------------------
17+
18+
19+
DESCRIPTION
20+
-----------
21+
The _mongoc_collection_get_last_error()_ function shall return getLastError
22+
document, according to write_concern on last executed command for current
23+
collection instance.
24+
25+
write_concern must be at least MONGOC_WRITE_CONCERN_W_DEFAULT (1) in
26+
last command execution for getLastError to be available.
27+
28+
Last executed command must be any of:
29+
_mongoc_collection_insert()_
30+
_mongoc_collection_insert_bulk()_
31+
_mongoc_collection_update()_
32+
_mongoc_collection_delete()_
33+
_mongoc_collection_save()_
34+
_mongoc_collection_ensure_index()_
35+
36+
37+
RETURN VALUE
38+
------------
39+
A newly allocated bson_t getLastError document, that *must* be destroyed
40+
with _bson_destroy()_. Or NULL if no getLastError present.
41+
42+
43+
AUTHORS
44+
-------
45+
46+
This page was written by MongoDB Inc.

doc/mongoc_symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ linkmongoc:mongoc_collection_drop[3] +
2929
linkmongoc:mongoc_collection_drop_index[3] +
3030
linkmongoc:mongoc_collection_ensure_index[3] +
3131
linkmongoc:mongoc_collection_find[3] +
32+
linkmongoc:mongoc_collection_get_last_error[3] +
3233
linkmongoc:mongoc_collection_get_name[3] +
3334
linkmongoc:mongoc_collection_get_read_prefs[3] +
3435
linkmongoc:mongoc_collection_get_write_concern[3] +

src/libmongoc.symbols

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mongoc_collection_drop
2929
mongoc_collection_drop_index
3030
mongoc_collection_ensure_index
3131
mongoc_collection_find
32+
mongoc_collection_get_last_error
3233
mongoc_collection_get_name
3334
mongoc_collection_get_read_prefs
3435
mongoc_collection_get_write_concern

src/mongoc/mongoc-client-private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ bool _mongoc_client_recv (mongoc_client_t *cli
7474
mongoc_buffer_t *buffer,
7575
uint32_t hint,
7676
bson_error_t *error);
77-
bool _mongoc_client_recv_gle (mongoc_client_t *client,
77+
bool _mongoc_client_recv_gle (mongoc_collection_t *collection,
7878
uint32_t hint,
7979
bson_error_t *error);
8080
uint32_t _mongoc_client_stamp (mongoc_client_t *client,

src/mongoc/mongoc-client.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,11 @@ _bson_to_error (const bson_t *b,
554554
*/
555555

556556
bool
557-
_mongoc_client_recv_gle (mongoc_client_t *client,
557+
_mongoc_client_recv_gle (mongoc_collection_t *collection,
558558
uint32_t hint,
559559
bson_error_t *error)
560560
{
561+
mongoc_client_t *client;
561562
mongoc_buffer_t buffer;
562563
mongoc_rpc_t rpc;
563564
bson_iter_t iter;
@@ -566,6 +567,8 @@ _mongoc_client_recv_gle (mongoc_client_t *client,
566567

567568
ENTRY;
568569

570+
bson_return_val_if_fail (collection, false);
571+
client = collection->client;
569572
bson_return_val_if_fail (client, false);
570573
bson_return_val_if_fail (hint, false);
571574

@@ -594,9 +597,14 @@ _mongoc_client_recv_gle (mongoc_client_t *client,
594597

595598
if (_mongoc_rpc_reply_get_first (&rpc.reply, &b)) {
596599
if (!bson_iter_init_find (&iter, &b, "ok") ||
597-
!BSON_ITER_HOLDS_DOUBLE (&iter) ||
598-
(bson_iter_double (&iter) == 0.0)) {
599-
_bson_to_error (&b, error);
600+
BSON_ITER_HOLDS_DOUBLE (&iter)) {
601+
if (bson_iter_double (&iter) == 0.0) {
602+
_bson_to_error (&b, error);
603+
}
604+
if (collection->gle) {
605+
bson_destroy (collection->gle);
606+
}
607+
collection->gle = bson_copy (&b);
600608
}
601609
bson_destroy (&b);
602610
}

src/mongoc/mongoc-collection-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct _mongoc_collection_t
4040

4141
mongoc_read_prefs_t *read_prefs;
4242
mongoc_write_concern_t *write_concern;
43+
bson_t *gle;
4344
};
4445

4546

src/mongoc/mongoc-collection.c

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ _mongoc_collection_new (mongoc_client_t *client,
9999

100100
_mongoc_buffer_init(&col->buffer, NULL, 0, NULL);
101101

102+
col->gle = NULL;
103+
102104
RETURN(col);
103105
}
104106

@@ -127,6 +129,10 @@ mongoc_collection_destroy (mongoc_collection_t *collection) /* IN */
127129

128130
bson_return_if_fail(collection);
129131

132+
if (collection->gle) {
133+
bson_destroy(collection->gle);
134+
}
135+
130136
_mongoc_buffer_destroy(&collection->buffer);
131137

132138
if (collection->read_prefs) {
@@ -293,6 +299,10 @@ mongoc_collection_find (mongoc_collection_t *collection, /* IN */
293299
read_prefs = collection->read_prefs;
294300
}
295301

302+
if (collection->gle) {
303+
bson_destroy(collection->gle);
304+
}
305+
296306
return _mongoc_cursor_new(collection->client, collection->ns, flags, skip,
297307
limit, batch_size, false, query, fields, read_prefs);
298308
}
@@ -344,6 +354,10 @@ mongoc_collection_command (mongoc_collection_t *collection,
344354
read_prefs = collection->read_prefs;
345355
}
346356

357+
if (collection->gle) {
358+
bson_destroy(collection->gle);
359+
}
360+
347361
return mongoc_client_command (collection->client, collection->db, flags,
348362
skip, limit, batch_size, query, fields, read_prefs);
349363
}
@@ -358,6 +372,10 @@ mongoc_collection_command_simple (mongoc_collection_t *collection,
358372
BSON_ASSERT (collection);
359373
BSON_ASSERT (command);
360374

375+
if (collection->gle) {
376+
bson_destroy(collection->gle);
377+
}
378+
361379
return mongoc_client_command_simple (collection->client, collection->db,
362380
command, read_prefs, reply, error);
363381
}
@@ -722,6 +740,11 @@ _mongoc_collection_insert_bulk_raw (mongoc_collection_t *collection,
722740
bson_init_static (&reply_bson, reply.reply.documents,
723741
reply.reply.documents_len);
724742

743+
if (collection->gle) {
744+
bson_destroy (collection->gle);
745+
}
746+
collection->gle = bson_copy (&reply_bson);
747+
725748
if (bson_iter_init_find (&reply_iter, &reply_bson, "err") &&
726749
BSON_ITER_HOLDS_UTF8 (&reply_iter)) {
727750
errmsg = bson_iter_utf8 (&reply_iter, NULL);
@@ -771,6 +794,7 @@ _mongoc_collection_insert_bulk_raw (mongoc_collection_t *collection,
771794
* not actually inserted on the MongoDB server or cluster.
772795
*
773796
* Side effects:
797+
* @collection->gle is setup, depending on write_concern->w value.
774798
* @error may be set upon failure if non-NULL.
775799
*
776800
*--------------------------------------------------------------------------
@@ -856,6 +880,7 @@ mongoc_collection_insert_bulk (mongoc_collection_t *collection,
856880
* not actually inserted on the MongoDB server or cluster.
857881
*
858882
* Side effects:
883+
* @collection->gle is setup, depending on write_concern->w value.
859884
* @error may be set upon failure if non-NULL.
860885
*
861886
*--------------------------------------------------------------------------
@@ -911,6 +936,7 @@ mongoc_collection_insert (mongoc_collection_t *collection,
911936
* true if successful; otherwise false and @error is set.
912937
*
913938
* Side effects:
939+
* @collection->gle is setup, depending on write_concern->w value.
914940
* @error is setup upon failure.
915941
*
916942
*--------------------------------------------------------------------------
@@ -979,7 +1005,7 @@ mongoc_collection_update (mongoc_collection_t *collection,
9791005
}
9801006

9811007
if (_mongoc_write_concern_has_gle (write_concern)) {
982-
if (!_mongoc_client_recv_gle (collection->client, hint, error)) {
1008+
if (!_mongoc_client_recv_gle (collection, hint, error)) {
9831009
RETURN(false);
9841010
}
9851011
}
@@ -1068,7 +1094,8 @@ mongoc_collection_save (mongoc_collection_t *collection,
10681094
* function may return true even if it failed.
10691095
*
10701096
* Side effects:
1071-
* None.
1097+
* @collection->gle is setup, depending on write_concern->w value.
1098+
* @error is setup upon failure.
10721099
*
10731100
*--------------------------------------------------------------------------
10741101
*/
@@ -1109,7 +1136,7 @@ mongoc_collection_delete (mongoc_collection_t *collection,
11091136
}
11101137

11111138
if (_mongoc_write_concern_has_gle(write_concern)) {
1112-
if (!_mongoc_client_recv_gle(collection->client, hint, error)) {
1139+
if (!_mongoc_client_recv_gle (collection, hint, error)) {
11131140
return false;
11141141
}
11151142
}
@@ -1256,3 +1283,30 @@ mongoc_collection_get_name (mongoc_collection_t *collection)
12561283

12571284
return collection->collection;
12581285
}
1286+
1287+
1288+
/*
1289+
*--------------------------------------------------------------------------
1290+
*
1291+
* mongoc_collection_get_last_error --
1292+
*
1293+
* Returns getLastError document, according to write_concern on last
1294+
* executed command for current collection instance.
1295+
*
1296+
* Returns:
1297+
* A newly allocated bson_t getLastError document, that *must* be
1298+
* destroyed with bson_destroy(). Or NULL if no getLastError present.
1299+
*
1300+
* Side effects:
1301+
* None.
1302+
*
1303+
*--------------------------------------------------------------------------
1304+
*/
1305+
1306+
bson_t *
1307+
mongoc_collection_get_last_error (const mongoc_collection_t *collection)
1308+
{
1309+
bson_return_val_if_fail (collection, NULL);
1310+
1311+
return collection->gle ? bson_copy (collection->gle) : NULL;
1312+
}

src/mongoc/mongoc-collection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ const mongoc_write_concern_t *mongoc_collection_get_write_concern (const mong
114114
void mongoc_collection_set_write_concern (mongoc_collection_t *collection,
115115
const mongoc_write_concern_t *write_concern);
116116
const char *mongoc_collection_get_name (mongoc_collection_t *collection);
117+
bson_t *mongoc_collection_get_last_error (const mongoc_collection_t *collection);
117118
char *mongoc_collection_keys_to_index_string (const bson_t *keys);
118119

119120

0 commit comments

Comments
 (0)