Skip to content

Commit 58aa8c3

Browse files
authored
CDRIVER-4363 add nsInfo builder (#1584)
* add uthash.h 2.3.0 * add `mcd_nsinfo_t` Intended to be used to construct the `nsInfo` payload for the upcoming `bulkWrite` command * update utlist to 2.3.0
1 parent db98dc7 commit 58aa8c3

File tree

11 files changed

+2561
-854
lines changed

11 files changed

+2561
-854
lines changed

THIRD_PARTY_NOTICES

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
The MongoDB C Driver uses third-party code distributed under different licenses.
22

3+
License notice for uthash
4+
-------------------------------------------------------------------------------
5+
6+
Copyright (c) 2003-2021, Troy D. Hanson http://troydhanson.github.com/uthash/
7+
All rights reserved.
8+
9+
Redistribution and use in source and binary forms, with or without
10+
modification, are permitted provided that the following conditions are met:
11+
12+
* Redistributions of source code must retain the above copyright
13+
notice, this list of conditions and the following disclaimer.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16+
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17+
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18+
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19+
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
327
License notice for common-b64.c
428
-------------------------------------------------------------------------------
529

src/libmongoc/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ endif ()
535535

536536
set (SOURCES ${SOURCES}
537537
${PROJECT_SOURCE_DIR}/src/mongoc/mcd-azure.c
538+
${PROJECT_SOURCE_DIR}/src/mongoc/mcd-nsinfo.c
538539
${PROJECT_SOURCE_DIR}/src/mongoc/mcd-rpc.c
539540
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-aggregate.c
540541
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-apm.c
@@ -820,6 +821,11 @@ set_property(
820821
"MONGOC_CYRUS_PLUGIN_PATH_PREFIX=$<IF:$<STREQUAL:${CYRUS_PLUGIN_PATH_PREFIX},>,NULL,\"${CYRUS_PLUGIN_PATH_PREFIX}\">"
821822
)
822823

824+
set (
825+
UTHASH_INCLUDE_DIR
826+
"${SOURCE_DIR}/src/uthash"
827+
)
828+
823829
if (ENABLE_SHARED)
824830
add_library (mongoc_shared SHARED ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
825831
if(WIN32)
@@ -832,6 +838,7 @@ if (ENABLE_SHARED)
832838
target_include_directories (mongoc_shared PRIVATE ${ZLIB_INCLUDE_DIRS})
833839
target_include_directories (mongoc_shared PRIVATE ${UTF8PROC_INCLUDE_DIRS})
834840
target_include_directories (mongoc_shared PRIVATE ${LIBMONGOCRYPT_INCLUDE_DIRECTORIES})
841+
target_include_directories (mongoc_shared PRIVATE ${UTHASH_INCLUDE_DIR})
835842
if (MONGOC_ENABLE_MONGODB_AWS_AUTH)
836843
target_include_directories (mongoc_shared PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../kms-message/src")
837844
if (APPLE)
@@ -916,6 +923,7 @@ if (MONGOC_ENABLE_STATIC_BUILD)
916923
target_include_directories (mongoc_static PRIVATE ${ZLIB_INCLUDE_DIRS})
917924
target_include_directories (mongoc_static PRIVATE ${UTF8PROC_INCLUDE_DIRS})
918925
target_include_directories (mongoc_static PRIVATE ${LIBMONGOCRYPT_INCLUDE_DIRECTORIES})
926+
target_include_directories (mongoc_static PRIVATE ${UTHASH_INCLUDE_DIR})
919927
if (MONGOC_ENABLE_MONGODB_AWS_AUTH)
920928
target_include_directories (mongoc_static PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../kms-message/src")
921929
endif ()
@@ -1083,6 +1091,7 @@ set (test-libmongoc-sources
10831091
${PROJECT_SOURCE_DIR}/tests/test-mcd-azure-imds.c
10841092
${PROJECT_SOURCE_DIR}/tests/test-service-gcp.c
10851093
${PROJECT_SOURCE_DIR}/tests/test-mcd-integer.c
1094+
${PROJECT_SOURCE_DIR}/tests/test-mcd-nsinfo.c
10861095
${PROJECT_SOURCE_DIR}/tests/test-mcd-rpc.c
10871096
${PROJECT_SOURCE_DIR}/tests/TestSuite.c
10881097
${PROJECT_SOURCE_DIR}/tests/unified/operation.c
@@ -1128,6 +1137,7 @@ if (ENABLE_TESTS)
11281137
PRIVATE
11291138
${PROJECT_SOURCE_DIR}/tests
11301139
${BSON_STATIC_INCLUDE_DIRS}
1140+
${UTHASH_INCLUDE_DIR}
11311141
)
11321142
target_compile_definitions (test-libmongoc-lib
11331143
PUBLIC

src/libmongoc/THIRD_PARTY_NOTICES

-26
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,3 @@ OR OTHER DEALINGS IN THE SOFTWARE.
2828
Except as contained in this notice, the name of a copyright holder shall not
2929
be used in advertising or otherwise to promote the sale, use or other dealings
3030
in this Software without prior written authorization of the copyright holder.
31-
32-
33-
License notice for utlist.h
34-
-------------------------------------------------------------------------------
35-
36-
BSD 1-Clause License
37-
38-
Copyright: 2007-2014, Troy D. Hanson http://troydhanson.github.com/uthash/
39-
40-
Redistribution and use in source and binary forms, with or without
41-
modification, are permitted provided that the following conditions are met:
42-
43-
* Redistributions of source code must retain the above copyright
44-
notice, this list of conditions and the following disclaimer.
45-
46-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
47-
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48-
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
49-
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
50-
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
51-
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
52-
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
53-
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
54-
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55-
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
56-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

src/libmongoc/src/mongoc/mcd-nsinfo.c

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright 2024-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <mcd-nsinfo.h>
18+
19+
#include <mongoc/mongoc-buffer-private.h>
20+
#include <mongoc/mongoc-error.h>
21+
#include <mongoc/uthash.h>
22+
23+
typedef struct {
24+
char *ns; // Hash key.
25+
int32_t index;
26+
UT_hash_handle hh;
27+
} ns_to_index_t;
28+
29+
struct _mcd_nsinfo_t {
30+
ns_to_index_t *n2i;
31+
int32_t count;
32+
mongoc_buffer_t payload;
33+
};
34+
35+
mcd_nsinfo_t *
36+
mcd_nsinfo_new (void)
37+
{
38+
mcd_nsinfo_t *self = bson_malloc0 (sizeof (*self));
39+
_mongoc_buffer_init (&self->payload, NULL, 0, NULL, NULL);
40+
return self;
41+
}
42+
43+
void
44+
mcd_nsinfo_destroy (mcd_nsinfo_t *self)
45+
{
46+
if (!self) {
47+
return;
48+
}
49+
// Delete hash table.
50+
ns_to_index_t *entry, *tmp;
51+
HASH_ITER (hh, self->n2i, entry, tmp)
52+
{
53+
HASH_DEL (self->n2i, entry);
54+
bson_free (entry->ns);
55+
bson_free (entry);
56+
}
57+
_mongoc_buffer_destroy (&self->payload);
58+
bson_free (self);
59+
}
60+
61+
int32_t
62+
mcd_nsinfo_append (mcd_nsinfo_t *self, const char *ns, bson_error_t *error)
63+
{
64+
BSON_ASSERT_PARAM (self);
65+
BSON_ASSERT_PARAM (ns);
66+
BSON_ASSERT (error || true);
67+
68+
const int32_t ns_index = self->count;
69+
if (self->count == INT32_MAX) {
70+
bson_set_error (error,
71+
MONGOC_ERROR_COMMAND,
72+
MONGOC_ERROR_COMMAND_INVALID_ARG,
73+
"Only %" PRId32 " distinct collections may be used",
74+
INT32_MAX);
75+
return -1;
76+
}
77+
self->count++;
78+
79+
// Add to hash table.
80+
ns_to_index_t *entry = bson_malloc (sizeof (*entry));
81+
*entry = (ns_to_index_t){.index = ns_index, .ns = bson_strdup (ns), .hh = {0}};
82+
HASH_ADD_KEYPTR (hh, self->n2i, entry->ns, strlen (entry->ns), entry);
83+
84+
// Append to buffer.
85+
bson_t mcd_nsinfo_bson = BSON_INITIALIZER;
86+
BSON_ASSERT (bson_append_utf8 (&mcd_nsinfo_bson, "ns", 2, ns, -1));
87+
BSON_ASSERT (_mongoc_buffer_append (&self->payload, bson_get_data (&mcd_nsinfo_bson), mcd_nsinfo_bson.len));
88+
bson_destroy (&mcd_nsinfo_bson);
89+
return ns_index;
90+
}
91+
92+
int32_t
93+
mcd_nsinfo_find (const mcd_nsinfo_t *self, const char *ns)
94+
{
95+
BSON_ASSERT_PARAM (self);
96+
BSON_ASSERT_PARAM (ns);
97+
98+
ns_to_index_t *found;
99+
HASH_FIND_STR (self->n2i, ns, found);
100+
if (found == NULL) {
101+
return -1;
102+
}
103+
104+
return found->index;
105+
}
106+
107+
uint32_t
108+
mcd_nsinfo_get_bson_size (const char *ns)
109+
{
110+
BSON_ASSERT_PARAM (ns);
111+
112+
// Calculate overhead of the BSON document { "ns": "<ns>" }. See BSON specification.
113+
bson_t as_bson = BSON_INITIALIZER;
114+
BSON_ASSERT (bson_append_utf8 (&as_bson, "ns", 2, ns, -1));
115+
uint32_t size = as_bson.len;
116+
bson_destroy (&as_bson);
117+
return size;
118+
}
119+
120+
const mongoc_buffer_t *
121+
mcd_nsinfo_as_document_sequence (const mcd_nsinfo_t *self)
122+
{
123+
BSON_ASSERT_PARAM (self);
124+
125+
return &self->payload;
126+
}

src/libmongoc/src/mongoc/mcd-nsinfo.h

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MCD_NSINFO_H
18+
#define MCD_NSINFO_H
19+
20+
#include "mongoc-prelude.h"
21+
22+
#include <mongoc/mongoc-buffer-private.h>
23+
24+
// `mcd_nsinfo_t` builds the `nsInfo` payload for a `bulkWrite` command.
25+
typedef struct _mcd_nsinfo_t mcd_nsinfo_t;
26+
27+
mcd_nsinfo_t *
28+
mcd_nsinfo_new (void);
29+
30+
void
31+
mcd_nsinfo_destroy (mcd_nsinfo_t *self);
32+
33+
// `mcd_nsinfo_append` adds `ns`. It is the callers responsibility to ensure duplicates are not inserted.
34+
// Namespaces are assigned indexes in order of insertion, starting at 0.
35+
// Returns the resulting non-negative index on success. Returns -1 on error.
36+
int32_t
37+
mcd_nsinfo_append (mcd_nsinfo_t *self, const char *ns, bson_error_t *error);
38+
39+
// `mcd_nsinfo_find` returns the non-negative index if found. Returns -1 if not found.
40+
int32_t
41+
mcd_nsinfo_find (const mcd_nsinfo_t *self, const char *ns);
42+
43+
// `mcd_nsinfo_get_bson_size` returns the size of the BSON document { "ns": "<ns>" }
44+
// Useful for checking whether a namespace can be added without exceeding a size limit.
45+
uint32_t
46+
mcd_nsinfo_get_bson_size (const char *ns);
47+
48+
// `mcd_nsinfo_as_document_sequence` returns a document sequence.
49+
// Useful for constructing an OP_MSG Section with payloadType=1.
50+
const mongoc_buffer_t *
51+
mcd_nsinfo_as_document_sequence (const mcd_nsinfo_t *self);
52+
53+
#endif // MCD_NSINFO_H

src/libmongoc/src/mongoc/uthash.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2024-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MONGOC_UTHASH_H
18+
#define MONGOC_UTHASH_H
19+
20+
#include <mongoc-prelude.h>
21+
#include <bson/bson.h>
22+
23+
#define uthash_malloc(sz) bson_malloc (sz)
24+
#define uthash_free(ptr, sz) bson_free (ptr)
25+
26+
#include <uthash-2.3.0/uthash.h>
27+
28+
#endif // MONGOC_UTHASH_H

0 commit comments

Comments
 (0)