serializable-bptree 2.0.0 → 3.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 +152 -10
- package/dist/cjs/index.js +926 -407
- package/dist/esm/index.js +926 -407
- package/dist/typings/BPTreeAsync.d.ts +45 -0
- package/dist/typings/BPTreeSync.d.ts +46 -0
- package/dist/typings/SerializeStrategyAsync.d.ts +21 -0
- package/dist/typings/SerializeStrategySync.d.ts +21 -0
- package/dist/typings/base/BPTree.d.ts +138 -0
- package/dist/typings/{SerializeStrategy.d.ts → base/SerializeStrategy.d.ts} +7 -19
- package/dist/typings/index.d.ts +6 -2
- package/package.json +5 -1
- package/dist/typings/BPTree.d.ts +0 -127
- package/dist/typings/utils/BinarySearch.d.ts +0 -9
package/README.md
CHANGED
|
@@ -7,9 +7,13 @@ This is a B+tree that's totally okay with duplicate values. If you need to keep
|
|
|
7
7
|
|
|
8
8
|
```typescript
|
|
9
9
|
import { readFileSync, writeFileSync, existsSync } from 'fs'
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
BPTreeSync,
|
|
12
|
+
SerializeStrategySync,
|
|
13
|
+
NumericComparator
|
|
14
|
+
} from 'serializable-bptree'
|
|
11
15
|
|
|
12
|
-
class
|
|
16
|
+
class FileStoreStrategySync extends SerializeStrategySync<K, V> {
|
|
13
17
|
id(): number {
|
|
14
18
|
const random = Math.ceil(Math.random()*1000000)
|
|
15
19
|
return random
|
|
@@ -40,11 +44,12 @@ class FileStoreStrategy extends SerializeStrategy<K, V> {
|
|
|
40
44
|
}
|
|
41
45
|
|
|
42
46
|
const order = 5
|
|
43
|
-
const tree = new
|
|
44
|
-
new
|
|
47
|
+
const tree = new BPTreeSync(
|
|
48
|
+
new FileStoreStrategySync(order),
|
|
45
49
|
new NumericComparator()
|
|
46
50
|
)
|
|
47
51
|
|
|
52
|
+
tree.init()
|
|
48
53
|
tree.insert('a', 1)
|
|
49
54
|
tree.insert('b', 2)
|
|
50
55
|
tree.insert('c', 3)
|
|
@@ -61,6 +66,8 @@ tree.where({ gt: 0, lt: 4 }) // [{ key: 'a', value: 1 }, { key: 'c', value: 3 }]
|
|
|
61
66
|
|
|
62
67
|
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
68
|
|
|
69
|
+
Additionally, this library supports asynchronous operations. Please refer to the section below for instructions on using it asynchronously.
|
|
70
|
+
|
|
64
71
|
## How to use
|
|
65
72
|
|
|
66
73
|
### Node.js (cjs)
|
|
@@ -70,14 +77,29 @@ npm i serializable-bptree
|
|
|
70
77
|
```
|
|
71
78
|
|
|
72
79
|
```typescript
|
|
73
|
-
import {
|
|
80
|
+
import {
|
|
81
|
+
BPTreeSync,
|
|
82
|
+
BPTreeAsync,
|
|
83
|
+
SerializeStrategySync,
|
|
84
|
+
SerializeStrategyAsync,
|
|
85
|
+
NumericComparator,
|
|
86
|
+
StringComparator
|
|
87
|
+
} from 'serializable-bptree'
|
|
74
88
|
```
|
|
75
89
|
|
|
76
90
|
### Browser (esm)
|
|
77
91
|
|
|
78
92
|
```html
|
|
79
93
|
<script type="module">
|
|
80
|
-
import {
|
|
94
|
+
import {
|
|
95
|
+
BPTreeSync,
|
|
96
|
+
BPTreeAsync,
|
|
97
|
+
InMemoryStoreStrategySync,
|
|
98
|
+
InMemoryStoreStrategyAsync,
|
|
99
|
+
ValueComparator,
|
|
100
|
+
NumericComparator,
|
|
101
|
+
StringComparator
|
|
102
|
+
} from 'https://cdn.jsdelivr.net/npm/serializable-bptree@3.x.x/dist/esm/index.min.js'
|
|
81
103
|
</script>
|
|
82
104
|
```
|
|
83
105
|
|
|
@@ -119,9 +141,9 @@ A B+tree instance is made up of numerous nodes. You would want to store this val
|
|
|
119
141
|
You need to construct a logic for input/output from the file by inheriting the SerializeStrategy class. Look at the class structure below:
|
|
120
142
|
|
|
121
143
|
```typescript
|
|
122
|
-
import {
|
|
144
|
+
import { SerializeStrategySync } from 'serializable-bptree'
|
|
123
145
|
|
|
124
|
-
class
|
|
146
|
+
class MyFileIOStrategySync extends SerializeStrategySync {
|
|
125
147
|
id(): number
|
|
126
148
|
read(id: number): BPTreeNode<K, V>
|
|
127
149
|
write(id: number, node: BPTreeNode<K, V>): void
|
|
@@ -227,14 +249,134 @@ import { StringComparator } from 'serializable-bptree'
|
|
|
227
249
|
|
|
228
250
|
#### SerializeStrategy
|
|
229
251
|
|
|
230
|
-
* `
|
|
252
|
+
* `InMemoryStoreStrategySync`
|
|
253
|
+
* `InMemoryStoreStrategyAsync`
|
|
231
254
|
|
|
232
255
|
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.
|
|
233
256
|
|
|
234
257
|
```typescript
|
|
235
|
-
import {
|
|
258
|
+
import {
|
|
259
|
+
InMemoryStoreStrategySync,
|
|
260
|
+
InMemoryStoreStrategyAsync
|
|
261
|
+
} from 'serializable-bptree'
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Data Query Condition Clause
|
|
265
|
+
|
|
266
|
+
This library supports various conditional clauses. Currently, it supports **gte**, **gt**, **lte**, **lt**, **equal**, **notEqual**, and **like** conditions. Each condition is as follows:
|
|
267
|
+
|
|
268
|
+
### `gte`
|
|
269
|
+
|
|
270
|
+
Queries values that are greater than or equal to the given value.
|
|
271
|
+
|
|
272
|
+
### `gt`
|
|
273
|
+
|
|
274
|
+
Queries values that are greater than the given value.
|
|
275
|
+
|
|
276
|
+
### `lte`
|
|
277
|
+
|
|
278
|
+
Queries values that are less than or equal to the given value.
|
|
279
|
+
|
|
280
|
+
### `lt`
|
|
281
|
+
|
|
282
|
+
Queries values that are less than the given value.
|
|
283
|
+
|
|
284
|
+
### `equal`
|
|
285
|
+
|
|
286
|
+
Queries values that match the given value.
|
|
287
|
+
|
|
288
|
+
### `notEqual`
|
|
289
|
+
|
|
290
|
+
Queries values that do not match the given value.
|
|
291
|
+
|
|
292
|
+
### `like`
|
|
293
|
+
|
|
294
|
+
Queries values that contain the given value in a manner similar to regular expressions. Special characters such as % and _ can be used.
|
|
295
|
+
|
|
296
|
+
**%** matches zero or more characters. For example, **%ada%** means all strings that contain "ada" anywhere in the string. **%ada** means strings that end with "ada". **ada%** means strings that start with **"ada"**.
|
|
297
|
+
|
|
298
|
+
**_** matches exactly one character.
|
|
299
|
+
Using **p_t**, it can match any string where the underscore is replaced by any character, such as "pit", "put", etc.
|
|
300
|
+
|
|
301
|
+
You can obtain matching data by combining these condition clauses. If there are multiple conditions, an **AND** operation is used to retrieve only the data that satisfies all conditions.
|
|
302
|
+
|
|
303
|
+
## Using Asynchronously
|
|
304
|
+
|
|
305
|
+
Support for asynchronous trees has been available since version 3.0.0. Asynchronous is useful for operations with delays, such as file input/output and remote storage. Here is an example of how to use it:
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
import { existsSync } from 'fs'
|
|
309
|
+
import { readFile, writeFile } from 'fs/promises'
|
|
310
|
+
import {
|
|
311
|
+
BPTreeAsync,
|
|
312
|
+
SerializeStrategyAsync,
|
|
313
|
+
NumericComparator,
|
|
314
|
+
StringComparator
|
|
315
|
+
} from 'serializable-bptree'
|
|
316
|
+
|
|
317
|
+
class FileStoreStrategyAsync extends SerializeStrategyAsync<K, V> {
|
|
318
|
+
async id(): Promise<number> {
|
|
319
|
+
const random = Math.ceil(Math.random()*1000000)
|
|
320
|
+
return random
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async read(id: number): Promise<BPTreeNode<K, V>> {
|
|
324
|
+
const raw = await readFile(id.toString(), 'utf8')
|
|
325
|
+
return JSON.parse(raw)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async write(id: number, node: BPTreeNode<K, V>): Promise<void> {
|
|
329
|
+
const stringify = JSON.stringify(node)
|
|
330
|
+
await writeFile(id.toString(), stringify, 'utf8')
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async readHead(): Promise<SerializeStrategyHead|null> {
|
|
334
|
+
if (!existsSync('head')) {
|
|
335
|
+
return null
|
|
336
|
+
}
|
|
337
|
+
const raw = await readFile('head', 'utf8')
|
|
338
|
+
return JSON.parse(raw)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async writeHead(head: SerializeStrategyHead): Promise<void> {
|
|
342
|
+
const stringify = JSON.stringify(head)
|
|
343
|
+
await writeFile('head', stringify, 'utf8')
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const order = 5
|
|
348
|
+
const tree = new BPTreeAsync(
|
|
349
|
+
new FileStoreStrategyAsync(order),
|
|
350
|
+
new NumericComparator()
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
await tree.init()
|
|
354
|
+
await tree.insert('a', 1)
|
|
355
|
+
await tree.insert('b', 2)
|
|
356
|
+
await tree.insert('c', 3)
|
|
357
|
+
|
|
358
|
+
await tree.delete('b', 2)
|
|
359
|
+
|
|
360
|
+
await tree.where({ equal: 1 }) // [{ key: 'a', value: 1 }]
|
|
361
|
+
await tree.where({ gt: 1 }) // [{ key: 'c', value: 3 }]
|
|
362
|
+
await tree.where({ lt: 2 }) // [{ key: 'a', value: 1 }]
|
|
363
|
+
await tree.where({ gt: 0, lt: 4 }) // [{ key: 'a', value: 1 }, { key: 'c', value: 3 }]
|
|
236
364
|
```
|
|
237
365
|
|
|
366
|
+
The implementation method for asynchronous operations is not significantly different. The **-Async** suffix is used instead of the **-Sync** suffix in the **BPTree** and **SerializeStrategy** classes. The only difference is that the methods become asynchronous. The **ValueComparator** class and similar value comparators do not use asynchronous operations.
|
|
367
|
+
|
|
368
|
+
## Precautions for Use
|
|
369
|
+
|
|
370
|
+
### Synchronization Issue
|
|
371
|
+
|
|
372
|
+
The serializable-bptree minimizes file I/O by storing loaded nodes in-memory. This approach works well in situations where there is a 1:1 relationship between the remote storage and the client. However, in a 1:n scenario, where multiple clients read from and write to a single remote storage, data inconsistency between the remote storage and the clients can occur.
|
|
373
|
+
|
|
374
|
+
To solve this issue, it's necessary to update the cached nodes. The forceUpdate method was created for this purpose. It allows for the update of specific nodes, but when updating a node ID, a signal must be sent to all clients connected to the remote storage, informing them that the node has been updated. Clients must receive this signal and call the forceUpdate method to update the node. This logic goes beyond the scope of the library, so it must be implemented directly.
|
|
375
|
+
|
|
376
|
+
### Concurrency Issue in Asynchronous Trees
|
|
377
|
+
|
|
378
|
+
This issue occurs only in asynchronous trees and can also occur in a 1:1 relationship between remote storage and client. During the process of inserting/removing data asynchronously, querying the data can result in inconsistent data. To prevent concurrency issues, do not query data while inserting/removing it.
|
|
379
|
+
|
|
238
380
|
## LICENSE
|
|
239
381
|
|
|
240
382
|
MIT LICENSE
|