Skip to content

Commit 7e2c32a

Browse files
committed
CDRIVER-407 only include GLE in write concern when needed
1 parent 2b61688 commit 7e2c32a

File tree

7 files changed

+99
-42
lines changed

7 files changed

+99
-42
lines changed

src/mongoc/mongoc-cluster.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,7 +2565,7 @@ _mongoc_cluster_sendv (mongoc_cluster_t *cluster,
25652565
gle.query.collection = cmdname;
25662566
gle.query.skip = 0;
25672567
gle.query.n_return = 1;
2568-
b = _mongoc_write_concern_freeze((void*)write_concern);
2568+
b = _mongoc_write_concern_get_gle((void*)write_concern);
25692569
gle.query.query = bson_get_data(b);
25702570
gle.query.fields = NULL;
25712571
_mongoc_rpc_gather(&gle, &cluster->iov);
@@ -2704,7 +2704,7 @@ _mongoc_cluster_try_sendv (mongoc_cluster_t *cluster,
27042704
gle.query.skip = 0;
27052705
gle.query.n_return = 1;
27062706

2707-
b = _mongoc_write_concern_freeze ((void *)write_concern);
2707+
b = _mongoc_write_concern_get_gle ((void *)write_concern);
27082708

27092709
gle.query.query = bson_get_data (b);
27102710
gle.query.fields = NULL;

src/mongoc/mongoc-write-command.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
(((n)->min_wire_version <= 2) && ((n)->max_wire_version >= 2))
3838
#define WRITE_CONCERN_DOC(wc) \
3939
(wc && _mongoc_write_concern_needs_gle ((wc))) ? \
40-
(_mongoc_write_concern_freeze((mongoc_write_concern_t*)(wc))) : \
40+
(_mongoc_write_concern_get_bson((mongoc_write_concern_t*)(wc))) : \
4141
(&gEmptyWriteConcern)
4242

4343

src/mongoc/mongoc-write-concern-private.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ struct _mongoc_write_concern_t
3939
char *wtag;
4040
bool frozen;
4141
bson_t compiled;
42+
bson_t compiled_gle;
4243
};
4344

4445

45-
const bson_t *_mongoc_write_concern_freeze (mongoc_write_concern_t *write_concern);
46-
46+
const bson_t *_mongoc_write_concern_get_gle (mongoc_write_concern_t *write_cocnern);
47+
const bson_t *_mongoc_write_concern_get_bson (mongoc_write_concern_t *write_concern);
4748
bool _mongoc_write_concern_needs_gle (const mongoc_write_concern_t *write_concern);
4849

4950
BSON_END_DECLS

src/mongoc/mongoc-write-concern.c

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ _mongoc_write_concern_warn_frozen (mongoc_write_concern_t *write_concern)
3030
return write_concern->frozen;
3131
}
3232

33+
static void
34+
_mongoc_write_concern_freeze (mongoc_write_concern_t *write_concern);
35+
3336

3437
/**
3538
* mongoc_write_concern_new:
@@ -82,6 +85,7 @@ mongoc_write_concern_destroy (mongoc_write_concern_t *write_concern)
8285
if (write_concern) {
8386
if (write_concern->compiled.len) {
8487
bson_destroy (&write_concern->compiled);
88+
bson_destroy (&write_concern->compiled_gle);
8589
}
8690

8791
bson_free (write_concern->wtag);
@@ -261,62 +265,104 @@ mongoc_write_concern_set_wtag (mongoc_write_concern_t *write_concern,
261265
}
262266
}
263267

268+
/**
269+
* mongoc_write_concern_get_bson:
270+
* @write_concern: A mongoc_write_concern_t.
271+
*
272+
* This is an internal function.
273+
*
274+
* Freeze the write concern if necessary and retrieve the encoded bson_t
275+
* representing the write concern.
276+
*
277+
* You may not modify the write concern further after calling this function.
278+
*
279+
* Returns: A bson_t that should not be modified or freed as it is owned by
280+
* the mongoc_write_concern_t instance.
281+
*/
282+
const bson_t *
283+
_mongoc_write_concern_get_bson (mongoc_write_concern_t *write_concern) {
284+
if (!write_concern->frozen) {
285+
_mongoc_write_concern_freeze(write_concern);
286+
}
287+
288+
return &write_concern->compiled;
289+
}
264290

