serializable-bptree 3.3.0 → 3.3.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 CHANGED
@@ -187,12 +187,12 @@ class MyFileIOStrategySync extends SerializeStrategySync {
187
187
 
188
188
  What does this method mean? And why do we need to construct such a method?
189
189
 
190
- #### id(): `number`
190
+ #### id(isLeaf: `boolean`): `number`
191
191
 
192
192
  When a node is created in the B+tree, the node needs a unique value to represent itself. This is the **node.id** attribute, and you can specify this attribute yourself. For example, it could be implemented like this.
193
193
 
194
194
  ```typescript
195
- id(): number {
195
+ id(isLeaf: boolean): number {
196
196
  const current = before + 1
197
197
  before = current
198
198
  return current
@@ -204,7 +204,7 @@ Or, you could use file input/output to save and load the value of the **before**
204
204
  Typically, such an **id** value increases sequentially, and it would be beneficial to store such a value separately within the tree. For that purpose, the **setHeadData** and **getHeadData** methods are available. These methods are responsible for storing arbitrary data in the tree's header or retrieving stored data. Below is an example of usage:
205
205
 
206
206
  ```typescript
207
- id(): number {
207
+ id(isLeaf: boolean): number {
208
208
  const current = this.getHeadData('index', 1) as number
209
209
  this.setHeadData('index', current+1)
210
210
  return current
@@ -214,7 +214,7 @@ id(): number {
214
214
  Additionally, there is a more user-friendly usage of this code.
215
215
 
216
216
  ```typescript
217
- id(): number {
217
+ id(isLeaf: boolean): number {
218
218
  return this.autoIncrement('index', 1)
219
219
  }
220
220
  ```
@@ -366,7 +366,7 @@ import {
366
366
  } from 'serializable-bptree'
367
367
 
368
368
  class FileStoreStrategyAsync extends SerializeStrategyAsync<K, V> {
369
- async id(): Promise<number> {
369
+ async id(isLeaf: boolean): Promise<number> {
370
370
  return await this.autoIncrement('index', 1)
371
371
  }
372
372
 
@@ -602,15 +602,15 @@ var BPTreeSync = class extends BPTree {
602
602
  throw new Error(`Direction must be -1 or 1. but got a ${direction}`);
603
603
  }
604
604
  }
605
- _createNodeId() {
606
- const id = this.strategy.id();
605
+ _createNodeId(isLeaf) {
606
+ const id = this.strategy.id(isLeaf);
607
607
  if (id === 0) {
608
608
  throw new Error(`The node's id should never be 0.`);
609
609
  }
610
610
  return id;
611
611
  }
612
- _createNode(keys2, values, leaf = false, parent = 0, next = 0, prev = 0) {
613
- const id = this._createNodeId();
612
+ _createNode(isLeaf, keys2, values, leaf = false, parent = 0, next = 0, prev = 0) {
613
+ const id = this._createNodeId(isLeaf);
614
614
  const node = {
615
615
  id,
616
616
  keys: keys2,
@@ -823,7 +823,7 @@ var BPTreeSync = class extends BPTree {
823
823
  }
824
824
  _insertInParent(node, value, pointer) {
825
825
  if (this.root === node) {
826
- const root = this._createNode([node.id, pointer.id], [value]);
826
+ const root = this._createNode(false, [node.id, pointer.id], [value]);
827
827
  this.root = root;
828
828
  this.strategy.head.root = root.id;
829
829
  node.parent = root.id;
@@ -841,7 +841,7 @@ var BPTreeSync = class extends BPTree {
841
841
  parentNode.keys.splice(i + 1, 0, pointer.id);
842
842
  this.bufferForNodeUpdate(parentNode);
843
843
  if (parentNode.keys.length > this.order) {
844
- const parentPointer = this._createNode([], []);
844
+ const parentPointer = this._createNode(false, [], []);
845
845
  parentPointer.parent = parentNode.parent;
846
846
  const mid = Math.ceil(this.order / 2) - 1;
847
847
  parentPointer.values = parentNode.values.slice(mid + 1);
@@ -874,7 +874,7 @@ var BPTreeSync = class extends BPTree {
874
874
  const head = this.strategy.readHead();
875
875
  if (head === null) {
876
876
  this.order = this.strategy.order;
877
- this.root = this._createNode([], [], true);
877
+ this.root = this._createNode(true, [], [], true);
878
878
  this.strategy.head.root = this.root.id;
879
879
  this.bufferForNodeCreate(this.root);
880
880
  this.commitHeadBuffer();
@@ -990,6 +990,7 @@ var BPTreeSync = class extends BPTree {
990
990
  this._insertAtLeaf(before, key, value);
991
991
  if (before.values.length === this.order) {
992
992
  const after = this._createNode(
993
+ true,
993
994
  [],
994
995
  [],
995
996
  true,
@@ -1143,15 +1144,15 @@ var BPTreeAsync = class extends BPTree {
1143
1144
  throw new Error(`Direction must be -1 or 1. but got a ${direction}`);
1144
1145
  }
1145
1146
  }
1146
- async _createNodeId() {
1147
- const id = await this.strategy.id();
1147
+ async _createNodeId(isLeaf) {
1148
+ const id = await this.strategy.id(isLeaf);
1148
1149
  if (id === 0) {
1149
1150
  throw new Error(`The node's id should never be 0.`);
1150
1151
  }
1151
1152
  return id;
1152
1153
  }
1153
- async _createNode(keys2, values, leaf = false, parent = 0, next = 0, prev = 0) {
1154
- const id = await this._createNodeId();
1154
+ async _createNode(isLeaf, keys2, values, leaf = false, parent = 0, next = 0, prev = 0) {
1155
+ const id = await this._createNodeId(isLeaf);
1155
1156
  const node = {
1156
1157
  id,
1157
1158
  keys: keys2,
@@ -1364,7 +1365,7 @@ var BPTreeAsync = class extends BPTree {
1364
1365
  }
1365
1366
  async _insertInParent(node, value, pointer) {
1366
1367
  if (this.root === node) {
1367
- const root = await this._createNode([node.id, pointer.id], [value]);
1368
+ const root = await this._createNode(false, [node.id, pointer.id], [value]);
1368
1369
  this.root = root;
1369
1370
  this.strategy.head.root = root.id;
1370
1371
  node.parent = root.id;
@@ -1382,7 +1383,7 @@ var BPTreeAsync = class extends BPTree {
1382
1383
  parentNode.keys.splice(i + 1, 0, pointer.id);
1383
1384
  this.bufferForNodeUpdate(parentNode);
1384
1385
  if (parentNode.keys.length > this.order) {
1385
- const parentPointer = await this._createNode([], []);
1386
+ const parentPointer = await this._createNode(false, [], []);
1386
1387
  parentPointer.parent = parentNode.parent;
1387
1388
  const mid = Math.ceil(this.order / 2) - 1;
1388
1389
  parentPointer.values = parentNode.values.slice(mid + 1);
@@ -1415,7 +1416,7 @@ var BPTreeAsync = class extends BPTree {
1415
1416
  const head = await this.strategy.readHead();
1416
1417
  if (head === null) {
1417
1418
  this.order = this.strategy.order;
1418
- this.root = await this._createNode([], [], true);
1419
+ this.root = await this._createNode(true, [], [], true);
1419
1420
  this.strategy.head.root = this.root.id;
1420
1421
  this.bufferForNodeCreate(this.root);
1421
1422
  this.commitHeadBuffer();
@@ -1531,6 +1532,7 @@ var BPTreeAsync = class extends BPTree {
1531
1532
  this._insertAtLeaf(before, key, value);
1532
1533
  if (before.values.length === this.order) {
1533
1534
  const after = await this._createNode(
1535
+ true,
1534
1536
  [],
1535
1537
  [],
1536
1538
  true,
@@ -1652,7 +1654,7 @@ var InMemoryStoreStrategySync = class extends SerializeStrategySync {
1652
1654
  super(order);
1653
1655
  this.node = {};
1654
1656
  }
1655
- id() {
1657
+ id(isLeaf) {
1656
1658
  return this.autoIncrement("index", 1);
1657
1659
  }
1658
1660
  read(id) {
@@ -1700,7 +1702,7 @@ var InMemoryStoreStrategyAsync = class extends SerializeStrategyAsync {
1700
1702
  super(order);
1701
1703
  this.node = {};
1702
1704
  }
1703
- async id() {
1705
+ async id(isLeaf) {
1704
1706
  return await this.autoIncrement("index", 1);
1705
1707
  }
1706
1708
  async read(id) {
@@ -568,15 +568,15 @@ var BPTreeSync = class extends BPTree {
568
568
  throw new Error(`Direction must be -1 or 1. but got a ${direction}`);
569
569
  }
570
570
  }
571
- _createNodeId() {
572
- const id = this.strategy.id();
571
+ _createNodeId(isLeaf) {
572
+ const id = this.strategy.id(isLeaf);
573
573
  if (id === 0) {
574
574
  throw new Error(`The node's id should never be 0.`);
575
575
  }
576
576
  return id;
577
577
  }
578
- _createNode(keys2, values, leaf = false, parent = 0, next = 0, prev = 0) {
579
- const id = this._createNodeId();
578
+ _createNode(isLeaf, keys2, values, leaf = false, parent = 0, next = 0, prev = 0) {
579
+ const id = this._createNodeId(isLeaf);
580
580
  const node = {
581
581
  id,
582
582
  keys: keys2,
@@ -789,7 +789,7 @@ var BPTreeSync = class extends BPTree {
789
789
  }
790
790
  _insertInParent(node, value, pointer) {
791
791
  if (this.root === node) {
792
- const root = this._createNode([node.id, pointer.id], [value]);
792
+ const root = this._createNode(false, [node.id, pointer.id], [value]);
793
793
  this.root = root;
794
794
  this.strategy.head.root = root.id;
795
795
  node.parent = root.id;
@@ -807,7 +807,7 @@ var BPTreeSync = class extends BPTree {
807
807
  parentNode.keys.splice(i + 1, 0, pointer.id);
808
808
  this.bufferForNodeUpdate(parentNode);
809
809
  if (parentNode.keys.length > this.order) {
810
- const parentPointer = this._createNode([], []);
810
+ const parentPointer = this._createNode(false, [], []);
811
811
  parentPointer.parent = parentNode.parent;
812
812
  const mid = Math.ceil(this.order / 2) - 1;
813
813
  parentPointer.values = parentNode.values.slice(mid + 1);
@@ -840,7 +840,7 @@ var BPTreeSync = class extends BPTree {
840
840
  const head = this.strategy.readHead();
841
841
  if (head === null) {
842
842
  this.order = this.strategy.order;
843
- this.root = this._createNode([], [], true);
843
+ this.root = this._createNode(true, [], [], true);
844
844
  this.strategy.head.root = this.root.id;
845
845
  this.bufferForNodeCreate(this.root);
846
846
  this.commitHeadBuffer();
@@ -956,6 +956,7 @@ var BPTreeSync = class extends BPTree {
956
956
  this._insertAtLeaf(before, key, value);
957
957
  if (before.values.length === this.order) {
958
958
  const after = this._createNode(
959
+ true,
959
960
  [],
960
961
  [],
961
962
  true,
@@ -1109,15 +1110,15 @@ var BPTreeAsync = class extends BPTree {
1109
1110
  throw new Error(`Direction must be -1 or 1. but got a ${direction}`);
1110
1111
  }
1111
1112
  }
1112
- async _createNodeId() {
1113
- const id = await this.strategy.id();
1113
+ async _createNodeId(isLeaf) {
1114
+ const id = await this.strategy.id(isLeaf);
1114
1115
  if (id === 0) {
1115
1116
  throw new Error(`The node's id should never be 0.`);
1116
1117
  }
1117
1118
  return id;
1118
1119
  }
1119
- async _createNode(keys2, values, leaf = false, parent = 0, next = 0, prev = 0) {
1120
- const id = await this._createNodeId();
1120
+ async _createNode(isLeaf, keys2, values, leaf = false, parent = 0, next = 0, prev = 0) {
1121
+ const id = await this._createNodeId(isLeaf);
1121
1122
  const node = {
1122
1123
  id,
1123
1124
  keys: keys2,
@@ -1330,7 +1331,7 @@ var BPTreeAsync = class extends BPTree {
1330
1331
  }
1331
1332
  async _insertInParent(node, value, pointer) {
1332
1333
  if (this.root === node) {
1333
- const root = await this._createNode([node.id, pointer.id], [value]);
1334
+ const root = await this._createNode(false, [node.id, pointer.id], [value]);
1334
1335
  this.root = root;
1335
1336
  this.strategy.head.root = root.id;
1336
1337
  node.parent = root.id;
@@ -1348,7 +1349,7 @@ var BPTreeAsync = class extends BPTree {
1348
1349
  parentNode.keys.splice(i + 1, 0, pointer.id);
1349
1350
  this.bufferForNodeUpdate(parentNode);
1350
1351
  if (parentNode.keys.length > this.order) {
1351
- const parentPointer = await this._createNode([], []);
1352
+ const parentPointer = await this._createNode(false, [], []);
1352
1353
  parentPointer.parent = parentNode.parent;
1353
1354
  const mid = Math.ceil(this.order / 2) - 1;
1354
1355
  parentPointer.values = parentNode.values.slice(mid + 1);
@@ -1381,7 +1382,7 @@ var BPTreeAsync = class extends BPTree {
1381
1382
  const head = await this.strategy.readHead();
1382
1383
  if (head === null) {
1383
1384
  this.order = this.strategy.order;
1384
- this.root = await this._createNode([], [], true);
1385
+ this.root = await this._createNode(true, [], [], true);
1385
1386
  this.strategy.head.root = this.root.id;
1386
1387
  this.bufferForNodeCreate(this.root);
1387
1388
  this.commitHeadBuffer();
@@ -1497,6 +1498,7 @@ var BPTreeAsync = class extends BPTree {
1497
1498
  this._insertAtLeaf(before, key, value);
1498
1499
  if (before.values.length === this.order) {
1499
1500
  const after = await this._createNode(
1501
+ true,
1500
1502
  [],
1501
1503
  [],
1502
1504
  true,
@@ -1618,7 +1620,7 @@ var InMemoryStoreStrategySync = class extends SerializeStrategySync {
1618
1620
  super(order);
1619
1621
  this.node = {};
1620
1622
  }
1621
- id() {
1623
+ id(isLeaf) {
1622
1624
  return this.autoIncrement("index", 1);
1623
1625
  }
1624
1626
  read(id) {
@@ -1666,7 +1668,7 @@ var InMemoryStoreStrategyAsync = class extends SerializeStrategyAsync {
1666
1668
  super(order);
1667
1669
  this.node = {};
1668
1670
  }
1669
- async id() {
1671
+ async id(isLeaf) {
1670
1672
  return await this.autoIncrement("index", 1);
1671
1673
  }
1672
1674
  async read(id) {
@@ -8,8 +8,8 @@ export declare class BPTreeAsync<K, V> extends BPTree<K, V> {
8
8
  protected _getPairsRightToLeft(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean): Promise<BPTreePair<K, V>[]>;
9
9
  protected _getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean): Promise<BPTreePair<K, V>[]>;
10
10
  protected getPairs(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean, direction: 1 | -1): Promise<BPTreePair<K, V>[]>;
11
- protected _createNodeId(): Promise<number>;
12
- protected _createNode(keys: number[] | K[][], values: V[], leaf?: boolean, parent?: number, next?: number, prev?: number): Promise<BPTreeUnknownNode<K, V>>;
11
+ protected _createNodeId(isLeaf: boolean): Promise<number>;
12
+ protected _createNode(isLeaf: boolean, keys: number[] | K[][], values: V[], leaf?: boolean, parent?: number, next?: number, prev?: number): Promise<BPTreeUnknownNode<K, V>>;
13
13
  protected _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>, value: V): Promise<void>;
14
14
  protected _insertInParent(node: BPTreeUnknownNode<K, V>, value: V, pointer: BPTreeUnknownNode<K, V>): Promise<void>;
15
15
  init(): Promise<void>;
@@ -8,8 +8,8 @@ export declare class BPTreeSync<K, V> extends BPTree<K, V> {
8
8
  protected _getPairsRightToLeft(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean): BPTreePair<K, V>[];
9
9
  protected _getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean): BPTreePair<K, V>[];
10
10
  protected getPairs(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean, direction: 1 | -1): BPTreePair<K, V>[];
11
- protected _createNodeId(): number;
12
- protected _createNode(keys: number[] | K[][], values: V[], leaf?: boolean, parent?: number, next?: number, prev?: number): BPTreeUnknownNode<K, V>;
11
+ protected _createNodeId(isLeaf: boolean): number;
12
+ protected _createNode(isLeaf: boolean, keys: number[] | K[][], values: V[], leaf?: boolean, parent?: number, next?: number, prev?: number): BPTreeUnknownNode<K, V>;
13
13
  protected _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>, value: V): void;
14
14
  protected _insertInParent(node: BPTreeUnknownNode<K, V>, value: V, pointer: BPTreeUnknownNode<K, V>): void;
15
15
  init(): void;
@@ -2,7 +2,7 @@ import { BPTreeNode } from './base/BPTree';
2
2
  import { SerializeStrategy, SerializeStrategyHead } from './base/SerializeStrategy';
3
3
  import { Json } from './utils/types';
4
4
  export declare abstract class SerializeStrategyAsync<K, V> extends SerializeStrategy<K, V> {
5
- abstract id(): Promise<number>;
5
+ abstract id(isLeaf: boolean): Promise<number>;
6
6
  abstract read(id: number): Promise<BPTreeNode<K, V>>;
7
7
  abstract write(id: number, node: BPTreeNode<K, V>): Promise<void>;
8
8
  abstract readHead(): Promise<SerializeStrategyHead | null>;
@@ -14,7 +14,7 @@ export declare abstract class SerializeStrategyAsync<K, V> extends SerializeStra
14
14
  export declare class InMemoryStoreStrategyAsync<K, V> extends SerializeStrategyAsync<K, V> {
15
15
  protected readonly node: Record<number, BPTreeNode<K, V>>;
16
16
  constructor(order: number);
17
- id(): Promise<number>;
17
+ id(isLeaf: boolean): Promise<number>;
18
18
  read(id: number): Promise<BPTreeNode<K, V>>;
19
19
  write(id: number, node: BPTreeNode<K, V>): Promise<void>;
20
20
  readHead(): Promise<SerializeStrategyHead | null>;
@@ -2,7 +2,7 @@ import { BPTreeNode } from './base/BPTree';
2
2
  import { SerializeStrategy, SerializeStrategyHead } from './base/SerializeStrategy';
3
3
  import { Json } from './utils/types';
4
4
  export declare abstract class SerializeStrategySync<K, V> extends SerializeStrategy<K, V> {
5
- abstract id(): number;
5
+ abstract id(isLeaf: boolean): number;
6
6
  abstract read(id: number): BPTreeNode<K, V>;
7
7
  abstract write(id: number, node: BPTreeNode<K, V>): void;
8
8
  abstract readHead(): SerializeStrategyHead | null;
@@ -14,7 +14,7 @@ export declare abstract class SerializeStrategySync<K, V> extends SerializeStrat
14
14
  export declare class InMemoryStoreStrategySync<K, V> extends SerializeStrategySync<K, V> {
15
15
  protected readonly node: Record<number, BPTreeNode<K, V>>;
16
16
  constructor(order: number);
17
- id(): number;
17
+ id(isLeaf: boolean): number;
18
18
  read(id: number): BPTreeNode<K, V>;
19
19
  write(id: number, node: BPTreeNode<K, V>): void;
20
20
  readHead(): SerializeStrategyHead | null;
@@ -59,8 +59,8 @@ export declare abstract class BPTree<K, V> {
59
59
  protected abstract _getPairsRightToLeft(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean): Deferred<BPTreePair<K, V>[]>;
60
60
  protected abstract _getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean): Deferred<BPTreePair<K, V>[]>;
61
61
  protected abstract getPairs(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean, direction: -1 | 1): Deferred<BPTreePair<K, V>[]>;
62
- protected abstract _createNodeId(): Deferred<number>;
63
- protected abstract _createNode(keys: number[] | K[][], values: V[], leaf?: boolean, parent?: number, next?: number, prev?: number): Deferred<BPTreeUnknownNode<K, V>>;
62
+ protected abstract _createNodeId(isLeaf: boolean): Deferred<number>;
63
+ protected abstract _createNode(isLeaf: boolean, keys: number[] | K[][], values: V[], leaf?: boolean, parent?: number, next?: number, prev?: number): Deferred<BPTreeUnknownNode<K, V>>;
64
64
  protected abstract _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>, value: V): Deferred<void>;
65
65
  protected abstract _insertInParent(node: BPTreeUnknownNode<K, V>, value: V, pointer: BPTreeUnknownNode<K, V>): Deferred<void>;
66
66
  protected abstract getNode(id: number): Deferred<BPTreeUnknownNode<K, V>>;
@@ -15,8 +15,9 @@ export declare abstract class SerializeStrategy<K, V> {
15
15
  * When a new node is created within the tree, the value returned by this method becomes the node's ID.
16
16
  *
17
17
  * **WARNING!** The return value should never be `0`.
18
+ * @param isLeaf This is a flag that indicates whether the node is a leaf node or not.
18
19
  */
19
- abstract id(): number | Promise<number>;
20
+ abstract id(isLeaf: boolean): number | Promise<number>;
20
21
  /**
21
22
  * Read the stored node from the ID.
22
23
  * The JSON object of the read node should be returned.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "3.3.0",
3
+ "version": "3.3.1",
4
4
  "description": "Store the B+tree flexibly, not only in-memory.",
5
5
  "main": "dist/cjs/index.cjs",
6
6
  "module": "dist/esm/index.mjs",