serializable-bptree 1.0.4 → 1.1.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/dist/cjs/index.js CHANGED
@@ -92,6 +92,7 @@ var BPTree = class {
92
92
  search;
93
93
  order;
94
94
  nodes;
95
+ data;
95
96
  root;
96
97
  _creates;
97
98
  _updates;
@@ -131,14 +132,16 @@ var BPTree = class {
131
132
  this.comparator = comparator;
132
133
  if (head === null) {
133
134
  this.order = strategy.order;
135
+ this.data = {};
134
136
  this.root = this._createNode([], [], true);
135
137
  this._setHeadUpdate(this._headState);
136
138
  this._setCreates(this.root);
137
139
  this._emitHeadUpdates();
138
140
  this._emitCreates();
139
141
  } else {
140
- const { root, order } = head;
142
+ const { root, order, data } = head;
141
143
  this.order = order;
144
+ this.data = data ?? {};
142
145
  this.root = this.getNode(root);
143
146
  }
144
147
  if (this.order < 3) {
@@ -148,9 +151,11 @@ var BPTree = class {
148
151
  get _headState() {
149
152
  const root = this.root.id;
150
153
  const order = this.order;
154
+ const data = this.data;
151
155
  return {
152
156
  root,
153
- order
157
+ order,
158
+ data
154
159
  };
155
160
  }
156
161
  _setHeadUpdate(head) {
@@ -849,6 +854,25 @@ var BPTree = class {
849
854
  }
850
855
  }
851
856
  }
857
+ /**
858
+ * Returns the user-defined data stored in the head of the tree.
859
+ * This value can be set using the `setHeadData` method. If no data has been previously inserted, the default value is returned, and the default value is `{}`.
860
+ * @returns User-defined data stored in the head of the tree.
861
+ */
862
+ getHeadData() {
863
+ return this.data;
864
+ }
865
+ /**
866
+ * Inserts user-defined data into the head of the tree.
867
+ * This feature is useful when you need to store separate, non-volatile information in the tree.
868
+ * For example, you can store information such as the last update time and the number of insertions.
869
+ * @param data User-defined data to be stored in the head of the tree.
870
+ */
871
+ setHeadData(data) {
872
+ this.data = data;
873
+ this._updatedHead = this._headState;
874
+ this._emitHeadUpdates();
875
+ }
852
876
  };
853
877
 
854
878
  // src/SerializeStrategy.ts
package/dist/esm/index.js CHANGED
@@ -61,6 +61,7 @@ var BPTree = class {
61
61
  search;
62
62
  order;
63
63
  nodes;
64
+ data;
64
65
  root;
65
66
  _creates;
66
67
  _updates;
@@ -100,14 +101,16 @@ var BPTree = class {
100
101
  this.comparator = comparator;
101
102
  if (head === null) {
102
103
  this.order = strategy.order;
104
+ this.data = {};
103
105
  this.root = this._createNode([], [], true);
104
106
  this._setHeadUpdate(this._headState);
105
107
  this._setCreates(this.root);
106
108
  this._emitHeadUpdates();
107
109
  this._emitCreates();
108
110
  } else {
109
- const { root, order } = head;
111
+ const { root, order, data } = head;
110
112
  this.order = order;
113
+ this.data = data ?? {};
111
114
  this.root = this.getNode(root);
112
115
  }
113
116
  if (this.order < 3) {
@@ -117,9 +120,11 @@ var BPTree = class {
117
120
  get _headState() {
118
121
  const root = this.root.id;
119
122
  const order = this.order;
123
+ const data = this.data;
120
124
  return {
121
125
  root,
122
- order
126
+ order,
127
+ data
123
128
  };
124
129
  }
125
130
  _setHeadUpdate(head) {
@@ -818,6 +823,25 @@ var BPTree = class {
818
823
  }
819
824
  }
820
825
  }
826
+ /**
827
+ * Returns the user-defined data stored in the head of the tree.
828
+ * This value can be set using the `setHeadData` method. If no data has been previously inserted, the default value is returned, and the default value is `{}`.
829
+ * @returns User-defined data stored in the head of the tree.
830
+ */
831
+ getHeadData() {
832
+ return this.data;
833
+ }
834
+ /**
835
+ * Inserts user-defined data into the head of the tree.
836
+ * This feature is useful when you need to store separate, non-volatile information in the tree.
837
+ * For example, you can store information such as the last update time and the number of insertions.
838
+ * @param data User-defined data to be stored in the head of the tree.
839
+ */
840
+ setHeadData(data) {
841
+ this.data = data;
842
+ this._updatedHead = this._headState;
843
+ this._emitHeadUpdates();
844
+ }
821
845
  };
822
846
 
823
847
  // src/SerializeStrategy.ts
@@ -1,3 +1,4 @@
1
+ import type { Json } from './utils/types';
1
2
  import { BinarySearch } from './utils/BinarySearch';
2
3
  import { ValueComparator } from './ValueComparator';
3
4
  import { SerializeStrategy } from './SerializeStrategy';
@@ -36,6 +37,7 @@ export declare class BPTree<K, V> {
36
37
  protected readonly search: BinarySearch<V>;
37
38
  protected readonly order: number;
38
39
  protected readonly nodes: Map<number, BPTreeUnknownNode<K, V>>;
40
+ protected data: Record<string, Json>;
39
41
  protected root: BPTreeUnknownNode<K, V>;
40
42
  private readonly _creates;
41
43
  private readonly _updates;
@@ -107,5 +109,18 @@ export declare class BPTree<K, V> {
107
109
  */
108
110
  delete(key: K, value: V): void;
109
111
  private _deleteEntry;
112
+ /**
113
+ * Returns the user-defined data stored in the head of the tree.
114
+ * This value can be set using the `setHeadData` method. If no data has been previously inserted, the default value is returned, and the default value is `{}`.
115
+ * @returns User-defined data stored in the head of the tree.
116
+ */
117
+ getHeadData(): Record<string, Json>;
118
+ /**
119
+ * Inserts user-defined data into the head of the tree.
120
+ * This feature is useful when you need to store separate, non-volatile information in the tree.
121
+ * For example, you can store information such as the last update time and the number of insertions.
122
+ * @param data User-defined data to be stored in the head of the tree.
123
+ */
124
+ setHeadData(data: Record<string, Json>): void;
110
125
  }
111
126
  export {};
@@ -1,7 +1,9 @@
1
1
  import { BPTreeNode } from './BPTree';
2
+ import type { Json } from './utils/types';
2
3
  export interface SerializeStrategyHead {
3
4
  root: number;
4
5
  order: number;
6
+ data: Record<string, Json>;
5
7
  }
6
8
  export declare abstract class SerializeStrategy<K, V> {
7
9
  readonly order: number;
@@ -0,0 +1,5 @@
1
+ type Primitive = string | number | null | boolean;
2
+ type Json = Primitive | Primitive[] | Json[] | {
3
+ [key: string]: Json;
4
+ };
5
+ export type { Json };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "description": "Store the B+tree flexibly, not only in-memory.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",