serializable-bptree 1.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.
@@ -0,0 +1,91 @@
1
+ import { BinarySearch } from './utils/BinarySearch';
2
+ import { ValueComparator } from './ValueComparator';
3
+ import { SerializeStrategy } from './SerializeStrategy';
4
+ type BPTreeNodeKey<K, V> = K[] | BPTreeNode<K, V>;
5
+ type BPTreeCondition<V> = {
6
+ gt?: V;
7
+ lt?: V;
8
+ } | {
9
+ equal: V;
10
+ } | {
11
+ notEqual: V;
12
+ };
13
+ type BPTreePair<K, V> = {
14
+ key: K;
15
+ value: V;
16
+ };
17
+ export interface BPTreeNode<K, V> {
18
+ id: number;
19
+ keys: BPTreeNodeKey<K, V>[];
20
+ values: V[];
21
+ leaf: boolean;
22
+ parent: number;
23
+ next: number;
24
+ }
25
+ export declare class BPTree<K, V> {
26
+ protected readonly strategy: SerializeStrategy<K, V>;
27
+ protected readonly comparator: ValueComparator<V>;
28
+ protected readonly search: BinarySearch<V>;
29
+ protected readonly order: number;
30
+ protected readonly nodes: Map<number, BPTreeNode<K, V>>;
31
+ protected root: BPTreeNode<K, V>;
32
+ private readonly _creates;
33
+ private readonly _updates;
34
+ private _updatedHead;
35
+ private _createNodeId;
36
+ private _createNode;
37
+ /**
38
+ * @param strategy An instance of a strategy that manages the read/write state of a node.
39
+ * @param comparator An instance of a comparator that compares the size of values.
40
+ */
41
+ constructor(strategy: SerializeStrategy<K, V>, comparator: ValueComparator<V>);
42
+ private get _headState();
43
+ private _setHeadUpdate;
44
+ private _setCreates;
45
+ private _setUpdates;
46
+ private _emitHeadUpdates;
47
+ private _emitCreates;
48
+ private _emitUpdates;
49
+ protected getNode(id: number): BPTreeNode<K, V>;
50
+ protected leftestNode(): BPTreeNode<K, V>;
51
+ private _insertableNode;
52
+ /**
53
+ * It returns whether there is a value in the tree.
54
+ * @param key The key value to search for.
55
+ * @param value The value to search for.
56
+ */
57
+ exists(key: K, value: V): boolean;
58
+ private _insertAtLeaf;
59
+ private _insertInParent;
60
+ private _equalCondition;
61
+ private _notEqualCondition;
62
+ private _onlyGtCondition;
63
+ private _onlyLtCondition;
64
+ private _rangeCondition;
65
+ private _getPairsFromValue;
66
+ private _getPairsFromNEValue;
67
+ private _getPairsFromRange;
68
+ private _getPairsFromGt;
69
+ private _getPairsFromLt;
70
+ /**
71
+ * It searches for a value within the tree. The result is returned as an array sorted in ascending order based on the value.
72
+ * The result includes the key and value attributes, and you can use the `gt`, `lt`, `equal`, `notEqual` condition statements.
73
+ * @param condition You can use the `gt`, `lt`, `equal`, `notEqual` condition statements.
74
+ */
75
+ where(condition: BPTreeCondition<V>): BPTreePair<K, V>[];
76
+ /**
77
+ * You enter the key and value as a pair. You can later search for the pair by value.
78
+ * This data is stored in the tree, sorted in ascending order of value.
79
+ * @param key The key of the pair.
80
+ * @param value The value of the pair.
81
+ */
82
+ insert(key: K, value: V): void;
83
+ /**
84
+ * Deletes the pair that matches the key and value.
85
+ * @param key The key of the pair.
86
+ * @param value The value of the pair.
87
+ */
88
+ delete(key: K, value: V): void;
89
+ private _deleteEntry;
90
+ }
91
+ export {};
@@ -0,0 +1,55 @@
1
+ import { BPTreeNode } from './BPTree';
2
+ export interface SerializeStrategyHead {
3
+ root: number;
4
+ order: number;
5
+ }
6
+ export declare abstract class SerializeStrategy<K, V> {
7
+ readonly order: number;
8
+ constructor(order: number);
9
+ /**
10
+ * The rule for generating node IDs is set.
11
+ * When a new node is created within the tree, the value returned by this method becomes the node's ID.
12
+ *
13
+ * **WARNING!** The return value should never be `0`.
14
+ */
15
+ abstract id(): number;
16
+ /**
17
+ * Read the stored node from the ID.
18
+ * The JSON object of the read node should be returned.
19
+ * @param id This is the ID of the node to be read.
20
+ */
21
+ abstract read(id: number): BPTreeNode<K, V>;
22
+ /**
23
+ * It is called when a node is created or updated and needs to be stored.
24
+ * The node ID and the node JSON object are passed as parameters. Use this to store the data.
25
+ * @param id This is the ID of the node to be stored.
26
+ * @param node This is the JSON object of the node to be stored.
27
+ */
28
+ abstract write(id: number, node: BPTreeNode<K, V>): void;
29
+ /**
30
+ * It is called when a tree instance is created.
31
+ * This method should return the information needed to initialize the tree. This information refers to the values stored in the `writeHead` method.
32
+ *
33
+ * If it's the first creation and there are no saved root nodes, return `null`.
34
+ * In this case, the tree is created based on the order specified in the strategy instance constructor parameters.
35
+ */
36
+ abstract readHead(): SerializeStrategyHead | null;
37
+ /**
38
+ * It is called when the root node is created or updated.
39
+ * The method takes the current state of the tree as a parameter. Serialize and store this value. It will be used for the `readHead` method later.
40
+ * @param head This is the current state of the tree.
41
+ */
42
+ abstract writeHead(head: SerializeStrategyHead): void;
43
+ }
44
+ export declare class InMemoryStoreStrategy<K, V> extends SerializeStrategy<K, V> {
45
+ protected readonly data: {
46
+ head: SerializeStrategyHead | null;
47
+ node: Record<number, BPTreeNode<K, V>>;
48
+ };
49
+ constructor(order: number);
50
+ id(): number;
51
+ read(id: number): BPTreeNode<K, V>;
52
+ write(id: number, node: BPTreeNode<K, V>): void;
53
+ readHead(): SerializeStrategyHead | null;
54
+ writeHead(head: SerializeStrategyHead): void;
55
+ }
@@ -0,0 +1,18 @@
1
+ export declare abstract class ValueComparator<V> {
2
+ /**
3
+ * Implement an algorithm that sorts values in ascending order.
4
+ * If it returns a negative number, a is less than b. If it returns 0, the two values are equal. If it returns a positive number, a is greater than b.
5
+ * @param a Value a.
6
+ * @param b Value b.
7
+ */
8
+ abstract asc(a: V, b: V): number;
9
+ isLower(value: V, than: V): boolean;
10
+ isSame(value: V, than: V): boolean;
11
+ isHigher(value: V, than: V): boolean;
12
+ }
13
+ export declare class NumericComparator extends ValueComparator<number> {
14
+ asc(a: number, b: number): number;
15
+ }
16
+ export declare class StringComparator extends ValueComparator<string> {
17
+ asc(a: string, b: string): number;
18
+ }
@@ -0,0 +1,3 @@
1
+ export { BPTree, BPTreeNode } from './BPTree';
2
+ export { SerializeStrategy, SerializeStrategyHead, InMemoryStoreStrategy } from './SerializeStrategy';
3
+ export { ValueComparator, NumericComparator, StringComparator } from './ValueComparator';
@@ -0,0 +1,9 @@
1
+ import { ValueComparator } from '../ValueComparator';
2
+ export declare class BinarySearch<T> {
3
+ protected readonly comparator: ValueComparator<T>;
4
+ constructor(comparator: ValueComparator<T>);
5
+ _withRange(array: T[], value: T, left?: number, right?: number): number;
6
+ leftest(array: T[], value: T): number;
7
+ rightest(array: T[], value: T): number;
8
+ range(array: T[], value: T): [number, number];
9
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "serializable-bptree",
3
+ "version": "1.0.0",
4
+ "description": "Store the B+tree flexibly, not only in-memory.",
5
+ "main": "dist/cjs/index.js",
6
+ "module": "dist/esm/index.js",
7
+ "typings": "dist/typings/index.d.ts",
8
+ "files": [
9
+ "dist/**/*"
10
+ ],
11
+ "scripts": {
12
+ "test": "jest",
13
+ "build": "node build/index.js && tsc"
14
+ },
15
+ "author": "izure1 <admin@izure.org>",
16
+ "keywords": [
17
+ "serializable-bptree",
18
+ "b-plus-tree",
19
+ "serialize",
20
+ "store",
21
+ "btree",
22
+ "bptree",
23
+ "event"
24
+ ],
25
+ "license": "MIT",
26
+ "devDependencies": {
27
+ "@types/jest": "^29.5.8",
28
+ "esbuild": "^0.19.5",
29
+ "jest": "^29.7.0",
30
+ "ts-jest": "^29.1.1",
31
+ "typescript": "^5.2.2"
32
+ }
33
+ }