265291
/**
266-
* mongoc_write_concern_freeze:
292+
* mongoc_write_concern_get_gle:
267293
* @write_concern: A mongoc_write_concern_t.
268294
*
269295
* This is an internal function.
270296
*
271-
* Freeze the write concern if necessary and compile the getlasterror command
272-
* associated with it.
297+
* Freeze the write concern if necessary and retrieve the encoded bson_t
298+
* representing the write concern as a get last error command.
273299
*
274300
* You may not modify the write concern further after calling this function.
275301
*
276302
* Returns: A bson_t that should not be modified or freed as it is owned by
277303
* the mongoc_write_concern_t instance.
278304
*/
279305
const bson_t *
306+
_mongoc_write_concern_get_gle (mongoc_write_concern_t *write_concern) {
307+
if (!write_concern->frozen) {
308+
_mongoc_write_concern_freeze(write_concern);
309+
}
310+
311+
return &write_concern->compiled_gle;
312+
}
313+
314+
/**
315+
* mongoc_write_concern_freeze:
316+
* @write_concern: A mongoc_write_concern_t.
317+
*
318+
* This is an internal function.
319+
*
320+
* Freeze the write concern if necessary and encode it into a bson_ts which
321+
* represent the raw bson form and the get last error command form.
322+
*
323+
* You may not modify the write concern further after calling this function.
324+
*/
325+
static void
280326
_mongoc_write_concern_freeze (mongoc_write_concern_t *write_concern)
281327
{
282-
bson_t *b;
328+
bson_t *compiled;
329+
bson_t *compiled_gle;
283330

284331
bson_return_val_if_fail(write_concern, NULL);
285332

286-
b = &write_concern->compiled;
333+
compiled = &write_concern->compiled;
334+
compiled_gle = &write_concern->compiled_gle;
287335

288-
if (!write_concern->frozen) {
289-
write_concern->frozen = true;
290-
291-
bson_init (b);
336+
write_concern->frozen = true;
292337

293-
BSON_APPEND_INT32 (b, "getlasterror", 1);
338+
bson_init (compiled);
339+
bson_init (compiled_gle);
294340

295-
if (write_concern->w == MONGOC_WRITE_CONCERN_W_TAG) {
296-
BSON_ASSERT (write_concern->wtag);
297-
BSON_APPEND_UTF8 (b, "w", write_concern->wtag);
298-
} else if (write_concern->w == MONGOC_WRITE_CONCERN_W_MAJORITY) {
299-
BSON_APPEND_UTF8 (b, "w", "majority");
300-
} else if (write_concern->w == MONGOC_WRITE_CONCERN_W_DEFAULT) {
301-
/* Do Nothing */
302-
} else if (write_concern->w > 0) {
303-
BSON_APPEND_INT32 (b, "w", write_concern->w);
304-
}
341+
if (write_concern->w == MONGOC_WRITE_CONCERN_W_TAG) {
342+
BSON_ASSERT (write_concern->wtag);
343+
BSON_APPEND_UTF8 (compiled, "w", write_concern->wtag);
344+
} else if (write_concern->w == MONGOC_WRITE_CONCERN_W_MAJORITY) {
345+
BSON_APPEND_UTF8 (compiled, "w", "majority");
346+
} else if (write_concern->w == MONGOC_WRITE_CONCERN_W_DEFAULT) {
347+
/* Do Nothing */
348+
} else if (write_concern->w > 0) {
349+
BSON_APPEND_INT32 (compiled, "w", write_concern->w);
350+
}
305351

306-
if (write_concern->fsync_) {
307-
bson_append_bool(b, "fsync", 5, true);
308-
}
352+
if (write_concern->fsync_) {
353+
bson_append_bool(compiled, "fsync", 5, true);
354+
}
309355

310-
if (write_concern->journal) {
311-
bson_append_bool(b, "j", 1, true);
312-
}
356+
if (write_concern->journal) {
357+
bson_append_bool(compiled, "j", 1, true);
358+
}
313359

314-
if (write_concern->wtimeout) {
315-
bson_append_int32(b, "wtimeout", 8, write_concern->wtimeout);
316-
}
360+
if (write_concern->wtimeout) {
361+
bson_append_int32(compiled, "wtimeout", 8, write_concern->wtimeout);
317362
}
318363

319-
return b;
364+
BSON_APPEND_INT32 (compiled_gle, "getlasterror", 1);
365+
bson_concat (compiled_gle, compiled);
320366
}
321367

322368

tests/test-libmongoc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ main (int argc,
145145
test_rpc_install (&suite);
146146
test_stream_install (&suite);
147147
test_uri_install (&suite);
148+
test_write_concern_install (&suite);
148149
#ifdef MONGOC_ENABLE_SSL
149150
test_x509_install (&suite);
150151
test_stream_tls_install (&suite);

tests/test-mongoc-write-concern.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ static void
88
test_write_concern_basic (void)
99
{
1010
mongoc_write_concern_t *write_concern;
11-
const bson_t *b;
11+
const bson_t *gle;
12+
const bson_t *bson;
1213
bson_iter_t iter;
1314

1415
write_concern = mongoc_write_concern_new();
@@ -55,11 +56,19 @@ test_write_concern_basic (void)
5556
*/
5657
mongoc_write_concern_set_fsync(write_concern, true);
5758
mongoc_write_concern_set_journal(write_concern, true);
58-
b = _mongoc_write_concern_freeze(write_concern);
59-
ASSERT(bson_iter_init_find(&iter, b, "fsync") && BSON_ITER_HOLDS_BOOL(&iter) && bson_iter_bool(&iter));
60-
ASSERT(bson_iter_init_find(&iter, b, "j") && BSON_ITER_HOLDS_BOOL(&iter) && bson_iter_bool(&iter));
61-
ASSERT(bson_iter_init_find(&iter, b, "w") && BSON_ITER_HOLDS_INT32(&iter) && bson_iter_int32(&iter) == 3);
62-
ASSERT(b);
59+
gle = _mongoc_write_concern_get_gle(write_concern);
60+
ASSERT(bson_iter_init_find(&iter, gle, "getlasterror") && BSON_ITER_HOLDS_INT32(&iter) && bson_iter_int32(&iter) == 1);
61+
ASSERT(bson_iter_init_find(&iter, gle, "fsync") && BSON_ITER_HOLDS_BOOL(&iter) && bson_iter_bool(&iter));
62+
ASSERT(bson_iter_init_find(&iter, gle, "j") && BSON_ITER_HOLDS_BOOL(&iter) && bson_iter_bool(&iter));
63+
ASSERT(bson_iter_init_find(&iter, gle, "w") && BSON_ITER_HOLDS_INT32(&iter) && bson_iter_int32(&iter) == 3);
64+
ASSERT(gle);
65+
66+
bson = _mongoc_write_concern_get_bson(write_concern);
67+
ASSERT(!bson_iter_init_find(&iter, bson, "getlasterror"));
68+
ASSERT(bson_iter_init_find(&iter, bson, "fsync") && BSON_ITER_HOLDS_BOOL(&iter) && bson_iter_bool(&iter));
69+
ASSERT(bson_iter_init_find(&iter, bson, "j") && BSON_ITER_HOLDS_BOOL(&iter) && bson_iter_bool(&iter));
70+
ASSERT(bson_iter_init_find(&iter, bson, "w") && BSON_ITER_HOLDS_INT32(&iter) && bson_iter_int32(&iter) == 3);
71+
ASSERT(bson);
6372

6473
mongoc_write_concern_destroy(write_concern);
6574
}

tests/test-replica-set.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ insert_test_docs (mongoc_collection_t *collection)
3838
const bson_t *wc;
3939
char *str;
4040

41-
wc = _mongoc_write_concern_freeze(write_concern);
41+
wc = _mongoc_write_concern_get_gle(write_concern);
4242
str = bson_as_json(wc, NULL);
4343
fprintf(stderr, "Write Concern: %s\n", str);
4444
bson_free(str);

0 commit comments

Comments
 (0)