Skip to content

Commit 2da0543

Browse files
committed
CDRIVER-3909 topology description copy/destroy methods
1 parent d50003c commit 2da0543

6 files changed

+178
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
:man_page: mongoc_topology_description_destroy
2+
3+
mongoc_topology_description_destroy()
4+
===================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
void
12+
mongoc_topology_description_destroy (mongoc_topology_description_t *description);
13+
14+
Parameters
15+
----------
16+
17+
* ``description``: A :symbol:`mongoc_topology_description_t`.
18+
19+
Description
20+
-----------
21+
22+
Frees all resources associated with the topology description. Does nothing if ``description`` is NULL.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
:man_page: mongoc_topology_description_new_copy
2+
3+
mongoc_topology_description_new_copy()
4+
====================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
mongoc_topology_description_t *
12+
mongoc_topology_description_new_copy (
13+
const mongoc_topology_description_t *description);
14+
15+
Parameters
16+
----------
17+
18+
* ``description``: A :symbol:`mongoc_topology_description_t`.
19+
20+
Description
21+
-----------
22+
23+
Performs a deep copy of ``description``.
24+
25+
Returns
26+
-------
27+
28+
Returns a newly allocated copy of ``description`` that should be freed with :symbol:`mongoc_topology_description_destroy()` when no longer in use. Returns NULL if ``description`` is NULL.

src/libmongoc/doc/mongoc_topology_description_t.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Synopsis
1515
``mongoc_topology_description_t`` is an opaque type representing the driver's knowledge of the MongoDB server or servers it is connected to.
1616
Its API conforms to the `SDAM Monitoring Specification <https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring-monitoring.rst>`_.
1717

18-
Applications receive a temporary reference to a ``mongoc_topology_description_t`` as a parameter to an SDAM Monitoring callback. See :doc:`Introduction to Application Performance Monitoring <application-performance-monitoring>`.
18+
Applications receive a temporary reference to a ``mongoc_topology_description_t`` as a parameter to an SDAM Monitoring callback that must not be destroyed. See :doc:`Introduction to Application Performance Monitoring <application-performance-monitoring>`.
1919

2020
.. only:: html
2121

