Skip to content

Commit 7f12d67

Browse files
committed
CDRIVER-2796 more bson_iter_overwrite functions
Add bson_iter_overwrite_date_time, bson_iter_overwrite_oid, and bson_iter_overwrite_timestamp.
1 parent 812cd64 commit 7f12d67

File tree

8 files changed

+210
-1
lines changed

8 files changed

+210
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Features:
99
and all bson headers are under bson/. The preferred way of including the
1010
headers are mongoc/mongoc.h and bson/bson.h respectively.
1111
Forwarding headers in the root are provided for backwards compatibility.
12+
* Additional functions bson_iter_overwrite_date_time, bson_iter_overwrite_oid,
13+
and bson_iter_overwrite_timestamp. All fixed-length BSON values can now be
14+
updated in place.
1215

1316
Bug fixes:
1417

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
:man_page: bson_iter_overwrite_date_time
2+
3+
bson_iter_overwrite_date_time()
4+
===============================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
void
12+
bson_iter_overwrite_date_time (bson_iter_t *iter, int64_t value);
13+
14+
Parameters
15+
----------
16+
17+
* ``iter``: A :symbol:`bson_iter_t`.
18+
* ``value``: The date and time as specified in milliseconds since the UNIX epoch.
19+
20+
Description
21+
-----------
22+
23+
The ``bson_iter_overwrite_date_time()`` function shall overwrite the contents of a BSON_TYPE_DATE_TIME element in place.
24+
25+
This may only be done when the underlying bson document allows mutation.
26+
27+
It is a programming error to call this function when ``iter`` is not observing an element of type BSON_TYPE_DATE_TIME.
28+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
:man_page: bson_iter_overwrite_oid
2+
3+
bson_iter_overwrite_oid()
4+
=========================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
void
12+
bson_iter_overwrite_oid (bson_iter_t *iter, const bson_oid_t *value);
13+
14+
Parameters
15+
----------
16+
17+
* ``iter``: A :symbol:`bson_iter_t`.
18+
* ``oid``: A :symbol:`bson_oid_t`.
19+
20+
Description
21+
-----------
22+
23+
The ``bson_iter_overwrite_oid()`` function shall overwrite the contents of a BSON_TYPE_OID element in place.
24+
25+
This may only be done when the underlying bson document allows mutation.
26+
27+
It is a programming error to call this function when ``iter`` is not observing an element of type BSON_TYPE_OID.
28+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
:man_page: bson_iter_overwrite_timestamp
2+
3+
bson_iter_overwrite_timestamp()
4+
===============================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
void
12+
bson_iter_overwrite_timestamp (bson_iter_t *iter,
13+
uint32_t timestamp,
14+
uint32_t increment);
15+
16+
Parameters
17+
----------
18+
19+
* ``iter``: A :symbol:`bson_iter_t`.
20+
* ``timestamp``: A uint32_t.
21+
* ``increment``: A uint32_t.
22+
23+
Description
24+
-----------
25+
26+
The ``bson_iter_overwrite_timestamp()`` function shall overwrite the contents of a BSON_TYPE_TIMESTAMP element in place.
27+
28+
This may only be done when the underlying bson document allows mutation.
29+
30+
It is a programming error to call this function when ``iter`` is not observing an element of type BSON_TYPE_TIMESTAMP.
31+

src/libbson/doc/bson_iter_t.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,13 @@ The :symbol:`bson_t` *MUST* be valid for the lifetime of the iter and it is an e
115115
bson_iter_offset
116116
bson_iter_oid
117117
bson_iter_overwrite_bool
118+
bson_iter_overwrite_date_time
118119
bson_iter_overwrite_decimal128
119120
bson_iter_overwrite_double
120121
bson_iter_overwrite_int32
121122
bson_iter_overwrite_int64
123+
bson_iter_overwrite_oid
124+
bson_iter_overwrite_timestamp
122125
bson_iter_recurse
123126
bson_iter_regex
124127
bson_iter_symbol

src/libbson/src/bson/bson-iter.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "bson/bson-iter.h"
1919
#include "bson/bson-config.h"
2020
#include "bson/bson-decimal128.h"
21-
21+
#include "bson-types.h"
2222

2323
#define ITER_TYPE(i) ((bson_type_t) * ((i)->raw + (i)->type))
2424

@@ -2185,6 +2185,46 @@ bson_iter_overwrite_bool (bson_iter_t *iter, /* IN */
21852185
}
21862186

21872187

