mongory 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +30 -0
- data/README.md +55 -73
- data/docs/clang_bridge.md +2 -2
- data/docs/performance.md +43 -7
- data/ext/mongory_ext/extconf.rb +15 -0
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +28 -43
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -1
- data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +3 -4
- data/ext/mongory_ext/mongory-core/src/foundations/array.c +28 -5
- data/ext/mongory_ext/mongory-core/src/foundations/config.c +24 -22
- data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +3 -3
- data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +22 -13
- data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +4 -3
- data/ext/mongory_ext/mongory-core/src/foundations/table.c +60 -224
- data/ext/mongory_ext/mongory-core/src/foundations/table_private.h +36 -0
- data/ext/mongory_ext/mongory-core/src/foundations/utils.c +30 -47
- data/ext/mongory_ext/mongory-core/src/foundations/utils.h +2 -5
- data/ext/mongory_ext/mongory-core/src/foundations/value.c +18 -32
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +89 -104
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +18 -11
- data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +35 -150
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +244 -418
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +30 -0
- data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +15 -56
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -39
- data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +14 -15
- data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +382 -74
- data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +373 -123
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +20 -12
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -4
- data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +214 -171
- data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +8 -16
- data/ext/mongory_ext/mongory_ext.c +26 -4
- data/ext/mongory_ext/mongory_ext_internal.h +15 -3
- data/ext/mongory_ext/rb_mongory_adapters.c +52 -19
- data/ext/mongory_ext/rb_mongory_matcher.c +115 -36
- data/ext/mongory_ext/rb_mongory_value.c +124 -51
- data/lib/mongory/converters/abstract_converter.rb +5 -5
- data/lib/mongory/converters/converted.rb +0 -4
- data/lib/mongory/converters/key_converter.rb +13 -36
- data/lib/mongory/converters/value_converter.rb +4 -4
- data/lib/mongory/matchers.rb +8 -7
- data/lib/mongory/query_builder.rb +9 -0
- data/lib/mongory/utils/context.rb +0 -6
- data/lib/mongory/utils/singleton_builder.rb +1 -4
- data/lib/mongory/version.rb +1 -1
- data/lib/mongory.rb +0 -1
- data/script/bench.rb +77 -4
- data/script/coverage.rb +84 -0
- metadata +7 -7
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
#include "mongory-core/foundations/memory_pool.h"
|
|
21
21
|
#include "mongory-core/foundations/table.h"
|
|
22
22
|
#include "mongory-core/foundations/value.h"
|
|
23
|
+
#include <stdlib.h>
|
|
23
24
|
|
|
24
25
|
#if defined(_WIN32)
|
|
25
26
|
# ifndef WIN32_LEAN_AND_MEAN
|
|
@@ -42,6 +43,9 @@ static inline void mongory_config_unlock(void) { pthread_mutex_unlock(&mongory_c
|
|
|
42
43
|
static bool mongory_regex_default_func(mongory_memory_pool *pool, mongory_value *pattern, mongory_value *value);
|
|
43
44
|
static char *mongory_regex_default_stringify_func(mongory_memory_pool *pool, mongory_value *pattern);
|
|
44
45
|
|
|
46
|
+
mongory_global_malloc_func mongory_global_malloc = malloc;
|
|
47
|
+
mongory_global_free_func mongory_global_free = free;
|
|
48
|
+
|
|
45
49
|
// Global internal memory pool for the library.
|
|
46
50
|
mongory_memory_pool *mongory_internal_pool = NULL;
|
|
47
51
|
// Global adapter for regex operations.
|
|
@@ -54,9 +58,7 @@ mongory_regex_adapter mongory_internal_regex_adapter = {
|
|
|
54
58
|
mongory_table *mongory_matcher_mapping = NULL;
|
|
55
59
|
// Global converter for handling external data types.
|
|
56
60
|
mongory_value_converter mongory_internal_value_converter = {
|
|
57
|
-
.deep_convert = NULL,
|
|
58
61
|
.shallow_convert = NULL,
|
|
59
|
-
.recover = NULL,
|
|
60
62
|
};
|
|
61
63
|
// Global adapter for custom matchers.
|
|
62
64
|
mongory_matcher_custom_adapter mongory_custom_matcher_adapter = {
|
|
@@ -157,6 +159,9 @@ static inline void mongory_matcher_mapping_init() {
|
|
|
157
159
|
* @param build_func A function pointer to the matcher's constructor.
|
|
158
160
|
*/
|
|
159
161
|
static void mongory_matcher_register_unlocked(char *name, mongory_matcher_build_func build_func) {
|
|
162
|
+
if (!name || !build_func) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
160
165
|
// Ensure mapping is initialized.
|
|
161
166
|
if (mongory_matcher_mapping == NULL) {
|
|
162
167
|
mongory_matcher_mapping_init();
|
|
@@ -191,6 +196,9 @@ void mongory_matcher_register(char *name, mongory_matcher_build_func build_func)
|
|
|
191
196
|
* constructor, or NULL if not found.
|
|
192
197
|
*/
|
|
193
198
|
mongory_matcher_build_func mongory_matcher_build_func_get(char *name) {
|
|
199
|
+
if (!name) {
|
|
200
|
+
return NULL;
|
|
201
|
+
}
|
|
194
202
|
mongory_config_lock();
|
|
195
203
|
mongory_matcher_build_func result = NULL;
|
|
196
204
|
if (mongory_matcher_mapping != NULL) {
|
|
@@ -203,15 +211,6 @@ mongory_matcher_build_func mongory_matcher_build_func_get(char *name) {
|
|
|
203
211
|
return result;
|
|
204
212
|
}
|
|
205
213
|
|
|
206
|
-
/**
|
|
207
|
-
* @brief Sets the function for deep conversion of external values.
|
|
208
|
-
* Initializes the value converter if necessary.
|
|
209
|
-
* @param deep_convert The deep conversion function.
|
|
210
|
-
*/
|
|
211
|
-
void mongory_value_converter_deep_convert_set(mongory_deep_convert_func deep_convert) {
|
|
212
|
-
mongory_internal_value_converter.deep_convert = deep_convert;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
214
|
/**
|
|
216
215
|
* @brief Sets the function for shallow conversion of external values.
|
|
217
216
|
* Initializes the value converter if necessary.
|
|
@@ -221,16 +220,6 @@ void mongory_value_converter_shallow_convert_set(mongory_shallow_convert_func sh
|
|
|
221
220
|
mongory_internal_value_converter.shallow_convert = shallow_convert;
|
|
222
221
|
}
|
|
223
222
|
|
|
224
|
-
/**
|
|
225
|
-
* @brief Sets the function for recovering external values from mongory_value.
|
|
226
|
-
* Initializes the value converter if necessary.
|
|
227
|
-
* @param recover The recovery function.
|
|
228
|
-
*/
|
|
229
|
-
void mongory_value_converter_recover_set(mongory_recover_func recover) {
|
|
230
|
-
mongory_internal_value_converter.recover = recover;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
|
|
234
223
|
void mongory_custom_matcher_match_func_set(bool (*match)(void *external_matcher, mongory_value *value)) {
|
|
235
224
|
mongory_custom_matcher_adapter.match = match;
|
|
236
225
|
}
|
|
@@ -239,7 +228,7 @@ void mongory_custom_matcher_build_func_set(mongory_matcher_custom_context *(*bui
|
|
|
239
228
|
mongory_custom_matcher_adapter.build = build;
|
|
240
229
|
}
|
|
241
230
|
|
|
242
|
-
void mongory_custom_matcher_lookup_func_set(bool (*lookup)(char *key)) {
|
|
231
|
+
void mongory_custom_matcher_lookup_func_set(bool (*lookup)(char *key, void *extern_ctx)) {
|
|
243
232
|
mongory_custom_matcher_adapter.lookup = lookup;
|
|
244
233
|
}
|
|
245
234
|
|
|
@@ -273,6 +262,18 @@ size_t mongory_max_nodes_get(void) {
|
|
|
273
262
|
return val;
|
|
274
263
|
}
|
|
275
264
|
|
|
265
|
+
void mongory_global_malloc_set(mongory_global_malloc_func func) {
|
|
266
|
+
mongory_config_lock();
|
|
267
|
+
mongory_global_malloc = func ? func : malloc;
|
|
268
|
+
mongory_config_unlock();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
void mongory_global_free_set(mongory_global_free_func func) {
|
|
272
|
+
mongory_config_lock();
|
|
273
|
+
mongory_global_free = func ? func : free;
|
|
274
|
+
mongory_config_unlock();
|
|
275
|
+
}
|
|
276
|
+
|
|
276
277
|
/**
|
|
277
278
|
* @brief Initializes all core Mongory library components.
|
|
278
279
|
* This includes the internal memory pool, regex adapter, matcher mapping table,
|
|
@@ -304,6 +305,7 @@ void mongory_init() {
|
|
|
304
305
|
mongory_matcher_register_unlocked("$regex", mongory_matcher_regex_new);
|
|
305
306
|
mongory_matcher_register_unlocked("$and", mongory_matcher_and_new);
|
|
306
307
|
mongory_matcher_register_unlocked("$or", mongory_matcher_or_new);
|
|
308
|
+
mongory_matcher_register_unlocked("$nor", mongory_matcher_nor_new);
|
|
307
309
|
mongory_matcher_register_unlocked("$elemMatch", mongory_matcher_elem_match_new);
|
|
308
310
|
mongory_matcher_register_unlocked("$every", mongory_matcher_every_new);
|
|
309
311
|
mongory_matcher_register_unlocked("$not", mongory_matcher_not_new);
|
|
@@ -12,9 +12,7 @@ typedef struct mongory_regex_adapter {
|
|
|
12
12
|
} mongory_regex_adapter;
|
|
13
13
|
|
|
14
14
|
typedef struct mongory_value_converter {
|
|
15
|
-
mongory_deep_convert_func deep_convert;
|
|
16
15
|
mongory_shallow_convert_func shallow_convert;
|
|
17
|
-
mongory_recover_func recover;
|
|
18
16
|
} mongory_value_converter;
|
|
19
17
|
|
|
20
18
|
/**
|
|
@@ -30,7 +28,7 @@ typedef struct mongory_value_converter {
|
|
|
30
28
|
typedef struct mongory_matcher_custom_adapter {
|
|
31
29
|
bool (*match)(void *external_matcher, mongory_value *value); // Match a value against an external matcher.
|
|
32
30
|
mongory_matcher_custom_context *(*build)(char *key, mongory_value *condition, void *extern_ctx); // Build an external matcher reference.
|
|
33
|
-
bool (*lookup)(char *key); // Lookup a matcher reference by key.
|
|
31
|
+
bool (*lookup)(char *key, void *extern_ctx); // Lookup a matcher reference by key.
|
|
34
32
|
} mongory_matcher_custom_adapter;
|
|
35
33
|
|
|
36
34
|
extern mongory_memory_pool *mongory_internal_pool;
|
|
@@ -39,6 +37,8 @@ extern mongory_table *mongory_matcher_mapping;
|
|
|
39
37
|
extern mongory_value_converter mongory_internal_value_converter;
|
|
40
38
|
extern mongory_matcher_custom_adapter mongory_custom_matcher_adapter;
|
|
41
39
|
extern bool mongory_matcher_trace_result_colorful;
|
|
40
|
+
extern mongory_global_malloc_func mongory_global_malloc;
|
|
41
|
+
extern mongory_global_free_func mongory_global_free;
|
|
42
42
|
|
|
43
43
|
typedef mongory_matcher *(*mongory_matcher_build_func)(mongory_memory_pool *pool,
|
|
44
44
|
mongory_value *condition,
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
# define _POSIX_C_SOURCE 200809L
|
|
12
12
|
#endif
|
|
13
13
|
|
|
14
|
+
#include "config_private.h"
|
|
14
15
|
#include "memory_pool_private.h"
|
|
15
16
|
#include <mongory-core/foundations/memory_pool.h>
|
|
16
17
|
#include <stdint.h> // For SIZE_MAX
|
|
@@ -104,15 +105,23 @@ typedef struct mongory_memory_pool_ctx {
|
|
|
104
105
|
* @return mongory_memory_node* Pointer to the new memory node, or NULL on
|
|
105
106
|
* failure.
|
|
106
107
|
*/
|
|
108
|
+
static inline void *mongory_pool_zalloc(size_t size) {
|
|
109
|
+
void *ptr = mongory_global_malloc(size);
|
|
110
|
+
if (ptr) {
|
|
111
|
+
memset(ptr, 0, size);
|
|
112
|
+
}
|
|
113
|
+
return ptr;
|
|
114
|
+
}
|
|
115
|
+
|
|
107
116
|
static inline mongory_memory_node *mongory_memory_chunk_new(size_t chunk_size) {
|
|
108
|
-
mongory_memory_node *node = (mongory_memory_node *)
|
|
117
|
+
mongory_memory_node *node = (mongory_memory_node *)mongory_pool_zalloc(sizeof(mongory_memory_node));
|
|
109
118
|
if (!node) {
|
|
110
119
|
return NULL; // Failed to allocate node structure.
|
|
111
120
|
}
|
|
112
121
|
|
|
113
|
-
void *mem =
|
|
122
|
+
void *mem = mongory_pool_zalloc(chunk_size);
|
|
114
123
|
if (!mem) {
|
|
115
|
-
|
|
124
|
+
mongory_global_free(node); // Clean up allocated node structure.
|
|
116
125
|
return NULL; // Failed to allocate memory block for the chunk.
|
|
117
126
|
}
|
|
118
127
|
|
|
@@ -248,11 +257,11 @@ static inline void mongory_memory_pool_node_list_free(mongory_memory_node *head)
|
|
|
248
257
|
mongory_memory_node *next = node->next;
|
|
249
258
|
if (node->ptr) {
|
|
250
259
|
memset(node->ptr, 0, node->size); // Clear memory for safety.
|
|
251
|
-
|
|
260
|
+
mongory_global_free(node->ptr); // Free the actual memory block.
|
|
252
261
|
}
|
|
253
262
|
memset(node, 0,
|
|
254
263
|
sizeof(mongory_memory_node)); // Clear the node structure itself.
|
|
255
|
-
|
|
264
|
+
mongory_global_free(node); // Free the node structure.
|
|
256
265
|
node = next;
|
|
257
266
|
}
|
|
258
267
|
}
|
|
@@ -281,11 +290,11 @@ static inline void mongory_memory_pool_destroy(mongory_memory_pool *pool) {
|
|
|
281
290
|
|
|
282
291
|
memset(ctx, 0,
|
|
283
292
|
sizeof(mongory_memory_pool_ctx)); // Clear the context structure.
|
|
284
|
-
|
|
293
|
+
mongory_global_free(ctx); // Free the context structure.
|
|
285
294
|
}
|
|
286
295
|
|
|
287
296
|
memset(pool, 0, sizeof(mongory_memory_pool)); // Clear the pool structure.
|
|
288
|
-
|
|
297
|
+
mongory_global_free(pool); // Free the pool structure.
|
|
289
298
|
}
|
|
290
299
|
|
|
291
300
|
/**
|
|
@@ -308,7 +317,7 @@ static inline void mongory_memory_pool_trace(mongory_memory_pool *pool, void *pt
|
|
|
308
317
|
mongory_memory_pool_ctx *pool_ctx = (mongory_memory_pool_ctx *)pool->ctx;
|
|
309
318
|
|
|
310
319
|
// Create a new node to trace this external allocation.
|
|
311
|
-
mongory_memory_node *extra_alloc_tracer = (mongory_memory_node *)
|
|
320
|
+
mongory_memory_node *extra_alloc_tracer = (mongory_memory_node *)mongory_pool_zalloc(sizeof(mongory_memory_node));
|
|
312
321
|
if (!extra_alloc_tracer) {
|
|
313
322
|
pool->error = &MONGORY_ALLOC_ERROR;
|
|
314
323
|
return;
|
|
@@ -331,23 +340,23 @@ static inline void mongory_memory_pool_trace(mongory_memory_pool *pool, void *pt
|
|
|
331
340
|
*/
|
|
332
341
|
mongory_memory_pool *mongory_memory_pool_new() {
|
|
333
342
|
// Allocate the main pool structure.
|
|
334
|
-
mongory_memory_pool *pool = (mongory_memory_pool *)
|
|
343
|
+
mongory_memory_pool *pool = (mongory_memory_pool *)mongory_pool_zalloc(sizeof(mongory_memory_pool));
|
|
335
344
|
if (!pool) {
|
|
336
345
|
return NULL;
|
|
337
346
|
}
|
|
338
347
|
|
|
339
348
|
// Allocate the internal context for the pool.
|
|
340
|
-
mongory_memory_pool_ctx *ctx = (mongory_memory_pool_ctx *)
|
|
349
|
+
mongory_memory_pool_ctx *ctx = (mongory_memory_pool_ctx *)mongory_pool_zalloc(sizeof(mongory_memory_pool_ctx));
|
|
341
350
|
if (!ctx) {
|
|
342
|
-
|
|
351
|
+
mongory_global_free(pool); // Clean up partially allocated pool.
|
|
343
352
|
return NULL;
|
|
344
353
|
}
|
|
345
354
|
|
|
346
355
|
// Allocate the first memory chunk.
|
|
347
356
|
mongory_memory_node *first_chunk = mongory_memory_chunk_new(MONGORY_INITIAL_CHUNK_SIZE);
|
|
348
357
|
if (!first_chunk) {
|
|
349
|
-
|
|
350
|
-
|
|
358
|
+
mongory_global_free(ctx); // Clean up context.
|
|
359
|
+
mongory_global_free(pool); // Clean up pool structure.
|
|
351
360
|
return NULL;
|
|
352
361
|
}
|
|
353
362
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#include "string_buffer.h"
|
|
2
|
+
#include "utils.h"
|
|
2
3
|
#include <mongory-core.h>
|
|
3
4
|
#include <stdarg.h>
|
|
4
5
|
#include <stdint.h>
|
|
@@ -8,7 +9,7 @@
|
|
|
8
9
|
#define MONGORY_STRING_BUFFER_INITIAL_CAPACITY 16
|
|
9
10
|
|
|
10
11
|
mongory_string_buffer *mongory_string_buffer_new(mongory_memory_pool *pool) {
|
|
11
|
-
if (!pool
|
|
12
|
+
if (!mongory_validate_pool(pool)) {
|
|
12
13
|
return NULL;
|
|
13
14
|
}
|
|
14
15
|
mongory_string_buffer *buffer = MG_ALLOC_PTR(pool, mongory_string_buffer);
|
|
@@ -30,7 +31,7 @@ mongory_string_buffer *mongory_string_buffer_new(mongory_memory_pool *pool) {
|
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
static inline bool mongory_string_buffer_grow(mongory_string_buffer *buffer, size_t additional_size) {
|
|
33
|
-
if (!buffer || !buffer->pool) {
|
|
34
|
+
if (!buffer || !mongory_validate_pool(buffer->pool)) {
|
|
34
35
|
return false;
|
|
35
36
|
}
|
|
36
37
|
if (additional_size > SIZE_MAX - 1 || buffer->size > SIZE_MAX - 1 - additional_size) {
|
|
@@ -102,7 +103,7 @@ char *mongory_string_buffer_cstr(mongory_string_buffer *buffer) {
|
|
|
102
103
|
}
|
|
103
104
|
|
|
104
105
|
void mongory_string_buffer_clear(mongory_string_buffer *buffer) {
|
|
105
|
-
if (!buffer || !buffer->pool) {
|
|
106
|
+
if (!buffer || !mongory_validate_pool(buffer->pool)) {
|
|
106
107
|
return;
|
|
107
108
|
}
|
|
108
109
|
char *new_buffer = (char *)MG_ALLOC(buffer->pool, MONGORY_STRING_BUFFER_INITIAL_CAPACITY);
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* when the load factor exceeds a threshold.
|
|
10
10
|
*/
|
|
11
11
|
#include "array_private.h" // For mongory_array_private details if needed
|
|
12
|
+
#include "table_private.h"
|
|
12
13
|
#include <mongory-core/foundations/array.h>
|
|
13
14
|
#include <mongory-core/foundations/config.h> // For mongory_string_cpy
|
|
14
15
|
#include <mongory-core/foundations/table.h>
|
|
@@ -32,29 +33,6 @@
|
|
|
32
33
|
*/
|
|
33
34
|
#define MONGORY_TABLE_LOAD_FACTOR 0.75
|
|
34
35
|
|
|
35
|
-
/**
|
|
36
|
-
* @struct mongory_table_node
|
|
37
|
-
* @brief Represents a node in a hash table bucket's linked list.
|
|
38
|
-
* Stores a key-value pair and a pointer to the next node in the chain.
|
|
39
|
-
*/
|
|
40
|
-
typedef struct mongory_table_node {
|
|
41
|
-
char *key; /**< The string key for this entry. */
|
|
42
|
-
mongory_value *value; /**< The mongory_value associated with the key. */
|
|
43
|
-
struct mongory_table_node *next; /**< Pointer to the next node in the collision chain. */
|
|
44
|
-
} mongory_table_node;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @struct mongory_table_internal
|
|
48
|
-
* @brief Internal representation of the hash table.
|
|
49
|
-
* Extends the public mongory_table structure with capacity information
|
|
50
|
-
* and the underlying array used for buckets.
|
|
51
|
-
*/
|
|
52
|
-
typedef struct mongory_table_internal {
|
|
53
|
-
mongory_table base; /**< Public part of the table structure. */
|
|
54
|
-
size_t capacity; /**< Current number of buckets in the table. */
|
|
55
|
-
mongory_array *array; /**< Array of mongory_table_node pointers (the buckets). */
|
|
56
|
-
} mongory_table_internal;
|
|
57
|
-
|
|
58
36
|
// ============================================================================
|
|
59
37
|
// Static Helper Functions
|
|
60
38
|
//
|
|
@@ -125,48 +103,15 @@ static inline size_t hash_string(const char *str) {
|
|
|
125
103
|
* false.
|
|
126
104
|
* @return true if iteration completed, false if callback stopped it.
|
|
127
105
|
*/
|
|
128
|
-
static inline
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (!callback(node, acc)) {
|
|
133
|
-
return false; // Callback requested stop.
|
|
106
|
+
static inline mongory_table_node *mongory_table_find_node(mongory_table_node *head, const char *key) {
|
|
107
|
+
for (mongory_table_node *node = head; node != NULL; node = node->next) {
|
|
108
|
+
if (strcmp(node->key, key) == 0) {
|
|
109
|
+
return node;
|
|
134
110
|
}
|
|
135
|
-
node = next;
|
|
136
111
|
}
|
|
137
|
-
return
|
|
112
|
+
return NULL;
|
|
138
113
|
}
|
|
139
114
|
|
|
140
|
-
/**
|
|
141
|
-
* @brief Callback used during rehashing to re-insert a node into the new table
|
|
142
|
-
* structure.
|
|
143
|
-
* Calculates the new bucket index for the node and prepends it to that
|
|
144
|
-
* bucket's list.
|
|
145
|
-
* @param node The node to rehash.
|
|
146
|
-
* @param acc Pointer to the mongory_table_internal structure (cast from void*).
|
|
147
|
-
* @return Always true to continue walking the old bucket list.
|
|
148
|
-
*/
|
|
149
|
-
static inline bool mongory_table_rehash_on_node(mongory_table_node *node, void *acc) {
|
|
150
|
-
mongory_table_internal *internal = (mongory_table_internal *)acc;
|
|
151
|
-
mongory_array *new_bucket_array = internal->array;
|
|
152
|
-
// Calculate index in the new array based on new capacity.
|
|
153
|
-
size_t new_index = hash_string(node->key) % internal->capacity;
|
|
154
|
-
|
|
155
|
-
// Get current head of the new bucket's list.
|
|
156
|
-
mongory_table_node *current_bucket_head = (mongory_table_node *)new_bucket_array->get(new_bucket_array, new_index);
|
|
157
|
-
// Prepend the node to this list.
|
|
158
|
-
node->next = current_bucket_head;
|
|
159
|
-
new_bucket_array->set(new_bucket_array, new_index, (mongory_value *)node);
|
|
160
|
-
return true;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* @brief Rehashes the table when the load factor is exceeded.
|
|
165
|
-
* Creates a new underlying array with a larger, prime capacity, and
|
|
166
|
-
* re-inserts all existing nodes into this new array.
|
|
167
|
-
* @param self Pointer to the mongory_table.
|
|
168
|
-
* @return true if rehashing was successful, false otherwise.
|
|
169
|
-
*/
|
|
170
115
|
static inline bool mongory_table_rehash(mongory_table *self) {
|
|
171
116
|
mongory_table_internal *internal = (mongory_table_internal *)self;
|
|
172
117
|
mongory_array_private *old_array_private_view = (mongory_array_private *)internal->array;
|
|
@@ -185,152 +130,81 @@ static inline bool mongory_table_rehash(mongory_table *self) {
|
|
|
185
130
|
|
|
186
131
|
if (!mongory_array_resize(internal->array, new_capacity) ||
|
|
187
132
|
!internal->array->set(internal->array, new_capacity - 1, NULL)) {
|
|
188
|
-
// Error: Rehashing failed because the underlying array could not be resized.
|
|
189
|
-
// The table remains functional but may have a suboptimal load factor.
|
|
190
133
|
self->pool->error = &MONGORY_ALLOC_ERROR;
|
|
191
|
-
internal->array->count = original_capacity;
|
|
134
|
+
internal->array->count = original_capacity;
|
|
192
135
|
return false;
|
|
193
136
|
}
|
|
194
|
-
// After resize, internal->array (and old_array_private_view) points to the
|
|
195
|
-
// new items array. The old items array (original_items_ptr) is now detached
|
|
196
|
-
// but its contents (the nodes) are still valid.
|
|
197
137
|
|
|
198
|
-
internal->capacity = new_capacity;
|
|
199
|
-
|
|
200
|
-
// Iterate through all buckets of the old array structure.
|
|
138
|
+
internal->capacity = new_capacity;
|
|
139
|
+
mongory_array *new_buckets = internal->array;
|
|
201
140
|
for (size_t i = 0; i < original_capacity; i++) {
|
|
202
|
-
|
|
203
|
-
|
|
141
|
+
for (mongory_table_node *node = (mongory_table_node *)original_items_ptr[i]; node != NULL;) {
|
|
142
|
+
mongory_table_node *next = node->next;
|
|
143
|
+
size_t new_index = hash_string(node->key) % internal->capacity;
|
|
144
|
+
node->next = (mongory_table_node *)new_buckets->get(new_buckets, new_index);
|
|
145
|
+
new_buckets->set(new_buckets, new_index, (mongory_value *)node);
|
|
146
|
+
node = next;
|
|
147
|
+
}
|
|
204
148
|
}
|
|
205
|
-
// The memory for original_items_ptr itself (the array of pointers) is now
|
|
206
|
-
// stale / managed by the memory pool if mongory_array_resize reallocated.
|
|
207
|
-
// The mongory_table_nodes it pointed to have been relinked into the new array.
|
|
208
149
|
return true;
|
|
209
150
|
}
|
|
210
151
|
|
|
211
|
-
/**
|
|
212
|
-
* @struct mongory_table_kv_context
|
|
213
|
-
* @brief Context structure used for get and set operations within a bucket
|
|
214
|
-
* list.
|
|
215
|
-
*/
|
|
216
|
-
typedef struct mongory_table_kv_context {
|
|
217
|
-
char *key; /**< The key being searched for or set. */
|
|
218
|
-
mongory_value *value; /**< Stores the found value (for get) or the value to set. */
|
|
219
|
-
} mongory_table_kv_context;
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* @brief Callback for mongory_table_get to find a key in a bucket's list.
|
|
223
|
-
* If the node's key matches ctx->key, sets ctx->value and returns false to stop.
|
|
224
|
-
* @param node Current node in the list.
|
|
225
|
-
* @param acc Pointer to mongory_table_kv_context.
|
|
226
|
-
* @return true to continue search, false if key found.
|
|
227
|
-
*/
|
|
228
|
-
static inline bool mongory_table_get_on_node(mongory_table_node *node, void *acc) {
|
|
229
|
-
mongory_table_kv_context *ctx = (mongory_table_kv_context *)acc;
|
|
230
|
-
if (strcmp(node->key, ctx->key) == 0) {
|
|
231
|
-
ctx->value = node->value; // Key found, store value.
|
|
232
|
-
return false; // Stop search.
|
|
233
|
-
}
|
|
234
|
-
return true; // Continue search.
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* @brief Retrieves a value associated with a key. Implements `table->get`.
|
|
239
|
-
* @param self Pointer to the mongory_table.
|
|
240
|
-
* @param key The key to search for.
|
|
241
|
-
* @return Pointer to the mongory_value, or NULL if not found.
|
|
242
|
-
*/
|
|
243
152
|
mongory_value *mongory_table_get(mongory_table *self, char *key) {
|
|
153
|
+
if (!self || !key) {
|
|
154
|
+
return NULL;
|
|
155
|
+
}
|
|
244
156
|
mongory_table_internal *internal = (mongory_table_internal *)self;
|
|
245
|
-
mongory_array *bucket_array = internal->array;
|
|
246
157
|
size_t index = hash_string(key) % internal->capacity;
|
|
247
|
-
|
|
248
|
-
mongory_table_node *
|
|
249
|
-
|
|
250
|
-
mongory_table_kv_context ctx = {key, NULL};
|
|
251
|
-
mongory_table_node_walk(bucket_head, &ctx, mongory_table_get_on_node);
|
|
252
|
-
return ctx.value; // NULL if not found, otherwise the value.
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* @brief Callback for mongory_table_set to update a value if key exists in a
|
|
257
|
-
* bucket's list.
|
|
258
|
-
* If node's key matches ctx->key, updates node's value and returns false to
|
|
259
|
-
* stop.
|
|
260
|
-
* @param node Current node in the list.
|
|
261
|
-
* @param acc Pointer to mongory_table_kv_context.
|
|
262
|
-
* @return true to continue search (if key not matched), false if key updated.
|
|
263
|
-
*/
|
|
264
|
-
static inline bool mongory_table_set_on_node(mongory_table_node *node, void *acc) {
|
|
265
|
-
mongory_table_kv_context *ctx = (mongory_table_kv_context *)acc;
|
|
266
|
-
if (strcmp(node->key, ctx->key) == 0) {
|
|
267
|
-
node->value = ctx->value; // Key found, update value.
|
|
268
|
-
return false; // Stop search, indicates update happened.
|
|
269
|
-
}
|
|
270
|
-
return true; // Continue search.
|
|
158
|
+
mongory_table_node *bucket_head = (mongory_table_node *)internal->array->get(internal->array, index);
|
|
159
|
+
mongory_table_node *found = mongory_table_find_node(bucket_head, key);
|
|
160
|
+
return found ? found->value : NULL;
|
|
271
161
|
}
|
|
272
162
|
|
|
273
|
-
/**
|
|
274
|
-
* @brief Sets (adds or updates) a key-value pair in the table. Implements
|
|
275
|
-
* `table->set`.
|
|
276
|
-
* If key exists, its value is updated. Otherwise, a new node is created and
|
|
277
|
-
* added. Triggers rehashing if load factor is exceeded after adding.
|
|
278
|
-
* @param self Pointer to the mongory_table.
|
|
279
|
-
* @param key The key to set. A copy of this key will be made.
|
|
280
|
-
* @param value The value to associate with the key.
|
|
281
|
-
* @return true if successful, false on failure (e.g., memory allocation).
|
|
282
|
-
*/
|
|
283
163
|
bool mongory_table_set(mongory_table *self, char *key, mongory_value *value) {
|
|
164
|
+
if (!self || !self->pool || !MONGORY_VALIDATE_PTR(self->pool, key)) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
284
167
|
mongory_table_internal *internal = (mongory_table_internal *)self;
|
|
285
168
|
mongory_array *bucket_array = internal->array;
|
|
286
169
|
size_t index = hash_string(key) % internal->capacity;
|
|
287
170
|
|
|
288
171
|
mongory_table_node *bucket_head = (mongory_table_node *)bucket_array->get(bucket_array, index);
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
if (!mongory_table_node_walk(bucket_head, &ctx, mongory_table_set_on_node)) {
|
|
294
|
-
return true; // Update successful.
|
|
172
|
+
mongory_table_node *existing = mongory_table_find_node(bucket_head, key);
|
|
173
|
+
if (existing != NULL) {
|
|
174
|
+
existing->value = value;
|
|
175
|
+
return true;
|
|
295
176
|
}
|
|
296
177
|
|
|
297
|
-
// Key not found, create a new node and prepend it to the bucket list.
|
|
298
178
|
mongory_table_node *new_node = mongory_table_node_new(self);
|
|
299
179
|
if (!new_node) {
|
|
300
180
|
self->pool->error = &MONGORY_ALLOC_ERROR;
|
|
301
|
-
return false;
|
|
181
|
+
return false;
|
|
302
182
|
}
|
|
303
183
|
|
|
304
184
|
char *key_copy = mongory_string_cpy(self->pool, key);
|
|
305
185
|
if (!key_copy) {
|
|
306
186
|
self->pool->error = &MONGORY_ALLOC_ERROR;
|
|
307
|
-
return false;
|
|
187
|
+
return false;
|
|
308
188
|
}
|
|
309
189
|
|
|
310
190
|
new_node->key = key_copy;
|
|
311
191
|
new_node->value = value;
|
|
312
|
-
new_node->next = bucket_head;
|
|
192
|
+
new_node->next = bucket_head;
|
|
313
193
|
bucket_array->set(bucket_array, index, (mongory_value *)new_node);
|
|
314
194
|
|
|
315
195
|
self->count++;
|
|
316
|
-
// Check load factor and rehash if necessary.
|
|
317
196
|
if (self->count > internal->capacity * MONGORY_TABLE_LOAD_FACTOR) {
|
|
318
197
|
if (!mongory_table_rehash(self)) {
|
|
319
|
-
// Rehashing failed. The table will still work, but its performance
|
|
320
|
-
// may be degraded due to a higher-than-optimal load factor.
|
|
321
198
|
self->pool->error = &MONGORY_ALLOC_ERROR;
|
|
322
199
|
}
|
|
323
200
|
}
|
|
324
201
|
return true;
|
|
325
202
|
}
|
|
326
203
|
|
|
327
|
-
/**
|
|
328
|
-
* @brief Deletes a key-value pair from the table. Implements `table->del`.
|
|
329
|
-
* @param self Pointer to the mongory_table.
|
|
330
|
-
* @param key The key to delete.
|
|
331
|
-
* @return true if key was found and deleted, false otherwise.
|
|
332
|
-
*/
|
|
333
204
|
bool mongory_table_del(mongory_table *self, char *key) {
|
|
205
|
+
if (!self || !key) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
334
208
|
mongory_table_internal *internal = (mongory_table_internal *)self;
|
|
335
209
|
mongory_array *bucket_array = internal->array;
|
|
336
210
|
size_t index = hash_string(key) % internal->capacity;
|
|
@@ -341,73 +215,35 @@ bool mongory_table_del(mongory_table *self, char *key) {
|
|
|
341
215
|
while (node) {
|
|
342
216
|
if (strcmp(node->key, key) == 0) {
|
|
343
217
|
if (prev) {
|
|
344
|
-
prev->next = node->next;
|
|
218
|
+
prev->next = node->next;
|
|
345
219
|
} else {
|
|
346
|
-
// Node is the head of the list for this bucket.
|
|
347
220
|
bucket_array->set(bucket_array, index, (mongory_value *)node->next);
|
|
348
221
|
}
|
|
349
|
-
// The memory for the node and its key is not freed here.
|
|
350
|
-
// It was allocated from the memory pool and will be reclaimed all at
|
|
351
|
-
// once when the pool is destroyed. This is a core design principle
|
|
352
|
-
// of the library's memory management.
|
|
353
222
|
self->count--;
|
|
354
|
-
return true;
|
|
223
|
+
return true;
|
|
355
224
|
}
|
|
356
225
|
prev = node;
|
|
357
226
|
node = node->next;
|
|
358
227
|
}
|
|
359
|
-
return false;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* @struct mongory_table_each_pair_context
|
|
364
|
-
* @brief Context for iterating over table key-value pairs.
|
|
365
|
-
*/
|
|
366
|
-
typedef struct mongory_table_each_pair_context {
|
|
367
|
-
void *acc; /**< User-provided accumulator. */
|
|
368
|
-
mongory_table_each_pair_callback_func callback; /**< User callback function. */
|
|
369
|
-
} mongory_table_each_pair_context;
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* @brief Callback for mongory_table_each_pair, applied to each node in a
|
|
373
|
-
* bucket list. Invokes the user's callback.
|
|
374
|
-
* @param node Current table node.
|
|
375
|
-
* @param acc Pointer to mongory_table_each_pair_context.
|
|
376
|
-
* @return Result of the user's callback.
|
|
377
|
-
*/
|
|
378
|
-
static inline bool mongory_table_each_pair_on_node(mongory_table_node *node, void *acc) {
|
|
379
|
-
mongory_table_each_pair_context *ctx = (mongory_table_each_pair_context *)acc;
|
|
380
|
-
return ctx->callback(node->key, node->value, ctx->acc);
|
|
228
|
+
return false;
|
|
381
229
|
}
|
|
382
230
|
|
|
383
|
-
/**
|
|
384
|
-
* @brief Callback for iterating through the table's underlying array of buckets.
|
|
385
|
-
* For each non-empty bucket (which is a mongory_table_node* cast to
|
|
386
|
-
* mongory_value*), it walks that bucket's linked list.
|
|
387
|
-
* @param value The head of a bucket's linked list (cast to mongory_value*).
|
|
388
|
-
* @param acc Pointer to mongory_table_each_pair_context.
|
|
389
|
-
* @return Result of mongory_table_node_walk on the bucket list.
|
|
390
|
-
*/
|
|
391
|
-
static inline bool mongory_table_each_pair_on_root(mongory_value *value, void *acc) {
|
|
392
|
-
mongory_table_node *node_head = (mongory_table_node *)value;
|
|
393
|
-
// If bucket is empty (value is NULL from array->get), this will do nothing.
|
|
394
|
-
return mongory_table_node_walk(node_head, acc, mongory_table_each_pair_on_node);
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* @brief Iterates over all key-value pairs in the table. Implements
|
|
399
|
-
* `table->each`.
|
|
400
|
-
* @param self Pointer to the mongory_table.
|
|
401
|
-
* @param acc Accumulator/context for the user callback.
|
|
402
|
-
* @param callback User function to call for each pair.
|
|
403
|
-
* @return true if iteration completed, false if callback stopped it.
|
|
404
|
-
*/
|
|
405
231
|
bool mongory_table_each_pair(mongory_table *self, void *acc, mongory_table_each_pair_callback_func callback) {
|
|
232
|
+
if (!self || !callback) {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
406
235
|
mongory_table_internal *internal = (mongory_table_internal *)self;
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
236
|
+
mongory_array *bucket_array = internal->array;
|
|
237
|
+
for (size_t i = 0; i < internal->capacity; i++) {
|
|
238
|
+
for (mongory_table_node *node = (mongory_table_node *)bucket_array->get(bucket_array, i); node != NULL;) {
|
|
239
|
+
mongory_table_node *next = node->next;
|
|
240
|
+
if (!callback(node->key, node->value, acc)) {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
node = next;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return true;
|
|
411
247
|
}
|
|
412
248
|
|
|
413
249
|
/**
|
|
@@ -418,7 +254,7 @@ bool mongory_table_each_pair(mongory_table *self, void *acc, mongory_table_each_
|
|
|
418
254
|
* @return Pointer to the new mongory_table, or NULL on failure.
|
|
419
255
|
*/
|
|
420
256
|
mongory_table *mongory_table_new(mongory_memory_pool *pool) {
|
|
421
|
-
if (!pool)
|
|
257
|
+
if (!mongory_validate_pool(pool))
|
|
422
258
|
return NULL; // Must have a valid pool.
|
|
423
259
|
|
|
424
260
|
size_t init_capacity = MONGORY_TABLE_INIT_SIZE;
|
|
@@ -486,17 +322,17 @@ mongory_table *mongory_table_nested_wrap(mongory_memory_pool *pool, int argc, ..
|
|
|
486
322
|
|
|
487
323
|
static inline bool mongory_table_merge_cb(char *key, mongory_value *value, void *acc) {
|
|
488
324
|
mongory_table *table = (mongory_table *)acc;
|
|
489
|
-
table->set(table, key, value);
|
|
490
|
-
return true;
|
|
325
|
+
return table->set(table, key, value);
|
|
491
326
|
}
|
|
492
327
|
|
|
493
328
|
mongory_table *mongory_table_merge(mongory_table *table, mongory_table *other) {
|
|
494
|
-
mongory_memory_pool *pool = other->pool;
|
|
495
|
-
MONGORY_VALIDATE_PTR(pool, other
|
|
496
|
-
|
|
497
|
-
|
|
329
|
+
mongory_memory_pool *pool = (table && table->pool) ? table->pool : (other ? other->pool : NULL);
|
|
330
|
+
if (!MONGORY_VALIDATE_PTR(pool, table) || !MONGORY_VALIDATE_PTR(pool, other) ||
|
|
331
|
+
!MONGORY_VALIDATE_PTR(pool, other->each) || !MONGORY_VALIDATE_PTR(pool, table->set)) {
|
|
332
|
+
return NULL;
|
|
333
|
+
}
|
|
334
|
+
if (!other->each(other, table, mongory_table_merge_cb) || pool->error != NULL) {
|
|
498
335
|
return NULL;
|
|
499
336
|
}
|
|
500
|
-
other->each(other, table, mongory_table_merge_cb);
|
|
501
337
|
return table;
|
|
502
338
|
}
|