mongory 2.1.1 → 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 +22 -0
- data/README.md +45 -74
- data/docs/clang_bridge.md +2 -2
- data/docs/performance.md +28 -1
- 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 +67 -118
- 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/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
|
@@ -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
|
|
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
|
-
(
|
|
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
|
-
|
|
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,
|
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
154
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
290
|
-
(
|
|
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
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
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
|
-
|
|
309
|
-
|
|
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
|
-
|
|
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
|
|
48
|
-
return target.instance_exec(&convert_strategy) if other
|
|
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.'
|
|
99
|
-
raise 'A class or module is
|
|
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
|
|
105
|
+
@convert_strategy_map.clear
|
|
106
106
|
else
|
|
107
107
|
raise 'Support Symbol and block only.'
|
|
108
108
|
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
|
|
7
|
-
# and
|
|
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
|
|
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
|
|
16
|
+
# Used by ConditionConverter to normalize query keys from Ruby DSL input.
|
|
17
17
|
#
|
|
18
|
-
#
|
|
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
|
|
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
|
-
#
|
|
60
|
-
#
|
|
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
|
|
64
|
-
# @param value [Object] the value
|
|
65
|
-
# @return [Hash]
|
|
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
|
-
|
|
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
|
-
# -
|
|
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
|
|
17
|
-
# ValueConverter.instance.convert(/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
|
-
|
|
48
|
+
Mongory.condition_converter
|
|
49
49
|
end
|
|
50
50
|
end
|
|
51
51
|
end
|
data/lib/mongory/matchers.rb
CHANGED
|
@@ -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?(
|
|
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
|
|
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?(
|
|
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
|
|
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
|
#
|
data/lib/mongory/version.rb
CHANGED
data/lib/mongory.rb
CHANGED
data/script/coverage.rb
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'coverage'
|
|
4
|
+
|
|
5
|
+
# Prints per-file and total Ruby line/branch coverage using stdlib Coverage (zero gem deps).
|
|
6
|
+
module MongoryCoverageReporter
|
|
7
|
+
ROW_FORMAT = "%<label>-42s Line: %<lp>5.1f%% (%<hit_l>3d/%<tot_l>3d) Branch: %<bp>5.1f%% (%<hit_b>3d/%<tot_b>3d)\n"
|
|
8
|
+
|
|
9
|
+
def self.summarize(result, lib_root)
|
|
10
|
+
totals = { hit_l: 0, tot_l: 0, hit_b: 0, tot_b: 0 }
|
|
11
|
+
result.select { |path, _| path.start_with?(lib_root) }.sort.each do |file, data|
|
|
12
|
+
print_file_row(file.sub("#{lib_root}/", ''), data, totals)
|
|
13
|
+
end
|
|
14
|
+
print_row('TOTAL', totals)
|
|
15
|
+
totals
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.print_file_row(rel, data, totals)
|
|
19
|
+
row = count_hits(data)
|
|
20
|
+
row.each { |k, v| totals[k] += v }
|
|
21
|
+
print_row(rel, row)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.count_hits(data)
|
|
25
|
+
lines = data[:lines].compact
|
|
26
|
+
branches = data[:branches].values.flat_map(&:values)
|
|
27
|
+
{ hit_l: lines.count(&:positive?), tot_l: lines.size, hit_b: branches.count(&:positive?), tot_b: branches.size }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.pct(hit, total)
|
|
31
|
+
total.positive? ? (hit * 100.0 / total) : 100.0
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.print_row(label, counts)
|
|
35
|
+
printf(ROW_FORMAT, counts.merge(label: label,
|
|
36
|
+
lp: pct(counts[:hit_l], counts[:tot_l]),
|
|
37
|
+
bp: pct(counts[:hit_b], counts[:tot_b])))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.write_lcov(result, lib_root, out_path)
|
|
41
|
+
return if out_path.nil? || out_path.empty?
|
|
42
|
+
|
|
43
|
+
repo_root = File.expand_path('..', lib_root)
|
|
44
|
+
File.open(out_path, 'w') do |io|
|
|
45
|
+
result.select { |path, _| path.start_with?(lib_root) }.sort.each do |file, data|
|
|
46
|
+
write_lcov_record(io, file.sub("#{repo_root}/", ''), data[:lines])
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.write_lcov_record(io, rel_path, lines)
|
|
52
|
+
io.puts("SF:#{rel_path}")
|
|
53
|
+
lines.each_with_index { |hits, idx| io.puts("DA:#{idx + 1},#{hits}") unless hits.nil? }
|
|
54
|
+
io.puts('end_of_record')
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self.enforce_threshold!(totals)
|
|
58
|
+
min_line = Float(ENV.fetch('MIN_LINE_COVERAGE', '0'))
|
|
59
|
+
return if min_line <= 0 || totals[:tot_l].zero?
|
|
60
|
+
|
|
61
|
+
actual = totals[:hit_l] * 100.0 / totals[:tot_l]
|
|
62
|
+
return if actual >= min_line
|
|
63
|
+
|
|
64
|
+
warn format('ERROR: Ruby line coverage %<actual>.1f%% is below threshold %<min>.1f%%',
|
|
65
|
+
actual: actual, min: min_line)
|
|
66
|
+
exit(1)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
Coverage.start(lines: true, branches: true)
|
|
71
|
+
|
|
72
|
+
at_exit do
|
|
73
|
+
status = $ERROR_INFO
|
|
74
|
+
next if status.is_a?(SystemExit) && !status.success?
|
|
75
|
+
|
|
76
|
+
result = Coverage.result
|
|
77
|
+
lib_root = File.expand_path('../lib', __dir__)
|
|
78
|
+
totals = MongoryCoverageReporter.summarize(result, lib_root)
|
|
79
|
+
MongoryCoverageReporter.write_lcov(result, lib_root, ENV.fetch('LCOV_OUT', nil))
|
|
80
|
+
MongoryCoverageReporter.enforce_threshold!(totals)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
ARGV.replace(%w(spec --format progress)) if ARGV.empty?
|
|
84
|
+
load Gem.bin_path('rspec-core', 'rspec')
|