serializable-bptree 9.0.1 → 9.0.3

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.
@@ -3236,6 +3236,9 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
3236
3236
  this._verifierMapCached = createVerifierMap(this.comparator, this._cachedRegexp, ensureValues);
3237
3237
  this._searchConfigsCached = createSearchConfigs(this.comparator, ensureValues);
3238
3238
  }
3239
+ getRootNode() {
3240
+ return this.getNode(this.rootId);
3241
+ }
3239
3242
  // ─── Legacy protected methods (delegating to ops) ────────────────
3240
3243
  getNode(id) {
3241
3244
  return this._ops.getNode(id);
@@ -4674,6 +4677,9 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
4674
4677
  this.lock.writeUnlock(lockId);
4675
4678
  });
4676
4679
  }
4680
+ async getRootNode() {
4681
+ return this.getNode(this.rootId);
4682
+ }
4677
4683
  // ─── Legacy protected methods (delegating to ops) ────────────────
4678
4684
  async getNode(id) {
4679
4685
  return this._ops.getNode(id);
@@ -4990,8 +4996,6 @@ var BPTreePureSync = class {
4990
4996
  comparator;
4991
4997
  option;
4992
4998
  _cachedRegexp = /* @__PURE__ */ new Map();
4993
- _ctx;
4994
- _ops;
4995
4999
  _verifierMap;
4996
5000
  _searchConfigs;
4997
5001
  constructor(strategy, comparator, option) {
@@ -5005,7 +5009,7 @@ var BPTreePureSync = class {
5005
5009
  _ensureValues(v) {
5006
5010
  return Array.isArray(v) ? v : [v];
5007
5011
  }
5008
- _createOps() {
5012
+ _createReadOps() {
5009
5013
  const strategy = this.strategy;
5010
5014
  return {
5011
5015
  getNode(id) {
@@ -5072,16 +5076,35 @@ var BPTreePureSync = class {
5072
5076
  }
5073
5077
  return { ops, flush };
5074
5078
  }
5079
+ _readCtx() {
5080
+ const head = this.strategy.readHead();
5081
+ if (head === null) {
5082
+ throw new Error("Tree not initialized. Call init() first.");
5083
+ }
5084
+ return { rootId: head.root, order: head.order };
5085
+ }
5086
+ _createCtx() {
5087
+ const strategy = this.strategy;
5088
+ const head = strategy.readHead();
5089
+ if (head === null) {
5090
+ throw new Error("Tree not initialized. Call init() first.");
5091
+ }
5092
+ return {
5093
+ rootId: head.root,
5094
+ order: head.order,
5095
+ headData: () => strategy.head.data
5096
+ };
5097
+ }
5075
5098
  init() {
5076
5099
  const { ops, flush } = this._createBufferedOps();
5077
- this._ctx = {
5100
+ const ctx = {
5078
5101
  rootId: "",
5079
5102
  order: this.strategy.order,
5080
5103
  headData: () => this.strategy.head.data
5081
5104
  };
5082
5105
  initOp(
5083
5106
  ops,
5084
- this._ctx,
5107
+ ctx,
5085
5108
  this.strategy.order,
5086
5109
  this.strategy.head,
5087
5110
  (head) => {
@@ -5089,23 +5112,19 @@ var BPTreePureSync = class {
5089
5112
  }
5090
5113
  );
5091
5114
  flush();
5092
- this._ops = this._createOps();
5093
5115
  }
5094
- /**
5095
- * Returns the ID of the root node.
5096
- */
5116
+ getRootNode() {
5117
+ const ctx = this._readCtx();
5118
+ return this.strategy.read(ctx.rootId);
5119
+ }
5097
5120
  getRootId() {
5098
- return this._ctx.rootId;
5121
+ const ctx = this._readCtx();
5122
+ return ctx.rootId;
5099
5123
  }
5100
- /**
5101
- * Returns the order of the B+Tree.
5102
- */
5103
5124
  getOrder() {
5104
- return this._ctx.order;
5125
+ const ctx = this._readCtx();
5126
+ return ctx.order;
5105
5127
  }
5106
- /**
5107
- * Verified if the value satisfies the condition.
5108
- */
5109
5128
  verify(nodeValue, condition) {
5110
5129
  for (const key in condition) {
5111
5130
  const verifyFn = this._verifierMap[key];
@@ -5118,15 +5137,18 @@ var BPTreePureSync = class {
5118
5137
  }
5119
5138
  // ─── Query ───────────────────────────────────────────────────────
5120
5139
  get(key) {
5121
- return getOp(this._ops, this._ctx.rootId, key);
5140
+ const ctx = this._readCtx();
5141
+ return getOp(this._createReadOps(), ctx.rootId, key);
5122
5142
  }
5123
5143
  exists(key, value) {
5124
- return existsOp(this._ops, this._ctx.rootId, key, value, this.comparator);
5144
+ const ctx = this._readCtx();
5145
+ return existsOp(this._createReadOps(), ctx.rootId, key, value, this.comparator);
5125
5146
  }
5126
5147
  *keysStream(condition, options) {
5148
+ const ctx = this._readCtx();
5127
5149
  yield* keysStreamOp(
5128
- this._ops,
5129
- this._ctx.rootId,
5150
+ this._createReadOps(),
5151
+ ctx.rootId,
5130
5152
  condition,
5131
5153
  this.comparator,
5132
5154
  this._verifierMap,
@@ -5136,9 +5158,10 @@ var BPTreePureSync = class {
5136
5158
  );
5137
5159
  }
5138
5160
  *whereStream(condition, options) {
5161
+ const ctx = this._readCtx();
5139
5162
  yield* whereStreamOp(
5140
- this._ops,
5141
- this._ctx.rootId,
5163
+ this._createReadOps(),
5164
+ ctx.rootId,
5142
5165
  condition,
5143
5166
  this.comparator,
5144
5167
  this._verifierMap,
@@ -5164,27 +5187,31 @@ var BPTreePureSync = class {
5164
5187
  // ─── Mutation ────────────────────────────────────────────────────
5165
5188
  insert(key, value) {
5166
5189
  const { ops, flush } = this._createBufferedOps();
5167
- insertOp(ops, this._ctx, key, value, this.comparator);
5190
+ const ctx = this._createCtx();
5191
+ insertOp(ops, ctx, key, value, this.comparator);
5168
5192
  flush();
5169
5193
  }
5170
5194
  delete(key, value) {
5171
5195
  const { ops, flush } = this._createBufferedOps();
5172
- deleteOp(ops, this._ctx, key, this.comparator, value);
5196
+ const ctx = this._createCtx();
5197
+ deleteOp(ops, ctx, key, this.comparator, value);
5173
5198
  flush();
5174
5199
  }
5175
5200
  batchInsert(entries) {
5176
5201
  const { ops, flush } = this._createBufferedOps();
5177
- batchInsertOp(ops, this._ctx, entries, this.comparator);
5202
+ const ctx = this._createCtx();
5203
+ batchInsertOp(ops, ctx, entries, this.comparator);
5178
5204
  flush();
5179
5205
  }
5180
5206
  bulkLoad(entries) {
5181
5207
  const { ops, flush } = this._createBufferedOps();
5182
- bulkLoadOp(ops, this._ctx, entries, this.comparator);
5208
+ const ctx = this._createCtx();
5209
+ bulkLoadOp(ops, ctx, entries, this.comparator);
5183
5210
  flush();
5184
5211
  }
5185
5212
  // ─── Head Data ───────────────────────────────────────────────────
5186
5213
  getHeadData() {
5187
- const head = this._ops.readHead();
5214
+ const head = this.strategy.readHead();
5188
5215
  if (head === null) {
5189
5216
  throw new Error("Head not found");
5190
5217
  }
@@ -5214,8 +5241,6 @@ var BPTreePureAsync = class {
5214
5241
  option;
5215
5242
  lock = new Ryoiki2();
5216
5243
  _cachedRegexp = /* @__PURE__ */ new Map();
5217
- _ctx;
5218
- _ops;
5219
5244
  _verifierMap;
5220
5245
  _searchConfigs;
5221
5246
  constructor(strategy, comparator, option) {
@@ -5229,7 +5254,7 @@ var BPTreePureAsync = class {
5229
5254
  _ensureValues(v) {
5230
5255
  return Array.isArray(v) ? v : [v];
5231
5256
  }
5232
- _createOps() {
5257
+ _createReadOps() {
5233
5258
  const strategy = this.strategy;
5234
5259
  return {
5235
5260
  async getNode(id) {
@@ -5296,6 +5321,25 @@ var BPTreePureAsync = class {
5296
5321
  }
5297
5322
  return { ops, flush };
5298
5323
  }
5324
+ async _readCtx() {
5325
+ const head = await this.strategy.readHead();
5326
+ if (head === null) {
5327
+ throw new Error("Tree not initialized. Call init() first.");
5328
+ }
5329
+ return { rootId: head.root, order: head.order };
5330
+ }
5331
+ async _createCtx() {
5332
+ const strategy = this.strategy;
5333
+ const head = await strategy.readHead();
5334
+ if (head === null) {
5335
+ throw new Error("Tree not initialized. Call init() first.");
5336
+ }
5337
+ return {
5338
+ rootId: head.root,
5339
+ order: head.order,
5340
+ headData: () => strategy.head.data
5341
+ };
5342
+ }
5299
5343
  async writeLock(fn) {
5300
5344
  let lockId;
5301
5345
  return this.lock.writeLock(async (_lockId) => {
@@ -5308,14 +5352,14 @@ var BPTreePureAsync = class {
5308
5352
  async init() {
5309
5353
  return this.writeLock(async () => {
5310
5354
  const { ops, flush } = this._createBufferedOps();
5311
- this._ctx = {
5355
+ const ctx = {
5312
5356
  rootId: "",
5313
5357
  order: this.strategy.order,
5314
5358
  headData: () => this.strategy.head.data
5315
5359
  };
5316
5360
  await initOpAsync(
5317
5361
  ops,
5318
- this._ctx,
5362
+ ctx,
5319
5363
  this.strategy.order,
5320
5364
  this.strategy.head,
5321
5365
  (head) => {
@@ -5323,14 +5367,19 @@ var BPTreePureAsync = class {
5323
5367
  }
5324
5368
  );
5325
5369
  await flush();
5326
- this._ops = this._createOps();
5327
5370
  });
5328
5371
  }
5329
- getRootId() {
5330
- return this._ctx.rootId;
5372
+ async getRootNode() {
5373
+ const ctx = await this._readCtx();
5374
+ return await this.strategy.read(ctx.rootId);
5331
5375
  }
5332
- getOrder() {
5333
- return this._ctx.order;
5376
+ async getRootId() {
5377
+ const ctx = await this._readCtx();
5378
+ return ctx.rootId;
5379
+ }
5380
+ async getOrder() {
5381
+ const ctx = await this._readCtx();
5382
+ return ctx.order;
5334
5383
  }
5335
5384
  verify(nodeValue, condition) {
5336
5385
  for (const key in condition) {
@@ -5342,18 +5391,21 @@ var BPTreePureAsync = class {
5342
5391
  }
5343
5392
  // ─── Query ───────────────────────────────────────────────────────
5344
5393
  async get(key) {
5345
- return getOpAsync(this._ops, this._ctx.rootId, key);
5394
+ const ctx = await this._readCtx();
5395
+ return getOpAsync(this._createReadOps(), ctx.rootId, key);
5346
5396
  }
5347
5397
  async exists(key, value) {
5348
- return existsOpAsync(this._ops, this._ctx.rootId, key, value, this.comparator);
5398
+ const ctx = await this._readCtx();
5399
+ return existsOpAsync(this._createReadOps(), ctx.rootId, key, value, this.comparator);
5349
5400
  }
5350
5401
  async *keysStream(condition, options) {
5351
5402
  let lockId;
5352
5403
  try {
5353
5404
  lockId = await this.lock.readLock([0, 0.1], async (id) => id);
5405
+ const ctx = await this._readCtx();
5354
5406
  yield* keysStreamOpAsync(
5355
- this._ops,
5356
- this._ctx.rootId,
5407
+ this._createReadOps(),
5408
+ ctx.rootId,
5357
5409
  condition,
5358
5410
  this.comparator,
5359
5411
  this._verifierMap,
@@ -5369,9 +5421,10 @@ var BPTreePureAsync = class {
5369
5421
  let lockId;
5370
5422
  try {
5371
5423
  lockId = await this.lock.readLock([0, 0.1], async (id) => id);
5424
+ const ctx = await this._readCtx();
5372
5425
  yield* whereStreamOpAsync(
5373
- this._ops,
5374
- this._ctx.rootId,
5426
+ this._createReadOps(),
5427
+ ctx.rootId,
5375
5428
  condition,
5376
5429
  this.comparator,
5377
5430
  this._verifierMap,
@@ -5401,34 +5454,38 @@ var BPTreePureAsync = class {
5401
5454
  async insert(key, value) {
5402
5455
  return this.writeLock(async () => {
5403
5456
  const { ops, flush } = this._createBufferedOps();
5404
- await insertOpAsync(ops, this._ctx, key, value, this.comparator);
5457
+ const ctx = await this._createCtx();
5458
+ await insertOpAsync(ops, ctx, key, value, this.comparator);
5405
5459
  await flush();
5406
5460
  });
5407
5461
  }
5408
5462
  async delete(key, value) {
5409
5463
  return this.writeLock(async () => {
5410
5464
  const { ops, flush } = this._createBufferedOps();
5411
- await deleteOpAsync(ops, this._ctx, key, this.comparator, value);
5465
+ const ctx = await this._createCtx();
5466
+ await deleteOpAsync(ops, ctx, key, this.comparator, value);
5412
5467
  await flush();
5413
5468
  });
5414
5469
  }
5415
5470
  async batchInsert(entries) {
5416
5471
  return this.writeLock(async () => {
5417
5472
  const { ops, flush } = this._createBufferedOps();
5418
- await batchInsertOpAsync(ops, this._ctx, entries, this.comparator);
5473
+ const ctx = await this._createCtx();
5474
+ await batchInsertOpAsync(ops, ctx, entries, this.comparator);
5419
5475
  await flush();
5420
5476
  });
5421
5477
  }
5422
5478
  async bulkLoad(entries) {
5423
5479
  return this.writeLock(async () => {
5424
5480
  const { ops, flush } = this._createBufferedOps();
5425
- await bulkLoadOpAsync(ops, this._ctx, entries, this.comparator);
5481
+ const ctx = await this._createCtx();
5482
+ await bulkLoadOpAsync(ops, ctx, entries, this.comparator);
5426
5483
  await flush();
5427
5484
  });
5428
5485
  }
5429
5486
  // ─── Head Data ───────────────────────────────────────────────────
5430
5487
  async getHeadData() {
5431
- const head = await this._ops.readHead();
5488
+ const head = await this.strategy.readHead();
5432
5489
  if (head === null) throw new Error("Head not found");
5433
5490
  return head.data;
5434
5491
  }
@@ -3198,6 +3198,9 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
3198
3198
  this._verifierMapCached = createVerifierMap(this.comparator, this._cachedRegexp, ensureValues);
3199
3199
  this._searchConfigsCached = createSearchConfigs(this.comparator, ensureValues);
3200
3200
  }
3201
+ getRootNode() {
3202
+ return this.getNode(this.rootId);
3203
+ }
3201
3204
  // ─── Legacy protected methods (delegating to ops) ────────────────
3202
3205
  getNode(id) {
3203
3206
  return this._ops.getNode(id);
@@ -4636,6 +4639,9 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
4636
4639
  this.lock.writeUnlock(lockId);
4637
4640
  });
4638
4641
  }
4642
+ async getRootNode() {
4643
+ return this.getNode(this.rootId);
4644
+ }
4639
4645
  // ─── Legacy protected methods (delegating to ops) ────────────────
4640
4646
  async getNode(id) {
4641
4647
  return this._ops.getNode(id);
@@ -4952,8 +4958,6 @@ var BPTreePureSync = class {
4952
4958
  comparator;
4953
4959
  option;
4954
4960
  _cachedRegexp = /* @__PURE__ */ new Map();
4955
- _ctx;
4956
- _ops;
4957
4961
  _verifierMap;
4958
4962
  _searchConfigs;
4959
4963
  constructor(strategy, comparator, option) {
@@ -4967,7 +4971,7 @@ var BPTreePureSync = class {
4967
4971
  _ensureValues(v) {
4968
4972
  return Array.isArray(v) ? v : [v];
4969
4973
  }
4970
- _createOps() {
4974
+ _createReadOps() {
4971
4975
  const strategy = this.strategy;
4972
4976
  return {
4973
4977
  getNode(id) {
@@ -5034,16 +5038,35 @@ var BPTreePureSync = class {
5034
5038
  }
5035
5039
  return { ops, flush };
5036
5040
  }
5041
+ _readCtx() {
5042
+ const head = this.strategy.readHead();
5043
+ if (head === null) {
5044
+ throw new Error("Tree not initialized. Call init() first.");
5045
+ }
5046
+ return { rootId: head.root, order: head.order };
5047
+ }
5048
+ _createCtx() {
5049
+ const strategy = this.strategy;
5050
+ const head = strategy.readHead();
5051
+ if (head === null) {
5052
+ throw new Error("Tree not initialized. Call init() first.");
5053
+ }
5054
+ return {
5055
+ rootId: head.root,
5056
+ order: head.order,
5057
+ headData: () => strategy.head.data
5058
+ };
5059
+ }
5037
5060
  init() {
5038
5061
  const { ops, flush } = this._createBufferedOps();
5039
- this._ctx = {
5062
+ const ctx = {
5040
5063
  rootId: "",
5041
5064
  order: this.strategy.order,
5042
5065
  headData: () => this.strategy.head.data
5043
5066
  };
5044
5067
  initOp(
5045
5068
  ops,
5046
- this._ctx,
5069
+ ctx,
5047
5070
  this.strategy.order,
5048
5071
  this.strategy.head,
5049
5072
  (head) => {
@@ -5051,23 +5074,19 @@ var BPTreePureSync = class {
5051
5074
  }
5052
5075
  );
5053
5076
  flush();
5054
- this._ops = this._createOps();
5055
5077
  }
5056
- /**
5057
- * Returns the ID of the root node.
5058
- */
5078
+ getRootNode() {
5079
+ const ctx = this._readCtx();
5080
+ return this.strategy.read(ctx.rootId);
5081
+ }
5059
5082
  getRootId() {
5060
- return this._ctx.rootId;
5083
+ const ctx = this._readCtx();
5084
+ return ctx.rootId;
5061
5085
  }
5062
- /**
5063
- * Returns the order of the B+Tree.
5064
- */
5065
5086
  getOrder() {
5066
- return this._ctx.order;
5087
+ const ctx = this._readCtx();
5088
+ return ctx.order;
5067
5089
  }
5068
- /**
5069
- * Verified if the value satisfies the condition.
5070
- */
5071
5090
  verify(nodeValue, condition) {
5072
5091
  for (const key in condition) {
5073
5092
  const verifyFn = this._verifierMap[key];
@@ -5080,15 +5099,18 @@ var BPTreePureSync = class {
5080
5099
  }
5081
5100
  // ─── Query ───────────────────────────────────────────────────────
5082
5101
  get(key) {
5083
- return getOp(this._ops, this._ctx.rootId, key);
5102
+ const ctx = this._readCtx();
5103
+ return getOp(this._createReadOps(), ctx.rootId, key);
5084
5104
  }
5085
5105
  exists(key, value) {
5086
- return existsOp(this._ops, this._ctx.rootId, key, value, this.comparator);
5106
+ const ctx = this._readCtx();
5107
+ return existsOp(this._createReadOps(), ctx.rootId, key, value, this.comparator);
5087
5108
  }
5088
5109
  *keysStream(condition, options) {
5110
+ const ctx = this._readCtx();
5089
5111
  yield* keysStreamOp(
5090
- this._ops,
5091
- this._ctx.rootId,
5112
+ this._createReadOps(),
5113
+ ctx.rootId,
5092
5114
  condition,
5093
5115
  this.comparator,
5094
5116
  this._verifierMap,
@@ -5098,9 +5120,10 @@ var BPTreePureSync = class {
5098
5120
  );
5099
5121
  }
5100
5122
  *whereStream(condition, options) {
5123
+ const ctx = this._readCtx();
5101
5124
  yield* whereStreamOp(
5102
- this._ops,
5103
- this._ctx.rootId,
5125
+ this._createReadOps(),
5126
+ ctx.rootId,
5104
5127
  condition,
5105
5128
  this.comparator,
5106
5129
  this._verifierMap,
@@ -5126,27 +5149,31 @@ var BPTreePureSync = class {
5126
5149
  // ─── Mutation ────────────────────────────────────────────────────
5127
5150
  insert(key, value) {
5128
5151
  const { ops, flush } = this._createBufferedOps();
5129
- insertOp(ops, this._ctx, key, value, this.comparator);
5152
+ const ctx = this._createCtx();
5153
+ insertOp(ops, ctx, key, value, this.comparator);
5130
5154
  flush();
5131
5155
  }
5132
5156
  delete(key, value) {
5133
5157
  const { ops, flush } = this._createBufferedOps();
5134
- deleteOp(ops, this._ctx, key, this.comparator, value);
5158
+ const ctx = this._createCtx();
5159
+ deleteOp(ops, ctx, key, this.comparator, value);
5135
5160
  flush();
5136
5161
  }
5137
5162
  batchInsert(entries) {
5138
5163
  const { ops, flush } = this._createBufferedOps();
5139
- batchInsertOp(ops, this._ctx, entries, this.comparator);
5164
+ const ctx = this._createCtx();
5165
+ batchInsertOp(ops, ctx, entries, this.comparator);
5140
5166
  flush();
5141
5167
  }
5142
5168
  bulkLoad(entries) {
5143
5169
  const { ops, flush } = this._createBufferedOps();
5144
- bulkLoadOp(ops, this._ctx, entries, this.comparator);
5170
+ const ctx = this._createCtx();
5171
+ bulkLoadOp(ops, ctx, entries, this.comparator);
5145
5172
  flush();
5146
5173
  }
5147
5174
  // ─── Head Data ───────────────────────────────────────────────────
5148
5175
  getHeadData() {
5149
- const head = this._ops.readHead();
5176
+ const head = this.strategy.readHead();
5150
5177
  if (head === null) {
5151
5178
  throw new Error("Head not found");
5152
5179
  }
@@ -5176,8 +5203,6 @@ var BPTreePureAsync = class {
5176
5203
  option;
5177
5204
  lock = new Ryoiki2();
5178
5205
  _cachedRegexp = /* @__PURE__ */ new Map();
5179
- _ctx;
5180
- _ops;
5181
5206
  _verifierMap;
5182
5207
  _searchConfigs;
5183
5208
  constructor(strategy, comparator, option) {
@@ -5191,7 +5216,7 @@ var BPTreePureAsync = class {
5191
5216
  _ensureValues(v) {
5192
5217
  return Array.isArray(v) ? v : [v];
5193
5218
  }
5194
- _createOps() {
5219
+ _createReadOps() {
5195
5220
  const strategy = this.strategy;
5196
5221
  return {
5197
5222
  async getNode(id) {
@@ -5258,6 +5283,25 @@ var BPTreePureAsync = class {
5258
5283
  }
5259
5284
  return { ops, flush };
5260
5285
  }
5286
+ async _readCtx() {
5287
+ const head = await this.strategy.readHead();
5288
+ if (head === null) {
5289
+ throw new Error("Tree not initialized. Call init() first.");
5290
+ }
5291
+ return { rootId: head.root, order: head.order };
5292
+ }
5293
+ async _createCtx() {
5294
+ const strategy = this.strategy;
5295
+ const head = await strategy.readHead();
5296
+ if (head === null) {
5297
+ throw new Error("Tree not initialized. Call init() first.");
5298
+ }
5299
+ return {
5300
+ rootId: head.root,
5301
+ order: head.order,
5302
+ headData: () => strategy.head.data
5303
+ };
5304
+ }
5261
5305
  async writeLock(fn) {
5262
5306
  let lockId;
5263
5307
  return this.lock.writeLock(async (_lockId) => {
@@ -5270,14 +5314,14 @@ var BPTreePureAsync = class {
5270
5314
  async init() {
5271
5315
  return this.writeLock(async () => {
5272
5316
  const { ops, flush } = this._createBufferedOps();
5273
- this._ctx = {
5317
+ const ctx = {
5274
5318
  rootId: "",
5275
5319
  order: this.strategy.order,
5276
5320
  headData: () => this.strategy.head.data
5277
5321
  };
5278
5322
  await initOpAsync(
5279
5323
  ops,
5280
- this._ctx,
5324
+ ctx,
5281
5325
  this.strategy.order,
5282
5326
  this.strategy.head,
5283
5327
  (head) => {
@@ -5285,14 +5329,19 @@ var BPTreePureAsync = class {
5285
5329
  }
5286
5330
  );
5287
5331
  await flush();
5288
- this._ops = this._createOps();
5289
5332
  });
5290
5333
  }
5291
- getRootId() {
5292
- return this._ctx.rootId;
5334
+ async getRootNode() {
5335
+ const ctx = await this._readCtx();
5336
+ return await this.strategy.read(ctx.rootId);
5293
5337
  }
5294
- getOrder() {
5295
- return this._ctx.order;
5338
+ async getRootId() {
5339
+ const ctx = await this._readCtx();
5340
+ return ctx.rootId;
5341
+ }
5342
+ async getOrder() {
5343
+ const ctx = await this._readCtx();
5344
+ return ctx.order;
5296
5345
  }
5297
5346
  verify(nodeValue, condition) {
5298
5347
  for (const key in condition) {
@@ -5304,18 +5353,21 @@ var BPTreePureAsync = class {
5304
5353
  }
5305
5354
  // ─── Query ───────────────────────────────────────────────────────
5306
5355
  async get(key) {
5307
- return getOpAsync(this._ops, this._ctx.rootId, key);
5356
+ const ctx = await this._readCtx();
5357
+ return getOpAsync(this._createReadOps(), ctx.rootId, key);
5308
5358
  }
5309
5359
  async exists(key, value) {
5310
- return existsOpAsync(this._ops, this._ctx.rootId, key, value, this.comparator);
5360
+ const ctx = await this._readCtx();
5361
+ return existsOpAsync(this._createReadOps(), ctx.rootId, key, value, this.comparator);
5311
5362
  }
5312
5363
  async *keysStream(condition, options) {
5313
5364
  let lockId;
5314
5365
  try {
5315
5366
  lockId = await this.lock.readLock([0, 0.1], async (id) => id);
5367
+ const ctx = await this._readCtx();
5316
5368
  yield* keysStreamOpAsync(
5317
- this._ops,
5318
- this._ctx.rootId,
5369
+ this._createReadOps(),
5370
+ ctx.rootId,
5319
5371
  condition,
5320
5372
  this.comparator,
5321
5373
  this._verifierMap,
@@ -5331,9 +5383,10 @@ var BPTreePureAsync = class {
5331
5383
  let lockId;
5332
5384
  try {
5333
5385
  lockId = await this.lock.readLock([0, 0.1], async (id) => id);
5386
+ const ctx = await this._readCtx();
5334
5387
  yield* whereStreamOpAsync(
5335
- this._ops,
5336
- this._ctx.rootId,
5388
+ this._createReadOps(),
5389
+ ctx.rootId,
5337
5390
  condition,
5338
5391
  this.comparator,
5339
5392
  this._verifierMap,
@@ -5363,34 +5416,38 @@ var BPTreePureAsync = class {
5363
5416
  async insert(key, value) {
5364
5417
  return this.writeLock(async () => {
5365
5418
  const { ops, flush } = this._createBufferedOps();
5366
- await insertOpAsync(ops, this._ctx, key, value, this.comparator);
5419
+ const ctx = await this._createCtx();
5420
+ await insertOpAsync(ops, ctx, key, value, this.comparator);
5367
5421
  await flush();
5368
5422
  });
5369
5423
  }
5370
5424
  async delete(key, value) {
5371
5425
  return this.writeLock(async () => {
5372
5426
  const { ops, flush } = this._createBufferedOps();
5373
- await deleteOpAsync(ops, this._ctx, key, this.comparator, value);
5427
+ const ctx = await this._createCtx();
5428
+ await deleteOpAsync(ops, ctx, key, this.comparator, value);
5374
5429
  await flush();
5375
5430
  });
5376
5431
  }
5377
5432
  async batchInsert(entries) {
5378
5433
  return this.writeLock(async () => {
5379
5434
  const { ops, flush } = this._createBufferedOps();
5380
- await batchInsertOpAsync(ops, this._ctx, entries, this.comparator);
5435
+ const ctx = await this._createCtx();
5436
+ await batchInsertOpAsync(ops, ctx, entries, this.comparator);
5381
5437
  await flush();
5382
5438
  });
5383
5439
  }
5384
5440
  async bulkLoad(entries) {
5385
5441
  return this.writeLock(async () => {
5386
5442
  const { ops, flush } = this._createBufferedOps();
5387
- await bulkLoadOpAsync(ops, this._ctx, entries, this.comparator);
5443
+ const ctx = await this._createCtx();
5444
+ await bulkLoadOpAsync(ops, ctx, entries, this.comparator);
5388
5445
  await flush();
5389
5446
  });
5390
5447
  }
5391
5448
  // ─── Head Data ───────────────────────────────────────────────────
5392
5449
  async getHeadData() {
5393
- const head = await this._ops.readHead();
5450
+ const head = await this.strategy.readHead();
5394
5451
  if (head === null) throw new Error("Head not found");
5395
5452
  return head.data;
5396
5453
  }
@@ -1,26 +1,27 @@
1
- import type { BPTreeCondition, BPTreeConstructorOption, BPTreePair, BPTreeSearchOption, SerializableData } from './types';
1
+ import type { BPTreeCondition, BPTreeConstructorOption, BPTreePair, BPTreeSearchOption, BPTreeUnknownNode, SerializableData } from './types';
2
+ import { Ryoiki } from 'ryoiki';
2
3
  import { SerializeStrategyAsync } from './SerializeStrategyAsync';
3
4
  import { ValueComparator } from './base/ValueComparator';
4
5
  import { BPTreeTransaction } from './base/BPTreeTransaction';
5
- import { Ryoiki } from 'ryoiki';
6
6
  export declare class BPTreePureAsync<K, V> {
7
7
  protected readonly strategy: SerializeStrategyAsync<K, V>;
8
8
  protected readonly comparator: ValueComparator<V>;
9
9
  protected readonly option: BPTreeConstructorOption;
10
10
  protected readonly lock: Ryoiki;
11
11
  private readonly _cachedRegexp;
12
- private _ctx;
13
- private _ops;
14
12
  private readonly _verifierMap;
15
13
  private readonly _searchConfigs;
16
14
  constructor(strategy: SerializeStrategyAsync<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
17
15
  private _ensureValues;
18
- private _createOps;
16
+ private _createReadOps;
19
17
  private _createBufferedOps;
18
+ private _readCtx;
19
+ private _createCtx;
20
20
  protected writeLock<T>(fn: () => Promise<T>): Promise<T>;
21
21
  init(): Promise<void>;
22
- getRootId(): string;
23
- getOrder(): number;
22
+ getRootNode(): Promise<BPTreeUnknownNode<K, V>>;
23
+ getRootId(): Promise<string>;
24
+ getOrder(): Promise<number>;
24
25
  verify(nodeValue: V, condition: BPTreeCondition<V>): boolean;
25
26
  get(key: K): Promise<V | undefined>;
26
27
  exists(key: K, value: V): Promise<boolean>;
@@ -1,4 +1,4 @@
1
- import type { BPTreeCondition, BPTreeConstructorOption, BPTreePair, BPTreeSearchOption, SerializableData } from './types';
1
+ import type { BPTreeCondition, BPTreeConstructorOption, BPTreePair, BPTreeSearchOption, BPTreeUnknownNode, SerializableData } from './types';
2
2
  import { SerializeStrategySync } from './SerializeStrategySync';
3
3
  import { ValueComparator } from './base/ValueComparator';
4
4
  import { BPTreeTransaction } from './base/BPTreeTransaction';
@@ -7,26 +7,18 @@ export declare class BPTreePureSync<K, V> {
7
7
  protected readonly comparator: ValueComparator<V>;
8
8
  protected readonly option: BPTreeConstructorOption;
9
9
  private readonly _cachedRegexp;
10
- private _ctx;
11
- private _ops;
12
10
  private readonly _verifierMap;
13
11
  private readonly _searchConfigs;
14
12
  constructor(strategy: SerializeStrategySync<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
15
13
  private _ensureValues;
16
- private _createOps;
14
+ private _createReadOps;
17
15
  private _createBufferedOps;
16
+ private _readCtx;
17
+ private _createCtx;
18
18
  init(): void;
19
- /**
20
- * Returns the ID of the root node.
21
- */
19
+ getRootNode(): BPTreeUnknownNode<K, V>;
22
20
  getRootId(): string;
23
- /**
24
- * Returns the order of the B+Tree.
25
- */
26
21
  getOrder(): number;
27
- /**
28
- * Verified if the value satisfies the condition.
29
- */
30
22
  verify(nodeValue: V, condition: BPTreeCondition<V>): boolean;
31
23
  get(key: K): V | undefined;
32
24
  exists(key: K, value: V): boolean;
@@ -57,6 +57,11 @@ export declare abstract class BPTreeTransaction<K, V> {
57
57
  * @returns An array of keys that are in conflict. Empty array if no conflicts.
58
58
  */
59
59
  static CheckConflicts<K, V>(transactions: BPTreeTransaction<K, V>[]): string[];
60
+ /**
61
+ * Returns the root node of the B+Tree.
62
+ * @returns The root node.
63
+ */
64
+ abstract getRootNode(): Deferred<BPTreeUnknownNode<K, V>>;
60
65
  /**
61
66
  * Returns the ID of the root node.
62
67
  * @returns The root node ID.
@@ -20,6 +20,7 @@ export declare class BPTreeAsyncTransaction<K, V> extends BPTreeTransaction<K, V
20
20
  constructor(rootTx: BPTreeAsyncTransaction<K, V> | null, mvccRoot: AsyncBPTreeMVCC<K, V>, mvcc: AsyncBPTreeMVCC<K, V>, strategy: SerializeStrategyAsync<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
21
21
  private _initAlgoContext;
22
22
  protected writeLock<T>(id: number, fn: () => Promise<T>): Promise<T>;
23
+ getRootNode(): Promise<BPTreeUnknownNode<K, V>>;
23
24
  protected getNode(id: string): Promise<BPTreeUnknownNode<K, V>>;
24
25
  protected _createNode(leaf: boolean, keys: string[] | K[][], values: V[], parent?: string | null, next?: string | null, prev?: string | null): Promise<BPTreeUnknownNode<K, V>>;
25
26
  protected _updateNode(node: BPTreeUnknownNode<K, V>): Promise<void>;
@@ -17,6 +17,7 @@ export declare class BPTreeSyncTransaction<K, V> extends BPTreeTransaction<K, V>
17
17
  private _searchConfigsCached;
18
18
  constructor(rootTx: BPTreeSyncTransaction<K, V>, mvccRoot: SyncBPTreeMVCC<K, V>, mvcc: SyncBPTreeMVCC<K, V>, strategy: SerializeStrategySync<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
19
19
  private _initAlgoContext;
20
+ getRootNode(): BPTreeUnknownNode<K, V>;
20
21
  protected getNode(id: string): BPTreeUnknownNode<K, V>;
21
22
  protected _createNode(leaf: boolean, keys: string[] | K[][], values: V[], parent?: string | null, next?: string | null, prev?: string | null): BPTreeUnknownNode<K, V>;
22
23
  protected _updateNode(node: BPTreeUnknownNode<K, V>): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "9.0.1",
3
+ "version": "9.0.3",
4
4
  "description": "Store the B+tree flexibly, not only in-memory.",
5
5
  "types": "./dist/types/index.d.ts",
6
6
  "main": "./dist/cjs/index.cjs",