@@ -26,8 +26,10 @@ Applications receive a temporary reference to a ``mongoc_topology_description_t`
2626
:titlesonly:
2727
:maxdepth: 1
2828

29+
mongoc_topology_description_destroy
2930
mongoc_topology_description_get_servers
3031
mongoc_topology_description_has_readable_server
3132
mongoc_topology_description_has_writable_server
33+
mongoc_topology_description_new_copy
3234
mongoc_topology_description_type
3335

src/libmongoc/src/mongoc/mongoc-topology-description.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,40 @@ _mongoc_topology_description_copy_to (const mongoc_topology_description_t *src,
163163
EXIT;
164164
}
165165

166+
/*
167+
*-------------------------------------------------------------------------
168+
*
169+
* mongoc_topology_description_new_copy --
170+
*
171+
* Allocates a new topology description and deep-copies @description to it
172+
* using _mongoc_topology_description_copy_to.
173+
*
174+
* Returns:
175+
* A copy of a topology description that you must destroy with
176+
* mongoc_topology_description_destroy, or NULL if @description is NULL.
177+
*
178+
* Side effects:
179+
* None.
180+
*
181+
*-------------------------------------------------------------------------
182+
*/
183+
mongoc_topology_description_t *
184+
mongoc_topology_description_new_copy (
185+
const mongoc_topology_description_t *description)
186+
{
187+
mongoc_topology_description_t *copy;
188+
189+
if (!description) {
190+
return NULL;
191+
}
192+
193+
copy = (mongoc_topology_description_t *) bson_malloc0 (sizeof (*copy));
194+
195+
_mongoc_topology_description_copy_to (description, copy);
196+
197+
return copy;
198+
}
199+
166200
/*
167201
*--------------------------------------------------------------------------
168202
*
@@ -198,6 +232,37 @@ mongoc_topology_description_cleanup (mongoc_topology_description_t *description)
198232
EXIT;
199233
}
200234

235+
/*
236+
*--------------------------------------------------------------------------
237+
*
238+
* mongoc_topology_description_destroy --
239+
*
240+
* Destroy allocated resources within @description and free
241+
* @description.
242+
*
243+
* Returns:
244+
* None.
245+
*
246+
* Side effects:
247+
* None.
248+
*
249+
*--------------------------------------------------------------------------
250+
*/
251+
void
252+
mongoc_topology_description_destroy (mongoc_topology_description_t *description)
253+
{
254+
ENTRY;
255+
256+
if (!description) {
257+
EXIT;
258+
}
259+
260+
mongoc_topology_description_cleanup (description);
261+
bson_free (description);
262+
263+
EXIT;
264+
}
265+
201266
/* find the primary, then stop iterating */
202267
static bool
203268
_mongoc_topology_description_has_primary_cb (void *item, void *ctx /* OUT */)

src/libmongoc/src/mongoc/mongoc-topology-description.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,25 @@ BSON_BEGIN_DECLS
2929

3030
typedef struct _mongoc_topology_description_t mongoc_topology_description_t;
3131

32+
MONGOC_EXPORT (void)
33+
mongoc_topology_description_destroy (
34+
mongoc_topology_description_t *description);
35+
36+
MONGOC_EXPORT (mongoc_topology_description_t *)
37+
mongoc_topology_description_new_copy (
38+
const mongoc_topology_description_t *description);
39+
3240
MONGOC_EXPORT (bool)
3341
mongoc_topology_description_has_readable_server (
3442
mongoc_topology_description_t *td, const mongoc_read_prefs_t *prefs);
43+
3544
MONGOC_EXPORT (bool)
3645
mongoc_topology_description_has_writable_server (
3746
mongoc_topology_description_t *td);
47+
3848
MONGOC_EXPORT (const char *)
3949
mongoc_topology_description_type (const mongoc_topology_description_t *td);
50+
4051
MONGOC_EXPORT (mongoc_server_description_t **)
4152
mongoc_topology_description_get_servers (
4253
const mongoc_topology_description_t *td, size_t *n);

src/libmongoc/tests/test-mongoc-topology-description.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,52 @@ test_topology_version_equal (void)
208208
mongoc_uri_destroy (uri);
209209
}
210210

211+
static void
212+
test_topology_description_new_copy (void)
213+
{
214+
mongoc_uri_t *uri;
215+
mongoc_topology_t *topology;
216+
mongoc_topology_description_t *td, *td_copy;
217+
mongoc_server_description_t *sd_a;
218+
mongoc_server_description_t *sd_c;
219+
mongoc_server_description_t **sds;
220+
size_t n;
221+
222+
uri = mongoc_uri_new ("mongodb://a,b,c");
223+
topology = mongoc_topology_new (uri, true /* single-threaded */);
224+
td = &topology->description;
225+
226+
td_copy = mongoc_topology_description_new_copy (td);
227+
228+
/* servers "a" and "c" are mongos, but "b" remains unknown */
229+
sd_a = _sd_for_host (td, "a");
230+
mongoc_topology_description_handle_hello (
231+
td, sd_a->id, tmp_bson ("{'ok': 1, 'msg': 'isdbgrid'}"), 100, NULL);
232+
233+
sd_c = _sd_for_host (td, "c");
234+
mongoc_topology_description_handle_hello (
235+
td, sd_c->id, tmp_bson ("{'ok': 1, 'msg': 'isdbgrid'}"), 100, NULL);
236+
237+
/* td was copied before original was updated */
238+
sds = mongoc_topology_description_get_servers (td_copy, &n);
239+
ASSERT_CMPSIZE_T ((size_t) 0, ==, n);
240+
241+
mongoc_server_descriptions_destroy_all (sds, n);
242+
mongoc_topology_description_destroy (td_copy);
243+
244+
td_copy = mongoc_topology_description_new_copy (td);
245+
246+
mongoc_topology_destroy (topology);
247+
mongoc_uri_destroy (uri);
248+
249+
/* td was copied after original was updated, but before it was destroyed */
250+
sds = mongoc_topology_description_get_servers (td_copy, &n);
251+
ASSERT_CMPSIZE_T ((size_t) 2, ==, n);
252+
253+
mongoc_server_descriptions_destroy_all (sds, n);
254+
mongoc_topology_description_destroy (td_copy);
255+
}
256+
211257
void
212258
test_topology_description_install (TestSuite *suite)
213259
{
@@ -221,4 +267,7 @@ test_topology_description_install (TestSuite *suite)
221267
TestSuite_Add (suite,
222268
"/TopologyDescription/topology_version_equal",
223269
test_topology_version_equal);
270+
TestSuite_Add (suite,
271+
"/TopologyDescription/new_copy",
272+
test_topology_description_new_copy);
224273
}

0 commit comments

Comments
 (0)