2188+
void
2189+
bson_iter_overwrite_oid (bson_iter_t *iter, const bson_oid_t *value)
2190+
{
2191+
BSON_ASSERT (iter);
2192+
2193+
if (ITER_TYPE (iter) == BSON_TYPE_OID) {
2194+
memcpy (
2195+
(void *) (iter->raw + iter->d1), value->bytes, sizeof (value->bytes));
2196+
}
2197+
}
2198+
2199+
2200+
void
2201+
bson_iter_overwrite_timestamp (bson_iter_t *iter,
2202+
uint32_t timestamp,
2203+
uint32_t increment)
2204+
{
2205+
uint64_t value;
2206+
BSON_ASSERT (iter);
2207+
2208+
if (ITER_TYPE (iter) == BSON_TYPE_TIMESTAMP) {
2209+
value = ((((uint64_t) timestamp) << 32U) | ((uint64_t) increment));
2210+
value = BSON_UINT64_TO_LE (value);
2211+
memcpy ((void *) (iter->raw + iter->d1), &value, sizeof (value));
2212+
}
2213+
}
2214+
2215+
2216+
void
2217+
bson_iter_overwrite_date_time (bson_iter_t *iter, int64_t value)
2218+
{
2219+
BSON_ASSERT (iter);
2220+
2221+
if (ITER_TYPE (iter) == BSON_TYPE_DATE_TIME) {
2222+
value = BSON_UINT64_TO_LE (value);
2223+
memcpy ((void *) (iter->raw + iter->d1), &value, sizeof (value));
2224+
}
2225+
}
2226+
2227+
21882228
/*
21892229
*--------------------------------------------------------------------------
21902230
*

src/libbson/src/bson/bson-iter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,20 @@ BSON_EXPORT (void)
521521
bson_iter_overwrite_bool (bson_iter_t *iter, bool value);
522522

523523

524+
BSON_EXPORT (void)
525+
bson_iter_overwrite_oid (bson_iter_t *iter, const bson_oid_t *value);
526+
527+
528+
BSON_EXPORT (void)
529+
bson_iter_overwrite_timestamp (bson_iter_t *iter,
530+
uint32_t timestamp,
531+
uint32_t increment);
532+
533+
534+
BSON_EXPORT (void)
535+
bson_iter_overwrite_date_time (bson_iter_t *iter, int64_t value);
536+
537+
524538
BSON_EXPORT (bool)
525539
bson_iter_visit_all (bson_iter_t *iter,
526540
const bson_visitor_t *visitor,

src/libbson/tests/test-iter.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,60 @@ test_bson_iter_overwrite_bool (void)
549549
}
550550

551551

552+
static void
553+
test_bson_iter_overwrite_oid (void)
554+
{
555+
bson_oid_t oid;
556+
bson_iter_t iter;
557+
bson_t b;
558+
559+
bson_oid_init_from_string (&oid, "000000000000000000001234");
560+
bson_init (&b);
561+
BSON_ASSERT (BSON_APPEND_OID (&b, "key", &oid));
562+
BSON_ASSERT (bson_iter_init_find (&iter, &b, "key"));
563+
bson_oid_init_from_string (&oid, "000000000000000000004321");
564+
bson_iter_overwrite_oid (&iter, &oid);
565+
BSON_ASSERT (bson_iter_init_find (&iter, &b, "key"));
566+
ASSERT_CMPOID (bson_iter_oid (&iter), &oid);
567+
bson_destroy (&b);
568+
}
569+
570+
571+
static void
572+
test_bson_iter_overwrite_timestamp (void)
573+
{
574+
bson_iter_t iter;
575+
bson_t b;
576+
uint32_t t, i;
577+
578+
bson_init (&b);
579+
BSON_ASSERT (BSON_APPEND_TIMESTAMP (&b, "key", 1, 2));
580+
BSON_ASSERT (bson_iter_init_find (&iter, &b, "key"));
581+
bson_iter_overwrite_timestamp (&iter, 3, 4);
582+
BSON_ASSERT (bson_iter_init_find (&iter, &b, "key"));
583+
bson_iter_timestamp (&iter, &t, &i);
584+
ASSERT_CMPUINT32 (t, ==, 3);
585+
ASSERT_CMPUINT32 (i, ==, 4);
586+
bson_destroy (&b);
587+
}
588+
589+
590+
static void
591+
test_bson_iter_overwrite_date_time (void)
592+
{
593+
bson_iter_t iter;
594+
bson_t b;
595+
596+
bson_init (&b);
597+
BSON_ASSERT (BSON_APPEND_DATE_TIME (&b, "key", 1234567890));
598+
BSON_ASSERT (bson_iter_init_find (&iter, &b, "key"));
599+
bson_iter_overwrite_date_time (&iter, -1234567890);
600+
BSON_ASSERT (bson_iter_init_find (&iter, &b, "key"));
601+
ASSERT_CMPINT64 (bson_iter_date_time (&iter), ==, -1234567890);
602+
bson_destroy (&b);
603+
}
604+
605+
552606
static void
553607
test_bson_iter_recurse (void)
554608
{
@@ -699,6 +753,14 @@ test_iter_install (TestSuite *suite)
699753
test_bson_iter_overwrite_double);
700754
TestSuite_Add (
701755
suite, "/bson/iter/test_overwrite_bool", test_bson_iter_overwrite_bool);
756+
TestSuite_Add (
757+
suite, "/bson/iter/test_overwrite_oid", test_bson_iter_overwrite_oid);
758+
TestSuite_Add (suite,
759+
"/bson/iter/test_overwrite_timestamp",
760+
test_bson_iter_overwrite_timestamp);
761+
TestSuite_Add (suite,
762+
"/bson/iter/test_overwrite_date_time",
763+
test_bson_iter_overwrite_date_time);
702764
TestSuite_Add (suite,
703765
"/bson/iter/test_bson_iter_overwrite_decimal128",
704766
test_bson_iter_overwrite_decimal128);

0 commit comments

Comments
 (0)