mongory 0.7.6 → 0.7.8

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 (47) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +0 -14
  3. data/ext/mongory_ext/extconf.rb +19 -0
  4. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +122 -0
  5. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +161 -0
  6. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +79 -0
  7. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +95 -0
  8. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +127 -0
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +175 -0
  10. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +76 -0
  11. data/ext/mongory_ext/mongory-core/include/mongory-core.h +12 -0
  12. data/ext/mongory_ext/mongory-core/src/foundations/array.c +287 -0
  13. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +19 -0
  14. data/ext/mongory_ext/mongory-core/src/foundations/config.c +270 -0
  15. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +48 -0
  16. data/ext/mongory_ext/mongory-core/src/foundations/error.c +38 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +298 -0
  18. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +65 -0
  19. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +49 -0
  20. data/ext/mongory_ext/mongory-core/src/foundations/table.c +498 -0
  21. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +210 -0
  22. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +70 -0
  23. data/ext/mongory_ext/mongory-core/src/foundations/value.c +500 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +164 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.h +47 -0
  26. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +122 -0
  27. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +100 -0
  28. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +217 -0
  29. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +83 -0
  30. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +573 -0
  31. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +125 -0
  32. data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.c +147 -0
  33. data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.h +48 -0
  34. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +124 -0
  35. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +46 -0
  36. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +126 -0
  37. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +46 -0
  38. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +314 -0
  39. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.h +97 -0
  40. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +252 -0
  41. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +79 -0
  42. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +23 -0
  43. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +60 -0
  44. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +23 -0
  45. data/lib/mongory/version.rb +1 -1
  46. data/lib/mongory.rb +2 -2
  47. metadata +43 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e866b7fdded31e87ff8a44eb76c6bf7f2f592b18b4eb8a0e2fb7dd9e81549b6
4
- data.tar.gz: e3113b959720dc718885ade589ef05ec1acb7f05186373b5a667552532a33399
3
+ metadata.gz: 69f848ea0c1b1b71ad0ab165fb63ae29fcea07538f1ad04321df692fce003720
4
+ data.tar.gz: f92879ac8385d752b4b07881219bb45c73d4aad7b52c91bf52d75c94f085a400
5
5
  SHA512:
6
- metadata.gz: 44bcad683fd980c72e3feda8617f5c281e86b94fd519671d9dc5be67b1e9d05cc1abe5635bcd229061dfe35aa963d3ef716cf751437edeeea93ef87990258988
7
- data.tar.gz: 7957b3825577c1dcee98c1de067bcc3340a0e3525a20209a4dd3bdb5128b090c354ba6c78c18eaeb7d204e2b4bc6c70d1d41a9c44b09e0b95ebc843db34bf6c9
6
+ metadata.gz: 40dcf855a25c8ddd056eaed5a29f643dfb7636136171f2823b474379b305054568cf86608a02d020a88cc943a226ae3125e342d784546056490a7455eda8d381
7
+ data.tar.gz: 9c4539742f5b088704b88c7420ce792b9cd5c6068e444b6669f6cd36b574fc0f082caf344d8039a875521061223f09c62d0661580279681478ca913e0b04c122
data/Rakefile CHANGED
@@ -14,8 +14,6 @@ end
14
14
 
15
15
  # Add support for rake-compiler if available
16
16
  begin
17
- ENV['RUBY_CC_VERSION'] ||= '2.6.0:2.7.0:3.0.0:3.1.0:3.2.0:3.3.0'
18
-
19
17
  spec = Gem::Specification.load('mongory.gemspec')
20
18
 
21
19
  Rake::ExtensionTask.new('mongory_ext', spec) do |ext|
@@ -24,18 +22,6 @@ begin
24
22
  ext.ext_dir = 'ext/mongory_ext'
25
23
  ext.source_pattern = '*.c'
26
24
  ext.gem_spec = spec
27
- ext.cross_compile = true
28
- ext.cross_platform = [
29
- 'x86_64-linux',
30
- 'aarch64-linux',
31
- 'x86_64-darwin',
32
- 'arm64-darwin',
33
- # 'arm64-mingw-ucrt', # TODO: add this when we have a mingw-ucrt rake-compiler-dock image
34
- 'x64-mingw32',
35
- 'x64-mingw-ucrt',
36
- 'x86_64-linux-musl',
37
- 'aarch64-linux-musl'
38
- ]
39
25
  end
