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/cjs/index.cjs
CHANGED
|
@@ -67,16 +67,26 @@ var LRUMap = class {
|
|
|
67
67
|
map;
|
|
68
68
|
head = null;
|
|
69
69
|
tail = null;
|
|
70
|
+
/**
|
|
71
|
+
* Creates an instance of LRUMap.
|
|
72
|
+
* @param capacity The maximum number of items the cache can hold.
|
|
73
|
+
*/
|
|
70
74
|
constructor(capacity) {
|
|
71
75
|
this.capacity = capacity;
|
|
72
76
|
this.map = /* @__PURE__ */ new Map();
|
|
73
77
|
}
|
|
74
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Promotes a node to the head of the linked list (marks as most recently used).
|
|
80
|
+
* @param node The node to promote.
|
|
81
|
+
*/
|
|
75
82
|
promote(node) {
|
|
76
83
|
this.extract(node);
|
|
77
84
|
this.prepend(node);
|
|
78
85
|
}
|
|
79
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Disconnects a node from the doubly linked list.
|
|
88
|
+
* @param node The node to extract.
|
|
89
|
+
*/
|
|
80
90
|
extract(node) {
|
|
81
91
|
if (node.prev) node.prev.next = node.next;
|
|
82
92
|
else this.head = node.next;
|
|
@@ -85,7 +95,10 @@ var LRUMap = class {
|
|
|
85
95
|
node.prev = null;
|
|
86
96
|
node.next = null;
|
|
87
97
|
}
|
|
88
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Inserts a node at the head of the doubly linked list.
|
|
100
|
+
* @param node The node to prepend.
|
|
101
|
+
*/
|
|
89
102
|
prepend(node) {
|
|
90
103
|
node.next = this.head;
|
|
91
104
|
if (this.head) this.head.prev = node;
|
|
@@ -93,8 +106,10 @@ var LRUMap = class {
|
|
|
93
106
|
if (!this.tail) this.tail = node;
|
|
94
107
|
}
|
|
95
108
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
109
|
+
* Stores or updates a value by key.
|
|
110
|
+
* If the capacity is exceeded, the least recently used item (tail) is removed.
|
|
111
|
+
* @param key The key to store.
|
|
112
|
+
* @param value The value to store.
|
|
98
113
|
*/
|
|
99
114
|
set(key, value) {
|
|
100
115
|
const existing = this.map.get(key);
|
|
@@ -112,7 +127,10 @@ var LRUMap = class {
|
|
|
112
127
|
}
|
|
113
128
|
}
|
|
114
129
|
/**
|
|
115
|
-
*
|
|
130
|
+
* Retrieves a value by key.
|
|
131
|
+
* Accessing the item moves it to the "most recently used" position.
|
|
132
|
+
* @param key The key to look for.
|
|
133
|
+
* @returns The value associated with the key, or undefined if not found.
|
|
116
134
|
*/
|
|
117
135
|
get(key) {
|
|
118
136
|
const node = this.map.get(key);
|
|
@@ -121,13 +139,17 @@ var LRUMap = class {
|
|
|
121
139
|
return node.value;
|
|
122
140
|
}
|
|
123
141
|
/**
|
|
124
|
-
*
|
|
142
|
+
* Checks if a key exists in the cache without changing its access order.
|
|
143
|
+
* @param key The key to check.
|
|
144
|
+
* @returns True if the key exists, false otherwise.
|
|
125
145
|
*/
|
|
126
146
|
has(key) {
|
|
127
147
|
return this.map.has(key);
|
|
128
148
|
}
|
|
129
149
|
/**
|
|
130
|
-
*
|
|
150
|
+
* Removes a key and its associated value from the cache.
|
|
151
|
+
* @param key The key to remove.
|
|
152
|
+
* @returns True if the key was found and removed, false otherwise.
|
|
131
153
|
*/
|
|
132
154
|
delete(key) {
|
|
133
155
|
const node = this.map.get(key);
|
|
@@ -137,7 +159,8 @@ var LRUMap = class {
|
|
|
137
159
|
return true;
|
|
138
160
|
}
|
|
139
161
|
/**
|
|
140
|
-
*
|
|
162
|
+
* Returns an iterator of keys in the order of most recently used to least recently used.
|
|
163
|
+
* @returns An iterable iterator of keys.
|
|
141
164
|
*/
|
|
142
165
|
*keys() {
|
|
143
166
|
let current = this.head;
|
|
@@ -147,13 +170,13 @@ var LRUMap = class {
|
|
|
147
170
|
}
|
|
148
171
|
}
|
|
149
172
|
/**
|
|
150
|
-
*
|
|
173
|
+
* Returns the current number of items in the cache.
|
|
151
174
|
*/
|
|
152
175
|
get size() {
|
|
153
176
|
return this.map.size;
|
|
154
177
|
}
|
|
155
178
|
/**
|
|
156
|
-
*
|
|
179
|
+
* Clears all items from the cache.
|
|
157
180
|
*/
|
|
158
181
|
clear() {
|
|
159
182
|
this.map.clear();
|
|
@@ -1167,11 +1190,271 @@ var BPTreeSync = class extends BPTree {
|
|
|
1167
1190
|
}
|
|
1168
1191
|
};
|
|
1169
1192
|
|
|
1193
|
+
// node_modules/ryoiki/dist/esm/index.mjs
|
|
1194
|
+
var Ryoiki = class _Ryoiki {
|
|
1195
|
+
readings;
|
|
1196
|
+
writings;
|
|
1197
|
+
readQueue;
|
|
1198
|
+
writeQueue;
|
|
1199
|
+
static async CatchError(promise) {
|
|
1200
|
+
return await promise.then((v) => [void 0, v]).catch((err) => [err]);
|
|
1201
|
+
}
|
|
1202
|
+
static IsRangeOverlap(a, b) {
|
|
1203
|
+
const [start1, end1] = a;
|
|
1204
|
+
const [start2, end2] = b;
|
|
1205
|
+
if (end1 <= start2 || end2 <= start1) {
|
|
1206
|
+
return false;
|
|
1207
|
+
}
|
|
1208
|
+
return true;
|
|
1209
|
+
}
|
|
1210
|
+
static ERR_ALREADY_EXISTS(lockId) {
|
|
1211
|
+
return new Error(`The '${lockId}' task already existing in queue or running.`);
|
|
1212
|
+
}
|
|
1213
|
+
static ERR_NOT_EXISTS(lockId) {
|
|
1214
|
+
return new Error(`The '${lockId}' task not existing in task queue.`);
|
|
1215
|
+
}
|
|
1216
|
+
static ERR_TIMEOUT(lockId, timeout) {
|
|
1217
|
+
return new Error(`The task with ID '${lockId}' failed to acquire the lock within the timeout(${timeout}ms).`);
|
|
1218
|
+
}
|
|
1219
|
+
/**
|
|
1220
|
+
* Constructs a new instance of the Ryoiki class.
|
|
1221
|
+
*/
|
|
1222
|
+
constructor() {
|
|
1223
|
+
this.readings = /* @__PURE__ */ new Map();
|
|
1224
|
+
this.writings = /* @__PURE__ */ new Map();
|
|
1225
|
+
this.readQueue = /* @__PURE__ */ new Map();
|
|
1226
|
+
this.writeQueue = /* @__PURE__ */ new Map();
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Creates a range based on a start value and length.
|
|
1230
|
+
* @param start - The starting value of the range.
|
|
1231
|
+
* @param length - The length of the range.
|
|
1232
|
+
* @returns A range tuple [start, start + length].
|
|
1233
|
+
*/
|
|
1234
|
+
range(start, length) {
|
|
1235
|
+
return [start, start + length];
|
|
1236
|
+
}
|
|
1237
|
+
rangeOverlapping(tasks, range) {
|
|
1238
|
+
return Array.from(tasks.values()).some((t) => _Ryoiki.IsRangeOverlap(t.range, range));
|
|
1239
|
+
}
|
|
1240
|
+
isSameRange(a, b) {
|
|
1241
|
+
const [a1, a2] = a;
|
|
1242
|
+
const [b1, b2] = b;
|
|
1243
|
+
return a1 === b1 && a2 === b2;
|
|
1244
|
+
}
|
|
1245
|
+
fetchUnitAndRun(queue, workspaces) {
|
|
1246
|
+
for (const [id, unit] of queue) {
|
|
1247
|
+
if (!unit.condition()) {
|
|
1248
|
+
continue;
|
|
1249
|
+
}
|
|
1250
|
+
this._alloc(queue, workspaces, id);
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
_handleOverload(args, handlers, argPatterns) {
|
|
1254
|
+
for (const [key, pattern] of Object.entries(argPatterns)) {
|
|
1255
|
+
if (this._matchArgs(args, pattern)) {
|
|
1256
|
+
return handlers[key](...args);
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
throw new Error("Invalid arguments");
|
|
1260
|
+
}
|
|
1261
|
+
_matchArgs(args, pattern) {
|
|
1262
|
+
return args.every((arg, index) => {
|
|
1263
|
+
const expectedType = pattern[index];
|
|
1264
|
+
if (expectedType === void 0) return typeof arg === "undefined";
|
|
1265
|
+
if (expectedType === Function) return typeof arg === "function";
|
|
1266
|
+
if (expectedType === Number) return typeof arg === "number";
|
|
1267
|
+
if (expectedType === Array) return Array.isArray(arg);
|
|
1268
|
+
return false;
|
|
1269
|
+
});
|
|
1270
|
+
}
|
|
1271
|
+
_createRandomId() {
|
|
1272
|
+
const timestamp = Date.now().toString(36);
|
|
1273
|
+
const random = Math.random().toString(36).substring(2);
|
|
1274
|
+
return `${timestamp}${random}`;
|
|
1275
|
+
}
|
|
1276
|
+
_alloc(queue, workspaces, lockId) {
|
|
1277
|
+
const unit = queue.get(lockId);
|
|
1278
|
+
if (!unit) {
|
|
1279
|
+
throw _Ryoiki.ERR_NOT_EXISTS(lockId);
|
|
1280
|
+
}
|
|
1281
|
+
workspaces.set(lockId, unit);
|
|
1282
|
+
queue.delete(lockId);
|
|
1283
|
+
unit.alloc();
|
|
1284
|
+
}
|
|
1285
|
+
_free(workspaces, lockId) {
|
|
1286
|
+
const unit = workspaces.get(lockId);
|
|
1287
|
+
if (!unit) {
|
|
1288
|
+
throw _Ryoiki.ERR_NOT_EXISTS(lockId);
|
|
1289
|
+
}
|
|
1290
|
+
workspaces.delete(lockId);
|
|
1291
|
+
unit.free();
|
|
1292
|
+
}
|
|
1293
|
+
_lock(queue, range, timeout, task, condition) {
|
|
1294
|
+
return new Promise((resolve, reject) => {
|
|
1295
|
+
let timeoutId = null;
|
|
1296
|
+
if (timeout >= 0) {
|
|
1297
|
+
timeoutId = setTimeout(() => {
|
|
1298
|
+
reject(_Ryoiki.ERR_TIMEOUT(id, timeout));
|
|
1299
|
+
}, timeout);
|
|
1300
|
+
}
|
|
1301
|
+
const id = this._createRandomId();
|
|
1302
|
+
const alloc = async () => {
|
|
1303
|
+
if (timeoutId !== null) {
|
|
1304
|
+
clearTimeout(timeoutId);
|
|
1305
|
+
}
|
|
1306
|
+
const [err, v] = await _Ryoiki.CatchError(task(id));
|
|
1307
|
+
if (err) reject(err);
|
|
1308
|
+
else resolve(v);
|
|
1309
|
+
};
|
|
1310
|
+
const fetch = () => {
|
|
1311
|
+
this.fetchUnitAndRun(this.readQueue, this.readings);
|
|
1312
|
+
this.fetchUnitAndRun(this.writeQueue, this.writings);
|
|
1313
|
+
};
|
|
1314
|
+
queue.set(id, { id, range, condition, alloc, free: fetch });
|
|
1315
|
+
fetch();
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
_checkWorking(range, workspaces) {
|
|
1319
|
+
let isLocked = false;
|
|
1320
|
+
for (const lock of workspaces.values()) {
|
|
1321
|
+
if (_Ryoiki.IsRangeOverlap(range, lock.range)) {
|
|
1322
|
+
isLocked = true;
|
|
1323
|
+
break;
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
return isLocked;
|
|
1327
|
+
}
|
|
1328
|
+
/**
|
|
1329
|
+
* Checks if there is any active read lock within the specified range.
|
|
1330
|
+
* @param range The range to check for active read locks.
|
|
1331
|
+
* @returns `true` if there is an active read lock within the range, `false` otherwise.
|
|
1332
|
+
*/
|
|
1333
|
+
isReading(range) {
|
|
1334
|
+
return this._checkWorking(range, this.readings);
|
|
1335
|
+
}
|
|
1336
|
+
/**
|
|
1337
|
+
* Checks if there is any active write lock within the specified range.
|
|
1338
|
+
* @param range The range to check for active write locks.
|
|
1339
|
+
* @returns `true` if there is an active write lock within the range, `false` otherwise.
|
|
1340
|
+
*/
|
|
1341
|
+
isWriting(range) {
|
|
1342
|
+
return this._checkWorking(range, this.writings);
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* Checks if a read lock can be acquired within the specified range.
|
|
1346
|
+
* @param range The range to check for read lock availability.
|
|
1347
|
+
* @returns `true` if a read lock can be acquired, `false` otherwise.
|
|
1348
|
+
*/
|
|
1349
|
+
canRead(range) {
|
|
1350
|
+
const writing = this.isWriting(range);
|
|
1351
|
+
return !writing;
|
|
1352
|
+
}
|
|
1353
|
+
/**
|
|
1354
|
+
* Checks if a write lock can be acquired within the specified range.
|
|
1355
|
+
* @param range The range to check for write lock availability.
|
|
1356
|
+
* @returns `true` if a write lock can be acquired, `false` otherwise.
|
|
1357
|
+
*/
|
|
1358
|
+
canWrite(range) {
|
|
1359
|
+
const reading = this.isReading(range);
|
|
1360
|
+
const writing = this.isWriting(range);
|
|
1361
|
+
return !reading && !writing;
|
|
1362
|
+
}
|
|
1363
|
+
/**
|
|
1364
|
+
* Internal implementation of the read lock. Handles both overloads.
|
|
1365
|
+
* @template T - The return type of the task.
|
|
1366
|
+
* @param arg0 - Either a range or a task callback.
|
|
1367
|
+
* If a range is provided, the task is the second argument.
|
|
1368
|
+
* @param arg1 - The task to execute, required if a range is provided.
|
|
1369
|
+
* @param arg2 - The timeout for acquiring the lock.
|
|
1370
|
+
* If the lock cannot be acquired within this period, an error will be thrown.
|
|
1371
|
+
* If this value is not provided, no timeout will be set.
|
|
1372
|
+
* @returns A promise resolving to the result of the task execution.
|
|
1373
|
+
*/
|
|
1374
|
+
readLock(arg0, arg1, arg2) {
|
|
1375
|
+
const [range, task, timeout] = this._handleOverload(
|
|
1376
|
+
[arg0, arg1, arg2],
|
|
1377
|
+
{
|
|
1378
|
+
rangeTask: (range2, task2) => [range2, task2, -1],
|
|
1379
|
+
rangeTaskTimeout: (range2, task2, timeout2) => [range2, task2, timeout2],
|
|
1380
|
+
task: (task2) => [[-Infinity, Infinity], task2, -1],
|
|
1381
|
+
taskTimeout: (task2, timeout2) => [[-Infinity, Infinity], task2, timeout2]
|
|
1382
|
+
},
|
|
1383
|
+
{
|
|
1384
|
+
task: [Function],
|
|
1385
|
+
taskTimeout: [Function, Number],
|
|
1386
|
+
rangeTask: [Array, Function],
|
|
1387
|
+
rangeTaskTimeout: [Array, Function, Number]
|
|
1388
|
+
}
|
|
1389
|
+
);
|
|
1390
|
+
return this._lock(
|
|
1391
|
+
this.readQueue,
|
|
1392
|
+
range,
|
|
1393
|
+
timeout,
|
|
1394
|
+
task,
|
|
1395
|
+
() => !this.rangeOverlapping(this.writings, range)
|
|
1396
|
+
);
|
|
1397
|
+
}
|
|
1398
|
+
/**
|
|
1399
|
+
* Internal implementation of the write lock. Handles both overloads.
|
|
1400
|
+
* @template T - The return type of the task.
|
|
1401
|
+
* @param arg0 - Either a range or a task callback.
|
|
1402
|
+
* If a range is provided, the task is the second argument.
|
|
1403
|
+
* @param arg1 - The task to execute, required if a range is provided.
|
|
1404
|
+
* @param arg2 - The timeout for acquiring the lock.
|
|
1405
|
+
* If the lock cannot be acquired within this period, an error will be thrown.
|
|
1406
|
+
* If this value is not provided, no timeout will be set.
|
|
1407
|
+
* @returns A promise resolving to the result of the task execution.
|
|
1408
|
+
*/
|
|
1409
|
+
writeLock(arg0, arg1, arg2) {
|
|
1410
|
+
const [range, task, timeout] = this._handleOverload(
|
|
1411
|
+
[arg0, arg1, arg2],
|
|
1412
|
+
{
|
|
1413
|
+
rangeTask: (range2, task2) => [range2, task2, -1],
|
|
1414
|
+
rangeTaskTimeout: (range2, task2, timeout2) => [range2, task2, timeout2],
|
|
1415
|
+
task: (task2) => [[-Infinity, Infinity], task2, -1],
|
|
1416
|
+
taskTimeout: (task2, timeout2) => [[-Infinity, Infinity], task2, timeout2]
|
|
1417
|
+
},
|
|
1418
|
+
{
|
|
1419
|
+
task: [Function],
|
|
1420
|
+
taskTimeout: [Function, Number],
|
|
1421
|
+
rangeTask: [Array, Function],
|
|
1422
|
+
rangeTaskTimeout: [Array, Function, Number]
|
|
1423
|
+
}
|
|
1424
|
+
);
|
|
1425
|
+
return this._lock(
|
|
1426
|
+
this.writeQueue,
|
|
1427
|
+
range,
|
|
1428
|
+
timeout,
|
|
1429
|
+
task,
|
|
1430
|
+
() => {
|
|
1431
|
+
return !this.rangeOverlapping(this.writings, range) && !this.rangeOverlapping(this.readings, range);
|
|
1432
|
+
}
|
|
1433
|
+
);
|
|
1434
|
+
}
|
|
1435
|
+
/**
|
|
1436
|
+
* Releases a read lock by its lock ID.
|
|
1437
|
+
* @param lockId - The unique identifier for the lock to release.
|
|
1438
|
+
*/
|
|
1439
|
+
readUnlock(lockId) {
|
|
1440
|
+
this._free(this.readings, lockId);
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Releases a write lock by its lock ID.
|
|
1444
|
+
* @param lockId - The unique identifier for the lock to release.
|
|
1445
|
+
*/
|
|
1446
|
+
writeUnlock(lockId) {
|
|
1447
|
+
this._free(this.writings, lockId);
|
|
1448
|
+
}
|
|
1449
|
+
};
|
|
1450
|
+
|
|
1170
1451
|
// src/BPTreeAsync.ts
|
|
1171
1452
|
var BPTreeAsync = class extends BPTree {
|
|
1453
|
+
lock;
|
|
1172
1454
|
constructor(strategy, comparator, option) {
|
|
1173
1455
|
super(strategy, comparator, option);
|
|
1174
1456
|
this.nodes = this._createCachedNode();
|
|
1457
|
+
this.lock = new Ryoiki();
|
|
1175
1458
|
}
|
|
1176
1459
|
_createCachedNode() {
|
|
1177
1460
|
return new CacheEntanglementAsync(async (key) => {
|
|
@@ -1180,6 +1463,24 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1180
1463
|
capacity: this.option.capacity ?? 1e3
|
|
1181
1464
|
});
|
|
1182
1465
|
}
|
|
1466
|
+
async readLock(callback) {
|
|
1467
|
+
let lockId;
|
|
1468
|
+
return await this.lock.readLock(async (_lockId) => {
|
|
1469
|
+
lockId = _lockId;
|
|
1470
|
+
return await callback();
|
|
1471
|
+
}).finally(() => {
|
|
1472
|
+
this.lock.readUnlock(lockId);
|
|
1473
|
+
});
|
|
1474
|
+
}
|
|
1475
|
+
async writeLock(callback) {
|
|
1476
|
+
let lockId;
|
|
1477
|
+
return await this.lock.writeLock(async (_lockId) => {
|
|
1478
|
+
lockId = _lockId;
|
|
1479
|
+
return await callback();
|
|
1480
|
+
}).finally(() => {
|
|
1481
|
+
this.lock.writeUnlock(lockId);
|
|
1482
|
+
});
|
|
1483
|
+
}
|
|
1183
1484
|
async getPairsRightToLeft(value, startNode, endNode, comparator) {
|
|
1184
1485
|
const pairs = [];
|
|
1185
1486
|
let node = startNode;
|
|
@@ -1623,139 +1924,153 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1623
1924
|
this._nodeDeleteBuffer.clear();
|
|
1624
1925
|
}
|
|
1625
1926
|
async keys(condition, filterValues) {
|
|
1626
|
-
|
|
1627
|
-
const
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
const
|
|
1640
|
-
|
|
1641
|
-
|
|
1927
|
+
return this.readLock(async () => {
|
|
1928
|
+
for (const k in condition) {
|
|
1929
|
+
const key = k;
|
|
1930
|
+
const value = condition[key];
|
|
1931
|
+
const startNode = await this.verifierStartNode[key](value);
|
|
1932
|
+
const endNode = await this.verifierEndNode[key](value);
|
|
1933
|
+
const direction = this.verifierDirection[key];
|
|
1934
|
+
const comparator = this.verifierMap[key];
|
|
1935
|
+
const pairs = await this.getPairs(value, startNode, endNode, comparator, direction);
|
|
1936
|
+
if (!filterValues) {
|
|
1937
|
+
filterValues = new Set(pairs.keys());
|
|
1938
|
+
} else {
|
|
1939
|
+
const intersections = /* @__PURE__ */ new Set();
|
|
1940
|
+
for (const key2 of filterValues) {
|
|
1941
|
+
const has = pairs.has(key2);
|
|
1942
|
+
if (has) {
|
|
1943
|
+
intersections.add(key2);
|
|
1944
|
+
}
|
|
1642
1945
|
}
|
|
1946
|
+
filterValues = intersections;
|
|
1643
1947
|
}
|
|
1644
|
-
filterValues = intersections;
|
|
1645
1948
|
}
|
|
1646
|
-
|
|
1647
|
-
|
|
1949
|
+
return filterValues ?? /* @__PURE__ */ new Set([]);
|
|
1950
|
+
});
|
|
1648
1951
|
}
|
|
1649
1952
|
async where(condition) {
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
const
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
result
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1953
|
+
return this.readLock(async () => {
|
|
1954
|
+
let result = null;
|
|
1955
|
+
for (const k in condition) {
|
|
1956
|
+
const key = k;
|
|
1957
|
+
const value = condition[key];
|
|
1958
|
+
const startNode = await this.verifierStartNode[key](value);
|
|
1959
|
+
const endNode = await this.verifierEndNode[key](value);
|
|
1960
|
+
const direction = this.verifierDirection[key];
|
|
1961
|
+
const comparator = this.verifierMap[key];
|
|
1962
|
+
const pairs = await this.getPairs(value, startNode, endNode, comparator, direction);
|
|
1963
|
+
if (result === null) {
|
|
1964
|
+
result = pairs;
|
|
1965
|
+
} else {
|
|
1966
|
+
const intersection = /* @__PURE__ */ new Map();
|
|
1967
|
+
for (const [k2, v] of pairs) {
|
|
1968
|
+
if (result.has(k2)) {
|
|
1969
|
+
intersection.set(k2, v);
|
|
1970
|
+
}
|
|
1666
1971
|
}
|
|
1972
|
+
result = intersection;
|
|
1667
1973
|
}
|
|
1668
|
-
result = intersection;
|
|
1669
1974
|
}
|
|
1670
|
-
|
|
1671
|
-
|
|
1975
|
+
return result ?? /* @__PURE__ */ new Map();
|
|
1976
|
+
});
|
|
1672
1977
|
}
|
|
1673
1978
|
async insert(key, value) {
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1979
|
+
return this.writeLock(async () => {
|
|
1980
|
+
const before = await this.insertableNode(value);
|
|
1981
|
+
this._insertAtLeaf(before, key, value);
|
|
1982
|
+
if (before.values.length === this.order) {
|
|
1983
|
+
const after = await this._createNode(
|
|
1984
|
+
true,
|
|
1985
|
+
[],
|
|
1986
|
+
[],
|
|
1987
|
+
true,
|
|
1988
|
+
before.parent,
|
|
1989
|
+
before.next,
|
|
1990
|
+
before.id
|
|
1991
|
+
);
|
|
1992
|
+
const mid = Math.ceil(this.order / 2) - 1;
|
|
1993
|
+
const beforeNext = before.next;
|
|
1994
|
+
after.values = before.values.slice(mid + 1);
|
|
1995
|
+
after.keys = before.keys.slice(mid + 1);
|
|
1996
|
+
before.values = before.values.slice(0, mid + 1);
|
|
1997
|
+
before.keys = before.keys.slice(0, mid + 1);
|
|
1998
|
+
before.next = after.id;
|
|
1999
|
+
if (beforeNext) {
|
|
2000
|
+
const node = await this.getNode(beforeNext);
|
|
2001
|
+
node.prev = after.id;
|
|
2002
|
+
this.bufferForNodeUpdate(node);
|
|
2003
|
+
}
|
|
2004
|
+
await this._insertInParent(before, after.values[0], after);
|
|
2005
|
+
this.bufferForNodeUpdate(before);
|
|
1697
2006
|
}
|
|
1698
|
-
await this.
|
|
1699
|
-
this.
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
await this.commitNodeCreateBuffer();
|
|
1703
|
-
await this.commitNodeUpdateBuffer();
|
|
2007
|
+
await this.commitHeadBuffer();
|
|
2008
|
+
await this.commitNodeCreateBuffer();
|
|
2009
|
+
await this.commitNodeUpdateBuffer();
|
|
2010
|
+
});
|
|
1704
2011
|
}
|
|
1705
2012
|
async delete(key, value) {
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
if (keys.
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
node.
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
2013
|
+
return this.writeLock(async () => {
|
|
2014
|
+
const node = await this.insertableNode(value);
|
|
2015
|
+
let i = node.values.length;
|
|
2016
|
+
while (i--) {
|
|
2017
|
+
const nValue = node.values[i];
|
|
2018
|
+
if (this.comparator.isSame(value, nValue)) {
|
|
2019
|
+
const keys = node.keys[i];
|
|
2020
|
+
if (keys.includes(key)) {
|
|
2021
|
+
if (keys.length > 1) {
|
|
2022
|
+
keys.splice(keys.indexOf(key), 1);
|
|
2023
|
+
this.bufferForNodeUpdate(node);
|
|
2024
|
+
} else if (node.id === this.root.id) {
|
|
2025
|
+
node.values.splice(i, 1);
|
|
2026
|
+
node.keys.splice(i, 1);
|
|
2027
|
+
this.bufferForNodeUpdate(node);
|
|
2028
|
+
} else {
|
|
2029
|
+
keys.splice(keys.indexOf(key), 1);
|
|
2030
|
+
node.keys.splice(i, 1);
|
|
2031
|
+
node.values.splice(node.values.indexOf(value), 1);
|
|
2032
|
+
await this._deleteEntry(node, key, value);
|
|
2033
|
+
this.bufferForNodeUpdate(node);
|
|
2034
|
+
}
|
|
1726
2035
|
}
|
|
1727
2036
|
}
|
|
1728
2037
|
}
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
2038
|
+
await this.commitHeadBuffer();
|
|
2039
|
+
await this.commitNodeCreateBuffer();
|
|
2040
|
+
await this.commitNodeUpdateBuffer();
|
|
2041
|
+
await this.commitNodeDeleteBuffer();
|
|
2042
|
+
});
|
|
1734
2043
|
}
|
|
1735
2044
|
async exists(key, value) {
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
2045
|
+
return this.readLock(async () => {
|
|
2046
|
+
const node = await this.insertableNode(value);
|
|
2047
|
+
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
2048
|
+
const nValue = node.values[i];
|
|
2049
|
+
if (this.comparator.isSame(value, nValue)) {
|
|
2050
|
+
const keys = node.keys[i];
|
|
2051
|
+
return keys.includes(key);
|
|
2052
|
+
}
|
|
1742
2053
|
}
|
|
1743
|
-
|
|
1744
|
-
|
|
2054
|
+
return false;
|
|
2055
|
+
});
|
|
1745
2056
|
}
|
|
1746
2057
|
async setHeadData(data) {
|
|
1747
|
-
this.
|
|
1748
|
-
|
|
1749
|
-
|
|
2058
|
+
return this.writeLock(async () => {
|
|
2059
|
+
this.strategy.head.data = data;
|
|
2060
|
+
this._strategyDirty = true;
|
|
2061
|
+
await this.commitHeadBuffer();
|
|
2062
|
+
});
|
|
1750
2063
|
}
|
|
1751
2064
|
async forceUpdate() {
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
2065
|
+
return this.readLock(async () => {
|
|
2066
|
+
const keys = [...this.nodes.keys()];
|
|
2067
|
+
this.nodes.clear();
|
|
2068
|
+
await this.init();
|
|
2069
|
+
for (const key of keys) {
|
|
2070
|
+
await this.getNode(key);
|
|
2071
|
+
}
|
|
2072
|
+
return keys.length;
|
|
2073
|
+
});
|
|
1759
2074
|
}
|
|
1760
2075
|
};
|
|
1761
2076
|
|