Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/cmake/libmongoc-ssl.def
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mongoc_collection_drop
mongoc_collection_drop_index
mongoc_collection_ensure_index
mongoc_collection_find
mongoc_collection_get_last_error
mongoc_collection_get_name
mongoc_collection_get_read_prefs
mongoc_collection_get_write_concern
Expand Down
1 change: 1 addition & 0 deletions build/cmake/libmongoc.def
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mongoc_collection_drop
mongoc_collection_drop_index
mongoc_collection_ensure_index
mongoc_collection_find
mongoc_collection_get_last_error
mongoc_collection_get_name
mongoc_collection_get_read_prefs
mongoc_collection_get_write_concern
Expand Down
46 changes: 46 additions & 0 deletions doc/mongoc_collection_get_last_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
mongoc_collection_get_last_error(3)
=============================


NAME
----
mongoc_collection_get_last_error - Returns getLastError document.


SYNOPSIS
--------
[source,c]
-----------------------
bson_t *
mongoc_collection_get_last_error (const mongoc_collection_t *collection);
-----------------------


DESCRIPTION
-----------
The _mongoc_collection_get_last_error()_ function shall return getLastError
document, according to write_concern on last executed command for current
collection instance.

write_concern must be at least MONGOC_WRITE_CONCERN_W_DEFAULT (1) in
last command execution for getLastError to be available.

Last executed command must be any of:
_mongoc_collection_insert()_
_mongoc_collection_insert_bulk()_
_mongoc_collection_update()_
_mongoc_collection_delete()_
_mongoc_collection_save()_
_mongoc_collection_ensure_index()_


RETURN VALUE
------------
A newly allocated bson_t getLastError document, that *must* be destroyed
with _bson_destroy()_. Or NULL if no getLastError present.


AUTHORS
-------

