event-storage 1.3.5 → 1.4.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.
- package/README.md +41 -3
- package/package.json +2 -2
- package/src/EventStore.js +143 -105
- package/src/IndexMatcher.js +66 -1
- package/src/JoinEventStream.js +124 -82
- package/src/Partition/ReadablePartition.js +14 -19
- package/src/utils/dcbUtil.js +70 -0
- package/src/utils/indexUtil.js +225 -0
- package/src/utils/metadataUtil.js +109 -65
- package/src/utils/util.js +24 -1
|
@@ -9,7 +9,8 @@ import {
|
|
|
9
9
|
compareNumeric
|
|
10
10
|
} from './jsonUtil.js';
|
|
11
11
|
|
|
12
|
-
const
|
|
12
|
+
const compiledMatcherCache = new WeakMap();
|
|
13
|
+
const rawBufferMatcherCache = new WeakMap();
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* @param {any} value Value to classify.
|
|
@@ -37,24 +38,71 @@ function isOperatorObject(obj) {
|
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
/**
|
|
40
|
-
*
|
|
41
|
-
|
|
41
|
+
* @typedef {object|function(object):boolean} Matcher
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Pre-compile an object matcher into an array of per-property closure checks so the hot path
|
|
46
|
+
* avoids repeated `Object.getOwnPropertyNames` allocation and per-property type dispatch.
|
|
42
47
|
*
|
|
43
|
-
* @param {
|
|
44
|
-
* @
|
|
45
|
-
* @returns {boolean} True when both values match under matcher semantics.
|
|
48
|
+
* @param {object} matcher Plain object matcher to compile.
|
|
49
|
+
* @returns {function(any): boolean} Compiled predicate over documents.
|
|
46
50
|
*/
|
|
47
|
-
function
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
function compileMatcher(matcher) {
|
|
52
|
+
const checks = [];
|
|
53
|
+
for (const prop of Object.getOwnPropertyNames(matcher)) {
|
|
54
|
+
const matcherValue = matcher[prop];
|
|
55
|
+
if (isObject(matcherValue)) {
|
|
56
|
+
if (Array.isArray(matcherValue)) {
|
|
57
|
+
checks.push(doc => matcherValue.includes(doc[prop]));
|
|
58
|
+
} else if (isOperatorObject(matcherValue)) {
|
|
59
|
+
const operatorChecks = buildOperatorChecks(matcherValue);
|
|
60
|
+
checks.push(doc => matchesCompiledOperators(doc[prop], operatorChecks));
|
|
61
|
+
} else {
|
|
62
|
+
const nestedCheck = getCompiledMatcher(matcherValue);
|
|
63
|
+
checks.push(doc => nestedCheck(doc[prop]));
|
|
64
|
+
}
|
|
65
|
+
} else if (typeof matcherValue !== 'undefined') {
|
|
66
|
+
checks.push(doc => doc[prop] === matcherValue);
|
|
54
67
|
}
|
|
55
|
-
return matches(documentValue, matcherValue);
|
|
56
68
|
}
|
|
57
|
-
|
|
69
|
+
if (checks.length === 0) {
|
|
70
|
+
return () => true;
|
|
71
|
+
}
|
|
72
|
+
return function matchesCompiled(doc) {
|
|
73
|
+
// The undefined guard is needed for nested matchers where doc[prop] may be undefined.
|
|
74
|
+
if (typeof doc === 'undefined') return false;
|
|
75
|
+
for (const check of checks) {
|
|
76
|
+
if (!check(doc)) return false;
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Return a cached compiled predicate for `matcher`, compiling and caching on first access.
|
|
84
|
+
*
|
|
85
|
+
* @param {object} matcher Plain object matcher.
|
|
86
|
+
* @returns {function(any): boolean} Cached or freshly compiled predicate.
|
|
87
|
+
*/
|
|
88
|
+
function getCompiledMatcher(matcher) {
|
|
89
|
+
const cached = compiledMatcherCache.get(matcher);
|
|
90
|
+
if (cached) return cached;
|
|
91
|
+
const compiled = compileMatcher(matcher);
|
|
92
|
+
compiledMatcherCache.set(matcher, compiled);
|
|
93
|
+
return compiled;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {object} document The document to check against the matcher.
|
|
98
|
+
* @param {Matcher} matcher An object of properties and their values that need to match in the object or a function that checks if the document matches.
|
|
99
|
+
* @returns {boolean} True if the document matches the matcher or false otherwise.
|
|
100
|
+
*/
|
|
101
|
+
function matches(document, matcher) {
|
|
102
|
+
if (typeof document === 'undefined') return false;
|
|
103
|
+
if (typeof matcher === 'undefined') return true;
|
|
104
|
+
if (typeof matcher === 'function') return matcher(document);
|
|
105
|
+
return getCompiledMatcher(matcher)(document);
|
|
58
106
|
}
|
|
59
107
|
|
|
60
108
|
/**
|
|
@@ -86,6 +134,12 @@ function buildOperatorChecks(operatorObj) {
|
|
|
86
134
|
case '$ne':
|
|
87
135
|
checks.push(value => value !== expectedValue);
|
|
88
136
|
break;
|
|
137
|
+
case '$has':
|
|
138
|
+
checks.push(value => Array.isArray(value) && value.includes(expectedValue));
|
|
139
|
+
break;
|
|
140
|
+
case '$hasAny':
|
|
141
|
+
checks.push(value => Array.isArray(value) && expectedValue.some(v => value.includes(v)));
|
|
142
|
+
break;
|
|
89
143
|
default:
|
|
90
144
|
throw new TypeError(`Unknown operator: ${operator}`);
|
|
91
145
|
}
|
|
@@ -93,23 +147,6 @@ function buildOperatorChecks(operatorObj) {
|
|
|
93
147
|
return checks;
|
|
94
148
|
}
|
|
95
149
|
|
|
96
|
-
/**
|
|
97
|
-
* Return cached compiled checks for `operatorObj`, compiling and caching on first access.
|
|
98
|
-
* Using WeakMap keeps operator objects GC-eligible and avoids mutating user-supplied objects.
|
|
99
|
-
*
|
|
100
|
-
* @param {object} operatorObj Object containing operator/value pairs.
|
|
101
|
-
* @returns {Array<function(any): boolean>} Cached or newly compiled operator checks.
|
|
102
|
-
*/
|
|
103
|
-
function getCompiledOperatorChecks(operatorObj) {
|
|
104
|
-
const cachedChecks = compiledOperatorMatcherCache.get(operatorObj);
|
|
105
|
-
if (cachedChecks) {
|
|
106
|
-
return cachedChecks;
|
|
107
|
-
}
|
|
108
|
-
const checks = buildOperatorChecks(operatorObj);
|
|
109
|
-
compiledOperatorMatcherCache.set(operatorObj, checks);
|
|
110
|
-
return checks;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
150
|
/**
|
|
114
151
|
* @param {any} documentValue Parsed scalar value from the document.
|
|
115
152
|
* @param {Array<function(any): boolean>} checks Compiled operator checks.
|
|
@@ -159,29 +196,6 @@ const createHmac = secret => string => {
|
|
|
159
196
|
return hmac.digest('hex');
|
|
160
197
|
};
|
|
161
198
|
|
|
162
|
-
/**
|
|
163
|
-
* @typedef {object|function(object):boolean} Matcher
|
|
164
|
-
*/
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* @param {object} document The document to check against the matcher.
|
|
168
|
-
* @param {Matcher} matcher An object of properties and their values that need to match in the object or a function that checks if the document matches.
|
|
169
|
-
* @returns {boolean} True if the document matches the matcher or false otherwise.
|
|
170
|
-
*/
|
|
171
|
-
function matches(document, matcher) {
|
|
172
|
-
if (typeof document === 'undefined') return false;
|
|
173
|
-
if (typeof matcher === 'undefined') return true;
|
|
174
|
-
|
|
175
|
-
if (typeof matcher === 'function') return matcher(document);
|
|
176
|
-
|
|
177
|
-
for (let prop of Object.getOwnPropertyNames(matcher)) {
|
|
178
|
-
if (!propertyMatchesValue(document[prop], matcher[prop])) {
|
|
179
|
-
return false;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
return true;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
199
|
/**
|
|
186
200
|
* @param {Matcher} matcher The matcher object or function that should be serialized.
|
|
187
201
|
* @param {function(string)} hmac A function that calculates a HMAC of the given string.
|
|
@@ -218,16 +232,22 @@ function buildMatcherFromMetadata(matcherMetadata, hmac) {
|
|
|
218
232
|
}
|
|
219
233
|
|
|
220
234
|
/**
|
|
221
|
-
* Builds a factory function that, given a
|
|
222
|
-
*
|
|
235
|
+
* Builds a factory function that, given a scalar value, returns an object matcher targeting
|
|
236
|
+
* the given dot-notation payload path. Without an operator (or with `'$eq'`) the leaf is a
|
|
237
|
+
* plain scalar equality check; with an operator like `'$has'` the leaf is wrapped as
|
|
238
|
+
* `{[operator]: value}` so array-containment or other single-operator checks are produced.
|
|
223
239
|
*
|
|
224
|
-
* @param {string} payloadPath Dot-notation path relative to the event payload (e.g. `'type'`, `'
|
|
225
|
-
* @
|
|
240
|
+
* @param {string} payloadPath Dot-notation path relative to the event payload (e.g. `'type'`, `'tags'`).
|
|
241
|
+
* @param {string} [operator] Optional matcher operator (e.g. `'$has'`). Missing or `'$eq'` produces a scalar matcher.
|
|
242
|
+
* @returns {function(any): object} A function `(value) => objectMatcher`.
|
|
226
243
|
*/
|
|
227
|
-
function
|
|
244
|
+
function buildMatcherFn(payloadPath, operator) {
|
|
228
245
|
const parts = payloadPath.split('.');
|
|
229
|
-
|
|
230
|
-
|
|
246
|
+
const wrapLeaf = (!operator || operator === '$eq')
|
|
247
|
+
? (value) => value
|
|
248
|
+
: (value) => ({[operator]: value});
|
|
249
|
+
return function (value) {
|
|
250
|
+
let obj = wrapLeaf(value);
|
|
231
251
|
for (let i = parts.length - 1; i >= 0; i--) {
|
|
232
252
|
obj = {[parts[i]]: obj};
|
|
233
253
|
}
|
|
@@ -245,6 +265,12 @@ function buildTypeMatcherFn(payloadPath) {
|
|
|
245
265
|
*/
|
|
246
266
|
function buildRawBufferMatcher(matcher = {}, options = {}) {
|
|
247
267
|
assert(isPlainObject(matcher), 'Matcher must be an object.', TypeError);
|
|
268
|
+
|
|
269
|
+
const cached = rawBufferMatcherCache.get(matcher);
|
|
270
|
+
if (cached) {
|
|
271
|
+
return cached;
|
|
272
|
+
}
|
|
273
|
+
|
|
248
274
|
const enableOperatorBufferMatcher = options.enableOperatorBufferMatcher !== false;
|
|
249
275
|
|
|
250
276
|
const root = buildMatcherTree(matcher, enableOperatorBufferMatcher);
|
|
@@ -253,7 +279,7 @@ function buildRawBufferMatcher(matcher = {}, options = {}) {
|
|
|
253
279
|
return () => true;
|
|
254
280
|
}
|
|
255
281
|
|
|
256
|
-
|
|
282
|
+
const matchesRawBuffer = function (buffer) {
|
|
257
283
|
if (!isOpeningObject(buffer[0])) {
|
|
258
284
|
return false;
|
|
259
285
|
}
|
|
@@ -262,6 +288,8 @@ function buildRawBufferMatcher(matcher = {}, options = {}) {
|
|
|
262
288
|
}
|
|
263
289
|
return matchesNode(buffer, 1, root);
|
|
264
290
|
};
|
|
291
|
+
rawBufferMatcherCache.set(matcher, matchesRawBuffer);
|
|
292
|
+
return matchesRawBuffer;
|
|
265
293
|
}
|
|
266
294
|
|
|
267
295
|
/**
|
|
@@ -325,6 +353,22 @@ function buildMatcherTreeChild(key, value) {
|
|
|
325
353
|
child.pattern = keyPrefix;
|
|
326
354
|
const nePattern = [Buffer.from(JSON.stringify(value['$ne']), 'utf8')];
|
|
327
355
|
child.matches = (buffer, valueStart) => !matchesAnyValuePattern(buffer, valueStart, nePattern);
|
|
356
|
+
} else if ('$has' in value && Object.keys(value).length === 1) {
|
|
357
|
+
// A lone $has is a same-level array-containment check. The pattern locates the array
|
|
358
|
+
// opener (`"key":[`) and the follow-up scans array elements for the serialized scalar
|
|
359
|
+
// at the array's element level (skipping nested objects/arrays).
|
|
360
|
+
child.isKeyPattern = true;
|
|
361
|
+
child.pattern = Buffer.concat([keyPrefix, Buffer.from('[', 'utf8')]);
|
|
362
|
+
const valuePattern = Buffer.from(JSON.stringify(value['$has']), 'utf8');
|
|
363
|
+
child.matches = (buffer, valueStart) => indexOfSameLevel(buffer, valuePattern, valueStart) !== -1;
|
|
364
|
+
} else if ('$hasAny' in value && Object.keys(value).length === 1) {
|
|
365
|
+
// A lone $hasAny is an any-of array-containment check: matches when the document
|
|
366
|
+
// array contains at least one of the expected values.
|
|
367
|
+
child.isKeyPattern = true;
|
|
368
|
+
child.pattern = Buffer.concat([keyPrefix, Buffer.from('[', 'utf8')]);
|
|
369
|
+
const hasAnyPatterns = value['$hasAny'].map(v => Buffer.from(JSON.stringify(v), 'utf8'));
|
|
370
|
+
child.matches = (buffer, valueStart) =>
|
|
371
|
+
hasAnyPatterns.some(vp => indexOfSameLevel(buffer, vp, valueStart) !== -1);
|
|
328
372
|
} else if (isOperatorObject(value)) {
|
|
329
373
|
child.isKeyPattern = true;
|
|
330
374
|
child.pattern = keyPrefix;
|
|
@@ -502,7 +546,7 @@ function buildOperatorBufferMatcher(operatorObj) {
|
|
|
502
546
|
}
|
|
503
547
|
|
|
504
548
|
// Non-numeric expected value: use generic operator checks.
|
|
505
|
-
const operatorChecks =
|
|
549
|
+
const operatorChecks = buildOperatorChecks(operatorObj);
|
|
506
550
|
return (buffer, startOffset) => matchesOperatorInBuffer(buffer, startOffset, operatorChecks);
|
|
507
551
|
}
|
|
508
552
|
|
|
@@ -512,6 +556,6 @@ export {
|
|
|
512
556
|
buildMetadataHeader,
|
|
513
557
|
buildMetadataForMatcher,
|
|
514
558
|
buildMatcherFromMetadata,
|
|
515
|
-
|
|
559
|
+
buildMatcherFn,
|
|
516
560
|
buildRawBufferMatcher
|
|
517
561
|
};
|
package/src/utils/util.js
CHANGED
|
@@ -192,6 +192,28 @@ function getPropertyAtPath(obj, dotPath) {
|
|
|
192
192
|
return current;
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
+
/**
|
|
196
|
+
* Compiles a dot-notation path string into a reusable accessor function,
|
|
197
|
+
* avoiding repeated string splitting and iteration on every call.
|
|
198
|
+
*
|
|
199
|
+
* @param {string} dotPath
|
|
200
|
+
* @returns {function(object): *}
|
|
201
|
+
*/
|
|
202
|
+
function compileAccessor(dotPath) {
|
|
203
|
+
const parts = dotPath.split('.');
|
|
204
|
+
if (parts.length === 1) {
|
|
205
|
+
return (obj) => obj == null ? undefined : obj[dotPath];
|
|
206
|
+
}
|
|
207
|
+
return (obj) => {
|
|
208
|
+
let cur = obj;
|
|
209
|
+
for (const part of parts) {
|
|
210
|
+
if (cur == null || typeof cur !== 'object') return undefined;
|
|
211
|
+
cur = cur[part];
|
|
212
|
+
}
|
|
213
|
+
return cur;
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
195
217
|
export {
|
|
196
218
|
assert,
|
|
197
219
|
assertEqual,
|
|
@@ -201,5 +223,6 @@ export {
|
|
|
201
223
|
binarySearch,
|
|
202
224
|
alignTo,
|
|
203
225
|
kWayMerge,
|
|
204
|
-
getPropertyAtPath
|
|
226
|
+
getPropertyAtPath,
|
|
227
|
+
compileAccessor
|
|
205
228
|
};
|