serializable-bptree 5.2.0 → 5.2.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 +9 -1
- package/dist/cjs/index.cjs +436 -121
- package/dist/esm/index.mjs +436 -121
- package/dist/types/BPTreeAsync.d.ts +3 -0
- package/package.json +3 -2
package/dist/esm/index.mjs
CHANGED
|
@@ -33,16 +33,26 @@ var LRUMap = class {
|
|
|
33
33
|
map;
|
|
34
34
|
head = null;
|
|
35
35
|
tail = null;
|
|
36
|
+
/**
|
|
37
|
+
* Creates an instance of LRUMap.
|
|
38
|
+
* @param capacity The maximum number of items the cache can hold.
|
|
39
|
+
*/
|
|
36
40
|
constructor(capacity) {
|
|
37
41
|
this.capacity = capacity;
|
|
38
42
|
this.map = /* @__PURE__ */ new Map();
|
|
39
43
|
}
|
|
40
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Promotes a node to the head of the linked list (marks as most recently used).
|
|
46
|
+
* @param node The node to promote.
|
|
47
|
+
*/
|
|
41
48
|
promote(node) {
|
|
42
49
|
this.extract(node);
|
|
43
50
|
this.prepend(node);
|
|
44
51
|
}
|
|
45
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Disconnects a node from the doubly linked list.
|
|
54
|
+
* @param node The node to extract.
|
|
55
|
+
*/
|
|
46
56
|
extract(node) {
|
|
47
57
|
if (node.prev) node.prev.next = node.next;
|
|
48
58
|
else this.head = node.next;
|
|
@@ -51,7 +61,10 @@ var LRUMap = class {
|
|
|
51
61
|
node.prev = null;
|
|
52
62
|
node.next = null;
|
|
53
63
|
}
|
|
54
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Inserts a node at the head of the doubly linked list.
|
|
66
|
+
* @param node The node to prepend.
|
|
67
|
+
*/
|
|
55
68
|
prepend(node) {
|
|
56
69
|
node.next = this.head;
|
|
57
70
|
if (this.head) this.head.prev = node;
|
|
@@ -59,8 +72,10 @@ var LRUMap = class {
|
|
|
59
72
|
if (!this.tail) this.tail = node;
|
|
60
73
|
}
|
|
61
74
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
75
|
+
* Stores or updates a value by key.
|
|
76
|
+
* If the capacity is exceeded, the least recently used item (tail) is removed.
|
|
77
|
+
* @param key The key to store.
|
|
78
|
+
* @param value The value to store.
|
|
64
79
|
*/
|
|
65
80
|
set(key, value) {
|
|
66
81
|
const existing = this.map.get(key);
|
|
@@ -78,7 +93,10 @@ var LRUMap = class {
|
|
|
78
93
|
}
|
|
79
94
|
}
|
|
80
95
|
/**
|
|
81
|
-
*
|
|
96
|
+
* Retrieves a value by key.
|
|
97
|
+
* Accessing the item moves it to the "most recently used" position.
|
|
98
|
+
* @param key The key to look for.
|
|
99
|
+
* @returns The value associated with the key, or undefined if not found.
|
|
82
100
|
*/
|
|
83
101
|
get(key) {
|
|
84
102
|
const node = this.map.get(key);
|
|
@@ -87,13 +105,17 @@ var LRUMap = class {
|
|
|
87
105
|
return node.value;
|
|
88
106
|
}
|
|
89
107
|
/**
|
|
90
|
-
*
|
|
108
|
+
* Checks if a key exists in the cache without changing its access order.
|
|
109
|
+
* @param key The key to check.
|
|
110
|
+
* @returns True if the key exists, false otherwise.
|
|
91
111
|
*/
|
|
92
112
|
has(key) {
|
|
93
113
|
return this.map.has(key);
|
|
94
114
|
}
|
|
95
115
|
/**
|
|
96
|
-
*
|
|
116
|
+
* Removes a key and its associated value from the cache.
|
|
117
|
+
* @param key The key to remove.
|
|
118
|
+
* @returns True if the key was found and removed, false otherwise.
|
|
97
119
|
*/
|
|
98
120
|
delete(key) {
|
|
99
121
|
const node = this.map.get(key);
|
|
@@ -103,7 +125,8 @@ var LRUMap = class {
|
|
|
103
125
|
return true;
|
|
104
126
|
}
|
|
105
127
|
/**
|
|
106
|
-
*
|
|
128
|
+
* Returns an iterator of keys in the order of most recently used to least recently used.
|
|
129
|
+
* @returns An iterable iterator of keys.
|
|
107
130
|
*/
|
|
108
131
|
*keys() {
|
|
109
132
|
let current = this.head;
|
|
@@ -113,13 +136,13 @@ var LRUMap = class {
|
|
|
113
136
|
}
|
|
114
137
|
}
|
|
115
138
|
/**
|
|
116
|
-
*
|
|
139
|
+
* Returns the current number of items in the cache.
|
|
117
140
|
*/
|
|
118
141
|
get size() {
|
|
119
142
|
return this.map.size;
|
|
120
143
|
}
|
|
121
144
|
/**
|
|
122
|
-
*
|
|
145
|
+
* Clears all items from the cache.
|
|
123
146
|
*/
|
|
124
147
|
clear() {
|
|
125
148
|
this.map.clear();
|
|
@@ -1133,11 +1156,271 @@ var BPTreeSync = class extends BPTree {
|
|
|
1133
1156
|
}
|
|
1134
1157
|
};
|
|
1135
1158
|
|
|
1159
|
+
// node_modules/ryoiki/dist/esm/index.mjs
|
|
1160
|
+
var Ryoiki = class _Ryoiki {
|
|
1161
|
+
readings;
|
|
1162
|
+
writings;
|
|
1163
|
+
readQueue;
|
|
1164
|
+
writeQueue;
|
|
1165
|
+
static async CatchError(promise) {
|
|
1166
|
+
return await promise.then((v) => [void 0, v]).catch((err) => [err]);
|
|
1167
|
+
}
|
|
1168
|
+
static IsRangeOverlap(a, b) {
|
|
1169
|
+
const [start1, end1] = a;
|
|
1170
|
+
const [start2, end2] = b;
|
|
1171
|
+
if (end1 <= start2 || end2 <= start1) {
|
|
1172
|
+
return false;
|
|
1173
|
+
}
|
|
1174
|
+
return true;
|
|
1175
|
+
}
|
|
1176
|
+
static ERR_ALREADY_EXISTS(lockId) {
|
|
1177
|
+
return new Error(`The '${lockId}' task already existing in queue or running.`);
|
|
1178
|
+
}
|
|
1179
|
+
static ERR_NOT_EXISTS(lockId) {
|
|
1180
|
+
return new Error(`The '${lockId}' task not existing in task queue.`);
|
|
1181
|
+
}
|
|
1182
|
+
static ERR_TIMEOUT(lockId, timeout) {
|
|
1183
|
+
return new Error(`The task with ID '${lockId}' failed to acquire the lock within the timeout(${timeout}ms).`);
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* Constructs a new instance of the Ryoiki class.
|
|
1187
|
+
*/
|
|
1188
|
+
constructor() {
|
|
1189
|
+
this.readings = /* @__PURE__ */ new Map();
|
|
1190
|
+
this.writings = /* @__PURE__ */ new Map();
|
|
1191
|
+
this.readQueue = /* @__PURE__ */ new Map();
|
|
1192
|
+
this.writeQueue = /* @__PURE__ */ new Map();
|
|
1193
|
+
}
|
|
1194
|
+
/**
|
|
1195
|
+
* Creates a range based on a start value and length.
|
|
1196
|
+
* @param start - The starting value of the range.
|
|
1197
|
+
* @param length - The length of the range.
|
|
1198
|
+
* @returns A range tuple [start, start + length].
|
|
1199
|
+
*/
|
|
1200
|
+
range(start, length) {
|
|
1201
|
+
return [start, start + length];
|
|
1202
|
+
}
|
|
1203
|
+
rangeOverlapping(tasks, range) {
|
|
1204
|
+
return Array.from(tasks.values()).some((t) => _Ryoiki.IsRangeOverlap(t.range, range));
|
|
1205
|
+
}
|
|
1206
|
+
isSameRange(a, b) {
|
|
1207
|
+
const [a1, a2] = a;
|
|
1208
|
+
const [b1, b2] = b;
|
|
1209
|
+
return a1 === b1 && a2 === b2;
|
|
1210
|
+
}
|
|
1211
|
+
fetchUnitAndRun(queue, workspaces) {
|
|
1212
|
+
for (const [id, unit] of queue) {
|
|
1213
|
+
if (!unit.condition()) {
|
|
1214
|
+
continue;
|
|
1215
|
+
}
|
|
1216
|
+
this._alloc(queue, workspaces, id);
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
_handleOverload(args, handlers, argPatterns) {
|
|
1220
|
+
for (const [key, pattern] of Object.entries(argPatterns)) {
|
|
1221
|
+
if (this._matchArgs(args, pattern)) {
|
|
1222
|
+
return handlers[key](...args);
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
throw new Error("Invalid arguments");
|
|
1226
|
+
}
|
|
1227
|
+
_matchArgs(args, pattern) {
|
|
1228
|
+
return args.every((arg, index) => {
|
|
1229
|
+
const expectedType = pattern[index];
|
|
1230
|
+
if (expectedType === void 0) return typeof arg === "undefined";
|
|
1231
|
+
if (expectedType === Function) return typeof arg === "function";
|
|
1232
|
+
if (expectedType === Number) return typeof arg === "number";
|
|
1233
|
+
if (expectedType === Array) return Array.isArray(arg);
|
|
1234
|
+
return false;
|
|
1235
|
+
});
|
|
1236
|
+
}
|
|
1237
|
+
_createRandomId() {
|
|
1238
|
+
const timestamp = Date.now().toString(36);
|
|
1239
|
+
const random = Math.random().toString(36).substring(2);
|
|
1240
|
+
return `${timestamp}${random}`;
|
|
1241
|
+
}
|
|
1242
|
+
_alloc(queue, workspaces, lockId) {
|
|
1243
|
+
const unit = queue.get(lockId);
|
|
1244
|
+
if (!unit) {
|
|
1245
|
+
throw _Ryoiki.ERR_NOT_EXISTS(lockId);
|
|
1246
|
+
}
|
|
1247
|
+
workspaces.set(lockId, unit);
|
|
1248
|
+
queue.delete(lockId);
|
|
1249
|
+
unit.alloc();
|
|
1250
|
+
}
|
|
1251
|
+
_free(workspaces, lockId) {
|
|
1252
|
+
const unit = workspaces.get(lockId);
|
|
1253
|
+
if (!unit) {
|
|
1254
|
+
throw _Ryoiki.ERR_NOT_EXISTS(lockId);
|
|
1255
|
+
}
|
|
1256
|
+
workspaces.delete(lockId);
|
|
1257
|
+
unit.free();
|
|
1258
|
+
}
|
|
1259
|
+
_lock(queue, range, timeout, task, condition) {
|
|
1260
|
+
return new Promise((resolve, reject) => {
|
|
1261
|
+
let timeoutId = null;
|
|
1262
|
+
if (timeout >= 0) {
|
|
1263
|
+
timeoutId = setTimeout(() => {
|
|
1264
|
+
reject(_Ryoiki.ERR_TIMEOUT(id, timeout));
|
|
1265
|
+
}, timeout);
|
|
1266
|
+
}
|
|
1267
|
+
const id = this._createRandomId();
|
|
1268
|
+
const alloc = async () => {
|
|
1269
|
+
if (timeoutId !== null) {
|
|
1270
|
+
clearTimeout(timeoutId);
|
|
1271
|
+
}
|
|
1272
|
+
const [err, v] = await _Ryoiki.CatchError(task(id));
|
|
1273
|
+
if (err) reject(err);
|
|
1274
|
+
else resolve(v);
|
|
1275
|
+
};
|
|
1276
|
+
const fetch = () => {
|
|
1277
|
+
this.fetchUnitAndRun(this.readQueue, this.readings);
|
|
1278
|
+
this.fetchUnitAndRun(this.writeQueue, this.writings);
|
|
1279
|
+
};
|
|
1280
|
+
queue.set(id, { id, range, condition, alloc, free: fetch });
|
|
1281
|
+
fetch();
|
|
1282
|
+
});
|
|
1283
|
+
}
|
|
1284
|
+
_checkWorking(range, workspaces) {
|
|
1285
|
+
let isLocked = false;
|
|
1286
|
+
for (const lock of workspaces.values()) {
|
|
1287
|
+
if (_Ryoiki.IsRangeOverlap(range, lock.range)) {
|
|
1288
|
+
isLocked = true;
|
|
1289
|
+
break;
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
return isLocked;
|
|
1293
|
+
}
|
|
1294
|
+
/**
|
|
1295
|
+
* Checks if there is any active read lock within the specified range.
|
|
1296
|
+
* @param range The range to check for active read locks.
|
|
1297
|
+
* @returns `true` if there is an active read lock within the range, `false` otherwise.
|
|
1298
|
+
*/
|
|
1299
|
+
isReading(range) {
|
|
1300
|
+
return this._checkWorking(range, this.readings);
|
|
1301
|
+
}
|
|
1302
|
+
/**
|
|
1303
|
+
* Checks if there is any active write lock within the specified range.
|
|
1304
|
+
* @param range The range to check for active write locks.
|
|
1305
|
+
* @returns `true` if there is an active write lock within the range, `false` otherwise.
|
|
1306
|
+
*/
|
|
1307
|
+
isWriting(range) {
|
|
1308
|
+
return this._checkWorking(range, this.writings);
|
|
1309
|
+
}
|
|
1310
|
+
/**
|
|
1311
|
+
* Checks if a read lock can be acquired within the specified range.
|
|
1312
|
+
* @param range The range to check for read lock availability.
|
|
1313
|
+
* @returns `true` if a read lock can be acquired, `false` otherwise.
|
|
1314
|
+
*/
|
|
1315
|
+
canRead(range) {
|
|
1316
|
+
const writing = this.isWriting(range);
|
|
1317
|
+
return !writing;
|
|
1318
|
+
}
|
|
1319
|
+
/**
|
|
1320
|
+
* Checks if a write lock can be acquired within the specified range.
|
|
1321
|
+
* @param range The range to check for write lock availability.
|
|
1322
|
+
* @returns `true` if a write lock can be acquired, `false` otherwise.
|
|
1323
|
+
*/
|
|
1324
|
+
canWrite(range) {
|
|
1325
|
+
const reading = this.isReading(range);
|
|
1326
|
+
const writing = this.isWriting(range);
|
|
1327
|
+
return !reading && !writing;
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* Internal implementation of the read lock. Handles both overloads.
|
|
1331
|
+
* @template T - The return type of the task.
|
|
1332
|
+
* @param arg0 - Either a range or a task callback.
|
|
1333
|
+
* If a range is provided, the task is the second argument.
|
|
1334
|
+
* @param arg1 - The task to execute, required if a range is provided.
|
|
1335
|
+
* @param arg2 - The timeout for acquiring the lock.
|
|
1336
|
+
* If the lock cannot be acquired within this period, an error will be thrown.
|
|
1337
|
+
* If this value is not provided, no timeout will be set.
|
|
1338
|
+
* @returns A promise resolving to the result of the task execution.
|
|
1339
|
+
*/
|
|
1340
|
+
readLock(arg0, arg1, arg2) {
|
|
1341
|
+
const [range, task, timeout] = this._handleOverload(
|
|
1342
|
+
[arg0, arg1, arg2],
|
|
1343
|
+
{
|
|
1344
|
+
rangeTask: (range2, task2) => [range2, task2, -1],
|
|
1345
|
+
rangeTaskTimeout: (range2, task2, timeout2) => [range2, task2, timeout2],
|
|
1346
|
+
task: (task2) => [[-Infinity, Infinity], task2, -1],
|
|
1347
|
+
taskTimeout: (task2, timeout2) => [[-Infinity, Infinity], task2, timeout2]
|
|
1348
|
+
},
|
|
1349
|
+
{
|
|
1350
|
+
task: [Function],
|
|
1351
|
+
taskTimeout: [Function, Number],
|
|
1352
|
+
rangeTask: [Array, Function],
|
|
1353
|
+
rangeTaskTimeout: [Array, Function, Number]
|
|
1354
|
+
}
|
|
1355
|
+
);
|
|
1356
|
+
return this._lock(
|
|
1357
|
+
this.readQueue,
|
|
1358
|
+
range,
|
|
1359
|
+
timeout,
|
|
1360
|
+
task,
|
|
1361
|
+
() => !this.rangeOverlapping(this.writings, range)
|
|
1362
|
+
);
|
|
1363
|
+
}
|
|
1364
|
+
/**
|
|
1365
|
+
* Internal implementation of the write lock. Handles both overloads.
|
|
1366
|
+
* @template T - The return type of the task.
|
|
1367
|
+
* @param arg0 - Either a range or a task callback.
|
|
1368
|
+
* If a range is provided, the task is the second argument.
|
|
1369
|
+
* @param arg1 - The task to execute, required if a range is provided.
|
|
1370
|
+
* @param arg2 - The timeout for acquiring the lock.
|
|
1371
|
+
* If the lock cannot be acquired within this period, an error will be thrown.
|
|
1372
|
+
* If this value is not provided, no timeout will be set.
|
|
1373
|
+
* @returns A promise resolving to the result of the task execution.
|
|
1374
|
+
*/
|
|
1375
|
+
writeLock(arg0, arg1, arg2) {
|
|
1376
|
+
const [range, task, timeout] = this._handleOverload(
|
|
1377
|
+
[arg0, arg1, arg2],
|
|
1378
|
+
{
|
|
1379
|
+
rangeTask: (range2, task2) => [range2, task2, -1],
|
|
1380
|
+
rangeTaskTimeout: (range2, task2, timeout2) => [range2, task2, timeout2],
|
|
1381
|
+
task: (task2) => [[-Infinity, Infinity], task2, -1],
|
|
1382
|
+
taskTimeout: (task2, timeout2) => [[-Infinity, Infinity], task2, timeout2]
|
|
1383
|
+
},
|
|
1384
|
+
{
|
|
1385
|
+
task: [Function],
|
|
1386
|
+
taskTimeout: [Function, Number],
|
|
1387
|
+
rangeTask: [Array, Function],
|
|
1388
|
+
rangeTaskTimeout: [Array, Function, Number]
|
|
1389
|
+
}
|
|
1390
|
+
);
|
|
1391
|
+
return this._lock(
|
|
1392
|
+
this.writeQueue,
|
|
1393
|
+
range,
|
|
1394
|
+
timeout,
|
|
1395
|
+
task,
|
|
1396
|
+
() => {
|
|
1397
|
+
return !this.rangeOverlapping(this.writings, range) && !this.rangeOverlapping(this.readings, range);
|
|
1398
|
+
}
|
|
1399
|
+
);
|
|
1400
|
+
}
|
|
1401
|
+
/**
|
|
1402
|
+
* Releases a read lock by its lock ID.
|
|
1403
|
+
* @param lockId - The unique identifier for the lock to release.
|
|
1404
|
+
*/
|
|
1405
|
+
readUnlock(lockId) {
|
|
1406
|
+
this._free(this.readings, lockId);
|
|
1407
|
+
}
|
|
1408
|
+
/**
|
|
1409
|
+
* Releases a write lock by its lock ID.
|
|
1410
|
+
* @param lockId - The unique identifier for the lock to release.
|
|
1411
|
+
*/
|
|
1412
|
+
writeUnlock(lockId) {
|
|
1413
|
+
this._free(this.writings, lockId);
|
|
1414
|
+
}
|
|
1415
|
+
};
|
|
1416
|
+
|
|
1136
1417
|
// src/BPTreeAsync.ts
|
|
1137
1418
|
var BPTreeAsync = class extends BPTree {
|
|
1419
|
+
lock;
|
|
1138
1420
|
constructor(strategy, comparator, option) {
|
|
1139
1421
|
super(strategy, comparator, option);
|
|
1140
1422
|
this.nodes = this._createCachedNode();
|
|
1423
|
+
this.lock = new Ryoiki();
|
|
1141
1424
|
}
|
|
1142
1425
|
_createCachedNode() {
|
|
1143
1426
|
return new CacheEntanglementAsync(async (key) => {
|
|
@@ -1146,6 +1429,24 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1146
1429
|
capacity: this.option.capacity ?? 1e3
|
|
1147
1430
|
});
|
|
1148
1431
|
}
|
|
1432
|
+
async readLock(callback) {
|
|
1433
|
+
let lockId;
|
|
1434
|
+
return await this.lock.readLock(async (_lockId) => {
|
|
1435
|
+
lockId = _lockId;
|
|
1436
|
+
return await callback();
|
|
1437
|
+
}).finally(() => {
|
|
1438
|
+
this.lock.readUnlock(lockId);
|
|
1439
|
+
});
|
|
1440
|
+
}
|
|
1441
|
+
async writeLock(callback) {
|
|
1442
|
+
let lockId;
|
|
1443
|
+
return await this.lock.writeLock(async (_lockId) => {
|
|
1444
|
+
lockId = _lockId;
|
|
1445
|
+
return await callback();
|
|
1446
|
+
}).finally(() => {
|
|
1447
|
+
this.lock.writeUnlock(lockId);
|
|
1448
|
+
});
|
|
1449
|
+
}
|
|
1149
1450
|
async getPairsRightToLeft(value, startNode, endNode, comparator) {
|
|
1150
1451
|
const pairs = [];
|
|
1151
1452
|
let node = startNode;
|
|
@@ -1589,139 +1890,153 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1589
1890
|
this._nodeDeleteBuffer.clear();
|
|
1590
1891
|
}
|
|
1591
1892
|
async keys(condition, filterValues) {
|
|
1592
|
-
|
|
1593
|
-
const
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
const
|
|
1606
|
-
|
|
1607
|
-
|
|
1893
|
+
return this.readLock(async () => {
|
|
1894
|
+
for (const k in condition) {
|
|
1895
|
+
const key = k;
|
|
1896
|
+
const value = condition[key];
|
|
1897
|
+
const startNode = await this.verifierStartNode[key](value);
|
|
1898
|
+
const endNode = await this.verifierEndNode[key](value);
|
|
1899
|
+
const direction = this.verifierDirection[key];
|
|
1900
|
+
const comparator = this.verifierMap[key];
|
|
1901
|
+
const pairs = await this.getPairs(value, startNode, endNode, comparator, direction);
|
|
1902
|
+
if (!filterValues) {
|
|
1903
|
+
filterValues = new Set(pairs.keys());
|
|
1904
|
+
} else {
|
|
1905
|
+
const intersections = /* @__PURE__ */ new Set();
|
|
1906
|
+
for (const key2 of filterValues) {
|
|
1907
|
+
const has = pairs.has(key2);
|
|
1908
|
+
if (has) {
|
|
1909
|
+
intersections.add(key2);
|
|
1910
|
+
}
|
|
1608
1911
|
}
|
|
1912
|
+
filterValues = intersections;
|
|
1609
1913
|
}
|
|
1610
|
-
filterValues = intersections;
|
|
1611
1914
|
}
|
|
1612
|
-
|
|
1613
|
-
|
|
1915
|
+
return filterValues ?? /* @__PURE__ */ new Set([]);
|
|
1916
|
+
});
|
|
1614
1917
|
}
|
|
1615
1918
|
async where(condition) {
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
const
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
result
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1919
|
+
return this.readLock(async () => {
|
|
1920
|
+
let result = null;
|
|
1921
|
+
for (const k in condition) {
|
|
1922
|
+
const key = k;
|
|
1923
|
+
const value = condition[key];
|
|
1924
|
+
const startNode = await this.verifierStartNode[key](value);
|
|
1925
|
+
const endNode = await this.verifierEndNode[key](value);
|
|
1926
|
+
const direction = this.verifierDirection[key];
|
|
1927
|
+
const comparator = this.verifierMap[key];
|
|
1928
|
+
const pairs = await this.getPairs(value, startNode, endNode, comparator, direction);
|
|
1929
|
+
if (result === null) {
|
|
1930
|
+
result = pairs;
|
|
1931
|
+
} else {
|
|
1932
|
+
const intersection = /* @__PURE__ */ new Map();
|
|
1933
|
+
for (const [k2, v] of pairs) {
|
|
1934
|
+
if (result.has(k2)) {
|
|
1935
|
+
intersection.set(k2, v);
|
|
1936
|
+
}
|
|
1632
1937
|
}
|
|
1938
|
+
result = intersection;
|
|
1633
1939
|
}
|
|
1634
|
-
result = intersection;
|
|
1635
1940
|
}
|
|
1636
|
-
|
|
1637
|
-
|
|
1941
|
+
return result ?? /* @__PURE__ */ new Map();
|
|
1942
|
+
});
|
|
1638
1943
|
}
|
|
1639
1944
|
async insert(key, value) {
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1945
|
+
return this.writeLock(async () => {
|
|
1946
|
+
const before = await this.insertableNode(value);
|
|
1947
|
+
this._insertAtLeaf(before, key, value);
|
|
1948
|
+
if (before.values.length === this.order) {
|
|
1949
|
+
const after = await this._createNode(
|
|
1950
|
+
true,
|
|
1951
|
+
[],
|
|
1952
|
+
[],
|
|
1953
|
+
true,
|
|
1954
|
+
before.parent,
|
|
1955
|
+
before.next,
|
|
1956
|
+
before.id
|
|
1957
|
+
);
|
|
1958
|
+
const mid = Math.ceil(this.order / 2) - 1;
|
|
1959
|
+
const beforeNext = before.next;
|
|
1960
|
+
after.values = before.values.slice(mid + 1);
|
|
1961
|
+
after.keys = before.keys.slice(mid + 1);
|
|
1962
|
+
before.values = before.values.slice(0, mid + 1);
|
|
1963
|
+
before.keys = before.keys.slice(0, mid + 1);
|
|
1964
|
+
before.next = after.id;
|
|
1965
|
+
if (beforeNext) {
|
|
1966
|
+
const node = await this.getNode(beforeNext);
|
|
1967
|
+
node.prev = after.id;
|
|
1968
|
+
this.bufferForNodeUpdate(node);
|
|
1969
|
+
}
|
|
1970
|
+
await this._insertInParent(before, after.values[0], after);
|
|
1971
|
+
this.bufferForNodeUpdate(before);
|
|
1663
1972
|
}
|
|
1664
|
-
await this.
|
|
1665
|
-
this.
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
await this.commitNodeCreateBuffer();
|
|
1669
|
-
await this.commitNodeUpdateBuffer();
|
|
1973
|
+
await this.commitHeadBuffer();
|
|
1974
|
+
await this.commitNodeCreateBuffer();
|
|
1975
|
+
await this.commitNodeUpdateBuffer();
|
|
1976
|
+
});
|
|
1670
1977
|
}
|
|
1671
1978
|
async delete(key, value) {
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
if (keys.
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
node.
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1979
|
+
return this.writeLock(async () => {
|
|
1980
|
+
const node = await this.insertableNode(value);
|
|
1981
|
+
let i = node.values.length;
|
|
1982
|
+
while (i--) {
|
|
1983
|
+
const nValue = node.values[i];
|
|
1984
|
+
if (this.comparator.isSame(value, nValue)) {
|
|
1985
|
+
const keys = node.keys[i];
|
|
1986
|
+
if (keys.includes(key)) {
|
|
1987
|
+
if (keys.length > 1) {
|
|
1988
|
+
keys.splice(keys.indexOf(key), 1);
|
|
1989
|
+
this.bufferForNodeUpdate(node);
|
|
1990
|
+
} else if (node.id === this.root.id) {
|
|
1991
|
+
node.values.splice(i, 1);
|
|
1992
|
+
node.keys.splice(i, 1);
|
|
1993
|
+
this.bufferForNodeUpdate(node);
|
|
1994
|
+
} else {
|
|
1995
|
+
keys.splice(keys.indexOf(key), 1);
|
|
1996
|
+
node.keys.splice(i, 1);
|
|
1997
|
+
node.values.splice(node.values.indexOf(value), 1);
|
|
1998
|
+
await this._deleteEntry(node, key, value);
|
|
1999
|
+
this.bufferForNodeUpdate(node);
|
|
2000
|
+
}
|
|
1692
2001
|
}
|
|
1693
2002
|
}
|
|
1694
2003
|
}
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
2004
|
+
await this.commitHeadBuffer();
|
|
2005
|
+
await this.commitNodeCreateBuffer();
|
|
2006
|
+
await this.commitNodeUpdateBuffer();
|
|
2007
|
+
await this.commitNodeDeleteBuffer();
|
|
2008
|
+
});
|
|
1700
2009
|
}
|
|
1701
2010
|
async exists(key, value) {
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
2011
|
+
return this.readLock(async () => {
|
|
2012
|
+
const node = await this.insertableNode(value);
|
|
2013
|
+
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
2014
|
+
const nValue = node.values[i];
|
|
2015
|
+
if (this.comparator.isSame(value, nValue)) {
|
|
2016
|
+
const keys = node.keys[i];
|
|
2017
|
+
return keys.includes(key);
|
|
2018
|
+
}
|
|
1708
2019
|
}
|
|
1709
|
-
|
|
1710
|
-
|
|
2020
|
+
return false;
|
|
2021
|
+
});
|
|
1711
2022
|
}
|
|
1712
2023
|
async setHeadData(data) {
|
|
1713
|
-
this.
|
|
1714
|
-
|
|
1715
|
-
|
|
2024
|
+
return this.writeLock(async () => {
|
|
2025
|
+
this.strategy.head.data = data;
|
|
2026
|
+
this._strategyDirty = true;
|
|
2027
|
+
await this.commitHeadBuffer();
|
|
2028
|
+
});
|
|
1716
2029
|
}
|
|
1717
2030
|
async forceUpdate() {
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
2031
|
+
return this.readLock(async () => {
|
|
2032
|
+
const keys = [...this.nodes.keys()];
|
|
2033
|
+
this.nodes.clear();
|
|
2034
|
+
await this.init();
|
|
2035
|
+
for (const key of keys) {
|
|
2036
|
+
await this.getNode(key);
|
|
2037
|
+
}
|
|
2038
|
+
return keys.length;
|
|
2039
|
+
});
|
|
1725
2040
|
}
|
|
1726
2041
|
};
|
|
1727
2042
|
|