mongory 2.1.1-x86_64-linux → 2.2.0-x86_64-linux

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.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -0
  3. data/README.md +45 -74
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +28 -1
  6. data/lib/core/2.6/mongory_ext.so +0 -0
  7. data/lib/core/2.7/mongory_ext.so +0 -0
  8. data/lib/core/3.0/mongory_ext.so +0 -0
  9. data/lib/core/3.1/mongory_ext.so +0 -0
  10. data/lib/core/3.2/mongory_ext.so +0 -0
  11. data/lib/core/3.3/mongory_ext.so +0 -0
  12. data/lib/core/3.4/mongory_ext.so +0 -0
  13. data/lib/core/4.0/mongory_ext.so +0 -0
  14. data/lib/mongory/converters/abstract_converter.rb +5 -5
  15. data/lib/mongory/converters/converted.rb +0 -4
  16. data/lib/mongory/converters/key_converter.rb +13 -36
  17. data/lib/mongory/converters/value_converter.rb +4 -4
  18. data/lib/mongory/matchers.rb +8 -7
  19. data/lib/mongory/query_builder.rb +9 -0
  20. data/lib/mongory/utils/context.rb +0 -6
  21. data/lib/mongory/utils/singleton_builder.rb +1 -4
  22. data/lib/mongory/version.rb +1 -1
  23. data/lib/mongory.rb +0 -1
  24. data/mongory.gemspec +13 -12
  25. data/script/coverage.rb +84 -0
  26. metadata +9 -54
  27. data/ext/mongory_ext/extconf.rb +0 -115
  28. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +0 -62
  29. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +0 -122
  30. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +0 -217
  31. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +0 -82
  32. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -115
  33. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +0 -127
  34. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +0 -175
  35. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +0 -103
  36. data/ext/mongory_ext/mongory-core/include/mongory-core.h +0 -12
  37. data/ext/mongory_ext/mongory-core/src/foundations/array.c +0 -336
  38. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +0 -20
  39. data/ext/mongory_ext/mongory-core/src/foundations/config.c +0 -335
  40. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +0 -49
  41. data/ext/mongory_ext/mongory-core/src/foundations/error.c +0 -48
  42. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +0 -406
  43. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +0 -45
  44. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -117
  45. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -43
  46. data/ext/mongory_ext/mongory-core/src/foundations/table.c +0 -502
  47. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +0 -206
  48. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +0 -77
  49. data/ext/mongory_ext/mongory-core/src/foundations/value.c +0 -548
  50. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +0 -193
  51. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +0 -117
  52. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +0 -241
  53. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +0 -83
  54. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +0 -730
  55. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +0 -131
  56. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +0 -158
  57. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.h +0 -48
  58. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +0 -199
  59. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +0 -58
  60. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +0 -204
  61. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.h +0 -47
  62. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +0 -483
  63. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +0 -46
  64. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +0 -529
  65. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  66. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
  67. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +0 -61
  68. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +0 -23
  69. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +0 -416
  70. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +0 -97
  71. data/ext/mongory_ext/mongory_ext.c +0 -66
  72. data/ext/mongory_ext/mongory_ext_internal.h +0 -74
  73. data/ext/mongory_ext/rb_mongory_adapters.c +0 -140
  74. data/ext/mongory_ext/rb_mongory_matcher.c +0 -440
  75. data/ext/mongory_ext/rb_mongory_value.c +0 -327