40
26
 
41
27
  # Add tasks for building with submodule
@@ -55,6 +55,13 @@ $CFLAGS << ' -Wno-declaration-after-statement -Wno-discarded-qualifiers'
55
55
  $CFLAGS << ' -O2' unless ENV['DEBUG']
56
56
  $CFLAGS << ' -g -O0 -DDEBUG' if ENV['DEBUG']
57
57
 
58
+ # --- macOS (darwin) linking: avoid hard-linking libruby, let symbols resolve at load time
59
+ if RbConfig::CONFIG['host_os'] =~ /darwin/
60
+ # Use dynamic_lookup so Ruby symbols (rb_*) are resolved by the host interpreter at dlopen time
61
+ $LDFLAGS << ' -Wl,-undefined,dynamic_lookup'
62
+ $DLDFLAGS << ' -Wl,-undefined,dynamic_lookup'
63
+ end
64
+
58
65
  # Let mkmf generate rules by listing all sources and corresponding objects
59
66
  $INCFLAGS << ' -I.'
60
67
  $INCFLAGS << " -I#{File.join(core_src_dir, 'foundations')}"
@@ -74,6 +81,18 @@ puts "Use 'make' to build the extension"
74
81
  # Append Makefile rules: explicit compilation rules for submodule sources to avoid copying/linking files
75
82
  mk = File.read('Makefile')
76
83
 
84
+ if RbConfig::CONFIG['host_os'] =~ /darwin/
85
+ # --- sanitize Makefile to ensure no hard link against Ruby framework/libruby ---
86
+ mk << <<~'MAKE'
87
+ # --- darwin: force unlink from libruby (final override) ---
88
+ LIBRUBYARG =
89
+ LIBRUBYARG_SHARED =
90
+ LIBRUBYARG_STATIC =
91
+ LIBRUBY =
92
+ LIBS =
93
+ MAKE
94
+ end
95
+
77
96
  # rubocop:disable Layout/HeredocIndentation
78
97
  rules = +"\n# --- custom rules for mongory-core submodule sources ---\n"
79
98
  (foundations_src + matchers_src).each do |src|
