event-storage 1.3.5 → 1.4.1
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 +4 -3
- package/src/EventStore.js +144 -105
- package/src/FileHandlePool.js +105 -0
- package/src/Index/ReadOnlyIndex.js +1 -1
- package/src/Index/ReadableIndex.js +32 -14
- package/src/Index/WritableIndex.js +16 -13
- package/src/IndexMatcher.js +66 -1
- package/src/JoinEventStream.js +124 -82
- package/src/Partition/ReadOnlyPartition.js +1 -1
- package/src/Partition/ReadablePartition.js +54 -38
- package/src/Partition/WritablePartition.js +29 -16
- package/src/Storage/ReadOnlyStorage.js +1 -1
- package/src/Storage/ReadableStorage.js +23 -10
- package/src/Storage/WritableStorage.js +6 -7
- package/src/Watcher.js +1 -0
- 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
- package/src/PartitionPool.js +0 -149
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import events from 'events';
|
|
4
|
+
import FileHandlePool from '../FileHandlePool.js';
|
|
4
5
|
import { assert, alignTo, hash, binarySearch } from '../utils/util.js';
|
|
5
6
|
import { resolvePath } from "../utils/fsUtil.js";
|
|
6
7
|
|
|
@@ -41,9 +42,11 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
41
42
|
* @param {object} [config] An object with storage parameters.
|
|
42
43
|
* @param {string} [config.dataDirectory] The path where the storage data should reside. Default '.'.
|
|
43
44
|
* @param {number} [config.readBufferSize] Size of the read buffer in bytes. Default 4096.
|
|
45
|
+
* @param {object} [config.fileHandlePool] Shared file-handle pool used to reopen evicted handles on demand.
|
|
44
46
|
*/
|
|
45
47
|
constructor(name, config = {}) {
|
|
46
48
|
super();
|
|
49
|
+
this.setMaxListeners(0);
|
|
47
50
|
assert(typeof name === 'string' && name !== '', 'Must specify a partition name.');
|
|
48
51
|
|
|
49
52
|
let defaults = {
|
|
@@ -57,7 +60,9 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
57
60
|
this.id = ReadablePartition.idFor(name);
|
|
58
61
|
this.fileName = path.resolve(this.dataDirectory, this.name);
|
|
59
62
|
this.fileMode = 'r';
|
|
63
|
+
this.fileHandlePool = config.fileHandlePool || new FileHandlePool();
|
|
60
64
|
this.headerSize = 0;
|
|
65
|
+
this.opened = false;
|
|
61
66
|
|
|
62
67
|
this.readBufferSize = config.readBufferSize >>> 0; // jshint ignore:line
|
|
63
68
|
}
|
|
@@ -68,7 +73,21 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
68
73
|
* @returns {boolean}
|
|
69
74
|
*/
|
|
70
75
|
isOpen() {
|
|
71
|
-
return
|
|
76
|
+
return this.opened;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @returns {boolean}
|
|
81
|
+
*/
|
|
82
|
+
hasFileHandle() {
|
|
83
|
+
return this.fileHandlePool.has(this);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @returns {number}
|
|
88
|
+
*/
|
|
89
|
+
getFileHandle() {
|
|
90
|
+
return this.fileHandlePool.get(this);
|
|
72
91
|
}
|
|
73
92
|
|
|
74
93
|
/**
|
|
@@ -78,12 +97,10 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
78
97
|
* @returns {boolean} Returns false if the file is not a valid partition.
|
|
79
98
|
*/
|
|
80
99
|
open() {
|
|
81
|
-
if (this.
|
|
100
|
+
if (this.opened) {
|
|
82
101
|
return true;
|
|
83
102
|
}
|
|
84
|
-
|
|
85
|
-
this.fd = fs.openSync(this.fileName, this.fileMode);
|
|
86
|
-
|
|
103
|
+
this.opened = this.getFileHandle() !== null;
|
|
87
104
|
// allocUnsafeSlow because we don't need buffer pooling for these relatively long-lived buffers
|
|
88
105
|
this.readBuffer = Buffer.allocUnsafeSlow(this.readBufferSize);
|
|
89
106
|
// Where inside the file the read buffer starts
|
|
@@ -97,9 +114,13 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
97
114
|
return false;
|
|
98
115
|
}
|
|
99
116
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
117
|
+
try {
|
|
118
|
+
this.size -= this.readMetadata();
|
|
119
|
+
return true;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
this.close();
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
103
124
|
}
|
|
104
125
|
|
|
105
126
|
/**
|
|
@@ -113,8 +134,9 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
113
134
|
readMetadata() {
|
|
114
135
|
assert(this.size >= 16, `Invalid file.`);
|
|
115
136
|
|
|
137
|
+
const fd = this.getFileHandle();
|
|
116
138
|
const headerBuffer = Buffer.allocUnsafe(8 + 4);
|
|
117
|
-
fs.readSync(
|
|
139
|
+
fs.readSync(fd, headerBuffer, 0, 8 + 4, 0);
|
|
118
140
|
const headerMagic = headerBuffer.toString('utf8', 0, 8);
|
|
119
141
|
|
|
120
142
|
assert(headerMagic.substring(0, 6) === HEADER_MAGIC.substring(0, 6), `Invalid file header in partition ${this.name}.`);
|
|
@@ -127,7 +149,7 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
127
149
|
|
|
128
150
|
const metadataBuffer = Buffer.allocUnsafe(metadataSize - 1);
|
|
129
151
|
metadataBuffer.fill(" ");
|
|
130
|
-
fs.readSync(
|
|
152
|
+
fs.readSync(fd, metadataBuffer, 0, metadataSize - 1, 8 + 4);
|
|
131
153
|
const metadata = metadataBuffer.toString('utf8').trim();
|
|
132
154
|
try {
|
|
133
155
|
this.metadata = JSON.parse(metadata);
|
|
@@ -155,7 +177,7 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
155
177
|
* @returns {number} The file size not including the file header.
|
|
156
178
|
*/
|
|
157
179
|
readFileSize() {
|
|
158
|
-
const stat = fs.
|
|
180
|
+
const stat = fs.fstatSync(this.getFileHandle());
|
|
159
181
|
this.crtime = stat.birthtimeMs;
|
|
160
182
|
return stat.size - this.headerSize;
|
|
161
183
|
}
|
|
@@ -167,10 +189,8 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
167
189
|
* @returns void
|
|
168
190
|
*/
|
|
169
191
|
close() {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
this.fd = null;
|
|
173
|
-
}
|
|
192
|
+
this.fileHandlePool.evict(this, false);
|
|
193
|
+
this.opened = false;
|
|
174
194
|
if (this.readBuffer) {
|
|
175
195
|
this.readBuffer = null;
|
|
176
196
|
this.readBufferPos = -1;
|
|
@@ -185,7 +205,8 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
185
205
|
* @param {number} [from] The file position to start filling the read buffer from. Default 0.
|
|
186
206
|
*/
|
|
187
207
|
fillBuffer(from = 0) {
|
|
188
|
-
|
|
208
|
+
const fd = this.getFileHandle();
|
|
209
|
+
this.readBufferLength = fs.readSync(fd, this.readBuffer, 0, this.readBuffer.byteLength, this.headerSize + from);
|
|
189
210
|
this.readBufferPos = from;
|
|
190
211
|
}
|
|
191
212
|
|
|
@@ -250,7 +271,7 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
250
271
|
* @throws {CorruptFileError} if the document at the given position can not be read completely.
|
|
251
272
|
*/
|
|
252
273
|
readFrom(position, size = 0, headerOut = null, backwardsHint = false) {
|
|
253
|
-
|
|
274
|
+
const fd = this.getFileHandle();
|
|
254
275
|
assert((position % DOCUMENT_ALIGNMENT) === 0, `Invalid read position ${position}. Needs to be a multiple of ${DOCUMENT_ALIGNMENT}.`);
|
|
255
276
|
|
|
256
277
|
const { reader, bufferOffset } = this.selectReader(position, size, backwardsHint);
|
|
@@ -272,7 +293,7 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
272
293
|
|
|
273
294
|
if (dataSize + DOCUMENT_HEADER_SIZE > reader.buffer.byteLength) {
|
|
274
295
|
const tempReadBuffer = Buffer.allocUnsafe(dataSize);
|
|
275
|
-
fs.readSync(
|
|
296
|
+
fs.readSync(fd, tempReadBuffer, 0, dataSize, this.headerSize + position + DOCUMENT_HEADER_SIZE);
|
|
276
297
|
return tempReadBuffer;
|
|
277
298
|
}
|
|
278
299
|
|
|
@@ -333,7 +354,7 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
333
354
|
* @returns {number|boolean} The start position of the first document before the given position or false if no header could be found.
|
|
334
355
|
*/
|
|
335
356
|
findDocumentPositionBefore(position) {
|
|
336
|
-
|
|
357
|
+
this.getFileHandle();
|
|
337
358
|
if (position > 0) position -= (position % DOCUMENT_ALIGNMENT);
|
|
338
359
|
if (position <= 0) {
|
|
339
360
|
return false;
|
|
@@ -414,21 +435,14 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
414
435
|
*/
|
|
415
436
|
findDocument(sequenceNumber, min = true) {
|
|
416
437
|
const last = this.readLast();
|
|
417
|
-
if (!last) {
|
|
418
|
-
return null;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
if (min && last.header.sequenceNumber < sequenceNumber) {
|
|
438
|
+
if (!last || (min && last.header.sequenceNumber < sequenceNumber)) {
|
|
422
439
|
return null;
|
|
423
440
|
}
|
|
424
441
|
|
|
425
442
|
const [low, high] = binarySearch(
|
|
426
443
|
sequenceNumber,
|
|
427
444
|
this.size,
|
|
428
|
-
(pos) =>
|
|
429
|
-
const doc = this.readDocumentBefore(pos);
|
|
430
|
-
return doc ? doc.header.sequenceNumber : Number.MIN_SAFE_INTEGER;
|
|
431
|
-
}
|
|
445
|
+
(pos) => this.readDocumentBefore(pos)?.header.sequenceNumber ?? Number.MIN_SAFE_INTEGER
|
|
432
446
|
);
|
|
433
447
|
|
|
434
448
|
const position = this.findDocumentPositionBefore(min ? low : high);
|
|
@@ -503,24 +517,26 @@ class ReadablePartition extends events.EventEmitter {
|
|
|
503
517
|
return;
|
|
504
518
|
}
|
|
505
519
|
|
|
506
|
-
const
|
|
507
|
-
|
|
520
|
+
const doc = {
|
|
521
|
+
entry: { partition: this.id },
|
|
522
|
+
header: {}
|
|
523
|
+
};
|
|
508
524
|
const iterator = forwards
|
|
509
|
-
? this.readAll(found.position, header)
|
|
510
|
-
: this.readAllBackwards(found.position + this.documentWriteSize(found.header.dataSize), header);
|
|
525
|
+
? this.readAll(found.position, doc.header)
|
|
526
|
+
: this.readAllBackwards(found.position + this.documentWriteSize(found.header.dataSize), doc.header);
|
|
511
527
|
|
|
512
528
|
for (const data of iterator) {
|
|
513
|
-
if (header.sequenceNumber < lo || header.sequenceNumber > hi) {
|
|
529
|
+
if (doc.header.sequenceNumber < lo || doc.header.sequenceNumber > hi) {
|
|
514
530
|
return;
|
|
515
531
|
}
|
|
516
|
-
entry.number = header.sequenceNumber;
|
|
517
|
-
entry.position = header.position;
|
|
518
|
-
entry.size = header.dataSize;
|
|
519
|
-
|
|
532
|
+
doc.entry.number = doc.header.sequenceNumber;
|
|
533
|
+
doc.entry.position = doc.header.position;
|
|
534
|
+
doc.entry.size = doc.header.dataSize;
|
|
535
|
+
doc.data = data;
|
|
536
|
+
yield doc;
|
|
520
537
|
}
|
|
521
538
|
}
|
|
522
539
|
|
|
523
|
-
|
|
524
540
|
}
|
|
525
541
|
|
|
526
542
|
export default ReadablePartition;
|
|
@@ -58,7 +58,7 @@ class WritablePartition extends ReadablePartition {
|
|
|
58
58
|
* @returns {boolean}
|
|
59
59
|
*/
|
|
60
60
|
open() {
|
|
61
|
-
if (this.
|
|
61
|
+
if (this.opened) {
|
|
62
62
|
return true;
|
|
63
63
|
}
|
|
64
64
|
|
|
@@ -93,16 +93,26 @@ class WritablePartition extends ReadablePartition {
|
|
|
93
93
|
* @returns void
|
|
94
94
|
*/
|
|
95
95
|
close() {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
fs.fsyncSync(this.fd);
|
|
99
|
-
|
|
96
|
+
super.close();
|
|
97
|
+
if (this.writeBuffer) {
|
|
100
98
|
this.writeBuffer = null;
|
|
101
99
|
this.writeBufferCursor = 0;
|
|
102
100
|
this.writeBufferDocuments = 0;
|
|
103
101
|
this.writeMetaBuffer = null;
|
|
104
102
|
}
|
|
105
|
-
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Flush pending buffered writes before the pool closes the descriptor.
|
|
107
|
+
*
|
|
108
|
+
* @param {number} fd
|
|
109
|
+
*/
|
|
110
|
+
onBeforeClose(fd) {
|
|
111
|
+
if (!this.writeBuffer) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
this.flush();
|
|
115
|
+
fs.fsyncSync(fd);
|
|
106
116
|
}
|
|
107
117
|
|
|
108
118
|
/**
|
|
@@ -124,16 +134,17 @@ class WritablePartition extends ReadablePartition {
|
|
|
124
134
|
* @returns {boolean}
|
|
125
135
|
*/
|
|
126
136
|
flush() {
|
|
127
|
-
if (!this.
|
|
137
|
+
if (!this.isOpen()) {
|
|
128
138
|
return false;
|
|
129
139
|
}
|
|
130
140
|
if (this.writeBufferCursor === 0) {
|
|
131
141
|
return false;
|
|
132
142
|
}
|
|
133
143
|
|
|
134
|
-
|
|
144
|
+
const fd = this.getFileHandle();
|
|
145
|
+
fs.writeSync(fd, this.writeBuffer, 0, this.writeBufferCursor);
|
|
135
146
|
if (this.syncOnFlush) {
|
|
136
|
-
fs.fsyncSync(
|
|
147
|
+
fs.fsyncSync(fd);
|
|
137
148
|
}
|
|
138
149
|
|
|
139
150
|
this.writeBufferCursor = 0;
|
|
@@ -217,15 +228,16 @@ class WritablePartition extends ReadablePartition {
|
|
|
217
228
|
writeUnbuffered(data, dataSize, sequenceNumber, callback) {
|
|
218
229
|
this.flush();
|
|
219
230
|
this.writeDocumentHeader(this.writeMetaBuffer, 0, dataSize, sequenceNumber);
|
|
231
|
+
const fd = this.getFileHandle();
|
|
220
232
|
|
|
221
233
|
let bytesWritten = 0;
|
|
222
|
-
bytesWritten += fs.writeSync(
|
|
223
|
-
bytesWritten += fs.writeSync(
|
|
234
|
+
bytesWritten += fs.writeSync(fd, this.writeMetaBuffer, 0, DOCUMENT_HEADER_SIZE);
|
|
235
|
+
bytesWritten += fs.writeSync(fd, data);
|
|
224
236
|
const padSize = alignTo(dataSize + DOCUMENT_FOOTER_SIZE, DOCUMENT_ALIGNMENT);
|
|
225
|
-
bytesWritten += fs.writeSync(
|
|
237
|
+
bytesWritten += fs.writeSync(fd, DOCUMENT_PAD.substring(0, padSize));
|
|
226
238
|
this.writeMetaBuffer.writeUInt32BE(dataSize, 0);
|
|
227
|
-
bytesWritten += fs.writeSync(
|
|
228
|
-
bytesWritten += fs.writeSync(
|
|
239
|
+
bytesWritten += fs.writeSync(fd, this.writeMetaBuffer, 0, 4);
|
|
240
|
+
bytesWritten += fs.writeSync(fd, DOCUMENT_SEPARATOR);
|
|
229
241
|
if (typeof callback === 'function') {
|
|
230
242
|
process.nextTick(callback);
|
|
231
243
|
}
|
|
@@ -270,7 +282,7 @@ class WritablePartition extends ReadablePartition {
|
|
|
270
282
|
* @returns {number|boolean} The file position at which the data was written or false on error.
|
|
271
283
|
*/
|
|
272
284
|
write(data, sequenceNumber, callback) {
|
|
273
|
-
|
|
285
|
+
this.getFileHandle();
|
|
274
286
|
({ sequenceNumber, callback } = this.normalizeWriteArguments(sequenceNumber, callback));
|
|
275
287
|
const dataSize = Buffer.byteLength(data, 'utf8');
|
|
276
288
|
assert(dataSize <= 64 * 1024 * 1024, 'Document is too large! Maximum is 64 MB');
|
|
@@ -419,9 +431,10 @@ class WritablePartition extends ReadablePartition {
|
|
|
419
431
|
branchOff(branchName, position) {
|
|
420
432
|
const deletedBranch = new WritablePartition(this.name + '-' + branchName + '-' + position + '.branch', { dataDirectory: this.dataDirectory, metadata: { epoch: this.metadata.epoch } });
|
|
421
433
|
deletedBranch.open();
|
|
434
|
+
const branchFd = deletedBranch.getFileHandle();
|
|
422
435
|
do {
|
|
423
436
|
const reader = this.prepareReadBuffer(position);
|
|
424
|
-
fs.writeSync(
|
|
437
|
+
fs.writeSync(branchFd, reader.buffer, reader.cursor, reader.length);
|
|
425
438
|
position += reader.length;
|
|
426
439
|
} while (position < this.size);
|
|
427
440
|
deletedBranch.close();
|
|
@@ -155,7 +155,7 @@ class ReadOnlyStorage extends ReadableStorage {
|
|
|
155
155
|
processAppendedEntries(index, indexShortName, entries, startIndex = 0) {
|
|
156
156
|
for (let i = startIndex; i < entries.length; i++) {
|
|
157
157
|
const entry = entries[i];
|
|
158
|
-
if (!
|
|
158
|
+
if (!(entry.partition in this.partitions)) {
|
|
159
159
|
this.scheduleScan(() => {
|
|
160
160
|
this.processAppendedEntries(index, indexShortName, entries, i);
|
|
161
161
|
});
|
|
@@ -8,7 +8,7 @@ import { resolvePath, scanForFiles } from '../utils/fsUtil.js';
|
|
|
8
8
|
import { createHmac, matches, buildMetadataForMatcher } from '../utils/metadataUtil.js';
|
|
9
9
|
import { normalizeNamedCtorArgs } from '../utils/apiHelpers.js';
|
|
10
10
|
import IndexMatcher from '../IndexMatcher.js';
|
|
11
|
-
import
|
|
11
|
+
import FileHandlePool from '../FileHandlePool.js';
|
|
12
12
|
import ReadablePartition from "../Partition/ReadablePartition.js";
|
|
13
13
|
|
|
14
14
|
const DEFAULT_READ_BUFFER_SIZE = 4 * 1024;
|
|
@@ -27,6 +27,7 @@ const DEFAULT_MATCHER_PROPERTIES = ['stream', 'payload.type'];
|
|
|
27
27
|
* Partitions beyond this limit are evicted using LRU order. 0 disables the limit.
|
|
28
28
|
*/
|
|
29
29
|
const DEFAULT_MAX_OPEN_PARTITIONS = 1024;
|
|
30
|
+
const DEFAULT_MAX_OPEN_INDEXES = 1024;
|
|
30
31
|
|
|
31
32
|
/**
|
|
32
33
|
* @typedef {object|function(object):boolean} Matcher
|
|
@@ -58,9 +59,13 @@ class ReadableStorage extends events.EventEmitter {
|
|
|
58
59
|
* @param {number} [config.maxOpenPartitions] Maximum number of partition file descriptors kept open at one time.
|
|
59
60
|
* When the limit is reached the least-recently-used partition is closed to make room. 0 disables the limit.
|
|
60
61
|
* Default: 1024.
|
|
62
|
+
* @param {number} [config.maxOpenIndexes] Maximum number of index file descriptors kept open at one time.
|
|
63
|
+
* When the limit is reached the least-recently-used index handle is closed to make room. 0 disables the limit.
|
|
64
|
+
* Default: 1024.
|
|
61
65
|
*/
|
|
62
66
|
constructor(storageName = 'storage', config = {}) {
|
|
63
67
|
super();
|
|
68
|
+
this.setMaxListeners(0);
|
|
64
69
|
({ name: storageName, options: config } = normalizeNamedCtorArgs(storageName, config));
|
|
65
70
|
|
|
66
71
|
this.storageFile = storageName || 'storage';
|
|
@@ -72,7 +77,8 @@ class ReadableStorage extends events.EventEmitter {
|
|
|
72
77
|
hmacSecret: '',
|
|
73
78
|
metadata: {},
|
|
74
79
|
matcherProperties: DEFAULT_MATCHER_PROPERTIES,
|
|
75
|
-
maxOpenPartitions: DEFAULT_MAX_OPEN_PARTITIONS
|
|
80
|
+
maxOpenPartitions: DEFAULT_MAX_OPEN_PARTITIONS,
|
|
81
|
+
maxOpenIndexes: DEFAULT_MAX_OPEN_INDEXES
|
|
76
82
|
};
|
|
77
83
|
config = Object.assign(defaults, config);
|
|
78
84
|
this.serializer = config.serializer;
|
|
@@ -83,7 +89,9 @@ class ReadableStorage extends events.EventEmitter {
|
|
|
83
89
|
|
|
84
90
|
const partitionDefaults = { readBufferSize: DEFAULT_READ_BUFFER_SIZE };
|
|
85
91
|
this.partitionConfig = Object.assign(partitionDefaults, config);
|
|
86
|
-
this.
|
|
92
|
+
this.partitionHandlePool = new FileHandlePool(config.maxOpenPartitions);
|
|
93
|
+
this.indexHandlePool = new FileHandlePool(config.maxOpenIndexes);
|
|
94
|
+
this.partitions = Object.create(null);
|
|
87
95
|
|
|
88
96
|
// initialized: null = not started (or scan cancelled), false = in progress, true = done
|
|
89
97
|
this.initialized = null;
|
|
@@ -99,7 +107,7 @@ class ReadableStorage extends events.EventEmitter {
|
|
|
99
107
|
*/
|
|
100
108
|
createIndex(name, options = {}) {
|
|
101
109
|
/** @type ReadableIndex */
|
|
102
|
-
const index = new ReadOnlyIndex(name, options);
|
|
110
|
+
const index = new ReadOnlyIndex(name, { ...options, fileHandlePool: this.indexHandlePool });
|
|
103
111
|
return { index };
|
|
104
112
|
}
|
|
105
113
|
|
|
@@ -110,7 +118,7 @@ class ReadableStorage extends events.EventEmitter {
|
|
|
110
118
|
* @returns {ReadablePartition}
|
|
111
119
|
*/
|
|
112
120
|
createPartition(name, options = {}) {
|
|
113
|
-
return new ReadOnlyPartition(name, options);
|
|
121
|
+
return new ReadOnlyPartition(name, { ...options, fileHandlePool: this.partitionHandlePool });
|
|
114
122
|
}
|
|
115
123
|
|
|
116
124
|
/**
|
|
@@ -154,9 +162,9 @@ class ReadableStorage extends events.EventEmitter {
|
|
|
154
162
|
*/
|
|
155
163
|
registerPartitionFile(filename) {
|
|
156
164
|
const partitionId = ReadablePartition.idFor(filename);
|
|
157
|
-
if (!this.partitions
|
|
165
|
+
if (!(partitionId in this.partitions)) {
|
|
158
166
|
const partition = this.createPartition(filename, this.partitionConfig);
|
|
159
|
-
this.partitions
|
|
167
|
+
this.partitions[partition.id] = partition;
|
|
160
168
|
this.emit('partition-created', partition.id);
|
|
161
169
|
}
|
|
162
170
|
return partitionId;
|
|
@@ -254,6 +262,7 @@ class ReadableStorage extends events.EventEmitter {
|
|
|
254
262
|
for (let index of Object.values(this.readonlyIndexes)) {
|
|
255
263
|
index.close();
|
|
256
264
|
}
|
|
265
|
+
this.readonlyIndexes = {};
|
|
257
266
|
this.forEachPartition(partition => partition.close());
|
|
258
267
|
this.emit('closed');
|
|
259
268
|
}
|
|
@@ -268,8 +277,10 @@ class ReadableStorage extends events.EventEmitter {
|
|
|
268
277
|
* @throws {Error} If no such partition exists.
|
|
269
278
|
*/
|
|
270
279
|
getPartition(partitionIdentifier) {
|
|
271
|
-
assert(this.partitions
|
|
272
|
-
|
|
280
|
+
assert(partitionIdentifier in this.partitions, `Partition #${partitionIdentifier} does not exist.`);
|
|
281
|
+
const partition = this.partitions[partitionIdentifier];
|
|
282
|
+
partition.open();
|
|
283
|
+
return partition;
|
|
273
284
|
}
|
|
274
285
|
|
|
275
286
|
/**
|
|
@@ -559,7 +570,9 @@ class ReadableStorage extends events.EventEmitter {
|
|
|
559
570
|
return;
|
|
560
571
|
}
|
|
561
572
|
|
|
562
|
-
this.partitions
|
|
573
|
+
for (const partitionId of Object.keys(this.partitions)) {
|
|
574
|
+
iterationHandler(this.partitions[partitionId]);
|
|
575
|
+
}
|
|
563
576
|
}
|
|
564
577
|
|
|
565
578
|
}
|
|
@@ -177,8 +177,6 @@ class WritableStorage extends ReadableStorage {
|
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
this.forEachPartition(partition => partition.close());
|
|
180
|
-
// Partitions were closed directly (bypassing the pool), so reset the open-handle tracking.
|
|
181
|
-
this.partitions.clearOpenHandles();
|
|
182
180
|
}
|
|
183
181
|
|
|
184
182
|
/**
|
|
@@ -294,6 +292,7 @@ class WritableStorage extends ReadableStorage {
|
|
|
294
292
|
return callback(indexPosition);
|
|
295
293
|
}
|
|
296
294
|
});
|
|
295
|
+
this.emit('index-add', '_all', this.index.length, document);
|
|
297
296
|
return entry;
|
|
298
297
|
}
|
|
299
298
|
|
|
@@ -331,12 +330,12 @@ class WritableStorage extends ReadableStorage {
|
|
|
331
330
|
}
|
|
332
331
|
|
|
333
332
|
createNamedPartition(partitionId, partitionName, partitionShortName) {
|
|
334
|
-
if (this.partitions
|
|
333
|
+
if (partitionId in this.partitions) {
|
|
335
334
|
return;
|
|
336
335
|
}
|
|
337
336
|
const partitionConfig = this.buildPartitionConfig(partitionShortName);
|
|
338
337
|
this.ensurePartitionDirectory(partitionName);
|
|
339
|
-
this.partitions
|
|
338
|
+
this.partitions[partitionId] = this.createPartition(partitionName, partitionConfig);
|
|
340
339
|
this.emit('partition-created', partitionId);
|
|
341
340
|
}
|
|
342
341
|
|
|
@@ -458,7 +457,7 @@ class WritableStorage extends ReadableStorage {
|
|
|
458
457
|
*/
|
|
459
458
|
forEachDistinctPartitionOf(entries, iterationHandler) {
|
|
460
459
|
const partitions = [];
|
|
461
|
-
const numPartitions = this.partitions.
|
|
460
|
+
const numPartitions = Object.keys(this.partitions).length;
|
|
462
461
|
for (let entry of entries) {
|
|
463
462
|
if (partitions.indexOf(entry.partition) >= 0) {
|
|
464
463
|
continue;
|
|
@@ -545,7 +544,7 @@ class WritableStorage extends ReadableStorage {
|
|
|
545
544
|
* @returns {{ index: WritableIndex, matcher: Matcher }}
|
|
546
545
|
*/
|
|
547
546
|
createIndex(name, options = {}) {
|
|
548
|
-
const index = new WritableIndex(name, options);
|
|
547
|
+
const index = new WritableIndex(name, { ...options, fileHandlePool: this.indexHandlePool });
|
|
549
548
|
let matcher;
|
|
550
549
|
|
|
551
550
|
// If the index contains a matcher (possibly a serialized function) we check HMAC
|
|
@@ -569,7 +568,7 @@ class WritableStorage extends ReadableStorage {
|
|
|
569
568
|
* @returns {WritablePartition}
|
|
570
569
|
*/
|
|
571
570
|
createPartition(name, config = {}) {
|
|
572
|
-
return new WritablePartition(name, config);
|
|
571
|
+
return new WritablePartition(name, { ...config, fileHandlePool: this.partitionHandlePool });
|
|
573
572
|
}
|
|
574
573
|
|
|
575
574
|
}
|
package/src/Watcher.js
CHANGED
|
@@ -229,6 +229,7 @@ class Watcher extends events.EventEmitter {
|
|
|
229
229
|
*/
|
|
230
230
|
constructor(fileOrDirectory, fileFilter = null, options = null) {
|
|
231
231
|
super();
|
|
232
|
+
this.setMaxListeners(0);
|
|
232
233
|
const watchOptions = Object.assign({ recursive: true }, options);
|
|
233
234
|
delete watchOptions.rootDirectory;
|
|
234
235
|
this.relativePrefixes = [];
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determine whether a value is a DcbQuery object (plain object with an `items` property).
|
|
3
|
+
*
|
|
4
|
+
* @param {any} value
|
|
5
|
+
* @returns {boolean}
|
|
6
|
+
*/
|
|
7
|
+
function isDcbQuery(value) {
|
|
8
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value) && 'items' in value;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Compile a {@link DcbQuery} into a selector-algebra array suitable for JoinEventStream.
|
|
13
|
+
*
|
|
14
|
+
* Each item in `query.items` produces one sub-selector (OR at the top level).
|
|
15
|
+
* Within an item, tags and types are combined with AND semantics; multiple types form a nested OR.
|
|
16
|
+
*
|
|
17
|
+
* @param {object} query - Object with a non-empty `items` array.
|
|
18
|
+
* @param {function(string): string} resolveType - Maps a type name to its stream name. Throws when
|
|
19
|
+
* `typeAccessor` is not configured.
|
|
20
|
+
* @param {function(string): string} resolveTag - Maps a tag value to its namespaced stream name
|
|
21
|
+
* (e.g. `'tags/' + tag`). Throws when `tagsAccessor` is not configured.
|
|
22
|
+
* @returns {Array} Selector-algebra array (non-empty).
|
|
23
|
+
* @throws {Error} When `items` is missing, null, or empty.
|
|
24
|
+
*/
|
|
25
|
+
function compileDcbQuery(query, resolveType, resolveTag) {
|
|
26
|
+
if (!Array.isArray(query.items) || query.items.length === 0) {
|
|
27
|
+
throw new Error("DcbQuery.items must be a non-empty array. Use ['_all'] for an unconstrained read.");
|
|
28
|
+
}
|
|
29
|
+
return query.items.map(item => compileDcbItem(item, resolveType, resolveTag));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Compile a single DcbQuery item into a sub-selector.
|
|
34
|
+
*
|
|
35
|
+
* An item with no constraints (empty/missing `types` and `tags`) compiles to `'_all'`.
|
|
36
|
+
* Explicit `null` on `types` or `tags` throws — it is almost always a programming error.
|
|
37
|
+
*
|
|
38
|
+
* @param {object} item - QueryItem with optional `types: string[]` and `tags: string[]`.
|
|
39
|
+
* @param {function(string): string} resolveType
|
|
40
|
+
* @param {function(string): string} resolveTag
|
|
41
|
+
* @returns {string|Array} Sub-selector for this item.
|
|
42
|
+
* @throws {Error} When item is null/undefined, or when `types`/`tags` is non-null and non-array.
|
|
43
|
+
*/
|
|
44
|
+
function compileDcbItem(item, resolveType, resolveTag) {
|
|
45
|
+
if (item === null || item === undefined) {
|
|
46
|
+
throw new Error('DcbQuery item must be an object, got ' + item);
|
|
47
|
+
}
|
|
48
|
+
if (item.tags !== undefined && !Array.isArray(item.tags)) {
|
|
49
|
+
throw new Error('DcbQuery item.tags must be an array or undefined, got ' + typeof item.tags);
|
|
50
|
+
}
|
|
51
|
+
if (item.types !== undefined && !Array.isArray(item.types)) {
|
|
52
|
+
throw new Error('DcbQuery item.types must be an array or undefined, got ' + typeof item.types);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const terms = [];
|
|
56
|
+
|
|
57
|
+
for (const tag of item.tags ?? []) {
|
|
58
|
+
terms.push(resolveTag(tag));
|
|
59
|
+
}
|
|
60
|
+
if (item.types?.length) {
|
|
61
|
+
const typeStreams = item.types.map(resolveType);
|
|
62
|
+
terms.push(typeStreams.length === 1 ? typeStreams[0] : typeStreams);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (terms.length === 0) return '_all';
|
|
66
|
+
if (terms.length === 1 && typeof terms[0] === 'string') return terms[0];
|
|
67
|
+
return terms; // inner array = AND level
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export {isDcbQuery, compileDcbQuery};
|