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.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +55 -73
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +43 -7
  6. data/ext/mongory_ext/extconf.rb +15 -0
  7. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +28 -43
  8. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -1
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +3 -4
  10. data/ext/mongory_ext/mongory-core/src/foundations/array.c +28 -5
  11. data/ext/mongory_ext/mongory-core/src/foundations/config.c +24 -22
  12. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +3 -3
  13. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +22 -13
  14. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +4 -3
  15. data/ext/mongory_ext/mongory-core/src/foundations/table.c +60 -224
  16. data/ext/mongory_ext/mongory-core/src/foundations/table_private.h +36 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +30 -47
  18. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +2 -5
  19. data/ext/mongory_ext/mongory-core/src/foundations/value.c +18 -32
  20. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +89 -104
  21. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +18 -11
  22. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +35 -150
  23. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +244 -418
  24. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +30 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +15 -56
  26. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -39
  27. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +14 -15
  28. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +382 -74
  29. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +373 -123
  30. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +20 -12
  31. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -4
  32. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +214 -171
  33. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +8 -16
  34. data/ext/mongory_ext/mongory_ext.c +26 -4
  35. data/ext/mongory_ext/mongory_ext_internal.h +15 -3
  36. data/ext/mongory_ext/rb_mongory_adapters.c +52 -19
  37. data/ext/mongory_ext/rb_mongory_matcher.c +115 -36
  38. data/ext/mongory_ext/rb_mongory_value.c +124 -51
  39. data/lib/mongory/converters/abstract_converter.rb +5 -5
  40. data/lib/mongory/converters/converted.rb +0 -4
  41. data/lib/mongory/converters/key_converter.rb +13 -36
  42. data/lib/mongory/converters/value_converter.rb +4 -4
  43. data/lib/mongory/matchers.rb +8 -7
  44. data/lib/mongory/query_builder.rb +9 -0
  45. data/lib/mongory/utils/context.rb +0 -6
  46. data/lib/mongory/utils/singleton_builder.rb +1 -4
  47. data/lib/mongory/version.rb +1 -1
  48. data/lib/mongory.rb +0 -1
  49. data/script/bench.rb +77 -4
  50. data/script/coverage.rb +84 -0
  51. metadata +7 -7
  52. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  53. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
@@ -3,7 +3,7 @@
3
3
  * @brief Ruby <-> mongory_value conversion, lazy wrappers, and key caches.
4
4
  */
5
5
  #include "mongory_ext_internal.h"
6
- #include <string.h>
6
+ #include "mongory-core/src/foundations/memory_pool_private.h"
7
7
 
