jsql-neo 5.5.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -202
- package/README.md +8155 -8097
- package/bin/jsql +320 -320
- package/bin/jsql-server +90 -90
- package/bin/jsql-server-control.js +177 -177
- package/index.d.ts +412 -385
- package/index.js +128 -124
- package/lib/ast.js +214 -214
- package/lib/btree.js +478 -413
- package/lib/cache.js +328 -328
- package/lib/client.js +125 -125
- package/lib/database.js +1741 -1744
- package/lib/date-types.js +182 -182
- package/lib/errors.js +89 -89
- package/lib/jsql_format.js +497 -497
- package/lib/migrate.js +254 -254
- package/lib/mod.js +292 -292
- package/lib/mongo_server.js +607 -607
- package/lib/multiserver.js +140 -140
- package/lib/mysql_compat.js +413 -413
- package/lib/mysql_server.js +1237 -1237
- package/lib/native_client.js +722 -732
- package/lib/nedb_compat.js +506 -506
- package/lib/pg_server.js +864 -864
- package/lib/plugin.js +259 -35
- package/lib/plugins/audit.js +37 -0
- package/lib/plugins/index.js +72 -0
- package/lib/plugins/logger.js +35 -0
- package/lib/plugins/metrics.js +38 -0
- package/lib/plugins/timestamps.js +57 -0
- package/lib/plugins/validation.js +128 -0
- package/lib/prepared.js +37 -0
- package/lib/query.js +834 -815
- package/lib/redis_server.js +571 -571
- package/lib/sql.js +4053 -3460
- package/lib/sqlite_compat.js +323 -323
- package/lib/sqlite_worker.js +264 -264
- package/lib/table.js +1351 -1304
- package/lib/tui.js +502 -502
- package/lib/wasm.js +1 -1
- package/lib/wasm_client.js +715 -718
- package/lib/web_ui.js +257 -257
- package/native/jsql-neo-native.node +0 -0
- package/nativesrc/jsql-neo-core/Cargo.lock +462 -455
- package/nativesrc/jsql-neo-core/Cargo.toml +26 -24
- package/nativesrc/jsql-neo-core/src/engine/hybrid.rs +492 -455
- package/nativesrc/jsql-neo-core/src/engine/memory.rs +191 -168
- package/nativesrc/jsql-neo-core/src/engine/mod.rs +74 -62
- package/nativesrc/jsql-neo-core/src/engine/table.rs +996 -700
- package/nativesrc/jsql-neo-core/src/lib.rs +2 -2
- package/nativesrc/jsql-neo-core/src/storage/mod.rs +2 -2
- package/nativesrc/jsql-neo-core/src/storage/persistent.rs +1 -1
- package/nativesrc/jsql-neo-core/src/storage/wal.rs +298 -85
- package/nativesrc/jsql-neo-core/src/types.rs +93 -93
- package/nativesrc/jsql-neo-native/Cargo.lock +613 -606
- package/nativesrc/jsql-neo-native/Cargo.toml +24 -16
- package/nativesrc/jsql-neo-native/build.rs +3 -3
- package/nativesrc/jsql-neo-native/package.json +7 -7
- package/nativesrc/jsql-neo-native/src/lib.rs +281 -269
- package/nativesrc/jsql-neo-wasm/Cargo.lock +472 -465
- package/nativesrc/jsql-neo-wasm/Cargo.toml +22 -15
- package/nativesrc/jsql-neo-wasm/src/lib.rs +463 -447
- package/package.json +87 -87
- package/postinstall.js +158 -158
- package/test/btree.test.js +144 -128
- package/test/coverage.js +187 -187
- package/test/join.test.js +110 -110
- package/test/native.test.js +158 -158
- package/test/query-opt.test.js +109 -0
- package/test/regress-5.1.0.js +271 -271
- package/test/smoke.js +58 -58
- package/test/sql-parser.test.js +450 -273
- package/test/wasm.test.js +110 -110
- package/wasm/browser.d.ts +89 -89
- package/wasm/browser.mjs +591 -569
- package/wasm/browser_bg.mjs +618 -597
- package/wasm/jsql_neo_wasm.d.ts +44 -38
- package/wasm/jsql_neo_wasm.js +641 -520
- package/wasm/jsql_neo_wasm_bg.wasm +0 -0
- package/wasm/jsql_neo_wasm_bg.wasm.d.ts +26 -26
- package/wasm/package.json +11 -11
package/lib/btree.js
CHANGED
|
@@ -1,414 +1,479 @@
|
|
|
1
|
-
// © Vexify 2026 All Rights Reserved.
|
|
2
|
-
/**
|
|
3
|
-
* JSQL B-Tree 索引引擎
|
|
4
|
-
* O(log n) 查找/插入/删除,支持范围查询和复合键
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
class BTreeNode {
|
|
8
|
-
constructor(leaf = true) {
|
|
9
|
-
this.keys = []; // 索引键值
|
|
10
|
-
this.values = []; // 对应的行号数组
|
|
11
|
-
this.children = []; // 子节点
|
|
12
|
-
this.leaf = leaf;
|
|
13
|
-
this.next = null; // 叶子节点链表(范围查询用)
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
class BTree {
|
|
18
|
-
/**
|
|
19
|
-
* @param {number} order - B-Tree 阶数,默认 64
|
|
20
|
-
* @param {boolean} unique - 是否唯一索引
|
|
21
|
-
*/
|
|
22
|
-
constructor(order = 64, unique = false) {
|
|
23
|
-
this._order = order;
|
|
24
|
-
this._unique = unique;
|
|
25
|
-
this._root = new BTreeNode();
|
|
26
|
-
this._size = 0;
|
|
27
|
-
this._minKeys = Math.floor(order / 2) - 1;
|
|
28
|
-
this._minDegree = Math.ceil(order / 2);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
get size() { return this._size; }
|
|
32
|
-
|
|
33
|
-
// ============================================================
|
|
34
|
-
// 插入
|
|
35
|
-
// ============================================================
|
|
36
|
-
|
|
37
|
-
insert(key, rowIndex) {
|
|
38
|
-
const root = this._root;
|
|
39
|
-
|
|
40
|
-
// 根节点满 → 分裂
|
|
41
|
-
if (root.keys.length === this._order - 1) {
|
|
42
|
-
const newRoot = new BTreeNode(false);
|
|
43
|
-
newRoot.children.push(root);
|
|
44
|
-
this._splitChild(newRoot, 0);
|
|
45
|
-
this._root = newRoot;
|
|
46
|
-
this._insertNonFull(newRoot, key, rowIndex);
|
|
47
|
-
} else {
|
|
48
|
-
this._insertNonFull(root, key, rowIndex);
|
|
49
|
-
}
|
|
50
|
-
this._size++;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
this.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
newChild.
|
|
102
|
-
child.
|
|
103
|
-
child.
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
return
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
i =
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
if (
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
1
|
+
// © Vexify 2026 All Rights Reserved.
|
|
2
|
+
/**
|
|
3
|
+
* JSQL B-Tree 索引引擎
|
|
4
|
+
* O(log n) 查找/插入/删除,支持范围查询和复合键
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
class BTreeNode {
|
|
8
|
+
constructor(leaf = true) {
|
|
9
|
+
this.keys = []; // 索引键值
|
|
10
|
+
this.values = []; // 对应的行号数组
|
|
11
|
+
this.children = []; // 子节点
|
|
12
|
+
this.leaf = leaf;
|
|
13
|
+
this.next = null; // 叶子节点链表(范围查询用)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class BTree {
|
|
18
|
+
/**
|
|
19
|
+
* @param {number} order - B-Tree 阶数,默认 64
|
|
20
|
+
* @param {boolean} unique - 是否唯一索引
|
|
21
|
+
*/
|
|
22
|
+
constructor(order = 64, unique = false) {
|
|
23
|
+
this._order = order;
|
|
24
|
+
this._unique = unique;
|
|
25
|
+
this._root = new BTreeNode();
|
|
26
|
+
this._size = 0;
|
|
27
|
+
this._minKeys = Math.floor(order / 2) - 1;
|
|
28
|
+
this._minDegree = Math.ceil(order / 2);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get size() { return this._size; }
|
|
32
|
+
|
|
33
|
+
// ============================================================
|
|
34
|
+
// 插入
|
|
35
|
+
// ============================================================
|
|
36
|
+
|
|
37
|
+
insert(key, rowIndex) {
|
|
38
|
+
const root = this._root;
|
|
39
|
+
|
|
40
|
+
// 根节点满 → 分裂
|
|
41
|
+
if (root.keys.length === this._order - 1) {
|
|
42
|
+
const newRoot = new BTreeNode(false);
|
|
43
|
+
newRoot.children.push(root);
|
|
44
|
+
this._splitChild(newRoot, 0);
|
|
45
|
+
this._root = newRoot;
|
|
46
|
+
this._insertNonFull(newRoot, key, rowIndex);
|
|
47
|
+
} else {
|
|
48
|
+
this._insertNonFull(root, key, rowIndex);
|
|
49
|
+
}
|
|
50
|
+
this._size++;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
_lowerBound(keys, key) {
|
|
54
|
+
let lo = 0, hi = keys.length;
|
|
55
|
+
while (lo < hi) {
|
|
56
|
+
const mid = (lo + hi) >> 1;
|
|
57
|
+
if (keys[mid] < key) lo = mid + 1;
|
|
58
|
+
else hi = mid;
|
|
59
|
+
}
|
|
60
|
+
return lo;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_insertNonFull(node, key, rowIndex) {
|
|
64
|
+
if (node.leaf) {
|
|
65
|
+
const i = this._lowerBound(node.keys, key);
|
|
66
|
+
|
|
67
|
+
if (i < node.keys.length && node.keys[i] === key) {
|
|
68
|
+
if (this._unique) {
|
|
69
|
+
this._size--;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (!node.values[i].includes(rowIndex)) {
|
|
73
|
+
node.values[i].push(rowIndex);
|
|
74
|
+
}
|
|
75
|
+
this._size--;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
node.keys.splice(i, 0, key);
|
|
80
|
+
node.values.splice(i, 0, [rowIndex]);
|
|
81
|
+
} else {
|
|
82
|
+
let i = this._route(node, key);
|
|
83
|
+
if (node.children[i] && node.children[i].keys.length === this._order - 1) {
|
|
84
|
+
this._splitChild(node, i);
|
|
85
|
+
i = this._route(node, key);
|
|
86
|
+
}
|
|
87
|
+
this._insertNonFull(node.children[i], key, rowIndex);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
_splitChild(parent, index) {
|
|
92
|
+
const child = parent.children[index];
|
|
93
|
+
const newChild = new BTreeNode(child.leaf);
|
|
94
|
+
const mid = Math.floor((this._order - 1) / 2);
|
|
95
|
+
|
|
96
|
+
const midKey = child.keys[mid];
|
|
97
|
+
const midVal = child.values[mid];
|
|
98
|
+
|
|
99
|
+
if (child.leaf) {
|
|
100
|
+
// 叶子:右半移到新节点;分隔键保留在左叶(它是真实数据键),父节点只存副本
|
|
101
|
+
newChild.keys = child.keys.splice(mid + 1);
|
|
102
|
+
newChild.values = child.values.splice(mid + 1);
|
|
103
|
+
newChild.next = child.next;
|
|
104
|
+
child.next = newChild;
|
|
105
|
+
} else {
|
|
106
|
+
// 内部:分隔键提升到父节点并从子节点移除,保持 children = keys + 1
|
|
107
|
+
newChild.keys = child.keys.splice(mid + 1);
|
|
108
|
+
newChild.values = child.values.splice(mid + 1);
|
|
109
|
+
child.keys.splice(mid, 1);
|
|
110
|
+
child.values.splice(mid, 1);
|
|
111
|
+
newChild.children = child.children.splice(mid + 1);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
parent.keys.splice(index, 0, midKey);
|
|
115
|
+
parent.values.splice(index, 0, midVal);
|
|
116
|
+
parent.children.splice(index + 1, 0, newChild);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// ============================================================
|
|
120
|
+
// 查找
|
|
121
|
+
// ============================================================
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 内部节点路由:返回 key 应下降到的子节点下标。
|
|
125
|
+
* 命中分隔键副本时,根据左子树最大键判定真实数据所在子树
|
|
126
|
+
* (分隔键副本可能在左子树作为最大值,或在右子树作为最小值)。
|
|
127
|
+
*/
|
|
128
|
+
_route(node, key) {
|
|
129
|
+
let i = this._lowerBound(node.keys, key);
|
|
130
|
+
if (i < node.keys.length && node.keys[i] === key) {
|
|
131
|
+
return this._getMax(node.children[i]).key === key ? i : i + 1;
|
|
132
|
+
}
|
|
133
|
+
return i;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 精确查找(始终下探到叶子取真实数据;内部分隔键只是叶子的副本,仅用于导航)
|
|
138
|
+
* @returns {number[]} 行号数组;未找到时返回空数组
|
|
139
|
+
*/
|
|
140
|
+
search(key) {
|
|
141
|
+
let node = this._root;
|
|
142
|
+
while (!node.leaf) node = node.children[this._route(node, key)];
|
|
143
|
+
const i = this._lowerBound(node.keys, key);
|
|
144
|
+
return (i < node.keys.length && node.keys[i] === key) ? node.values[i] : [];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
searchMany(keys) {
|
|
148
|
+
if (!keys || keys.length === 0) return [];
|
|
149
|
+
const out = [];
|
|
150
|
+
const seen = new Set();
|
|
151
|
+
for (let i = 0; i < keys.length; i++) {
|
|
152
|
+
const hits = this.search(keys[i]);
|
|
153
|
+
for (let j = 0; j < hits.length; j++) {
|
|
154
|
+
const idx = hits[j];
|
|
155
|
+
if (!seen.has(idx)) {
|
|
156
|
+
seen.add(idx);
|
|
157
|
+
out.push(idx);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
prefix(prefix) {
|
|
165
|
+
if (prefix === undefined || prefix === null) return [];
|
|
166
|
+
const p = String(prefix);
|
|
167
|
+
const result = [];
|
|
168
|
+
let node = this._findLeaf(p);
|
|
169
|
+
while (node) {
|
|
170
|
+
for (let i = 0; i < node.keys.length; i++) {
|
|
171
|
+
const k = node.keys[i];
|
|
172
|
+
const s = k === undefined || k === null ? '' : String(k);
|
|
173
|
+
if (s < p) continue;
|
|
174
|
+
if (!s.startsWith(p)) return result;
|
|
175
|
+
result.push(...node.values[i]);
|
|
176
|
+
}
|
|
177
|
+
node = node.next;
|
|
178
|
+
}
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 范围查询 [min, max]
|
|
184
|
+
*/
|
|
185
|
+
range(min, max) {
|
|
186
|
+
const result = [];
|
|
187
|
+
let node = this._findLeaf(min);
|
|
188
|
+
|
|
189
|
+
while (node) {
|
|
190
|
+
for (let i = 0; i < node.keys.length; i++) {
|
|
191
|
+
const k = node.keys[i];
|
|
192
|
+
if (k > max) return result;
|
|
193
|
+
if (k >= min) {
|
|
194
|
+
result.push(...node.values[i]);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
node = node.next;
|
|
198
|
+
}
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* 大于查询(严格开区间,不含 min 本身)
|
|
204
|
+
*/
|
|
205
|
+
greaterThan(min) {
|
|
206
|
+
const result = [];
|
|
207
|
+
let node = this._findLeaf(min);
|
|
208
|
+
while (node) {
|
|
209
|
+
for (let i = 0; i < node.keys.length; i++) {
|
|
210
|
+
if (node.keys[i] > min) result.push(...node.values[i]);
|
|
211
|
+
}
|
|
212
|
+
node = node.next;
|
|
213
|
+
}
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
greaterThanEqual(min) {
|
|
218
|
+
const result = [];
|
|
219
|
+
let node = this._findLeaf(min);
|
|
220
|
+
while (node) {
|
|
221
|
+
for (let i = 0; i < node.keys.length; i++) {
|
|
222
|
+
if (node.keys[i] >= min) result.push(...node.values[i]);
|
|
223
|
+
}
|
|
224
|
+
node = node.next;
|
|
225
|
+
}
|
|
226
|
+
return result;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 小于查询(严格开区间,不含 max 本身)
|
|
231
|
+
*/
|
|
232
|
+
lessThan(max) {
|
|
233
|
+
const result = [];
|
|
234
|
+
let node = this._root;
|
|
235
|
+
while (!node.leaf) node = node.children[0];
|
|
236
|
+
while (node) {
|
|
237
|
+
for (let i = 0; i < node.keys.length; i++) {
|
|
238
|
+
if (node.keys[i] < max) result.push(...node.values[i]);
|
|
239
|
+
else return result;
|
|
240
|
+
}
|
|
241
|
+
node = node.next;
|
|
242
|
+
}
|
|
243
|
+
return result;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
lessThanEqual(max) {
|
|
247
|
+
const result = [];
|
|
248
|
+
let node = this._root;
|
|
249
|
+
while (!node.leaf) node = node.children[0];
|
|
250
|
+
while (node) {
|
|
251
|
+
for (let i = 0; i < node.keys.length; i++) {
|
|
252
|
+
if (node.keys[i] <= max) result.push(...node.values[i]);
|
|
253
|
+
else return result;
|
|
254
|
+
}
|
|
255
|
+
node = node.next;
|
|
256
|
+
}
|
|
257
|
+
return result;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
_findLeaf(key) {
|
|
261
|
+
let node = this._root;
|
|
262
|
+
while (!node.leaf) {
|
|
263
|
+
node = node.children[this._lowerBound(node.keys, key)];
|
|
264
|
+
}
|
|
265
|
+
return node;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// ============================================================
|
|
269
|
+
// 删除
|
|
270
|
+
// ============================================================
|
|
271
|
+
|
|
272
|
+
remove(key, rowIndex) {
|
|
273
|
+
if (!this._root) return false;
|
|
274
|
+
const removed = this._removeFromNode(this._root, key, rowIndex);
|
|
275
|
+
if (removed) this._size--;
|
|
276
|
+
if (this._root.keys.length === 0 && !this._root.leaf && this._root.children.length > 0) {
|
|
277
|
+
this._root = this._root.children[0];
|
|
278
|
+
}
|
|
279
|
+
return removed;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
_removeFromNode(node, key, rowIndex) {
|
|
283
|
+
if (node.leaf) {
|
|
284
|
+
const i = this._lowerBound(node.keys, key);
|
|
285
|
+
if (i >= node.keys.length || node.keys[i] !== key) return false;
|
|
286
|
+
if (!node.values[i].includes(rowIndex)) return false;
|
|
287
|
+
const vals = node.values[i].filter(v => v !== rowIndex);
|
|
288
|
+
if (vals.length === 0) {
|
|
289
|
+
node.keys.splice(i, 1);
|
|
290
|
+
node.values.splice(i, 1);
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
node.values[i] = vals;
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
let i = this._route(node, key);
|
|
298
|
+
|
|
299
|
+
// 下降前确保子节点有足够键,避免欠满子节点
|
|
300
|
+
const child = node.children[i];
|
|
301
|
+
if (child.keys.length <= this._minKeys) {
|
|
302
|
+
i = this._rebalanceChild(node, i);
|
|
303
|
+
}
|
|
304
|
+
return this._removeFromNode(node.children[i], key, rowIndex);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
_rebalanceChild(parent, i) {
|
|
308
|
+
if (i > 0 && parent.children[i - 1].keys.length > this._minKeys) {
|
|
309
|
+
this._borrowFromLeft(parent, i);
|
|
310
|
+
return i;
|
|
311
|
+
}
|
|
312
|
+
if (i + 1 < parent.children.length && parent.children[i + 1].keys.length > this._minKeys) {
|
|
313
|
+
this._borrowFromRight(parent, i);
|
|
314
|
+
return i;
|
|
315
|
+
}
|
|
316
|
+
if (i > 0) {
|
|
317
|
+
this._mergeChildren(parent, i - 1);
|
|
318
|
+
return i - 1;
|
|
319
|
+
}
|
|
320
|
+
this._mergeChildren(parent, i);
|
|
321
|
+
return i;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
_borrowFromLeft(parent, i) {
|
|
325
|
+
const left = parent.children[i - 1];
|
|
326
|
+
const right = parent.children[i];
|
|
327
|
+
if (right.leaf) {
|
|
328
|
+
// 叶子节点:分隔键是叶子的副本,用被移动的键而非旧分隔键,避免重复
|
|
329
|
+
const k = left.keys.pop();
|
|
330
|
+
const v = left.values.pop();
|
|
331
|
+
right.keys.unshift(k);
|
|
332
|
+
right.values.unshift(v);
|
|
333
|
+
parent.keys[i - 1] = k;
|
|
334
|
+
parent.values[i - 1] = v;
|
|
335
|
+
} else {
|
|
336
|
+
right.keys.unshift(parent.keys[i - 1]);
|
|
337
|
+
right.values.unshift(parent.values[i - 1]);
|
|
338
|
+
parent.keys[i - 1] = left.keys.pop();
|
|
339
|
+
parent.values[i - 1] = left.values.pop();
|
|
340
|
+
right.children.unshift(left.children.pop());
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
_borrowFromRight(parent, i) {
|
|
345
|
+
const left = parent.children[i];
|
|
346
|
+
const right = parent.children[i + 1];
|
|
347
|
+
if (left.leaf) {
|
|
348
|
+
// 叶子节点:用被移动的键而非旧分隔键,避免重复
|
|
349
|
+
const k = right.keys.shift();
|
|
350
|
+
const v = right.values.shift();
|
|
351
|
+
left.keys.push(k);
|
|
352
|
+
left.values.push(v);
|
|
353
|
+
parent.keys[i] = k;
|
|
354
|
+
parent.values[i] = v;
|
|
355
|
+
} else {
|
|
356
|
+
left.keys.push(parent.keys[i]);
|
|
357
|
+
left.values.push(parent.values[i]);
|
|
358
|
+
parent.keys[i] = right.keys.shift();
|
|
359
|
+
parent.values[i] = right.values.shift();
|
|
360
|
+
left.children.push(right.children.shift());
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
_getMax(node) {
|
|
365
|
+
while (!node.leaf) node = node.children[node.children.length - 1];
|
|
366
|
+
const i = node.keys.length - 1;
|
|
367
|
+
return { key: node.keys[i], value: node.values[i] };
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
_mergeChildren(parent, index) {
|
|
371
|
+
const left = parent.children[index];
|
|
372
|
+
const right = parent.children[index + 1];
|
|
373
|
+
const midKey = parent.keys[index];
|
|
374
|
+
const midVal = parent.values[index];
|
|
375
|
+
|
|
376
|
+
// 叶子节点:分隔键是叶子的副本(已在 left 或 right 中),不能重复压入
|
|
377
|
+
if (!left.leaf) {
|
|
378
|
+
left.keys.push(midKey);
|
|
379
|
+
left.values.push(midVal);
|
|
380
|
+
}
|
|
381
|
+
left.keys.push(...right.keys);
|
|
382
|
+
left.values.push(...right.values);
|
|
383
|
+
if (!left.leaf) left.children.push(...right.children);
|
|
384
|
+
if (left.leaf) left.next = right.next;
|
|
385
|
+
|
|
386
|
+
parent.keys.splice(index, 1);
|
|
387
|
+
parent.values.splice(index, 1);
|
|
388
|
+
parent.children.splice(index + 1, 1);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// ============================================================
|
|
392
|
+
// 遍历
|
|
393
|
+
// ============================================================
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* 获取所有索引条目(仅叶子数据,分隔键为内部副本,不重复计入)
|
|
397
|
+
*/
|
|
398
|
+
entries() {
|
|
399
|
+
const result = [];
|
|
400
|
+
let node = this._root;
|
|
401
|
+
while (node && !node.leaf) node = node.children[0];
|
|
402
|
+
while (node) {
|
|
403
|
+
for (let i = 0; i < node.keys.length; i++) {
|
|
404
|
+
result.push({ key: node.keys[i], value: node.values[i] });
|
|
405
|
+
}
|
|
406
|
+
node = node.next;
|
|
407
|
+
}
|
|
408
|
+
return result;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* 清空索引
|
|
413
|
+
*/
|
|
414
|
+
clear() {
|
|
415
|
+
this._root = new BTreeNode();
|
|
416
|
+
this._size = 0;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* 重建索引(从数据行批量构建)
|
|
421
|
+
*/
|
|
422
|
+
rebuild(rows, getKey) {
|
|
423
|
+
this.clear();
|
|
424
|
+
for (let i = 0; i < rows.length; i++) {
|
|
425
|
+
const key = getKey(rows[i]);
|
|
426
|
+
if (key !== undefined && key !== null) {
|
|
427
|
+
this.insert(key, i);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* 批量加载:从已排序的 [key, rowIndex] 数组底部向上构建
|
|
434
|
+
* O(n) 时间,适合大数据量批量构建
|
|
435
|
+
*/
|
|
436
|
+
bulkLoad(sortedPairs) {
|
|
437
|
+
if (sortedPairs.length === 0) { this.clear(); return; }
|
|
438
|
+
const order = this._order;
|
|
439
|
+
// 第一层:构建叶子节点
|
|
440
|
+
const leaves = [];
|
|
441
|
+
let i = 0;
|
|
442
|
+
while (i < sortedPairs.length) {
|
|
443
|
+
const node = new BTreeNode(true);
|
|
444
|
+
const end = Math.min(i + order - 1, sortedPairs.length);
|
|
445
|
+
for (; i < end; i++) {
|
|
446
|
+
const [key, val] = sortedPairs[i];
|
|
447
|
+
node.keys.push(key);
|
|
448
|
+
node.values.push(Array.isArray(val) ? val : [val]);
|
|
449
|
+
}
|
|
450
|
+
leaves.push(node);
|
|
451
|
+
}
|
|
452
|
+
for (let i = 0; i < leaves.length - 1; i++) leaves[i].next = leaves[i + 1];
|
|
453
|
+
if (leaves.length === 1) { this._root = leaves[0]; this._size = sortedPairs.length; return; }
|
|
454
|
+
// 逐层向上构建内部节点
|
|
455
|
+
let currentLevel = leaves;
|
|
456
|
+
while (currentLevel.length > 1) {
|
|
457
|
+
const parents = [];
|
|
458
|
+
let j = 0;
|
|
459
|
+
while (j < currentLevel.length) {
|
|
460
|
+
const node = new BTreeNode(false);
|
|
461
|
+
const end = Math.min(j + order, currentLevel.length);
|
|
462
|
+
for (; j < end; j++) {
|
|
463
|
+
if (node.children.length > 0) {
|
|
464
|
+
const child = currentLevel[j];
|
|
465
|
+
node.keys.push(child.keys[0]);
|
|
466
|
+
node.values.push(child.values[0]);
|
|
467
|
+
}
|
|
468
|
+
node.children.push(currentLevel[j]);
|
|
469
|
+
}
|
|
470
|
+
parents.push(node);
|
|
471
|
+
}
|
|
472
|
+
currentLevel = parents;
|
|
473
|
+
}
|
|
474
|
+
this._root = currentLevel[0];
|
|
475
|
+
this._size = sortedPairs.length;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
414
479
|
module.exports = BTree;
|