@@ -1,217 +0,0 @@
1
- #ifndef MONGORY_FOUNDATIONS_CONFIG_H
2
- #define MONGORY_FOUNDATIONS_CONFIG_H
3
-
4
- /**
5
- * @file config.h
6
- * @brief Defines global configuration functions and types for the Mongory
7
- * library.
8
- *
9
- * This includes initialization and cleanup routines, as well as functions
10
- * for setting up custom behaviors like regex matching and value conversion.
11
- * It also provides a string copying utility that uses the library's memory
12
- * pool.
13
- */
14
-
15
- #include "mongory-core/foundations/memory_pool.h"
16
- #include "mongory-core/foundations/value.h"
17
- #include <stdbool.h>
18
-
19
- /**
20
- * @brief Function pointer type for custom regex matching.
21
- *
22
- * This function is called when a regex match is required.
23
- *
24
- * @note Security / ReDoS guidance: `mongory-core` is regex-engine agnostic.
25
- * When accepting untrusted patterns or inputs, adapters backed by backtracking
26
- * engines MUST enforce match/backtrack limits (e.g. PCRE2
27
- * `pcre2_set_match_limit` & `pcre2_set_depth_limit`, Oniguruma
28
- * `onig_set_retry_limit_in_match`) or use a linear-time engine such as RE2.
29
- *
30
- * @param pool The memory pool to use for any allocations during the match.
31
- * @param pattern The regex pattern (as a mongory_value, typically a string or
32
- * regex type).
33
- * @param value The value to match against (as a mongory_value, typically a
34
- * string).
35
- * @return bool True if the value matches the pattern, false otherwise.
36
- */
37
- typedef bool (*mongory_regex_func)(mongory_memory_pool *pool, mongory_value *pattern, mongory_value *value);
38
-
39
- /**
40
- * @brief Function pointer type for regex matching with `$options` flags.
41
- *
42
- * Called instead of `mongory_regex_func` when a `$regex` condition has a
43
- * non-empty sibling `$options`.
44
- *
45
- * @param pool The memory pool to use for any allocations during the match.
46
- * @param pattern The regex pattern (string or regex type).
47
- * @param options Validated flags, a non-empty combination of 'i' (caseless),
48
- * 'm' (multiline anchors) and 's' (dot matches newline).
49
- * @param value The value to match against (typically a string).
50
- * @return bool True if the value matches the pattern, false otherwise.
51
- */
52
- typedef bool (*mongory_regex_options_func)(mongory_memory_pool *pool, mongory_value *pattern, const char *options,
53
- mongory_value *value);
54
-
55
- /**
56
- * @brief Function pointer type for stringifying a regex pattern.
57
- *
58
- * This function is called when a regex pattern needs to be stringified.
59
- *
60
- * @param pool The memory pool to use for any allocations during the
61
- * stringification.
62
- * @param pattern The regex pattern (as a mongory_value, typically a string or
63
- * regex type).
64
- * @return char* A stringified representation of the regex pattern, or NULL on failure.
65
- */
66
- typedef char* (*mongory_regex_stringify_func)(mongory_memory_pool *pool, mongory_value *pattern);
67
-
68
- /**
69
- * @brief Function pointer type for deep conversion of an external value to a
70
- * mongory_value.
71
- *
72
- * "Deep" implies that if the external value is a complex type (e.g., a struct
73
- * representing a document or array), its contents should also be converted.
74
- *
75
- * @param pool The memory pool to use for allocating the new mongory_value and
76
- * its data.
77
- * @param value A pointer to the external value to convert.
78
- * @return mongory_value* A new mongory_value representing the converted data,
79
- * or NULL on failure.
80
- */
81
- typedef mongory_value *(*mongory_deep_convert_func)(mongory_memory_pool *pool, void *value);
82
-
83
- /**
84
- * @brief Function pointer type for shallow conversion of an external value to a
85
- * mongory_value.
86
- *
87
- * "Shallow" implies that if the external value is complex, only a wrapper or
88
- * pointer might be stored in the mongory_value, without deeply converting its
89
- * contents.
90
- *
91
- * @param pool The memory pool to use for allocating the new mongory_value.
92
- * @param value A pointer to the external value to convert.
93
- * @return mongory_value* A new mongory_value, or NULL on failure.
94
- */
95
- typedef mongory_value *(*mongory_shallow_convert_func)(mongory_memory_pool *pool, void *value);
96
-
97
- /**
98
- * @brief Function pointer type for recovering an external value from a
99
- * mongory_value.
100
- *
101
- * This is the reverse of a conversion, intended to get back the original
102
- * external data representation if it was stored or wrapped.
103
- *
104
- * @param pool The memory pool (can be used if recovery involves allocation,
105
- * though often it might just retrieve a stored pointer).
106
- * @param value The mongory_value from which to recover the external data.
107
- * @return void* A pointer to the recovered external value, or NULL if not
108
- * possible.
109
- */
110
- typedef void *(*mongory_recover_func)(mongory_memory_pool *pool, mongory_value *value);
111
-
112
- /**
113
- * @brief Initializes the Mongory library.
114
- *
115
- * Sets up internal structures, including the global memory pool,
116
- * regex adapter, matcher mapping, and value converter. This function
117
- * should be called before any other Mongory library functions are used.
118
- * It also registers the default set of matchers (e.g., $in, $eq).
119
- */
120
- void mongory_init();
121
-
122
- /**
123
- * @brief Cleans up resources used by the Mongory library.
124
- *
125
- * Frees the internal memory pool and resets global pointers. This should
126
- * be called when the library is no longer needed to prevent memory leaks.
127
- */
128
- void mongory_cleanup();
129
-
130
- /**
131
- * @brief Sets the custom regex matching function.
132
- *
133
- * If not called, a default function that always returns false is used.
134
- *
135
- * @param func The custom regex function to use.
136
- */
137
- void mongory_regex_func_set(mongory_regex_func func);
138
-
139
- /**
140
- * @brief Sets the regex matching function that supports `$options` flags.
141
- *
142
- * If not set, compiling a condition with a non-empty `$options` fails.
143
- *
144
- * @param func The custom regex function to use.
145
- */
146
- void mongory_regex_options_func_set(mongory_regex_options_func func);
147
-
148
- /**
149
- * @brief Sets the custom regex stringify function.
150
- *
151
- * If not called, a default function that always returns NULL is used.
152
- *
153
- * @param func The custom regex stringify function to use.
154
- */
155
- void mongory_regex_stringify_func_set(mongory_regex_stringify_func func);
156
-
157
- /**
158
- * @brief Sets the custom deep value conversion function.
159
- * @param deep_convert The function to use for deep conversions.
160
- */
161
- void mongory_value_converter_deep_convert_set(mongory_deep_convert_func deep_convert);
162
-
163
- /**
164
- * @brief Sets the custom shallow value conversion function.
165
- * @param shallow_convert The function to use for shallow conversions.
166
- */
167
- void mongory_value_converter_shallow_convert_set(mongory_shallow_convert_func shallow_convert);
168
-
169
- /**
170
- * @brief Sets the custom value recovery function.
171
- * @param recover The function to use for recovering external values.
172
- */
173
- void mongory_value_converter_recover_set(mongory_recover_func recover);
174
-
175
- /**
176
- * @brief Function pointer type for matching a value against an external matcher.
177
- * @param external_matcher The external reference to the matcher.
178
- * @param value The value to match against.
179
- * @return bool True if the value matches the matcher, false otherwise.
180
- */
181
- typedef struct mongory_matcher_custom_context {
182
- char *name; // Name of the custom matcher.
183
- void *external_matcher; // External reference to the custom matcher.
184
- } mongory_matcher_custom_context;
185
-
186
- void mongory_custom_matcher_match_func_set(bool (*match)(void *external_matcher, mongory_value *value));
187
- void mongory_custom_matcher_build_func_set(mongory_matcher_custom_context *(*build)(char *key, mongory_value *condition, void *extern_ctx));
188
- void mongory_custom_matcher_lookup_func_set(bool (*lookup)(char *key));
189
-
190
- void mongory_matcher_trace_result_colorful_set(bool colorful);
191
-
192
- #define MONGORY_DEFAULT_MAX_DEPTH 64
193
- #define MONGORY_DEFAULT_MAX_NODES 4096
194
-
195
- /**
196
- * @brief Sets the maximum condition nesting depth allowed during matcher compilation.
197
- * Pass 0 to disable the depth limit. Defaults to MONGORY_DEFAULT_MAX_DEPTH (64).
198
- */
199
- void mongory_max_depth_set(size_t max_depth);
200
-
201
- /**
202
- * @brief Returns the configured maximum condition nesting depth (0 = unlimited).
203
- */
204
- size_t mongory_max_depth_get(void);
205
-
206
- /**
207
- * @brief Sets the maximum number of matcher nodes allowed per compiled matcher.
208
- * Pass 0 to disable the node limit. Defaults to MONGORY_DEFAULT_MAX_NODES (4096).
209
- */
210
- void mongory_max_nodes_set(size_t max_nodes);
211
-
212
- /**
213
- * @brief Returns the configured maximum matcher node count (0 = unlimited).
214
- */
215
- size_t mongory_max_nodes_get(void);
216
-
217
- #endif /* MONGORY_FOUNDATIONS_CONFIG_H */
@@ -1,82 +0,0 @@
1
- #ifndef MONGORY_ERROR
2
- #define MONGORY_ERROR
3
-
4
- /**
5
- * @file error.h
6
- * @brief Defines error types and structures for the Mongory library.
7
- *
8
- * This file provides an enumeration of possible error codes and a structure
9
- * for representing error information, typically consisting of an error type
10
- * and an associated message.
11
- */
12
-
13
- #include <stdbool.h>
14
-
15
- /**
16
- * @def MONGORY_ERROR_TYPE_MAGIC
17
- * @brief A magic number used in generating enum values for error types.
18
- * Helps in creating somewhat unique integer values for the enums.
19
- */
20
- #define MONGORY_ERROR_TYPE_MAGIC 107
21
-
22
- /**
23
- * @def MONGORY_ERROR_TYPE_MACRO
24
- * @brief X-Macro for defining error types.
25
- * This macro allows defining the error enum, string conversion, etc.,
26
- * from a single list, promoting consistency.
27
- * Each entry takes the enum name, a numeric suffix, and a string description.
28
- */
29
- #define MONGORY_ERROR_TYPE_MACRO(_) \
30
- _(MONGORY_ERROR_NONE, 10, "No Error") \
31
- _(MONGORY_ERROR_MEMORY, 11, "Memory Allocation Error") \
32
- _(MONGORY_ERROR_INVALID_TYPE, 12, "Invalid Type Error") \
33
- _(MONGORY_ERROR_OUT_OF_BOUNDS, 13, "Out of Bounds Error") \
34
- _(MONGORY_ERROR_UNSUPPORTED_OPERATION, 14, "Unsupported Operation Error") \
35
- _(MONGORY_ERROR_INVALID_ARGUMENT, 15, "Invalid Argument Error") \
36
- _(MONGORY_ERROR_IO, 16, "I/O Error") \
37
- _(MONGORY_ERROR_PARSE, 17, "Parse Error") \
38
- _(MONGORY_ERROR_LIMIT_EXCEEDED, 18, "Limit Exceeded Error") \
39
- _(MONGORY_ERROR_UNKNOWN, 99, "Unknown Error")
40
-
41
- /**
42
- * @enum mongory_error_type
43
- * @brief Enumerates the types of errors that can occur within the Mongory
44
- * library. Values are generated using MONGORY_ERROR_TYPE_MACRO and
45
- * MONGORY_ERROR_TYPE_MAGIC.
46
- */
47
- typedef enum mongory_error_type {
48
- #define DEFINE_ERROR_ENUM(name, num, str) name = num * MONGORY_ERROR_TYPE_MAGIC,
49
- MONGORY_ERROR_TYPE_MACRO(DEFINE_ERROR_ENUM)
50
- #undef DEFINE_ERROR_ENUM
51
- } mongory_error_type;
52
-
53
- /**
54
- * @brief Converts a mongory_error_type enum to its string representation.
55
- *
56
- * @param type The error type enum value.
57
- * @return const char* A string describing the error type. Returns "Unknown
58
- * Error Type" if the type is not recognized.
59
- */
60
- const char *mongory_error_type_to_string(enum mongory_error_type type);
61
-
62
- /**
63
- * @struct mongory_error
64
- * @brief Structure to hold error information.
65
- *
66
- * Contains the type of error and an optional descriptive message.
67
- * The message string is expected to be a literal or have a lifetime
68
- * managed elsewhere (e.g., allocated from a memory pool).
69
- */
70
- typedef struct mongory_error {
71
- mongory_error_type type; /**< The type of the error. */
72
- const char *message; /**< A descriptive message for the error. This message
73
- might be a string literal or allocated from a
74
- memory pool. Its lifetime should be considered
75
- carefully. */
76
- } mongory_error;
77
-
78
- extern mongory_error MONGORY_ALLOC_ERROR;
79
- extern mongory_error MONGORY_DEPTH_LIMIT_ERROR;
80
- extern mongory_error MONGORY_NODE_LIMIT_ERROR;
81
-
82
- #endif /* MONGORY_ERROR */
@@ -1,115 +0,0 @@
1
- #ifndef MONGORY_MEMORY_POOL
2
- #define MONGORY_MEMORY_POOL
3
-
4
- /**
5
- * @file memory_pool.h
6
- * @brief Defines the mongory_memory_pool structure and its associated
7
- * operations.
8
- *
9
- * A memory pool is used for efficient memory management within the Mongory
10
- * library. It allows for allocating blocks of memory that can be freed
11
- * all at once when the pool itself is destroyed. This can reduce the overhead
12
- * of individual allocations and deallocations and help prevent memory leaks.
13
- */
14
-
15
- #include "mongory-core/foundations/error.h"
16
- #include <stdbool.h>
17
- #include <stdlib.h> // For size_t
18
-
19
- // Forward declaration of the memory pool structure.
20
- typedef struct mongory_memory_pool mongory_memory_pool;
21
-
22
- #define MG_ALLOC(p, n) (p->alloc(p, n))
23
- #define MG_ALLOC_PTR(p, t) ((t*)MG_ALLOC(p, sizeof(t)))
24
- #define MG_ALLOC_OBJ(p, t) ((t)MG_ALLOC(p, sizeof(t)))
25
- #define MG_ALLOC_ARY(p, t, n) ((t*)MG_ALLOC(p, sizeof(t) * (n)))
26
- /**
27
- * @struct mongory_memory_pool
28
- * @brief Represents a memory pool for managing memory allocations.
29
- *
30
- * The pool uses a context (`ctx`) to store its internal state, which typically
31
- * includes a list of allocated memory chunks.
32
- * It provides function pointers for allocation, tracing (optional), and freeing
33
- * the entire pool. An error field can store information about the last error
34
- * encountered during pool operations.
35
- */
36
- struct mongory_memory_pool {
37
- /**
38
- * @brief Allocates a block of memory from the pool.
39
- * @param ctx A pointer to the pool's internal context.
40
- * @param size The number of bytes to allocate.
41
- * @return void* A pointer to the allocated memory block, or NULL on failure.
42
- * Memory allocated this way is typically aligned.
43
- */
44
- void *(*alloc)(mongory_memory_pool *pool, size_t size);
45
-
46
- /**
47
- * @brief Traces an externally allocated memory block, associating it with the
48
- * pool.
49
- *
50
- * This is useful if memory is allocated outside the pool's `alloc` function
51
- * (e.g., by an external library) but its lifecycle should be tied to the
52
- * pool. When the pool is freed, traced memory blocks might also be freed
53
- * depending on the pool's implementation.
54
- *
55
- * @param ctx A pointer to the pool's internal context.
56
- * @param ptr A pointer to the memory block to trace.
57
- * @param size The size of the memory block.
58
- */
59
- void (*trace)(mongory_memory_pool *pool, void *ptr, size_t size);
60
-
61
- /**
62
- * @brief Resets the memory pool to its initial state.
63
- * @param ctx A pointer to the pool's internal context.
64
- */
65
- void (*reset)(mongory_memory_pool *pool);
66
-
67
- /**
68
- * @brief Frees the entire memory pool, including all memory blocks allocated
69
- * from it and any traced memory blocks (depending on implementation).
70
- * @param self A pointer to the mongory_memory_pool instance to be freed.
71
- */
72
- void (*free)(mongory_memory_pool *self);
73
-
74
- void *ctx; /**< Pointer to the internal context/state of the memory
75
- pool. This is managed by the pool implementation. */
76
- mongory_error *error; /**< Pointer to a mongory_error structure. If an
77
- operation fails (e.g., allocation), this may be set
78
- to describe the error. The memory for this error
79
- struct itself is typically allocated from the pool or
80
- is a static error object. */
81
- };
82
-
83
- #define MONGORY_DEFAULT_MAX_CHUNK_SIZE (4u * 1024u * 1024u)
84
-
85
- /**
86
- * @brief Creates a new mongory_memory_pool instance.
87
- *
88
- * Initializes the pool structure and its internal context, preparing it for
89
- * allocations.
90
- *
91
- * @return mongory_memory_pool* A pointer to the newly created memory pool, or
92
- * NULL if creation fails (e.g., initial memory allocation for the pool's
93
- * context failed).
94
- */
95
- mongory_memory_pool *mongory_memory_pool_new();
96
-
97
- /**
98
- * @brief Configures chunk growth cap and total byte budget for a core memory pool.
99
- *
100
- * Passing `0` for `max_chunk_size` restores `MONGORY_DEFAULT_MAX_CHUNK_SIZE`.
101
- * Passing `0` for `max_total_bytes` disables the total byte limit (unlimited).
102
- * Safe no-op if `pool` is NULL or uses a non-core custom pool implementation.
103
- *
104
- * @param pool The target memory pool.
105
- * @param max_chunk_size Maximum size for geometric chunk doubling (in bytes).
106
- * @param max_total_bytes Maximum cumulative chunk bytes allowed (0 = unlimited).
107
- */
108
- void mongory_memory_pool_set_limits(mongory_memory_pool *pool, size_t max_chunk_size, size_t max_total_bytes);
109
-
110
- /**
111
- * @brief Returns total chunk bytes currently allocated by a core memory pool.
112
- */
113
- size_t mongory_memory_pool_total_bytes(const mongory_memory_pool *pool);
114
-
115
- #endif /* MONGORY_MEMORY_POOL */
@@ -1,127 +0,0 @@
1
- #ifndef MONGORY_TABLE_H
2
- #define MONGORY_TABLE_H
3
-
4
- /**
5
- * @file table.h
6
- * @brief Defines the mongory_table structure (a hash table) and its
7
- * associated operations.
8
- *
9
- * mongory_table implements a hash table mapping string keys to mongory_value
10
- * pointers. It uses a mongory_memory_pool for its allocations and handles
11
- * operations like get, set, delete, and iteration over key-value pairs.
12
- * The table automatically resizes (rehashes) when its load factor is exceeded.
13
- */
14
-
15
- #include "mongory-core/foundations/array.h" // Used internally by the table
16
- #include "mongory-core/foundations/memory_pool.h"
17
- #include "mongory-core/foundations/value.h"
18
- #include <stdbool.h>
19
- #include <stddef.h> // For size_t
20
-
21
- // Forward declaration of the mongory_table structure.
22
- struct mongory_table;
23
- /**
24
- * @brief Alias for `struct mongory_table`.
25
- */
26
- typedef struct mongory_table mongory_table;
27
-
28
- /**
29
- * @brief Callback function type for iterating over key-value pairs in a table.
30
- * @param key The current key (null-terminated string).
31
- * @param value A pointer to the current mongory_value.
32
- * @param acc An accumulator or context pointer passed through the iteration.
33
- * @return bool Return true to continue iteration, false to stop.
34
- */
35
- typedef bool (*mongory_table_each_pair_callback_func)(char *key, mongory_value *value, void *acc);
36
-
37
- /**
38
- * @brief Function pointer type for retrieving a value by its key.
39
- * @param self A pointer to the mongory_table instance.
40
- * @param key The null-terminated string key.
41
- * @return mongory_value* A pointer to the mongory_value associated with the
42
- * key, or NULL if the key is not found.
43
- */
44
- typedef mongory_value *(*mongory_table_get_func)(mongory_table *self, char *key);
45
-
46
- /**
47
- * @brief Function pointer type for setting (adding or updating) a key-value
48
- * pair.
49
- * @param self A pointer to the mongory_table instance.
50
- * @param key The null-terminated string key. The table will make its own copy
51
- * of this key.
52
- * @param value A pointer to the mongory_value to associate with the key.
53
- * @return bool True if the operation was successful, false otherwise (e.g.,
54
- * memory allocation failure).
55
- */
56
- typedef bool (*mongory_table_set_func)(mongory_table *self, char *key, mongory_value *value);
57
-
58
- /**
59
- * @brief Function pointer type for iterating over all key-value pairs in the
60
- * table.
61
- * @param self A pointer to the mongory_table instance.
62
- * @param acc An accumulator or context pointer to be passed to the callback.
63
- * @param callback The function to be executed for each key-value pair.
64
- * @return bool True if the iteration completed fully, false if it was stopped
65
- * by a callback.
66
- */
67
- typedef bool (*mongory_table_each_func)(mongory_table *self, void *acc, mongory_table_each_pair_callback_func callback);
68
-
69
- /**
70
- * @brief Function pointer type for deleting a key-value pair from the table.
71
- * @param self A pointer to the mongory_table instance.
72
- * @param key The null-terminated string key of the pair to delete.
73
- * @return bool True if the key was found and deleted, false otherwise.
74
- */
75
- typedef bool (*mongory_table_del_func)(mongory_table *self, char *key);
76
-
77
- /**
78
- * @brief Creates a new mongory_table instance.
79
- *
80
- * Initializes the hash table with a default capacity and associates it with the
81
- * provided memory pool for all internal allocations.
82
- *
83
- * @param pool A pointer to the mongory_memory_pool to be used for allocations.
84
- * @return mongory_table* A pointer to the newly created mongory_table, or NULL
85
- * if creation fails (e.g., memory allocation failure).
86
- */
87
- mongory_table *mongory_table_new(mongory_memory_pool *pool);
88
-
89
- /**
90
- * @brief Creates a new mongory_table instance with a nested table.
91
- *
92
- * This function is used to create a new mongory_table instance with a nested
93
- * table. The nested table is created with the given memory pool and the given
94
- * key-value pairs. The nested table is returned as a pointer to the
95
- * mongory_table structure.
96
- *
97
- * @param pool A pointer to the mongory_memory_pool to be used for allocations.
98
- * @param argc The number of key-value pairs to be set.
99
- * @param ... The key-value pairs to be set.
100
- * @return mongory_table* A pointer to the newly created mongory_table, or NULL
101
- * if creation fails (e.g., memory allocation failure).
102
- */
103
- mongory_table *mongory_table_nested_wrap(mongory_memory_pool *pool, int argc, ...);
104
- #define MG_TABLE_WRAP(pool, n, ...) mongory_value_wrap_t(pool, mongory_table_nested_wrap(pool, n, __VA_ARGS__))
105
-
106
- /**
107
- * @struct mongory_table
108
- * @brief Represents a hash table mapping string keys to mongory_value pointers.
109
- *
110
- * Provides function pointers for common table operations, similar to an
111
- * object-oriented interface. The `count` field indicates the number of
112
- * key-value pairs currently in the table and should be treated as read-only by
113
- * users of the table.
114
- */
115
- struct mongory_table {
116
- mongory_memory_pool *pool; /**< The memory pool used for allocations. */
117
- size_t count; /**< Read-only. The current number of key-value pairs in the
118
- table. */
119
- mongory_table_get_func get; /**< Function to get a value by key. */
120
- mongory_table_each_func each; /**< Function to iterate over key-value pairs. */
121
- mongory_table_set_func set; /**< Function to set a key-value pair. */
122
- mongory_table_del_func del; /**< Function to delete a key-value pair. */
123
- };
124
-
125
- mongory_table *mongory_table_merge(mongory_table *table, mongory_table *other);
126
-
127
- #endif /* MONGORY_TABLE_H */