serializable-bptree 1.0.4 → 2.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/README.md +22 -18
- package/dist/cjs/index.js +166 -256
- package/dist/esm/index.js +166 -256
- package/dist/typings/BPTree.d.ts +42 -26
- package/dist/typings/SerializeStrategy.d.ts +2 -0
- package/dist/typings/utils/types.d.ts +5 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -59,7 +59,7 @@ tree.where({ gt: 0, lt: 4 }) // [{ key: 'a', value: 1 }, { key: 'c', value: 3 }]
|
|
|
59
59
|
|
|
60
60
|
## Why use a `serializable-bptree`?
|
|
61
61
|
|
|
62
|
-
Firstly, in most cases, there is no need to use a B+tree in JavaScript. This is because there is a great alternative, the Map object. Nonetheless, if you need to retrieve values in a sorted order, a B+tree can be a good solution. These cases are often related to databases, and you may want to store this state not just in memory, but on a remote server or in a file. In this case,
|
|
62
|
+
Firstly, in most cases, there is no need to use a B+tree in JavaScript. This is because there is a great alternative, the Map object. Nonetheless, if you need to retrieve values in a sorted order, a B+tree can be a good solution. These cases are often related to databases, and you may want to store this state not just in memory, but on a remote server or in a file. In this case, **serializable-bptree** can help you.
|
|
63
63
|
|
|
64
64
|
## How to use
|
|
65
65
|
|
|
@@ -77,7 +77,7 @@ import { BPTree } from 'serializable-bptree'
|
|
|
77
77
|
|
|
78
78
|
```html
|
|
79
79
|
<script type="module">
|
|
80
|
-
import { BPTree } from 'https://cdn.jsdelivr.net/npm/serializable-bptree@1.x.x/dist/esm/index.min.js'
|
|
80
|
+
import { BPTree, ValueComparator, NumericComparator, StringComparator, InMemoryStoreStrategy } from 'https://cdn.jsdelivr.net/npm/serializable-bptree@1.x.x/dist/esm/index.min.js'
|
|
81
81
|
</script>
|
|
82
82
|
```
|
|
83
83
|
|
|
@@ -85,15 +85,15 @@ import { BPTree } from 'serializable-bptree'
|
|
|
85
85
|
|
|
86
86
|
### Value comparator
|
|
87
87
|
|
|
88
|
-
B+tree needs to keep values in sorted order. Therefore, a process to compare the sizes of values is needed, and that role is played by the
|
|
88
|
+
B+tree needs to keep values in sorted order. Therefore, a process to compare the sizes of values is needed, and that role is played by the **ValueComparator**.
|
|
89
89
|
|
|
90
|
-
Commonly used numerical and string comparisons are natively supported by the
|
|
90
|
+
Commonly used numerical and string comparisons are natively supported by the **serializable-bptree** library. Use it as follows:
|
|
91
91
|
|
|
92
92
|
```typescript
|
|
93
93
|
import { NumericComparator, StringComparator } from 'serializable-bptree'
|
|
94
94
|
```
|
|
95
95
|
|
|
96
|
-
However, you may want to sort complex objects other than numbers and strings. For example, if you want to sort by the
|
|
96
|
+
However, you may want to sort complex objects other than numbers and strings. For example, if you want to sort by the **age** property order of an object, you need to create a new class that inherits from the **ValueComparator** class. Use it as follows:
|
|
97
97
|
|
|
98
98
|
```typescript
|
|
99
99
|
import { ValueComparator } from 'serializable-bptree'
|
|
@@ -103,13 +103,15 @@ interface MyObject {
|
|
|
103
103
|
name: string
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
class AgeComparator {
|
|
106
|
+
class AgeComparator extends ValueComparator<MyObject> {
|
|
107
107
|
asc(a: MyObject, b: MyObject): number {
|
|
108
108
|
return a.age - b.age
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
```
|
|
112
112
|
|
|
113
|
+
The **asc** method should return values in ascending order. If the return value is negative, it means that the parameter **a** is smaller than **b**. If the return value is positive, it means that **a** is greater than **b**. If the return value is **0**, it indicates that **a** and **b** are of the same size.
|
|
114
|
+
|
|
113
115
|
### Serialize strategy
|
|
114
116
|
|
|
115
117
|
A B+tree instance is made up of numerous nodes. You would want to store this value when such nodes are created or updated. Let's assume you want to save it to a file.
|
|
@@ -132,7 +134,7 @@ What does this method mean? And why do we need to construct such a method?
|
|
|
132
134
|
|
|
133
135
|
#### id(): `number`
|
|
134
136
|
|
|
135
|
-
When a node is created in the B+tree, the node needs a unique value to represent itself. This is the
|
|
137
|
+
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.
|
|
136
138
|
|
|
137
139
|
```typescript
|
|
138
140
|
id(): number {
|
|
@@ -142,7 +144,9 @@ id(): number {
|
|
|
142
144
|
}
|
|
143
145
|
```
|
|
144
146
|
|
|
145
|
-
Or, you could use file input/output to save and load the value of the
|
|
147
|
+
Or, you could use file input/output to save and load the value of the **before** variable.
|
|
148
|
+
|
|
149
|
+
This method is called before a node is created in the tree. Therefore, it can also be used to allocate space for storing the node.
|
|
146
150
|
|
|
147
151
|
#### read(id: `number`): `BPTreeNode<K, V>`
|
|
148
152
|
|
|
@@ -158,7 +162,7 @@ read(id: number): BPTreeNode<K, V> {
|
|
|
158
162
|
}
|
|
159
163
|
```
|
|
160
164
|
|
|
161
|
-
This method is called only once when loading a node from a tree instance.
|
|
165
|
+
This method is called only once when loading a node from a tree instance. The loaded node is loaded into memory, and subsequently, when the tree references the node, it operates based on the values in memory **without** re-invoking this method.
|
|
162
166
|
|
|
163
167
|
#### write(id: `number`, node: `BPTreeNode<K, V>`): `void`
|
|
164
168
|
|
|
@@ -181,7 +185,7 @@ function writeBack(id: number, node: BPTreeNode<K, V>, timer: number) {
|
|
|
181
185
|
|
|
182
186
|
...
|
|
183
187
|
write(id: number, node: BPTreeNode<K, V>): void {
|
|
184
|
-
const writeBackInterval =
|
|
188
|
+
const writeBackInterval = 10
|
|
185
189
|
writeBack(id, node, writeBackInterval)
|
|
186
190
|
}
|
|
187
191
|
```
|
|
@@ -190,32 +194,32 @@ This kind of delay writing should ideally occur within a few milliseconds. If th
|
|
|
190
194
|
|
|
191
195
|
#### readHead(): `SerializeStrategyHead`|`null`
|
|
192
196
|
|
|
193
|
-
This method is called only once when the tree is created. It's a method to restore the saved tree information. If it is the initial creation and there is no stored root node, it should return
|
|
197
|
+
This method is called only once when the tree is created. It's a method to restore the saved tree information. If it is the initial creation and there is no stored root node, it should return **null**.
|
|
194
198
|
|
|
195
|
-
This method should return the value stored in the
|
|
199
|
+
This method should return the value stored in the **writeHead** method.
|
|
196
200
|
|
|
197
201
|
#### writeHead(head: `SerializeStrategyHead`): `void`
|
|
198
202
|
|
|
199
|
-
This method is called whenever the head information of the tree changes, typically when the root node changes.
|
|
203
|
+
This method is called whenever the head information of the tree changes, typically when the root node changes. This method also works when the tree's **setHeadData** method is called. This is because the method attempts to store head data in the root node.
|
|
200
204
|
|
|
201
|
-
As a parameter, it receives the header information of the tree. This value should be serialized and stored. Later, the
|
|
205
|
+
As a parameter, it receives the header information of the tree. This value should be serialized and stored. Later, the **readHead** method should convert this serialized value into a json format and return it.
|
|
202
206
|
|
|
203
207
|
### The Default `ValueComparator` and `SerializeStrategy`
|
|
204
208
|
|
|
205
|
-
To utilize
|
|
209
|
+
To utilize **serializable-bptree**, you need to implement certain functions. However, a few basic helper classes are provided by default.
|
|
206
210
|
|
|
207
211
|
#### ValueComparator
|
|
208
212
|
|
|
209
213
|
* `NumericComparator`
|
|
210
214
|
* `StringComparator`
|
|
211
215
|
|
|
212
|
-
If the values being inserted into the tree are numeric, please use the
|
|
216
|
+
If the values being inserted into the tree are numeric, please use the **NumericComparator** class.
|
|
213
217
|
|
|
214
218
|
```typescript
|
|
215
219
|
import { NumericComparator } from 'serializable-bptree'
|
|
216
220
|
```
|
|
217
221
|
|
|
218
|
-
If the values being inserted into the tree can be strings, you can use the
|
|
222
|
+
If the values being inserted into the tree can be strings, you can use the **StringComparator** class in this case.
|
|
219
223
|
|
|
220
224
|
```typescript
|
|
221
225
|
import { StringComparator } from 'serializable-bptree'
|
|
@@ -225,7 +229,7 @@ import { StringComparator } from 'serializable-bptree'
|
|
|
225
229
|
|
|
226
230
|
* `InMemoryStoreStrategy`
|
|
227
231
|
|
|
228
|
-
As of now, the only class supported by default is the
|
|
232
|
+
As of now, the only class supported by default is the **InMemoryStoreStrategy**. This class is suitable for use when you prefer to operate the tree solely in-memory, similar to a typical B+ tree.
|
|
229
233
|
|
|
230
234
|
```typescript
|
|
231
235
|
import { InMemoryStoreStrategy } from 'serializable-bptree'
|