Skip to content

Commit b04fd67

Browse files
committed
collection: followup api cleanup for mongoc_collection_get_last_error().
This patches changes the semantics a bit of Maga's patch for mongoc_collection_get_last_error(). It uses the new bson_clear() macro to clear the value of collection->gle where appropriate. Previously, it was not setting the value to NULL after destroying. Additionally, we remove the concept of collection from the mongoc_client_t code and simply use an output parameter for the document. This may be useful for situations like gridfs or database if we want to directly communicate with the client layer. The return value from mongoc_collection_get_last_error() is now a const, and we rely on the caller to copy. This let's us avoid extra copies unless they are necessary by the caller.
1 parent 2f697ce commit b04fd67

File tree

5 files changed

+55
-45
lines changed

5 files changed

+55
-45
lines changed

doc/mongoc_collection_get_last_error.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mongoc_collection_get_last_error(3)
2-
=============================
2+
===================================
33

44

55
NAME
@@ -11,7 +11,7 @@ SYNOPSIS
1111
--------
1212
[source,c]
1313
-----------------------
14-
bson_t *
14+
const bson_t *
1515
mongoc_collection_get_last_error (const mongoc_collection_t *collection);
1616
-----------------------
1717

@@ -36,11 +36,11 @@ _mongoc_collection_ensure_index()_
3636

3737
RETURN VALUE
3838
------------
39-
A newly allocated bson_t getLastError document, that *must* be destroyed
40-
with _bson_destroy()_. Or NULL if no getLastError present.
39+
A pointer to a bson_t that must not be modified or freed. This document is not
40+
guaranteed to be valid between calls into mongoc_collection_t. Therefore, you
41+
must call bson_copy() if you want to hold on to it between API calls.
4142

4243

4344
AUTHORS
4445
-------
45-
46-
This page was written by MongoDB Inc.
46+
This page was written by MongoDB, Inc.

src/mongoc/mongoc-client-private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ 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_collection_t *collection,
77+
bool _mongoc_client_recv_gle (mongoc_client_t *client,
7878
uint32_t hint,
79+
bson_t **gle_doc,
7980
bson_error_t *error);
8081
uint32_t _mongoc_client_stamp (mongoc_client_t *client,
8182
uint32_t node);

