event-storage 1.4.0 → 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/package.json +3 -2
- package/src/EventStore.js +1 -0
- 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/Partition/ReadOnlyPartition.js +1 -1
- package/src/Partition/ReadablePartition.js +40 -19
- 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/PartitionPool.js +0 -149
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "event-storage",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "An optimized embedded event store for node.js",
|
|
6
6
|
"keywords": [
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"src/JoinEventStream*.js",
|
|
40
40
|
"src/Partition*.js",
|
|
41
41
|
"src/Storage*.js",
|
|
42
|
+
"src/FileHandlePool.js",
|
|
42
43
|
"src/Watcher*.js",
|
|
43
44
|
"src/Clock*.js",
|
|
44
45
|
"src/Index/*.js",
|
|
@@ -67,6 +68,6 @@
|
|
|
67
68
|
"coveralls-next": "^6.0.1",
|
|
68
69
|
"expect.js": "^0.3.1",
|
|
69
70
|
"fs-extra": "^11.3.4",
|
|
70
|
-
"mocha": "^
|
|
71
|
+
"mocha": "^12.0.0"
|
|
71
72
|
}
|
|
72
73
|
}
|
package/src/EventStore.js
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* LRU pool for file descriptors shared across multiple file-backed objects.
|
|
5
|
+
*
|
|
6
|
+
* Targets are expected to expose `fileName` and `fileMode` and may implement
|
|
7
|
+
* `onBeforeClose(fd, evicted)` to flush state before their descriptor closes.
|
|
8
|
+
*/
|
|
9
|
+
class FileHandlePool {
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {number} [maxOpen=0] Maximum number of simultaneously open handles. 0 disables eviction.
|
|
13
|
+
*/
|
|
14
|
+
constructor(maxOpen = 0) {
|
|
15
|
+
this.maxOpen = maxOpen;
|
|
16
|
+
this.handles = new Map();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {object} target
|
|
21
|
+
* @returns {number}
|
|
22
|
+
*/
|
|
23
|
+
get(target) {
|
|
24
|
+
const handle = this.handles.get(target);
|
|
25
|
+
if (handle) {
|
|
26
|
+
this.touch(target);
|
|
27
|
+
return handle.fd;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
this.evictLeastRecentlyUsedIfNeeded(target);
|
|
31
|
+
const fd = fs.openSync(target.fileName, target.fileMode);
|
|
32
|
+
this.handles.set(target, { fd });
|
|
33
|
+
return fd;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {object} target
|
|
38
|
+
* @returns {boolean}
|
|
39
|
+
*/
|
|
40
|
+
has(target) {
|
|
41
|
+
return this.handles.has(target);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {object} target
|
|
46
|
+
* @param {boolean} [evicted=true]
|
|
47
|
+
* @returns {boolean}
|
|
48
|
+
*/
|
|
49
|
+
evict(target, evicted = true) {
|
|
50
|
+
const handle = this.handles.get(target);
|
|
51
|
+
if (!handle) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
target.onBeforeClose?.(handle.fd, evicted);
|
|
56
|
+
} finally {
|
|
57
|
+
this.handles.delete(target);
|
|
58
|
+
fs.closeSync(handle.fd);
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param {object} target
|
|
65
|
+
* @returns {void}
|
|
66
|
+
*/
|
|
67
|
+
touch(target) {
|
|
68
|
+
const handle = this.handles.get(target);
|
|
69
|
+
if (!handle) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
this.handles.delete(target);
|
|
73
|
+
this.handles.set(target, handle);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Evict the least-recently-used descriptor before opening another one.
|
|
78
|
+
*
|
|
79
|
+
* @param {object} target
|
|
80
|
+
*/
|
|
81
|
+
evictLeastRecentlyUsedIfNeeded(target) {
|
|
82
|
+
if (this.maxOpen <= 0) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
this.handles.delete(target);
|
|
86
|
+
if (this.handles.size < this.maxOpen) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const lruTarget = this.handles.keys().next().value;
|
|
90
|
+
if (!lruTarget) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.evict(lruTarget);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @returns {number}
|
|
98
|
+
*/
|
|
99
|
+
get openCount() {
|
|
100
|
+
return this.handles.size;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export default FileHandlePool;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import events from 'events';
|
|
3
3
|
import Entry, { assertValidEntryClass } from '../IndexEntry.js';
|
|
4
|
+
import FileHandlePool from '../FileHandlePool.js';
|
|
4
5
|
import { assert, wrapAndCheck, binarySearch } from '../utils/util.js';
|
|
5
6
|
import { normalizeNamedCtorArgs } from '../utils/apiHelpers.js';
|
|
6
7
|
import { resolvePath } from "../utils/fsUtil.js";
|
|
@@ -42,9 +43,11 @@ class ReadableIndex extends events.EventEmitter {
|
|
|
42
43
|
* @param {number} [options.writeBufferSize] The number of bytes to use for the write buffer. Default 4096.
|
|
43
44
|
* @param {number} [options.flushDelay] How many ms to delay the write buffer flush to optimize throughput. Default 100.
|
|
44
45
|
* @param {object} [options.metadata] An object containing the metadata information for this index. Will be written on initial creation and checked on subsequent openings.
|
|
46
|
+
* @param {object} [options.fileHandlePool] Shared file-handle pool used to reopen evicted handles on demand.
|
|
45
47
|
*/
|
|
46
48
|
constructor(name = '.index', options = {}) {
|
|
47
49
|
super();
|
|
50
|
+
this.setMaxListeners(0);
|
|
48
51
|
({ name, options } = normalizeNamedCtorArgs(name, options, '.index'));
|
|
49
52
|
let defaults = {
|
|
50
53
|
dataDirectory: '.',
|
|
@@ -65,8 +68,9 @@ class ReadableIndex extends events.EventEmitter {
|
|
|
65
68
|
initialize(options) {
|
|
66
69
|
/* @type Array<Entry> */
|
|
67
70
|
this.data = [];
|
|
68
|
-
this.
|
|
71
|
+
this.opened = false;
|
|
69
72
|
this.fileMode = 'r';
|
|
73
|
+
this.fileHandlePool = options.fileHandlePool || new FileHandlePool();
|
|
70
74
|
this.EntryClass = options.EntryClass;
|
|
71
75
|
this.dataDirectory = options.dataDirectory;
|
|
72
76
|
this.fileName = resolvePath(options.dataDirectory, this.name);
|
|
@@ -109,7 +113,21 @@ class ReadableIndex extends events.EventEmitter {
|
|
|
109
113
|
* @returns {boolean}
|
|
110
114
|
*/
|
|
111
115
|
isOpen() {
|
|
112
|
-
return
|
|
116
|
+
return this.opened;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @returns {boolean}
|
|
121
|
+
*/
|
|
122
|
+
hasFileHandle() {
|
|
123
|
+
return this.fileHandlePool.has(this);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @returns {number}
|
|
128
|
+
*/
|
|
129
|
+
getFileHandle() {
|
|
130
|
+
return this.fileHandlePool.get(this);
|
|
113
131
|
}
|
|
114
132
|
|
|
115
133
|
/**
|
|
@@ -120,7 +138,7 @@ class ReadableIndex extends events.EventEmitter {
|
|
|
120
138
|
* @throws {Error} If the file is corrupt or can not be read correctly.
|
|
121
139
|
*/
|
|
122
140
|
checkFile() {
|
|
123
|
-
const stat = fs.fstatSync(this.
|
|
141
|
+
const stat = fs.fstatSync(this.getFileHandle());
|
|
124
142
|
if (stat.size === 0) {
|
|
125
143
|
return -1;
|
|
126
144
|
}
|
|
@@ -145,11 +163,10 @@ class ReadableIndex extends events.EventEmitter {
|
|
|
145
163
|
* @throws {Error} if the file can not be opened.
|
|
146
164
|
*/
|
|
147
165
|
open() {
|
|
148
|
-
if (this.
|
|
166
|
+
if (this.opened) {
|
|
149
167
|
return false;
|
|
150
168
|
}
|
|
151
|
-
|
|
152
|
-
this.fd = fs.openSync(this.fileName, this.fileMode);
|
|
169
|
+
this.opened = this.getFileHandle() !== null;
|
|
153
170
|
|
|
154
171
|
this.readUntil = -1;
|
|
155
172
|
|
|
@@ -209,8 +226,9 @@ class ReadableIndex extends events.EventEmitter {
|
|
|
209
226
|
*/
|
|
210
227
|
readMetadata() {
|
|
211
228
|
if (this.headerSize > 0) return this.headerSize;
|
|
229
|
+
const fd = this.getFileHandle();
|
|
212
230
|
const headerBuffer = Buffer.allocUnsafe(8 + 4);
|
|
213
|
-
fs.readSync(
|
|
231
|
+
fs.readSync(fd, headerBuffer, 0, 8 + 4, 0);
|
|
214
232
|
const headerMagic = headerBuffer.toString('utf8', 0, 8);
|
|
215
233
|
|
|
216
234
|
assert(headerMagic.substring(0, 6) === HEADER_MAGIC.substring(0, 6), 'Invalid file header.');
|
|
@@ -221,7 +239,7 @@ class ReadableIndex extends events.EventEmitter {
|
|
|
221
239
|
|
|
222
240
|
const metadataBuffer = Buffer.allocUnsafe(metadataSize - 1);
|
|
223
241
|
metadataBuffer.fill(" ");
|
|
224
|
-
fs.readSync(
|
|
242
|
+
fs.readSync(fd, metadataBuffer, 0, metadataSize - 1, 8 + 4);
|
|
225
243
|
const metadata = metadataBuffer.toString('utf8').trim();
|
|
226
244
|
this.verifyAndSetMetadata(metadata);
|
|
227
245
|
this.headerSize = 8 + 4 + metadataSize;
|
|
@@ -236,10 +254,8 @@ class ReadableIndex extends events.EventEmitter {
|
|
|
236
254
|
this.data = [];
|
|
237
255
|
this.readUntil = -1;
|
|
238
256
|
this.readBuffer.fill(0);
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
this.fd = null;
|
|
242
|
-
}
|
|
257
|
+
this.fileHandlePool.evict(this, false);
|
|
258
|
+
this.opened = false;
|
|
243
259
|
}
|
|
244
260
|
|
|
245
261
|
/**
|
|
@@ -253,7 +269,8 @@ class ReadableIndex extends events.EventEmitter {
|
|
|
253
269
|
read(index) {
|
|
254
270
|
index = Number(index) - 1;
|
|
255
271
|
|
|
256
|
-
|
|
272
|
+
const fd = this.getFileHandle();
|
|
273
|
+
fs.readSync(fd, this.readBuffer, 0, this.EntryClass.size, this.headerSize + index * this.EntryClass.size);
|
|
257
274
|
if (index === this.readUntil + 1) {
|
|
258
275
|
this.readUntil++;
|
|
259
276
|
}
|
|
@@ -286,7 +303,8 @@ class ReadableIndex extends events.EventEmitter {
|
|
|
286
303
|
const readBuffer = bufferSize <= this.readBuffer.byteLength
|
|
287
304
|
? this.readBuffer
|
|
288
305
|
: Buffer.allocUnsafe(bufferSize);
|
|
289
|
-
|
|
306
|
+
const fd = this.getFileHandle();
|
|
307
|
+
let readSize = fs.readSync(fd, readBuffer, 0, bufferSize, this.headerSize + readFrom * this.EntryClass.size);
|
|
290
308
|
let index = 0;
|
|
291
309
|
while (index < amount && readSize > 0) {
|
|
292
310
|
this.data[index + readFrom] = this.EntryClass.fromBuffer(readBuffer, index * this.EntryClass.size);
|
|
@@ -89,7 +89,7 @@ class WritableIndex extends ReadableIndex {
|
|
|
89
89
|
* @throws {Error} if the file can not be opened.
|
|
90
90
|
*/
|
|
91
91
|
open() {
|
|
92
|
-
if (this.
|
|
92
|
+
if (this.opened) {
|
|
93
93
|
return false;
|
|
94
94
|
}
|
|
95
95
|
this.writeBufferCursor = 0;
|
|
@@ -108,20 +108,21 @@ class WritableIndex extends ReadableIndex {
|
|
|
108
108
|
this.metadata = {entryClass: this.EntryClass.name, entrySize: this.EntryClass.size};
|
|
109
109
|
}
|
|
110
110
|
const metadataBuffer = buildMetadataHeader(HEADER_MAGIC, this.metadata);
|
|
111
|
-
fs.writeSync(this.
|
|
111
|
+
fs.writeSync(this.getFileHandle(), metadataBuffer, 0, metadataBuffer.byteLength, 0);
|
|
112
112
|
this.headerSize = metadataBuffer.byteLength;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
116
|
+
* Flush pending buffered writes before the pool closes the descriptor.
|
|
117
|
+
*
|
|
118
|
+
* @param {number} fd
|
|
118
119
|
*/
|
|
119
|
-
|
|
120
|
-
if (this.
|
|
121
|
-
|
|
122
|
-
fs.fdatasyncSync(this.fd);
|
|
120
|
+
onBeforeClose(fd) {
|
|
121
|
+
if (!this.writeBuffer) {
|
|
122
|
+
return;
|
|
123
123
|
}
|
|
124
|
-
|
|
124
|
+
this.flush();
|
|
125
|
+
fs.fdatasyncSync(fd);
|
|
125
126
|
}
|
|
126
127
|
|
|
127
128
|
/**
|
|
@@ -139,7 +140,7 @@ class WritableIndex extends ReadableIndex {
|
|
|
139
140
|
* @returns {boolean} If a flush actually was executed.
|
|
140
141
|
*/
|
|
141
142
|
flush() {
|
|
142
|
-
if (!this.
|
|
143
|
+
if (!this.isOpen()) {
|
|
143
144
|
return false;
|
|
144
145
|
}
|
|
145
146
|
if (this.flushTimeout) {
|
|
@@ -150,9 +151,10 @@ class WritableIndex extends ReadableIndex {
|
|
|
150
151
|
return false;
|
|
151
152
|
}
|
|
152
153
|
|
|
153
|
-
|
|
154
|
+
const fd = this.getFileHandle();
|
|
155
|
+
fs.writeSync(fd, this.writeBuffer, 0, this.writeBufferCursor);
|
|
154
156
|
if (this.syncOnFlush) {
|
|
155
|
-
fs.fsyncSync(
|
|
157
|
+
fs.fsyncSync(fd);
|
|
156
158
|
}
|
|
157
159
|
|
|
158
160
|
this.writeBufferCursor = 0;
|
|
@@ -215,9 +217,10 @@ class WritableIndex extends ReadableIndex {
|
|
|
215
217
|
* @param {number} after The index entry number to truncate after.
|
|
216
218
|
*/
|
|
217
219
|
truncate(after) {
|
|
218
|
-
if (!this.
|
|
220
|
+
if (!this.isOpen()) {
|
|
219
221
|
return;
|
|
220
222
|
}
|
|
223
|
+
this.getFileHandle();
|
|
221
224
|
if (after < 0) {
|
|
222
225
|
after = 0;
|
|
223
226
|
}
|
|
@@ -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;
|
|
@@ -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 = [];
|
package/src/PartitionPool.js
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A fixed-capacity registry of partitions with LRU eviction of open file handles.
|
|
3
|
-
*
|
|
4
|
-
* All partitions are stored by their numeric id and may be queried at any time.
|
|
5
|
-
* The pool additionally tracks which partitions currently have an open file descriptor
|
|
6
|
-
* in LRU (least-recently-used) order. When the pool is asked to open a partition and
|
|
7
|
-
* doing so would exceed the configured cap, the least-recently-used open partition is
|
|
8
|
-
* closed first to stay within the limit.
|
|
9
|
-
*
|
|
10
|
-
* Setting the cap to 0 disables eviction: all partitions are allowed to remain open
|
|
11
|
-
* simultaneously, which matches the uncapped behaviour of the original implementation.
|
|
12
|
-
*/
|
|
13
|
-
class PartitionPool {
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @param {number} [maxOpen=0] Maximum number of simultaneously open partition file
|
|
17
|
-
* handles. 0 disables the limit (no eviction).
|
|
18
|
-
*/
|
|
19
|
-
constructor(maxOpen = 0) {
|
|
20
|
-
this.maxOpen = maxOpen;
|
|
21
|
-
/** Registry of all known partitions keyed by id. */
|
|
22
|
-
this.registry = Object.create(null);
|
|
23
|
-
/**
|
|
24
|
-
* Insertion-order map used for LRU tracking of open file handles.
|
|
25
|
-
* Key = partition id, value = true.
|
|
26
|
-
* Oldest (least-recently-used) entry is first; newest (most-recently-used) is last.
|
|
27
|
-
*/
|
|
28
|
-
this.handles = new Map();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Register a partition under the given id.
|
|
33
|
-
*
|
|
34
|
-
* @param {number|string} id
|
|
35
|
-
* @param {object} partition
|
|
36
|
-
*/
|
|
37
|
-
add(id, partition) {
|
|
38
|
-
this.registry[id] = partition;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Retrieve a registered partition without opening it.
|
|
43
|
-
*
|
|
44
|
-
* @param {number|string} id
|
|
45
|
-
* @returns {object|undefined}
|
|
46
|
-
*/
|
|
47
|
-
get(id) {
|
|
48
|
-
return this.registry[id];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Check whether a partition with the given id is registered in the pool.
|
|
53
|
-
*
|
|
54
|
-
* @param {number|string} id
|
|
55
|
-
* @returns {boolean}
|
|
56
|
-
*/
|
|
57
|
-
has(id) {
|
|
58
|
-
return id in this.registry;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Open the partition with the given id, applying LRU eviction if necessary.
|
|
63
|
-
*
|
|
64
|
-
* If the partition is not yet open and adding it would exceed `maxOpen`, the
|
|
65
|
-
* least-recently-used open partition is closed first. Stale entries (partitions
|
|
66
|
-
* that were closed externally) are discarded from the LRU map as they are
|
|
67
|
-
* encountered; if all tracked entries turn out to be stale the loop exits without
|
|
68
|
-
* closing any partition — the handle count stays temporarily inflated (bounded by
|
|
69
|
-
* the number of external closes since the last `open()` call) but correctness is
|
|
70
|
-
* preserved.
|
|
71
|
-
*
|
|
72
|
-
* @param {number|string} id
|
|
73
|
-
* @returns {object} The opened partition.
|
|
74
|
-
*/
|
|
75
|
-
open(id) {
|
|
76
|
-
const partition = this.registry[id];
|
|
77
|
-
|
|
78
|
-
if (this.maxOpen > 0) {
|
|
79
|
-
// Remove id first — this may already bring the handle count below the cap.
|
|
80
|
-
this.handles.delete(id);
|
|
81
|
-
if (this.handles.size >= this.maxOpen) {
|
|
82
|
-
for (const [lruId] of this.handles) {
|
|
83
|
-
this.handles.delete(lruId);
|
|
84
|
-
const lruPartition = this.registry[lruId];
|
|
85
|
-
if (lruPartition && lruPartition.isOpen()) {
|
|
86
|
-
lruPartition.close();
|
|
87
|
-
break;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
// (Re-)add id at the MRU end of the map.
|
|
92
|
-
this.handles.set(id, true);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
partition.open();
|
|
96
|
-
return partition;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Invoke `callback` for every registered partition.
|
|
101
|
-
*
|
|
102
|
-
* @param {function(object): void} callback
|
|
103
|
-
*/
|
|
104
|
-
forEach(callback) {
|
|
105
|
-
for (const id of Object.keys(this.registry)) {
|
|
106
|
-
callback(this.registry[id]);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Yield every registered partition object.
|
|
112
|
-
*
|
|
113
|
-
* @returns {Generator<object>}
|
|
114
|
-
*/
|
|
115
|
-
*values() {
|
|
116
|
-
for (const id of Object.keys(this.registry)) {
|
|
117
|
-
yield this.registry[id];
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* The total number of registered partitions.
|
|
123
|
-
* @returns {number}
|
|
124
|
-
*/
|
|
125
|
-
get count() {
|
|
126
|
-
return Object.keys(this.registry).length;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* The number of open partition file handles currently tracked by the pool.
|
|
131
|
-
* @returns {number}
|
|
132
|
-
*/
|
|
133
|
-
get openCount() {
|
|
134
|
-
return this.handles.size;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Reset the open-handle tracking without closing any partitions.
|
|
139
|
-
*
|
|
140
|
-
* Call this after externally closing all partitions (e.g. after
|
|
141
|
-
* `checkTornWrites`) to keep the pool's LRU state consistent with reality.
|
|
142
|
-
*/
|
|
143
|
-
clearOpenHandles() {
|
|
144
|
-
this.handles.clear();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export default PartitionPool;
|