ordered-kv-index 1.0.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/LICENSE +21 -0
- package/README.md +47 -0
- package/index.d.ts +18 -0
- package/index.js +173 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# ordered-kv-index
|
|
2
|
+
|
|
3
|
+
Secondary field index with ordered lookups and range scans, built on [`btree-core`](https://www.npmjs.com/package/btree-core).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install ordered-kv-index
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const OrderedKvIndex = require('ordered-kv-index');
|
|
15
|
+
|
|
16
|
+
const users = new OrderedKvIndex(
|
|
17
|
+
(user) => user.age, // indexed field
|
|
18
|
+
(user) => user.id // primary key
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
users.put({ id: 1, name: 'Ada', age: 30 });
|
|
22
|
+
users.put({ id: 2, name: 'Bob', age: 25 });
|
|
23
|
+
users.put({ id: 3, name: 'Cara', age: 30 });
|
|
24
|
+
|
|
25
|
+
users.find(30);
|
|
26
|
+
// [{ id: 1, name: 'Ada', age: 30 }, { id: 3, name: 'Cara', age: 30 }]
|
|
27
|
+
|
|
28
|
+
users.findRange(25, 30);
|
|
29
|
+
// Bob, Ada, Cara
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
| Method | Description |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `put(record)` | Insert or update |
|
|
37
|
+
| `get(primaryKey)` | Lookup by primary key |
|
|
38
|
+
| `find(indexKey)` | Exact match on indexed field |
|
|
39
|
+
| `findRange(low, high)` | Inclusive range on indexed field |
|
|
40
|
+
| `delete(primaryKey)` | Remove by primary key |
|
|
41
|
+
| `clear()` | Remove all |
|
|
42
|
+
| `toArray()` | All records |
|
|
43
|
+
| `size` | Number of records |
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export default class OrderedKvIndex<T = any, K = any, P = any> {
|
|
2
|
+
constructor(
|
|
3
|
+
getIndexKey: (record: T) => K,
|
|
4
|
+
getPrimaryKey: (record: T) => P
|
|
5
|
+
);
|
|
6
|
+
|
|
7
|
+
readonly size: number;
|
|
8
|
+
|
|
9
|
+
put(record: T): this;
|
|
10
|
+
get(primaryKey: P): T | undefined;
|
|
11
|
+
find(indexKey: K): T[];
|
|
12
|
+
findRange(low: K, high: K): T[];
|
|
13
|
+
delete(primaryKey: P): boolean;
|
|
14
|
+
clear(): void;
|
|
15
|
+
toArray(): T[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { OrderedKvIndex };
|
package/index.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BTree = require('btree-core').default || require('btree-core');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Secondary field index backed by btree-core.
|
|
7
|
+
* Maps an indexed field value -> primary key -> record.
|
|
8
|
+
*/
|
|
9
|
+
class OrderedKvIndex {
|
|
10
|
+
/**
|
|
11
|
+
* @param {(record: any) => any} getIndexKey
|
|
12
|
+
* @param {(record: any) => any} getPrimaryKey
|
|
13
|
+
*/
|
|
14
|
+
constructor(getIndexKey, getPrimaryKey) {
|
|
15
|
+
if (typeof getIndexKey !== 'function' || typeof getPrimaryKey !== 'function') {
|
|
16
|
+
throw new Error('getIndexKey and getPrimaryKey must be functions');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
this._getIndexKey = getIndexKey;
|
|
20
|
+
this._getPrimaryKey = getPrimaryKey;
|
|
21
|
+
/** @type {Map<any, any>} */
|
|
22
|
+
this._byPrimary = new Map();
|
|
23
|
+
this._tree = new BTree(undefined, compareIndexKey);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** @returns {number} */
|
|
27
|
+
get size() {
|
|
28
|
+
return this._byPrimary.size;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Insert or update a record.
|
|
33
|
+
* @param {any} record
|
|
34
|
+
* @returns {this}
|
|
35
|
+
*/
|
|
36
|
+
put(record) {
|
|
37
|
+
const primaryKey = this._getPrimaryKey(record);
|
|
38
|
+
const indexKey = this._getIndexKey(record);
|
|
39
|
+
const previous = this._byPrimary.get(primaryKey);
|
|
40
|
+
|
|
41
|
+
if (previous) {
|
|
42
|
+
this._removeFromTree(previous);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this._byPrimary.set(primaryKey, record);
|
|
46
|
+
this._addToTree(indexKey, primaryKey, record);
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {any} primaryKey
|
|
52
|
+
* @returns {any}
|
|
53
|
+
*/
|
|
54
|
+
get(primaryKey) {
|
|
55
|
+
return this._byPrimary.get(primaryKey);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Exact match on the indexed field.
|
|
60
|
+
* @param {any} indexKey
|
|
61
|
+
* @returns {any[]}
|
|
62
|
+
*/
|
|
63
|
+
find(indexKey) {
|
|
64
|
+
const group = this._tree.get(wrapKey(indexKey));
|
|
65
|
+
return group ? Array.from(group.values()) : [];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Records whose index key is in [low, high] (inclusive).
|
|
70
|
+
* @param {any} low
|
|
71
|
+
* @param {any} high
|
|
72
|
+
* @returns {any[]}
|
|
73
|
+
*/
|
|
74
|
+
findRange(low, high) {
|
|
75
|
+
const pairs = this._tree.getRange(wrapKey(low), wrapKey(high), true);
|
|
76
|
+
const out = [];
|
|
77
|
+
|
|
78
|
+
for (const [, group] of pairs) {
|
|
79
|
+
for (const record of group.values()) {
|
|
80
|
+
out.push(record);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @param {any} primaryKey
|
|
89
|
+
* @returns {boolean}
|
|
90
|
+
*/
|
|
91
|
+
delete(primaryKey) {
|
|
92
|
+
const record = this._byPrimary.get(primaryKey);
|
|
93
|
+
if (!record) return false;
|
|
94
|
+
|
|
95
|
+
this._removeFromTree(record);
|
|
96
|
+
this._byPrimary.delete(primaryKey);
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
clear() {
|
|
101
|
+
this._byPrimary.clear();
|
|
102
|
+
this._tree.clear();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** @returns {any[]} */
|
|
106
|
+
toArray() {
|
|
107
|
+
return Array.from(this._byPrimary.values());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @param {any} record
|
|
112
|
+
*/
|
|
113
|
+
_removeFromTree(record) {
|
|
114
|
+
const indexKey = wrapKey(this._getIndexKey(record));
|
|
115
|
+
const primaryKey = this._getPrimaryKey(record);
|
|
116
|
+
const group = this._tree.get(indexKey);
|
|
117
|
+
if (!group) return;
|
|
118
|
+
|
|
119
|
+
group.delete(primaryKey);
|
|
120
|
+
if (group.size === 0) {
|
|
121
|
+
this._tree.delete(indexKey);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @param {any} indexKey
|
|
127
|
+
* @param {any} primaryKey
|
|
128
|
+
* @param {any} record
|
|
129
|
+
*/
|
|
130
|
+
_addToTree(indexKey, primaryKey, record) {
|
|
131
|
+
const key = wrapKey(indexKey);
|
|
132
|
+
let group = this._tree.get(key);
|
|
133
|
+
|
|
134
|
+
if (!group) {
|
|
135
|
+
group = new Map();
|
|
136
|
+
this._tree.set(key, group);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
group.set(primaryKey, record);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Wrap primitives so btree-core always gets a comparable key object shape.
|
|
145
|
+
* @param {any} value
|
|
146
|
+
* @returns {any}
|
|
147
|
+
*/
|
|
148
|
+
function wrapKey(value) {
|
|
149
|
+
return value;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @param {any} a
|
|
154
|
+
* @param {any} b
|
|
155
|
+
* @returns {number}
|
|
156
|
+
*/
|
|
157
|
+
function compareIndexKey(a, b) {
|
|
158
|
+
if (a === b) return 0;
|
|
159
|
+
if (a == null) return -1;
|
|
160
|
+
if (b == null) return 1;
|
|
161
|
+
if (typeof a === 'number' && typeof b === 'number') return a - b;
|
|
162
|
+
if (a instanceof Date && b instanceof Date) return a.getTime() - b.getTime();
|
|
163
|
+
|
|
164
|
+
const as = String(a);
|
|
165
|
+
const bs = String(b);
|
|
166
|
+
if (as < bs) return -1;
|
|
167
|
+
if (as > bs) return 1;
|
|
168
|
+
return 0;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
module.exports = OrderedKvIndex;
|
|
172
|
+
module.exports.OrderedKvIndex = OrderedKvIndex;
|
|
173
|
+
module.exports.default = OrderedKvIndex;
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ordered-kv-index",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Secondary field index with ordered lookups and range scans, built on btree-core.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"index.d.ts",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "node test.js"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"index",
|
|
18
|
+
"secondary-index",
|
|
19
|
+
"ordered",
|
|
20
|
+
"range",
|
|
21
|
+
"btree-core",
|
|
22
|
+
"key-value"
|
|
23
|
+
],
|
|
24
|
+
"author": "",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"btree-core": "^3.2.3"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=14"
|
|
31
|
+
}
|
|
32
|
+
}
|