src/mongoc/mongoc-client.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -543,22 +543,27 @@ _bson_to_error (const bson_t *b,
543543
*
544544
* If the operation was successful, true is returned.
545545
*
546+
* if @gle_doc is not NULL, then the actual response document for
547+
* the gle command will be stored as an out parameter. The caller
548+
* is responsible for freeing it in this case.
549+
*
546550
* Returns:
547551
* true if getlasterror was success; otherwise false and @error
548552
* is set.
549553
*
550554
* Side effects:
551555
* @error if return value is false.
556+
* @gle_doc will be set if non NULL and a reply was received.
552557
*
553558
*--------------------------------------------------------------------------
554559
*/
555560

556561
bool
557-
_mongoc_client_recv_gle (mongoc_collection_t *collection,
558-
uint32_t hint,
559-
bson_error_t *error)
562+
_mongoc_client_recv_gle (mongoc_client_t *client,
563+
uint32_t hint,
564+
bson_t **gle_doc,
565+
bson_error_t *error)
560566
{
561-
mongoc_client_t *client;
562567
mongoc_buffer_t buffer;
563568
mongoc_rpc_t rpc;
564569
bson_iter_t iter;
@@ -567,11 +572,13 @@ _mongoc_client_recv_gle (mongoc_collection_t *collection,
567572

568573
ENTRY;
569574

570-
bson_return_val_if_fail (collection, false);
571-
client = collection->client;
572575
bson_return_val_if_fail (client, false);
573576
bson_return_val_if_fail (hint, false);
574577

578+
if (gle_doc) {
579+
*gle_doc = NULL;
580+
}
581+
575582
_mongoc_buffer_init (&buffer, NULL, 0, NULL);
576583

577584
if (!_mongoc_cluster_try_recv (&client->cluster, &rpc, &buffer,
@@ -587,25 +594,24 @@ _mongoc_client_recv_gle (mongoc_collection_t *collection,
587594
GOTO (cleanup);
588595
}
589596

590-
if ((rpc.reply.flags & MONGOC_REPLY_QUERY_FAILURE)) {
591-
if (_mongoc_rpc_reply_get_first (&rpc.reply, &b)) {
597+
if (_mongoc_rpc_reply_get_first (&rpc.reply, &b)) {
598+
if (gle_doc) {
599+
*gle_doc = bson_copy (&b);
600+
}
601+
602+
if ((rpc.reply.flags & MONGOC_REPLY_QUERY_FAILURE)) {
592603
_bson_to_error (&b, error);
593604
bson_destroy (&b);
594605
GOTO (cleanup);
595606
}
596-
}
597607

598-
if (_mongoc_rpc_reply_get_first (&rpc.reply, &b)) {
599608
if (!bson_iter_init_find (&iter, &b, "ok") ||
600609
BSON_ITER_HOLDS_DOUBLE (&iter)) {
601610
if (bson_iter_double (&iter) == 0.0) {
602611
_bson_to_error (&b, error);
603612
}
604-
if (collection->gle) {
605-
bson_destroy (collection->gle);
606-
}
607-
collection->gle = bson_copy (&b);
608613
}
614+
609615
bson_destroy (&b);
610616
}
611617

src/mongoc/mongoc-collection.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ mongoc_collection_destroy (mongoc_collection_t *collection) /* IN */
129129

130130
bson_return_if_fail(collection);
131131

132-
if (collection->gle) {
133-
bson_destroy(collection->gle);
134-
}
132+
bson_clear (&collection->gle);
135133

136134
_mongoc_buffer_destroy(&collection->buffer);
137135

@@ -295,14 +293,12 @@ mongoc_collection_find (mongoc_collection_t *collection, /* IN */
295293
bson_return_val_if_fail(collection, NULL);
296294
bson_return_val_if_fail(query, NULL);
297295

296+
bson_clear (&collection->gle);
297+
298298
if (!read_prefs) {
299299
read_prefs = collection->read_prefs;
300300
}
301301

302-
if (collection->gle) {
303-
bson_destroy(collection->gle);
304-
}
305-
306302
return _mongoc_cursor_new(collection->client, collection->ns, flags, skip,
307303
limit, batch_size, false, query, fields, read_prefs);
308304
}
@@ -354,9 +350,7 @@ mongoc_collection_command (mongoc_collection_t *collection,
354350
read_prefs = collection->read_prefs;
355351
}
356352

357-
if (collection->gle) {
358-
bson_destroy(collection->gle);
359-
}
353+
bson_clear (&collection->gle);
360354

361355
return mongoc_client_command (collection->client, collection->db, flags,
362356
skip, limit, batch_size, query, fields, read_prefs);
@@ -372,9 +366,7 @@ mongoc_collection_command_simple (mongoc_collection_t *collection,
372366
BSON_ASSERT (collection);
373367
BSON_ASSERT (command);
374368

375-
if (collection->gle) {
376-
bson_destroy(collection->gle);
377-
}
369+
bson_clear (&collection->gle);
378370

379371
return mongoc_client_command_simple (collection->client, collection->db,
380372
command, read_prefs, reply, error);
@@ -680,6 +672,8 @@ _mongoc_collection_insert_bulk_raw (mongoc_collection_t *collection,
680672
BSON_ASSERT(documents);
681673
BSON_ASSERT(n_documents);
682674

675+
bson_clear (&collection->gle);
676+
683677
if (!write_concern) {
684678
write_concern = collection->write_concern;
685679
}
@@ -740,9 +734,6 @@ _mongoc_collection_insert_bulk_raw (mongoc_collection_t *collection,
740734
bson_init_static (&reply_bson, reply.reply.documents,
741735
reply.reply.documents_len);
742736

743-
if (collection->gle) {
744-
bson_destroy (collection->gle);
745-
}
746737
collection->gle = bson_copy (&reply_bson);
747738

748739
if (bson_iter_init_find (&reply_iter, &reply_bson, "err") &&
@@ -818,6 +809,8 @@ mongoc_collection_insert_bulk (mongoc_collection_t *collection,
818809
BSON_ASSERT (documents);
819810
BSON_ASSERT (n_documents);
820811

812+
bson_clear (&collection->gle);
813+
821814
if (!(flags & MONGOC_INSERT_NO_VALIDATE)) {
822815
for (i = 0; i < n_documents; i++) {
823816
if (!bson_validate (documents [i],
@@ -901,6 +894,8 @@ mongoc_collection_insert (mongoc_collection_t *collection,
901894
bson_return_val_if_fail (collection, false);
902895
bson_return_val_if_fail (document, false);
903896

897+
bson_clear (&collection->gle);
898+
904899
if (!bson_iter_init_find (&iter, document, "_id")) {
905900
bson_oid_init (&oid, NULL);
906901
bson_append_oid (&copy, "_id", 3, &oid);
@@ -961,6 +956,8 @@ mongoc_collection_update (mongoc_collection_t *collection,
961956
bson_return_val_if_fail(selector, false);
962957
bson_return_val_if_fail(update, false);
963958

959+
bson_clear (&collection->gle);
960+
964961
if (!(flags & MONGOC_UPDATE_NO_VALIDATE) &&
965962
bson_iter_init (&iter, update) &&
966963
bson_iter_next (&iter) &&
@@ -1005,7 +1002,8 @@ mongoc_collection_update (mongoc_collection_t *collection,
10051002
}
10061003

10071004
if (_mongoc_write_concern_has_gle (write_concern)) {
1008-
if (!_mongoc_client_recv_gle (collection, hint, error)) {
1005+
if (!_mongoc_client_recv_gle (collection->client, hint,
1006+
&collection->gle, error)) {
10091007
RETURN(false);
10101008
}
10111009
}
@@ -1113,6 +1111,8 @@ mongoc_collection_delete (mongoc_collection_t *collection,
11131111
bson_return_val_if_fail(collection, false);
11141112
bson_return_val_if_fail(selector, false);
11151113

1114+
bson_clear (&collection->gle);
1115+
11161116
if (!write_concern) {
11171117
write_concern = collection->write_concern;
11181118
}
@@ -1136,7 +1136,8 @@ mongoc_collection_delete (mongoc_collection_t *collection,
11361136
}
11371137

11381138
if (_mongoc_write_concern_has_gle(write_concern)) {
1139-
if (!_mongoc_client_recv_gle (collection, hint, error)) {
1139+
if (!_mongoc_client_recv_gle (collection->client, hint,
1140+
&collection->gle, error)) {
11401141
return false;
11411142
}
11421143
}
@@ -1294,19 +1295,21 @@ mongoc_collection_get_name (mongoc_collection_t *collection)
12941295
* executed command for current collection instance.
12951296
*
12961297
* Returns:
1297-
* A newly allocated bson_t getLastError document, that *must* be
1298-
* destroyed with bson_destroy(). Or NULL if no getLastError present.
1298+
* NULL or a bson_t that should not be modified or freed. This value
1299+
* is not guaranteed to be persistent between calls into the
1300+
* mongoc_collection_t instance, and therefore must be copied if
1301+
* you would like to keep it around.
12991302
*
13001303
* Side effects:
13011304
* None.
13021305
*
13031306
*--------------------------------------------------------------------------
13041307
*/
13051308

1306-
bson_t *
1307-
mongoc_collection_get_last_error (const mongoc_collection_t *collection)
1309+
const bson_t *
1310+
mongoc_collection_get_last_error (const mongoc_collection_t *collection) /* IN */
13081311
{
13091312
bson_return_val_if_fail (collection, NULL);
13101313

1311-
return collection->gle ? bson_copy (collection->gle) : NULL;
1314+
return collection->gle;
13121315
}

src/mongoc/mongoc-collection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +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);
117+
const bson_t *mongoc_collection_get_last_error (const mongoc_collection_t *collection);
118118
char *mongoc_collection_keys_to_index_string (const bson_t *keys);
119119

120120

0 commit comments

Comments
 (0)