Skip to content

CDRIVER-4363 add nsInfo builder #1584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 26, 2024
Merged
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
24 changes: 24 additions & 0 deletions THIRD_PARTY_NOTICES
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
The MongoDB C Driver uses third-party code distributed under different licenses.

License notice for uthash
-------------------------------------------------------------------------------

Copyright (c) 2003-2021, Troy D. Hanson http://troydhanson.github.com/uthash/
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

License notice for common-b64.c
-------------------------------------------------------------------------------

Expand Down
10 changes: 10 additions & 0 deletions src/libmongoc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ endif ()

set (SOURCES ${SOURCES}
${PROJECT_SOURCE_DIR}/src/mongoc/mcd-azure.c
${PROJECT_SOURCE_DIR}/src/mongoc/mcd-nsinfo.c
${PROJECT_SOURCE_DIR}/src/mongoc/mcd-rpc.c
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-aggregate.c
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-apm.c
Expand Down Expand Up @@ -820,6 +821,11 @@ set_property(
"MONGOC_CYRUS_PLUGIN_PATH_PREFIX=$<IF:$<STREQUAL:${CYRUS_PLUGIN_PATH_PREFIX},>,NULL,\"${CYRUS_PLUGIN_PATH_PREFIX}\">"
)

set (
UTHASH_INCLUDE_DIR
"${SOURCE_DIR}/src/uthash"
)

if (ENABLE_SHARED)
add_library (mongoc_shared SHARED ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
if(WIN32)
Expand All @@ -832,6 +838,7 @@ if (ENABLE_SHARED)
target_include_directories (mongoc_shared PRIVATE ${ZLIB_INCLUDE_DIRS})
target_include_directories (mongoc_shared PRIVATE ${UTF8PROC_INCLUDE_DIRS})
target_include_directories (mongoc_shared PRIVATE ${LIBMONGOCRYPT_INCLUDE_DIRECTORIES})
target_include_directories (mongoc_shared PRIVATE ${UTHASH_INCLUDE_DIR})
if (MONGOC_ENABLE_MONGODB_AWS_AUTH)
target_include_directories (mongoc_shared PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../kms-message/src")
if (APPLE)
Expand Down Expand Up @@ -916,6 +923,7 @@ if (MONGOC_ENABLE_STATIC_BUILD)
target_include_directories (mongoc_static PRIVATE ${ZLIB_INCLUDE_DIRS})
target_include_directories (mongoc_static PRIVATE ${UTF8PROC_INCLUDE_DIRS})
target_include_directories (mongoc_static PRIVATE ${LIBMONGOCRYPT_INCLUDE_DIRECTORIES})
target_include_directories (mongoc_static PRIVATE ${UTHASH_INCLUDE_DIR})
if (MONGOC_ENABLE_MONGODB_AWS_AUTH)
target_include_directories (mongoc_static PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../kms-message/src")
endif ()
Expand Down Expand Up @@ -1083,6 +1091,7 @@ set (test-libmongoc-sources
${PROJECT_SOURCE_DIR}/tests/test-mcd-azure-imds.c
${PROJECT_SOURCE_DIR}/tests/test-service-gcp.c
${PROJECT_SOURCE_DIR}/tests/test-mcd-integer.c
${PROJECT_SOURCE_DIR}/tests/test-mcd-nsinfo.c
${PROJECT_SOURCE_DIR}/tests/test-mcd-rpc.c
${PROJECT_SOURCE_DIR}/tests/TestSuite.c
${PROJECT_SOURCE_DIR}/tests/unified/operation.c
Expand Down Expand Up @@ -1128,6 +1137,7 @@ if (ENABLE_TESTS)
PRIVATE
${PROJECT_SOURCE_DIR}/tests
${BSON_STATIC_INCLUDE_DIRS}
${UTHASH_INCLUDE_DIR}
)
target_compile_definitions (test-libmongoc-lib
PUBLIC
Expand Down
26 changes: 0 additions & 26 deletions src/libmongoc/THIRD_PARTY_NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,3 @@ OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.


License notice for utlist.h
-------------------------------------------------------------------------------

BSD 1-Clause License

Copyright: 2007-2014, Troy D. Hanson http://troydhanson.github.com/uthash/

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
126 changes: 126 additions & 0 deletions src/libmongoc/src/mongoc/mcd-nsinfo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2024-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <mcd-nsinfo.h>

#include <mongoc/mongoc-buffer-private.h>
#include <mongoc/mongoc-error.h>
#include <mongoc/uthash.h>

typedef struct {
char *ns; // Hash key.
int32_t index;
UT_hash_handle hh;
} ns_to_index_t;

struct _mcd_nsinfo_t {
ns_to_index_t *n2i;
int32_t count;
mongoc_buffer_t payload;
};

mcd_nsinfo_t *
mcd_nsinfo_new (void)
{
mcd_nsinfo_t *self = bson_malloc0 (sizeof (*self));
_mongoc_buffer_init (&self->payload, NULL, 0, NULL, NULL);
return self;
}

void
mcd_nsinfo_destroy (mcd_nsinfo_t *self)
{
if (!self) {
return;
}
// Delete hash table.
ns_to_index_t *entry, *tmp;
HASH_ITER (hh, self->n2i, entry, tmp)
{
HASH_DEL (self->n2i, entry);
bson_free (entry->ns);
bson_free (entry);
}
_mongoc_buffer_destroy (&self->payload);
bson_free (self);
}

int32_t
mcd_nsinfo_append (mcd_nsinfo_t *self, const char *ns, bson_error_t *error)
{
BSON_ASSERT_PARAM (self);
BSON_ASSERT_PARAM (ns);
BSON_ASSERT (error || true);

const int32_t ns_index = self->count;
if (self->count == INT32_MAX) {
bson_set_error (error,
MONGOC_ERROR_COMMAND,
MONGOC_ERROR_COMMAND_INVALID_ARG,
"Only %" PRId32 " distinct collections may be used",
INT32_MAX);
return -1;
}
self->count++;

// Add to hash table.
ns_to_index_t *entry = bson_malloc (sizeof (*entry));
*entry = (ns_to_index_t){.index = ns_index, .ns = bson_strdup (ns), .hh = {0}};
HASH_ADD_KEYPTR (hh, self->n2i, entry->ns, strlen (entry->ns), entry);

// Append to buffer.
bson_t mcd_nsinfo_bson = BSON_INITIALIZER;
BSON_ASSERT (bson_append_utf8 (&mcd_nsinfo_bson, "ns", 2, ns, -1));
BSON_ASSERT (_mongoc_buffer_append (&self->payload, bson_get_data (&mcd_nsinfo_bson), mcd_nsinfo_bson.len));
bson_destroy (&mcd_nsinfo_bson);
return ns_index;
}

int32_t
mcd_nsinfo_find (const mcd_nsinfo_t *self, const char *ns)
{
BSON_ASSERT_PARAM (self);
BSON_ASSERT_PARAM (ns);

ns_to_index_t *found;
HASH_FIND_STR (self->n2i, ns, found);
if (found == NULL) {
return -1;
}

return found->index;
}

uint32_t
mcd_nsinfo_get_bson_size (const char *ns)
{
BSON_ASSERT_PARAM (ns);

// Calculate overhead of the BSON document { "ns": "<ns>" }. See BSON specification.
bson_t as_bson = BSON_INITIALIZER;
BSON_ASSERT (bson_append_utf8 (&as_bson, "ns", 2, ns, -1));
uint32_t size = as_bson.len;
bson_destroy (&as_bson);
return size;
}

const mongoc_buffer_t *
mcd_nsinfo_as_document_sequence (const mcd_nsinfo_t *self)
{
BSON_ASSERT_PARAM (self);

return &self->payload;
}
53 changes: 53 additions & 0 deletions src/libmongoc/src/mongoc/mcd-nsinfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MCD_NSINFO_H
#define MCD_NSINFO_H

#include "mongoc-prelude.h"

#include <mongoc/mongoc-buffer-private.h>

// `mcd_nsinfo_t` builds the `nsInfo` payload for a `bulkWrite` command.
typedef struct _mcd_nsinfo_t mcd_nsinfo_t;

mcd_nsinfo_t *
mcd_nsinfo_new (void);

void
mcd_nsinfo_destroy (mcd_nsinfo_t *self);

// `mcd_nsinfo_append` adds `ns`. It is the callers responsibility to ensure duplicates are not inserted.
// Namespaces are assigned indexes in order of insertion, starting at 0.
// Returns the resulting non-negative index on success. Returns -1 on error.
int32_t
mcd_nsinfo_append (mcd_nsinfo_t *self, const char *ns, bson_error_t *error);

// `mcd_nsinfo_find` returns the non-negative index if found. Returns -1 if not found.
int32_t
mcd_nsinfo_find (const mcd_nsinfo_t *self, const char *ns);

// `mcd_nsinfo_get_bson_size` returns the size of the BSON document { "ns": "<ns>" }
// Useful for checking whether a namespace can be added without exceeding a size limit.
uint32_t
mcd_nsinfo_get_bson_size (const char *ns);

// `mcd_nsinfo_as_document_sequence` returns a document sequence.
// Useful for constructing an OP_MSG Section with payloadType=1.
const mongoc_buffer_t *
mcd_nsinfo_as_document_sequence (const mcd_nsinfo_t *self);

#endif // MCD_NSINFO_H
28 changes: 28 additions & 0 deletions src/libmongoc/src/mongoc/uthash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MONGOC_UTHASH_H
#define MONGOC_UTHASH_H

#include <mongoc-prelude.h>
#include <bson/bson.h>

#define uthash_malloc(sz) bson_malloc (sz)
#define uthash_free(ptr, sz) bson_free (ptr)

#include <uthash-2.3.0/uthash.h>

#endif // MONGOC_UTHASH_H
Loading