@@ -0,0 +1,122 @@
1
+ #ifndef MONGORY_ARRAY
2
+ #define MONGORY_ARRAY
3
+
4
+ /**
5
+ * @file array.h
6
+ * @brief Defines the mongory_array structure and its associated operations.
7
+ *
8
+ * mongory_array is a dynamic array implementation that stores mongory_value
9
+ * pointers. It handles its own memory management for the array storage using a
10
+ * provided mongory_memory_pool. The array can grow automatically as new
11
+ * elements are added.
12
+ */
13
+
14
+ #include "mongory-core/foundations/memory_pool.h"
15
+ #include "mongory-core/foundations/value.h"
16
+ #include <stdbool.h>
17
+
18
+ /**
19
+ * @brief Forward declaration for the mongory_array structure.
20
+ */
21
+ typedef struct mongory_array mongory_array;
22
+
23
+ /**
24
+ * @brief Callback function type used by mongory_array_each_func.
25
+ *
26
+ * This function is called for each item in the array during an iteration.
27
+ *
28
+ * @param item A pointer to the current mongory_value item in the array.
29
+ * @param acc An accumulator or context pointer passed through the iteration.
30
+ * @return bool Return true to continue iteration, false to stop.
31
+ */
32
+ typedef bool (*mongory_array_callback_func)(mongory_value *item, void *acc);
33
+
34
+ /**
35
+ * @brief Function pointer type for iterating over the array.
36
+ * @param self A pointer to the mongory_array instance.
37
+ * @param acc An accumulator or context pointer to be passed to the callback.
38
+ * @param func The callback function to be executed for each item.
39
+ * @return bool Returns true if the iteration completed fully, false if it was
40
+ * stopped by a callback.
41
+ */
42
+ typedef bool (*mongory_array_each_func)(mongory_array *self, void *acc, mongory_array_callback_func func);
43
+
44
+ /**
45
+ * @brief Function pointer type for adding an element to the end of the array.
46
+ * @param self A pointer to the mongory_array instance.
47
+ * @param value A pointer to the mongory_value to add.
48
+ * @return bool Returns true if the value was successfully added, false
49
+ * otherwise (e.g., memory allocation failure).
50
+ */
51
+ typedef bool (*mongory_array_push_func)(mongory_array *self, mongory_value *value);
52
+
53
+ /**
54
+ * @brief Function pointer type for retrieving an element at a specific index.
55
+ * @param self A pointer to the mongory_array instance.
56
+ * @param index The index of the element to retrieve.
57
+ * @return mongory_value* A pointer to the mongory_value at the given index, or
58
+ * NULL if the index is out of bounds.
59
+ */
60
+ typedef mongory_value *(*mongory_array_get_func)(mongory_array *self, size_t index);
61
+
62
+ /**
63
+ * @brief Function pointer type for setting or replacing an element at a
64
+ * specific index.
65
+ *
66
+ * If the index is beyond the current count, the array will be grown, and
67
+ * intermediate elements will be initialized to NULL.
68
+ *
69
+ * @param self A pointer to the mongory_array instance.
70
+ * @param index The index at which to set the value.
71
+ * @param value A pointer to the mongory_value to set.
72
+ * @return bool Returns true if the value was successfully set, false otherwise
73
+ * (e.g., memory allocation failure).
74
+ */
75
+ typedef bool (*mongory_array_set_func)(mongory_array *self, size_t index, mongory_value *value);
76
+
77
+ /**
78
+ * @brief Creates a new mongory_array instance.
79
+ *
80
+ * The array is initialized with a default capacity and will use the provided
81
+ * memory pool for all its internal allocations related to storing elements.
82
+ *
83
+ * @param pool A pointer to the mongory_memory_pool to be used for allocations.
84
+ * @return mongory_array* A pointer to the newly created mongory_array, or NULL
85
+ * if creation fails (e.g., memory allocation failure).
86
+ */
87
+ mongory_array *mongory_array_new(mongory_memory_pool *pool);
88
+
89
+ /**
90
+ * @brief Creates a new mongory_array instance with a nested array.
91
+ *
92
+ * This function is used to create a new mongory_array instance with a nested
93
+ * array. The nested array is created with the given memory pool and the given
94
+ * values. The nested array is returned as a pointer to the mongory_array
95
+ * structure.
96
+ *
97
+ * @param pool A pointer to the mongory_memory_pool to be used for allocations.
98
+ * @param argc The number of values to be set.
99
+ * @param ... The values to be set.
100
+ * @return mongory_array* A pointer to the newly created mongory_array, or NULL
101
+ * if creation fails (e.g., memory allocation failure).
102
+ */
103
+ mongory_array *mongory_array_nested_wrap(mongory_memory_pool *pool, int argc, ...);
104
+ #define MG_ARRAY_WRAP(pool, n, ...) mongory_value_wrap_a(pool, mongory_array_nested_wrap(pool, n, __VA_ARGS__))
105
+
106
+ /**
107
+ * @struct mongory_array
108
+ * @brief Represents a dynamic array of mongory_value pointers.
109
+ *
110
+ * This structure provides function pointers for common array operations,
111
+ * allowing for a somewhat object-oriented interface in C.
112
+ */
113
+ struct mongory_array {
114
+ size_t count; /**< The current number of elements in the array. */
115
+ mongory_memory_pool *pool; /**< The memory pool used for allocations. */
116
+ mongory_array_each_func each; /**< Function to iterate over elements in the array. */
117
+ mongory_array_push_func push; /**< Function to add an element to the end of the array. */
118
+ mongory_array_get_func get; /**< Function to retrieve an element by index. */
119
+ mongory_array_set_func set; /**< Function to set or replace an element at an index. */
120
+ };
121
+
122
+ #endif /* MONGORY_ARRAY */
@@ -0,0 +1,161 @@
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
+ * @param pool The memory pool to use for any allocations during the match.
25
+ * @param pattern The regex pattern (as a mongory_value, typically a string or
26
+ * regex type).
27
+ * @param value The value to match against (as a mongory_value, typically a
28
+ * string).
29
+ * @return bool True if the value matches the pattern, false otherwise.
30
+ */
31
+ typedef bool (*mongory_regex_func)(mongory_memory_pool *pool, mongory_value *pattern, mongory_value *value);
32
+
33
+ /**
34
+ * @brief Function pointer type for stringifying a regex pattern.
35
+ *
36
+ * This function is called when a regex pattern needs to be stringified.
37
+ *
38
+ * @param pool The memory pool to use for any allocations during the
39
+ * stringification.
40
+ * @param pattern The regex pattern (as a mongory_value, typically a string or
41
+ * regex type).
42
+ * @return char* A stringified representation of the regex pattern, or NULL on failure.
43
+ */
44
+ typedef char* (*mongory_regex_stringify_func)(mongory_memory_pool *pool, mongory_value *pattern);
45
+
46
+ /**
47
+ * @brief Function pointer type for deep conversion of an external value to a
48
+ * mongory_value.
49
+ *
50
+ * "Deep" implies that if the external value is a complex type (e.g., a struct
51
+ * representing a document or array), its contents should also be converted.
52
+ *
53
+ * @param pool The memory pool to use for allocating the new mongory_value and
54
+ * its data.
55
+ * @param value A pointer to the external value to convert.
56
+ * @return mongory_value* A new mongory_value representing the converted data,
57
+ * or NULL on failure.
58
+ */
59
+ typedef mongory_value *(*mongory_deep_convert_func)(mongory_memory_pool *pool, void *value);
60
+
61
+ /**
62
+ * @brief Function pointer type for shallow conversion of an external value to a
63
+ * mongory_value.
64
+ *
65
+ * "Shallow" implies that if the external value is complex, only a wrapper or
66
+ * pointer might be stored in the mongory_value, without deeply converting its
67
+ * contents.
68
+ *
69
+ * @param pool The memory pool to use for allocating the new mongory_value.
70
+ * @param value A pointer to the external value to convert.
71
+ * @return mongory_value* A new mongory_value, or NULL on failure.
72
+ */
73
+ typedef mongory_value *(*mongory_shallow_convert_func)(mongory_memory_pool *pool, void *value);
74
+
75
+ /**
76
+ * @brief Function pointer type for recovering an external value from a
77
+ * mongory_value.
78
+ *
79
+ * This is the reverse of a conversion, intended to get back the original
80
+ * external data representation if it was stored or wrapped.
81
+ *
82
+ * @param pool The memory pool (can be used if recovery involves allocation,
83
+ * though often it might just retrieve a stored pointer).
84
+ * @param value The mongory_value from which to recover the external data.
85
+ * @return void* A pointer to the recovered external value, or NULL if not
86
+ * possible.
87
+ */
88
+ typedef void *(*mongory_recover_func)(mongory_memory_pool *pool, mongory_value *value);
89
+
90
+ /**
91
+ * @brief Initializes the Mongory library.
92
+ *
93
+ * Sets up internal structures, including the global memory pool,
94
+ * regex adapter, matcher mapping, and value converter. This function
95
+ * should be called before any other Mongory library functions are used.
96
+ * It also registers the default set of matchers (e.g., $in, $eq).
97
+ */
98
+ void mongory_init();
99
+
100
+ /**
101
+ * @brief Cleans up resources used by the Mongory library.
102
+ *
103
+ * Frees the internal memory pool and resets global pointers. This should
104
+ * be called when the library is no longer needed to prevent memory leaks.
105
+ */
106
+ void mongory_cleanup();
107
+
108
+ /**
109
+ * @brief Sets the custom regex matching function.
110
+ *
111
+ * If not called, a default function that always returns false is used.
112
+ *
113
+ * @param func The custom regex function to use.
114
+ */
115
+ void mongory_regex_func_set(mongory_regex_func func);
116
+
117
+ /**
118
+ * @brief Sets the custom regex stringify function.
119
+ *
120
+ * If not called, a default function that always returns NULL is used.
121
+ *
122
+ * @param func The custom regex stringify function to use.
123
+ */
124
+ void mongory_regex_stringify_func_set(mongory_regex_stringify_func func);
125
+
126
+ /**
127
+ * @brief Sets the custom deep value conversion function.
128
+ * @param deep_convert The function to use for deep conversions.
129
+ */
130
+ void mongory_value_converter_deep_convert_set(mongory_deep_convert_func deep_convert);
131
+
132
+ /**
133
+ * @brief Sets the custom shallow value conversion function.
134
+ * @param shallow_convert The function to use for shallow conversions.
135
+ */
136
+ void mongory_value_converter_shallow_convert_set(mongory_shallow_convert_func shallow_convert);
137
+
138
+ /**
139
+ * @brief Sets the custom value recovery function.
140
+ * @param recover The function to use for recovering external values.
141
+ */
142
+ void mongory_value_converter_recover_set(mongory_recover_func recover);
143
+
144
+ /**
145
+ * @brief Function pointer type for matching a value against an external matcher.
146
+ * @param external_matcher The external reference to the matcher.
147
+ * @param value The value to match against.
148
+ * @return bool True if the value matches the matcher, false otherwise.
149
+ */
150
+ typedef struct mongory_matcher_custom_context {
151
+ char *name; // Name of the custom matcher.
152
+ void *external_matcher; // External reference to the custom matcher.
153
+ } mongory_matcher_custom_context;
154
+
155
+ void mongory_custom_matcher_match_func_set(bool (*match)(void *external_matcher, mongory_value *value));
156
+ void mongory_custom_matcher_build_func_set(mongory_matcher_custom_context *(*build)(char *key, mongory_value *condition, void *extern_ctx));
157
+ void mongory_custom_matcher_lookup_func_set(bool (*lookup)(char *key));
158
+
159
+ void mongory_matcher_trace_result_colorful_set(bool colorful);
160
+
161
+ #endif /* MONGORY_FOUNDATIONS_CONFIG_H */
@@ -0,0 +1,79 @@
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_UNKNOWN, 99, "Unknown Error")
39
+
40
+ /**
41
+ * @enum mongory_error_type
42
+ * @brief Enumerates the types of errors that can occur within the Mongory
43
+ * library. Values are generated using MONGORY_ERROR_TYPE_MACRO and
44
+ * MONGORY_ERROR_TYPE_MAGIC.
45
+ */
46
+ typedef enum mongory_error_type {
47
+ #define DEFINE_ERROR_ENUM(name, num, str) name = num * MONGORY_ERROR_TYPE_MAGIC,
48
+ MONGORY_ERROR_TYPE_MACRO(DEFINE_ERROR_ENUM)
49
+ #undef DEFINE_ERROR_ENUM
50
+ } mongory_error_type;
51
+
52
+ /**
53
+ * @brief Converts a mongory_error_type enum to its string representation.
54
+ *
55
+ * @param type The error type enum value.
56
+ * @return const char* A string describing the error type. Returns "Unknown
57
+ * Error Type" if the type is not recognized.
58
+ */
59
+ const char *mongory_error_type_to_string(enum mongory_error_type type);
60
+
61
+ /**
62
+ * @struct mongory_error
63
+ * @brief Structure to hold error information.
64
+ *
65
+ * Contains the type of error and an optional descriptive message.
66
+ * The message string is expected to be a literal or have a lifetime
67
+ * managed elsewhere (e.g., allocated from a memory pool).
68
+ */
69
+ typedef struct mongory_error {
70
+ mongory_error_type type; /**< The type of the error. */
71
+ const char *message; /**< A descriptive message for the error. This message
72
+ might be a string literal or allocated from a
73
+ memory pool. Its lifetime should be considered
74
+ carefully. */
75
+ } mongory_error;
76
+
77
+ extern mongory_error MONGORY_ALLOC_ERROR;
78
+
79
+ #endif /* MONGORY_ERROR */
@@ -0,0 +1,95 @@
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
+ /**
84
+ * @brief Creates a new mongory_memory_pool instance.
85
+ *
86
+ * Initializes the pool structure and its internal context, preparing it for
87
+ * allocations.
88
+ *
89
+ * @return mongory_memory_pool* A pointer to the newly created memory pool, or
90
+ * NULL if creation fails (e.g., initial memory allocation for the pool's
91
+ * context failed).
92
+ */
93
+ mongory_memory_pool *mongory_memory_pool_new();
94
+
95
+ #endif /* MONGORY_MEMORY_POOL */
@@ -0,0 +1,127 @@
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 */