8
8
  typedef struct {
9
9
  mongory_table *table;
@@ -11,10 +11,25 @@ typedef struct {
11
11
  } hash_conv_ctx;
12
12
 
13
13
  static char *rb_mongory_value_to_cstr(mongory_value *value, mongory_memory_pool *pool) {
14
- (void)pool;
14
+ if (!pool) return NULL;
15
+ if (!value || !value->origin) {
16
+ return rb_mongory_pool_strdup(pool, "null");
17
+ }
15
18
  VALUE rb_value = (VALUE)value->origin;
16
19
  VALUE rb_str = rb_funcall(rb_value, rb_intern("inspect"), 0);
17
- return StringValueCStr(rb_str);
20
+ const char *cstr = StringValueCStr(rb_str);
21
+ char *copy = rb_mongory_pool_strdup(pool, cstr);
22
+ RB_GC_GUARD(rb_str);
23
+ return copy;
24
+ }
25
+
26
+ static mongory_value *rb_wrap_unsupported(mongory_memory_pool *pool, VALUE rb_value) {
27
+ mongory_value *mg_value = mongory_value_wrap_u(pool, (void *)rb_value);
28
+ if (mg_value) {
29
+ mg_value->origin = (void *)rb_value;
30
+ mg_value->to_str = rb_mongory_value_to_cstr;
31
+ }
32
+ return mg_value;
18
33
  }
19
34
 
20
35
  static mongory_value *rb_to_mongory_value_primitive(mongory_memory_pool *pool, VALUE rb_value) {
@@ -33,11 +48,11 @@ static mongory_value *rb_to_mongory_value_primitive(mongory_memory_pool *pool, V
33
48
  break;
34
49
 
35
50
  case T_FIXNUM:
36
- mg_value = mongory_value_wrap_i(pool, rb_num2long_inline(rb_value));
51
+ mg_value = mongory_value_wrap_i(pool, FIX2LONG(rb_value));
37
52
  break;
38
53
 
39
54
  case T_BIGNUM:
40
- mg_value = mongory_value_wrap_i(pool, rb_num2ll_inline(rb_value));
55
+ mg_value = mongory_value_wrap_i(pool, NUM2LL(rb_value));
41
56
  break;
42
57
 
43
58
  case T_FLOAT:
@@ -62,7 +77,7 @@ static mongory_value *rb_to_mongory_value_primitive(mongory_memory_pool *pool, V
62
77
  static mongory_value *rb_to_mongory_value_shallow_rec(mongory_memory_pool *pool, VALUE rb_value, bool converted) {
63
78
  mongory_value *mg_value = rb_to_mongory_value_primitive(pool, rb_value);
64
79
  if (mg_value) {
65
- mg_value->origin = rb_value;
80
+ mg_value->origin = (void *)rb_value;
66
81
  return mg_value;
67
82
  }
68
83
 
@@ -77,14 +92,26 @@ static mongory_value *rb_to_mongory_value_shallow_rec(mongory_memory_pool *pool,
77
92
 
78
93
  default:
79
94
  if (converted) {
80
- mg_value = mongory_value_wrap_u(pool, (void *)rb_value);
81
- break;
95
+ return rb_wrap_unsupported(pool, rb_value);
82
96
  } else {
83
97
  VALUE converted_value = rb_funcall(rb_mongory_data_converter, rb_intern("convert"), 1, rb_value);
84
- return rb_to_mongory_value_shallow_rec(pool, converted_value, true);
98
+ if (converted_value != rb_value) {
99
+ rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
100
+ if (rb_pool && rb_pool->scratch_refs_ptr) {
101
+ if (NIL_P(*rb_pool->scratch_refs_ptr)) {
102
+ *rb_pool->scratch_refs_ptr = rb_ary_new();
103
+ }
104
+ rb_ary_push(*rb_pool->scratch_refs_ptr, converted_value);
105
+ }
106
+ }
107
+ mongory_value *res = rb_to_mongory_value_shallow_rec(pool, converted_value, true);
108
+ RB_GC_GUARD(converted_value);
109
+ return res;
85
110
  }
86
111
  }
87
- mg_value->origin = rb_value;
112
+ if (mg_value) {
113
+ mg_value->origin = (void *)rb_value;
114
+ }
88
115
  return mg_value;
89
116
  }
90
117
 
@@ -109,26 +136,37 @@ static int hash_foreach_deep_convert_cb(VALUE key, VALUE val, VALUE ptr) {
109
136
  store_map = owner->string_map;
110
137
  }
111
138
  mongory_value *store = mongory_value_wrap_u(ctx->pool, NULL);
139
+ if (!store) {
140
+ return ST_STOP;
141
+ }
112
142
  store->origin = (void *)key;
113
143
  store_map->set(store_map, key_str, store);
114
144
  owner->mark_list->push(owner->mark_list, store);
115
145
  mongory_value *cval = rb_to_mongory_value_deep(ctx->pool, val);
146
+ if (!cval) {
147
+ return ST_STOP;
148
+ }
116
149
  ctx->table->set(ctx->table, key_str, cval);
150
+ RB_GC_GUARD(key);
117
151
  return ST_CONTINUE;
118
152
  }
119
153
 
120
154
  static mongory_value *rb_to_mongory_value_deep_rec(mongory_memory_pool *pool, VALUE rb_value, bool converted) {
121
155
  mongory_value *mg_value = rb_to_mongory_value_primitive(pool, rb_value);
122
156
  if (mg_value) {
123
- mg_value->origin = rb_value;
157
+ mg_value->origin = (void *)rb_value;
124
158
  return mg_value;
125
159
  }
126
160
  switch (TYPE(rb_value)) {
127
161
  case T_ARRAY: {
128
162
  mongory_array *array = mongory_array_new(pool);
163
+ if (!array) return NULL;
129
164
 
130
165
  for (long i = 0; i < RARRAY_LEN(rb_value); i++) {
131
- array->push(array, rb_to_mongory_value_deep(pool, RARRAY_AREF(rb_value, i)));
166
+ mongory_value *elem = rb_to_mongory_value_deep(pool, RARRAY_AREF(rb_value, i));
167
+ if (!elem || !array->push(array, elem)) {
168
+ return NULL;
169
+ }
132
170
  }
133
171
  mg_value = mongory_value_wrap_a(pool, array);
134
172
  break;
@@ -136,6 +174,7 @@ static mongory_value *rb_to_mongory_value_deep_rec(mongory_memory_pool *pool, VA
136
174
 
137
175
  case T_HASH: {
138
176
  mongory_table *table = mongory_table_new(pool);
177
+ if (!table) return NULL;
139
178
 
140
179
  hash_conv_ctx ctx = {table, pool};
141
180
  rb_hash_foreach(rb_value, hash_foreach_deep_convert_cb, (VALUE)&ctx);
@@ -146,15 +185,31 @@ static mongory_value *rb_to_mongory_value_deep_rec(mongory_memory_pool *pool, VA
146
185
 
147
186
  default:
148
187
  if (converted) {
149
- mg_value = mongory_value_wrap_u(pool, (void *)rb_value);
150
- break;
188
+ return rb_wrap_unsupported(pool, rb_value);
151
189
  } else {
152
190
  VALUE converted_value = rb_funcall(rb_mongory_data_converter, rb_intern("convert"), 1, rb_value);
153
- mg_value = rb_to_mongory_value_deep_rec(pool, converted_value, true);
154
- break;
191
+ if (converted_value != rb_value) {
192
+ rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
193
+ rb_mongory_matcher_t *owner = rb_pool ? rb_pool->owner : NULL;
194
+ if (owner && owner->mark_list) {
195
+ mongory_value *mark_entry = mongory_value_wrap_u(pool, NULL);
196
+ if (mark_entry) {
197
+ mark_entry->origin = (void *)converted_value;
198
+ owner->mark_list->push(owner->mark_list, mark_entry);
199
+ }
200
+ }
201
+ }
202
+ mongory_value *res = rb_to_mongory_value_deep_rec(pool, converted_value, true);
203
+ if (res) {
204
+ res->origin = (void *)rb_value;
205
+ }
206
+ RB_GC_GUARD(converted_value);
207
+ return res;
155
208
  }
156
209
  }
157
- mg_value->origin = rb_value;
210
+ if (mg_value) {
211
+ mg_value->origin = (void *)rb_value;
212
+ }
158
213
  return mg_value;
159
214
  }
160
215
 
@@ -190,17 +245,20 @@ typedef struct {
190
245
 
191
246
  static int rb_mongory_table_each_cb(VALUE key, VALUE val, VALUE arg) {
192
247
  rb_table_each_ctx_t *ctx = (rb_table_each_ctx_t *)arg;
248
+ VALUE key_str = Qnil;
193
249
  const char *key_cstr;
194
250
  if (RB_TYPE_P(key, T_SYMBOL)) {
195
251
  key_cstr = rb_id2name(rb_sym2id(key));
196
252
  } else if (RB_TYPE_P(key, T_STRING)) {
197
253
  key_cstr = StringValueCStr(key);
198
254
  } else {
199
- VALUE key_str = rb_funcall(key, rb_intern("to_s"), 0);
255
+ key_str = rb_funcall(key, rb_intern("to_s"), 0);
200
256
  key_cstr = StringValueCStr(key_str);
201
257
  }
202
258
  mongory_value *mg_val = rb_to_mongory_value_shallow(ctx->pool, val);
203
- if (!ctx->callback((char *)key_cstr, mg_val, ctx->acc)) {
259
+ bool cont = mg_val && ctx->callback((char *)key_cstr, mg_val, ctx->acc);
260
+ RB_GC_GUARD(key_str);
261
+ if (!cont) {
204
262
  ctx->completed = false;
205
263
  return ST_STOP;
206
264
  }
@@ -270,13 +328,11 @@ mongory_value *rb_mongory_array_wrap(mongory_memory_pool *pool, VALUE rb_array)
270
328
  return NULL;
271
329
  }
272
330
  memset(array, 0, sizeof(rb_mongory_array_t));
273
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
274
331
  array->base.pool = pool;
275
332
  array->base.get = rb_mongory_array_get;
276
333
  array->base.each = rb_mongory_array_each;
277
334
  array->rb_array = rb_array;
278
335
  array->base.count = RARRAY_LEN(rb_array);
279
- array->owner = rb_pool->owner;
280
336
  mongory_value *mg_value = mongory_value_wrap_a(pool, &array->base);
281
337
  if (!mg_value) {
282
338
  return NULL;
@@ -286,42 +342,59 @@ mongory_value *rb_mongory_array_wrap(mongory_memory_pool *pool, VALUE rb_array)
286
342
  return mg_value;
287
343
  }
288
344
 
289
- void *mongory_value_to_ruby(mongory_memory_pool *pool, mongory_value *value) {
290
- (void)pool;
291
- if (!value)
292
- return NULL;
293
- return value->origin;
345
+ static VALUE rb_mongory_build_string_key(const char *key) {
346
+ return rb_utf8_str_new_cstr(key);
294
347
  }
295
348
 
296
- VALUE rb_mongory_cache_fetch_string(rb_mongory_matcher_t *owner, const char *key) {
297
- if (!owner || !owner->string_map) return rb_utf8_str_new_cstr(key);
298
- mongory_value *v = owner->string_map->get(owner->string_map, (char *)key);
299
- if (v && v->origin) return (VALUE)v->origin;
300
- VALUE s = rb_utf8_str_new_cstr(key);
349
+ static VALUE rb_mongory_build_symbol_key(const char *key) {
350
+ rb_encoding *enc = rb_utf8_encoding();
351
+ long len = (long)strlen(key);
352
+ ID id = rb_check_id_cstr(key, len, enc);
353
+ if (!id) id = rb_intern3(key, len, enc);
354
+ return ID2SYM(id);
355
+ }
356
+
357
+ static VALUE rb_mongory_cache_fetch_key(rb_mongory_matcher_t *owner, mongory_table *map, const char *key,
358
+ VALUE (*build_fn)(const char *)) {
359
+ if (!owner || !map) {
360
+ return build_fn(key);
361
+ }
362
+ mongory_memory_pool_lock(owner->pool);
363
+ mongory_value *v = map->get(map, (char *)key);
364
+ if (v && v->origin) {
365
+ VALUE cached = (VALUE)v->origin;
366
+ mongory_memory_pool_unlock(owner->pool);
367
+ return cached;
368
+ }
369
+ mongory_memory_pool_unlock(owner->pool);
370
+
371
+ VALUE built = build_fn(key);
372
+
373
+ mongory_memory_pool_lock(owner->pool);
374
+ v = map->get(map, (char *)key);
375
+ if (v && v->origin) {
376
+ VALUE cached = (VALUE)v->origin;
377
+ mongory_memory_pool_unlock(owner->pool);
378
+ RB_GC_GUARD(built);
379
+ return cached;
380
+ }
301
381
  mongory_value *store = mongory_value_wrap_u(owner->pool, NULL);
302
- store->origin = (void *)s;
303
- owner->string_map->set(owner->string_map, (char *)key, store);
304
- owner->mark_list->push(owner->mark_list, store);
305
- return s;
382
+ if (store) {
383
+ store->origin = (void *)built;
384
+ map->set(map, (char *)key, store);
385
+ if (owner->mark_list) {
386
+ owner->mark_list->push(owner->mark_list, store);
387
+ }
388
+ }
389
+ mongory_memory_pool_unlock(owner->pool);
390
+ RB_GC_GUARD(built);
391
+ return built;
306
392
  }
307
393
 
308
- static inline VALUE char_key_to_symbol(const char *key, rb_encoding *enc) {
309
- ID id = rb_check_id_cstr(key, (long)strlen(key), enc);
310
- if (!id) id = rb_intern3(key, (long)strlen(key), enc);
311
- return ID2SYM(id);
394
+ VALUE rb_mongory_cache_fetch_string(rb_mongory_matcher_t *owner, const char *key) {
395
+ return rb_mongory_cache_fetch_key(owner, owner ? owner->string_map : NULL, key, rb_mongory_build_string_key);
312
396
  }
313
397
 
314
398
  VALUE rb_mongory_cache_fetch_symbol(rb_mongory_matcher_t *owner, const char *key) {
315
- rb_encoding *enc = rb_utf8_encoding();
316
- if (!owner || !owner->symbol_map) {
317
- return char_key_to_symbol(key, enc);
318
- }
319
- mongory_value *v = owner->symbol_map->get(owner->symbol_map, (char *)key);
320
- if (v && v->origin) return (VALUE)v->origin;
321
- VALUE sym = char_key_to_symbol(key, enc);
322
- mongory_value *store = mongory_value_wrap_u(owner->pool, NULL);
323
- store->origin = (void *)sym;
324
- owner->symbol_map->set(owner->symbol_map, (char *)key, store);
325
- owner->mark_list->push(owner->mark_list, store);
326
- return sym;
399
+ return rb_mongory_cache_fetch_key(owner, owner ? owner->symbol_map : NULL, key, rb_mongory_build_symbol_key);
327
400
  }
@@ -44,8 +44,8 @@ module Mongory
44
44
  def convert(target, other = NOTHING)
45
45
  convert_strategy = @convert_strategy_map[target.class] ||= find_strategy(target)
46
46
 
47
- return fallback(target, other) if convert_strategy == NOTHING
48
- return target.instance_exec(&convert_strategy) if other == NOTHING
47
+ return fallback(target, other) if NOTHING.equal?(convert_strategy)
48
+ return target.instance_exec(&convert_strategy) if NOTHING.equal?(other)
49
49
 
50
50
  target.instance_exec(other, &convert_strategy)
51
51
  end
@@ -95,14 +95,14 @@ module Mongory
95
95
  # @return [void]
96
96
  # @raise [RuntimeError] if input is invalid
97
97
  def register(klass, converter = nil, &block)
98
- raise 'converter or block is required.' if [converter, block].compact.empty?
99
- raise 'A class or module is reuqired.' unless klass.is_a?(Module)
98
+ raise 'converter or block is required.' unless converter || block
99
+ raise 'A class or module is required.' unless klass.is_a?(Module)
100
100
 
101
101
  if converter.is_a?(Symbol)
102
102
  register(klass) { |*args, &bl| send(converter, *args, &bl) }
103
103
  elsif block.is_a?(Proc)
104
104
  @registries.unshift(Registry.new(klass, block).freeze)
105
- @convert_strategy_map[klass] = block
105
+ @convert_strategy_map.clear
106
106
  else
107
107
  raise 'Support Symbol and block only.'
108
108
  end
@@ -39,10 +39,6 @@ module Mongory
39
39
  end
40
40
  end
41
41
 
42
- def deep_merge(other)
43
- dup.deep_merge!(Hash.new(other))
44
- end
45
-
46
42
  def deep_merge!(other)
47
43
  _deep_merge!(self, Hash.new(other))
48
44
  end
@@ -3,25 +3,21 @@
3
3
  module Mongory
4
4
  module Converters
5
5
  # KeyConverter handles transformation of field keys in query conditions.
6
- # It normalizes symbol keys into string paths, splits dotted keys,
7
- # and delegates to the appropriate converter logic.
6
+ # It normalizes symbol, integer, and QueryOperator keys into string keys;
7
+ # dotted-path expansion and escaped-dot unescaping are handled natively by `mongory-core`.
8
8
  #
9
9
  # This class inherits from AbstractConverter and provides specialized
10
10
  # handling for different key types:
11
- # - String keys with dots are split into nested paths
12
- # - Symbol keys are converted to strings
11
+ # - String keys are preserved verbatim for `mongory-core`
12
+ # - Symbol and Integer keys are converted to strings
13
13
  # - QueryOperator instances are handled via their DSL hooks
14
14
  # - Other types fall back to the parent converter
15
15
  #
16
- # Used by ConditionConverter to build query structures from flat input.
16
+ # Used by ConditionConverter to normalize query keys from Ruby DSL input.
17
17
  #
18
- # - `"a.b.c" => v` becomes `{ "a" => { "b" => { "c" => v } } }`
19
- # - Symbols are stringified and delegated to String logic
20
- # - QueryOperator dispatches to internal DSL hook
21
- #
22
- # @example Convert a dotted string key
18
+ # @example Preserve a dotted string key for `mongory-core`
23
19
  # KeyConverter.instance.convert("user.name", "John")
24
- # # => { "user" => { "name" => "John" } }
20
+ # # => { "user.name" => "John" }
25
21
  #
26
22
  # @example Convert a symbol key
27
23
  # KeyConverter.instance.convert(:status, "active")
@@ -56,33 +52,14 @@ module Mongory
56
52
  { target => other }
57
53
  end
58
54
 
59
- # Converts a dotted string key into nested hash form.
60
- # Splits the key on dots and builds a nested structure.
61
- # Handles escaped dots in the key.
55
+ # Preserves the string key verbatim; dotted path expansion and escaped-dot
56
+ # unescaping are handled natively by `mongory-core`.
62
57
  #
63
- # @param key [String] the dotted key string, e.g. "a.b.c"
64
- # @param value [Object] the value to assign at the deepest level
65
- # @return [Hash] nested hash structure
58
+ # @param key [String] the field key string
59
+ # @param value [Object] the condition value
60
+ # @return [Hash] single-entry hash `{ key => value }`
66
61
  def convert_string_key(key, value)
67
- return { '' => value } if key.empty?
68
-
69
- ret = {}
70
- *sub_keys, last_key = key.split(/(?<!\\)\./)
71
- last_hash = sub_keys.reduce(ret) do |res, sub_key|
72
- next_res = res[normalize_key(sub_key)] = {}
73
- next_res
74
- end
75
- last_hash[normalize_key(last_key)] = value
76
- ret
77
- end
78
-
79
- # Normalizes a key by unescaping escaped dots.
80
- # This allows for literal dots in field names.
81
- #
82
- # @param key [String] the key to normalize
83
- # @return [String] the normalized key
84
- def normalize_key(key)
85
- key.gsub(/\\\./, '.')
62
+ { key => value }
86
63
  end
87
64
  end
88
65
  end
@@ -9,12 +9,12 @@ module Mongory
9
9
  #
10
10
  # - Arrays are recursively converted
11
11
  # - Hashes are interpreted as nested conditions
12
- # - Regex becomes a Mongo-style `$regex` hash
12
+ # - Regexps are passed through as-is
13
13
  # - Strings and Integers are passed through
14
14
  # - Everything else falls back to DataConverter
15
15
  #
16
- # @example Convert a regex
17
- # ValueConverter.instance.convert(/foo/) #=> { "$regex" => "foo" }
16
+ # @example Pass through a regex
17
+ # ValueConverter.instance.convert(/foo/) #=> /foo/
18
18
  #
19
19
  class ValueConverter < AbstractConverter
20
20
  alias_method :super_convert, :convert
@@ -45,7 +45,7 @@ module Mongory
45
45
  #
46
46
  # @return [ConditionConverter] the condition converter instance
47
47
  def condition_converter
48
- @condition_converter ||= Mongory.condition_converter
48
+ Mongory.condition_converter
49
49
  end
50
50
  end
51
51
  end
@@ -26,6 +26,7 @@ module Mongory
26
26
  regex: '$regex',
27
27
  and: '$and',
28
28
  or: '$or',
29
+ nor: '$nor',
29
30
  not: '$not',
30
31
  elem_match: '$elemMatch',
31
32
  every: '$every',
@@ -130,20 +131,20 @@ module Mongory
130
131
  existing = Matchers.lookup(operator) || (Matchers.builtin?(operator) ? 'built-in' : nil)
131
132
  warn "Duplicate operator registration: #{operator} (#{existing} vs #{klass})" if existing
132
133
 
133
- return if operator.is_a?(String) && operator.match?(/^\$[a-z]+([A-Z][a-z]+)*$/)
134
+ return if operator.is_a?(String) && operator.match?(/\A\$[a-z]+([A-Z][a-z]+)*\z/)
134
135
 
135
- raise Mongory::TypeError, "Operator must match /^\$[a-z]+([A-Z][a-z]*)*$/, but got #{operator.inspect}"
136
+ raise Mongory::TypeError, "Operator must match /\\A\\$[a-z]+([A-Z][a-z]*)*\\z/, but got #{operator.inspect}"
136
137
  end
137
138
 
138
- # Validates the matcher class to ensure it is a subclass of AbstractMatcher.
139
+ # Validates the matcher class to ensure it is a named subclass of AbstractMatcher.
139
140
  #
140
141
  # @param klass [Class]
141
142
  # @return [void]
142
143
  # @raise [Mongory::TypeError] if class is not valid
143
144
  def self.validate_class(klass)
144
- return if klass.is_a?(Class) && klass < AbstractMatcher
145
+ return if klass.is_a?(Class) && klass < AbstractMatcher && !klass.name.nil? && !klass.name.empty?
145
146
 
146
- raise Mongory::TypeError, "Matcher class must be a subclass of AbstractMatcher, but got #{klass}"
147
+ raise Mongory::TypeError, "Matcher class must be a named subclass of AbstractMatcher, but got #{klass}"
147
148
  end
148
149
 
149
150
  # Validates the method symbol to ensure it is a valid lowercase underscore symbol (e.g., :gt, :not_match).
@@ -152,9 +153,9 @@ module Mongory
152
153
  # @return [void]
153
154
  # @raise [Mongory::TypeError] if symbol format is invalid
154
155
  def self.validate_method(method_sym)
155
- return if method_sym.is_a?(Symbol) && method_sym.match?(/^([a-z]+_)*[a-z]+$/)
156
+ return if method_sym.is_a?(Symbol) && method_sym.match?(/\A([a-z]+_)*[a-z]+\z/)
156
157
 
157
- raise Mongory::TypeError, "Method symbol must match /^([a-z]+_)*[a-z]+$/, but got #{method_sym.inspect}"
158
+ raise Mongory::TypeError, "Method symbol must match /\\A([a-z]+_)*[a-z]+\\z/, but got #{method_sym.inspect}"
158
159
  end
159
160
  end
160
161
  end
@@ -135,6 +135,15 @@ module Mongory
135
135
  self.and('$or' => conditions)
136
136
  end
137
137
 
138
+ # Adds a `$nor` query combined inside an `$and` block.
139
+ # None of the given conditions may match for a record to be included.
140
+ #
141
+ # @param conditions [Array<Hash>] the conditions to add
142
+ # @return [QueryBuilder] a new builder instance
143
+ def nor(*conditions)
144
+ self.and('$nor' => conditions)
145
+ end
146
+
138
147
  # Adds an `$in` condition to the query.
139
148
  # Matches records where the field value is in the given array.
140
149
  #
@@ -32,12 +32,6 @@ module Mongory
32
32
  new_context.config = @config.dup
33
33
  new_context
34
34
  end
35
-
36
- def to_hash
37
- {
38
- config: @config
39
- }
40
- end
41
35
  end
42
36
  end
43
37
  end
@@ -22,10 +22,7 @@ module Mongory
22
22
  "#<#{@label}>"
23
23
  end
24
24
 
25
- # @return [String] formatted label
26
- def to_s
27
- "#<#{@label}>"
28
- end
25
+ alias_method :to_s, :inspect
29
26
  end
30
27
  end
31
28
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mongory
4
- VERSION = '2.1.0'
4
+ VERSION = '2.2.0'
5
5
  end
data/lib/mongory.rb CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  require 'time'
4
4
  require 'date'
5
- require 'set'
6
5
  require 'singleton'
7
6
  require_relative 'mongory/version'
8
7
  require_relative 'mongory/utils'
data/script/bench.rb CHANGED
@@ -1,8 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'benchmark'
4
3
  require_relative '../lib/mongory'
5
4
 
5
+ HAS_MONGOID = begin
6
+ require 'mongoid'
7
+ true
8
+ rescue LoadError
9
+ false
10
+ end
11
+
6
12
  Mongory.register(Array)
7
13
  Mongory.enable_symbol_snippets!
8
14
 
@@ -12,11 +18,15 @@ ROUNDS = 5
12
18
  records = Array.new(N) do |i|
13
19
  {
14
20
  'age' => 10 + (i % 50),
15
- 'status' => (i % 3).zero? ? 'active' : 'inactive'
21
+ 'status' => (i % 3).zero? ? 'active' : 'inactive',
22
+ 'name' => (i % 5).zero? ? "user#{'abcdef'[i % 6]}" : "other_#{i}",
23
+ 'tags' => (i % 4).zero? ? %w(ruby go) : %w(js c)
16
24
  }
17
25
  end
18
26
 
19
- puts "Ruby: #{RUBY_VERSION} (#{RUBY_PLATFORM}), records=#{N}, rounds=#{ROUNDS}"
27
+ LARGE_IN_TARGETS = ['usera', 'userb'] + Array.new(498) { |i| "other-#{i + 2}" }
28
+
29
+ puts "Ruby: #{RUBY_VERSION} (#{RUBY_PLATFORM}), records=#{N}, rounds=#{ROUNDS}, mongoid=#{HAS_MONGOID}"
20
30
 
21
31
  def bench(label)
22
32
  # Warmup
@@ -27,7 +37,7 @@ def bench(label)
27
37
  elapsed_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0) * 1000.0) / ROUNDS
28
38
  ns_per_rec = (elapsed_ms * 1_000_000.0) / N
29
39
  puts format(
30
- '%<label>-34s %<ms>8.2f ms / 100k (%<ns>6.1f ns/rec, matched=%<count>d)',
40
+ '%<label>-38s %<ms>8.2f ms / 100k (%<ns>7.1f ns/rec, matched=%<count>d)',
31
41
  label: label,
32
42
  ms: elapsed_ms,
33
43
  ns: ns_per_rec,
@@ -35,6 +45,19 @@ def bench(label)
35
45
  )
36
46
  end
37
47
 
48
+ if HAS_MONGOID
49
+ class BenchDoc
50
+ include Mongoid::Document
51
+ field :age, type: Integer
52
+ field :status, type: String
53
+ field :name, type: String
54
+ field :tags, type: Array
55
+ end
56
+
57
+ mongoid_docs = records.map { |r| BenchDoc.new(r) }
58
+ end
59
+
60
+ # 1. simple
38
61
  bench('Plain Ruby (simple)') do
39
62
  records.count { |r| r['age'].is_a?(Numeric) && r['age'] >= 18 }
40
63
  end
@@ -44,6 +67,14 @@ bench('Mongory::Matcher (simple)') do
44
67
  records.count { |r| simple_m.match?(r) }
45
68
  end
46
69
 
70
+ if HAS_MONGOID
71
+ simple_expr = { 'age' => { '$gte' => 18 } }
72
+ bench('Mongoid::Matcher (simple)') do
73
+ mongoid_docs.count { |d| d._matches?(simple_expr) }
74
+ end
75
+ end
76
+
77
+ # 2. $or
47
78
  bench('Plain Ruby ($or)') do
48
79
  records.count { |r| (r['age'].is_a?(Numeric) && r['age'] >= 18) || r['status'] == 'active' }
49
80
  end
@@ -52,3 +83,45 @@ or_m = Mongory::Matcher.new({ :$or => [{ age: { :$gte => 18 } }, { status: 'acti
52
83
  bench('Mongory::Matcher ($or)') do
53
84
  records.count { |r| or_m.match?(r) }
54
85
  end
86
+
87
+ if HAS_MONGOID
88
+ or_expr = { '$or' => [{ 'age' => { '$gte' => 18 } }, { 'status' => 'active' }] }
89
+ bench('Mongoid::Matcher ($or)') do
90
+ mongoid_docs.count { |d| d._matches?(or_expr) }
91
+ end
92
+ end
93
+
94
+ # 3. $in on array field
95
+ bench('Plain Ruby (in_array_field)') do
96
+ records.count { |r| r['tags'].include?('ruby') || r['tags'].include?('rust') }
97
+ end
98
+
99
+ in_arr_m = Mongory::Matcher.new({ tags: { :$in => %w(ruby rust) } })
100
+ bench('Mongory::Matcher (in_array_field)') do
101
+ records.count { |r| in_arr_m.match?(r) }
102
+ end
103
+
104
+ if HAS_MONGOID
105
+ in_arr_expr = { 'tags' => { '$in' => %w(ruby rust) } }
106
+ bench('Mongoid::Matcher (in_array_field)') do
107
+ mongoid_docs.count { |d| d._matches?(in_arr_expr) }
108
+ end
109
+ end
110
+
111
+ # 4. large $in (500 items)
112
+ large_in_set = LARGE_IN_TARGETS.each_with_object({}) { |k, h| h[k] = true }
113
+ bench('Plain Ruby (large_in_500)') do
114
+ records.count { |r| large_in_set.key?(r['name']) }
115
+ end
116
+
117
+ large_in_m = Mongory::Matcher.new({ name: { :$in => LARGE_IN_TARGETS } })
118
+ bench('Mongory::Matcher (large_in_500)') do
119
+ records.count { |r| large_in_m.match?(r) }
120
+ end
121
+
122
+ if HAS_MONGOID
123
+ large_in_expr = { 'name' => { '$in' => LARGE_IN_TARGETS } }
124
+ bench('Mongoid::Matcher (large_in_500)') do
125
+ mongoid_docs.count { |d| d._matches?(large_in_expr) }
126
+ end
127
+ end