|
| 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 | +} |
0 commit comments