-
Notifications
You must be signed in to change notification settings - Fork 455
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3ffaddd
add uthash.h 2.3.0
kevinAlbs d0c6ee9
override uthash malloc/free functions
kevinAlbs 2fef9f0
add `mcd_nsinfo_t`
kevinAlbs 00536c7
move `uthash.h` into `src/uthash-2.3.0`
kevinAlbs 55d3ded
add another `uthash` directory
kevinAlbs 0dc21f0
update utlist to 2.3.0
kevinAlbs df34dea
use `const`
kevinAlbs 78077ba
avoid unnecessary zero-malloc
kevinAlbs 2de0f3a
document order of indexes
kevinAlbs af3a64c
use const for `ns_count`
kevinAlbs 80f5aeb
add missing `.hh` to designated initializer
kevinAlbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.