This page was written by MongoDB Inc.
1 change: 1 addition & 0 deletions doc/mongoc_symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ linkmongoc:mongoc_collection_drop[3] +
linkmongoc:mongoc_collection_drop_index[3] +
linkmongoc:mongoc_collection_ensure_index[3] +
linkmongoc:mongoc_collection_find[3] +
linkmongoc:mongoc_collection_get_last_error[3] +
linkmongoc:mongoc_collection_get_name[3] +
linkmongoc:mongoc_collection_get_read_prefs[3] +
linkmongoc:mongoc_collection_get_write_concern[3] +
Expand Down
1 change: 1 addition & 0 deletions src/libmongoc.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mongoc_collection_drop
mongoc_collection_drop_index
mongoc_collection_ensure_index
mongoc_collection_find
mongoc_collection_get_last_error
mongoc_collection_get_name
mongoc_collection_get_read_prefs
mongoc_collection_get_write_concern
Expand Down
2 changes: 1 addition & 1 deletion src/mongoc/mongoc-client-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ bool _mongoc_client_recv (mongoc_client_t *cli
mongoc_buffer_t *buffer,
uint32_t hint,
bson_error_t *error);
bool _mongoc_client_recv_gle (mongoc_client_t *client,
bool _mongoc_client_recv_gle (mongoc_collection_t *collection,
uint32_t hint,
bson_error_t *error);
uint32_t _mongoc_client_stamp (mongoc_client_t *client,
Expand Down
16 changes: 12 additions & 4 deletions src/mongoc/mongoc-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,11 @@ _bson_to_error (const bson_t *b,
*/

bool
_mongoc_client_recv_gle (mongoc_client_t *client,
_mongoc_client_recv_gle (mongoc_collection_t *collection,
uint32_t hint,
bson_error_t *error)
{
mongoc_client_t *client;
mongoc_buffer_t buffer;
mongoc_rpc_t rpc;
bson_iter_t iter;
Expand All @@ -566,6 +567,8 @@ _mongoc_client_recv_gle (mongoc_client_t *client,

ENTRY;

bson_return_val_if_fail (collection, false);
client = collection->client;
bson_return_val_if_fail (client, false);
bson_return_val_if_fail (hint, false);

Expand Down Expand Up @@ -594,9 +597,14 @@ _mongoc_client_recv_gle (mongoc_client_t *client,

if (_mongoc_rpc_reply_get_first (&rpc.reply, &b)) {
if (!bson_iter_init_find (&iter, &b, "ok") ||
!BSON_ITER_HOLDS_DOUBLE (&iter) ||
(bson_iter_double (&iter) == 0.0)) {
_bson_to_error (&b, error);
BSON_ITER_HOLDS_DOUBLE (&iter)) {
if (bson_iter_double (&iter) == 0.0) {
_bson_to_error (&b, error);
}
if (collection->gle) {
bson_destroy (collection->gle);
}
collection->gle = bson_copy (&b);
}
bson_destroy (&b);
}
Expand Down
1 change: 1 addition & 0 deletions src/mongoc/mongoc-collection-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct _mongoc_collection_t

mongoc_read_prefs_t *read_prefs;
mongoc_write_concern_t *write_concern;
bson_t *gle;
};


Expand Down
60 changes: 57 additions & 3 deletions src/mongoc/mongoc-collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ _mongoc_collection_new (mongoc_client_t *client,

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

col->gle = NULL;

RETURN(col);
}

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

bson_return_if_fail(collection);

if (collection->gle) {
bson_destroy(collection->gle);
}

_mongoc_buffer_destroy(&collection->buffer);

if (collection->read_prefs) {
Expand Down Expand Up @@ -293,6 +299,10 @@ mongoc_collection_find (mongoc_collection_t *collection, /* IN */
read_prefs = collection->read_prefs;
}

if (collection->gle) {
bson_destroy(collection->gle);
}

return _mongoc_cursor_new(collection->client, collection->ns, flags, skip,
limit, batch_size, false, query, fields, read_prefs);
}
Expand Down Expand Up @@ -344,6 +354,10 @@ mongoc_collection_command (mongoc_collection_t *collection,
read_prefs = collection->read_prefs;
}

if (collection->gle) {
bson_destroy(collection->gle);
}

return mongoc_client_command (collection->client, collection->db, flags,
skip, limit, batch_size, query, fields, read_prefs);
}
Expand All @@ -358,6 +372,10 @@ mongoc_collection_command_simple (mongoc_collection_t *collection,
BSON_ASSERT (collection);
BSON_ASSERT (command);

if (collection->gle) {
bson_destroy(collection->gle);
}

return mongoc_client_command_simple (collection->client, collection->db,
command, read_prefs, reply, error);
}
Expand Down Expand Up @@ -722,6 +740,11 @@ _mongoc_collection_insert_bulk_raw (mongoc_collection_t *collection,
bson_init_static (&reply_bson, reply.reply.documents,
reply.reply.documents_len);

if (collection->gle) {
bson_destroy (collection->gle);
}
collection->gle = bson_copy (&reply_bson);

if (bson_iter_init_find (&reply_iter, &reply_bson, "err") &&
BSON_ITER_HOLDS_UTF8 (&reply_iter)) {
errmsg = bson_iter_utf8 (&reply_iter, NULL);
Expand Down Expand Up @@ -771,6 +794,7 @@ _mongoc_collection_insert_bulk_raw (mongoc_collection_t *collection,
* not actually inserted on the MongoDB server or cluster.
*
* Side effects:
* @collection->gle is setup, depending on write_concern->w value.
* @error may be set upon failure if non-NULL.
*
*--------------------------------------------------------------------------
Expand Down Expand Up @@ -856,6 +880,7 @@ mongoc_collection_insert_bulk (mongoc_collection_t *collection,
* not actually inserted on the MongoDB server or cluster.
*
* Side effects:
* @collection->gle is setup, depending on write_concern->w value.
* @error may be set upon failure if non-NULL.
*
*--------------------------------------------------------------------------
Expand Down Expand Up @@ -911,6 +936,7 @@ mongoc_collection_insert (mongoc_collection_t *collection,
* true if successful; otherwise false and @error is set.
*
* Side effects:
* @collection->gle is setup, depending on write_concern->w value.
* @error is setup upon failure.
*
*--------------------------------------------------------------------------
Expand Down Expand Up @@ -979,7 +1005,7 @@ mongoc_collection_update (mongoc_collection_t *collection,
}

if (_mongoc_write_concern_has_gle (write_concern)) {
if (!_mongoc_client_recv_gle (collection->client, hint, error)) {
if (!_mongoc_client_recv_gle (collection, hint, error)) {
RETURN(false);
}
}
Expand Down Expand Up @@ -1068,7 +1094,8 @@ mongoc_collection_save (mongoc_collection_t *collection,
* function may return true even if it failed.
*
* Side effects:
* None.
* @collection->gle is setup, depending on write_concern->w value.
* @error is setup upon failure.
*
*--------------------------------------------------------------------------
*/
Expand Down Expand Up @@ -1109,7 +1136,7 @@ mongoc_collection_delete (mongoc_collection_t *collection,
}

if (_mongoc_write_concern_has_gle(write_concern)) {
if (!_mongoc_client_recv_gle(collection->client, hint, error)) {
if (!_mongoc_client_recv_gle (collection, hint, error)) {
return false;
}
}
Expand Down Expand Up @@ -1256,3 +1283,30 @@ mongoc_collection_get_name (mongoc_collection_t *collection)

return collection->collection;
}


/*
*--------------------------------------------------------------------------
*
* mongoc_collection_get_last_error --
*
* Returns getLastError document, according to write_concern on last
* executed command for current collection instance.
*
* Returns:
* A newly allocated bson_t getLastError document, that *must* be
* destroyed with bson_destroy(). Or NULL if no getLastError present.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/

bson_t *
mongoc_collection_get_last_error (const mongoc_collection_t *collection)
{
bson_return_val_if_fail (collection, NULL);

return collection->gle ? bson_copy (collection->gle) : NULL;
}
1 change: 1 addition & 0 deletions src/mongoc/mongoc-collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const mongoc_write_concern_t *mongoc_collection_get_write_concern (const mong
void mongoc_collection_set_write_concern (mongoc_collection_t *collection,
const mongoc_write_concern_t *write_concern);
const char *mongoc_collection_get_name (mongoc_collection_t *collection);
bson_t *mongoc_collection_get_last_error (const mongoc_collection_t *collection);
char *mongoc_collection_keys_to_index_string (const bson_t *keys);


Expand Down