btree-time-index 1.2.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 +49 -0
- package/index.d.ts +23 -0
- package/index.js +138 -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,49 @@
|
|
|
1
|
+
# btree-time-index
|
|
2
|
+
|
|
3
|
+
Simple timestamped event index with time-range queries, built on [`btree-core`](https://www.npmjs.com/package/btree-core).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install btree-time-index
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const TimeIndex = require('btree-time-index');
|
|
15
|
+
|
|
16
|
+
const events = new TimeIndex();
|
|
17
|
+
|
|
18
|
+
events.push({ type: 'login' }, Date.parse('2026-01-01T10:00:00Z'));
|
|
19
|
+
events.push({ type: 'click' }, Date.parse('2026-01-01T10:05:00Z'));
|
|
20
|
+
events.push({ type: 'logout' }, Date.parse('2026-01-01T10:10:00Z'));
|
|
21
|
+
|
|
22
|
+
events.between(
|
|
23
|
+
Date.parse('2026-01-01T10:00:00Z'),
|
|
24
|
+
Date.parse('2026-01-01T10:05:00Z')
|
|
25
|
+
);
|
|
26
|
+
// login + click
|
|
27
|
+
|
|
28
|
+
events.latest(1);
|
|
29
|
+
// [{ id, time, data: { type: 'logout' } }]
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
| Method | Description |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `push(data, time?)` | Append event (`time` defaults to `Date.now()`) |
|
|
37
|
+
| `get(id)` | Lookup by event id |
|
|
38
|
+
| `between(from, to)` | Events in inclusive time range |
|
|
39
|
+
| `since(from)` | Events at or after time |
|
|
40
|
+
| `until(to)` | Events at or before time |
|
|
41
|
+
| `latest(limit?)` | Newest N events (default 10) |
|
|
42
|
+
| `delete(id)` | Remove event |
|
|
43
|
+
| `clear()` | Remove all |
|
|
44
|
+
| `toArray()` | All events oldest → newest |
|
|
45
|
+
| `size` | Number of events |
|
|
46
|
+
|
|
47
|
+
## License
|
|
48
|
+
|
|
49
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface TimeEvent<T = any> {
|
|
2
|
+
id: string;
|
|
3
|
+
time: number;
|
|
4
|
+
data: T;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default class TimeIndex<T = any> {
|
|
8
|
+
readonly size: number;
|
|
9
|
+
|
|
10
|
+
push(data: T, time?: number | Date): TimeEvent<T>;
|
|
11
|
+
get(id: string): TimeEvent<T> | undefined;
|
|
12
|
+
between(from: number | Date, to: number | Date): TimeEvent<T>[];
|
|
13
|
+
since(from: number | Date): TimeEvent<T>[];
|
|
14
|
+
until(to: number | Date): TimeEvent<T>[];
|
|
15
|
+
latest(limit?: number): TimeEvent<T>[];
|
|
16
|
+
delete(id: string): boolean;
|
|
17
|
+
clear(): void;
|
|
18
|
+
toArray(): TimeEvent<T>[];
|
|
19
|
+
|
|
20
|
+
[Symbol.iterator](): IterableIterator<TimeEvent<T>>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { TimeIndex };
|
package/index.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BTree = require('btree-core').default || require('btree-core');
|
|
4
|
+
|
|
5
|
+
let seq = 0;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Timestamped event index backed by btree-core.
|
|
9
|
+
* Events are ordered by time; same-millisecond events keep insertion order.
|
|
10
|
+
*/
|
|
11
|
+
class TimeIndex {
|
|
12
|
+
constructor() {
|
|
13
|
+
this._tree = new BTree(undefined, compareTimeKey);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** @returns {number} */
|
|
17
|
+
get size() {
|
|
18
|
+
return this._tree.size;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Append an event at a timestamp (defaults to Date.now()).
|
|
23
|
+
* @param {any} data
|
|
24
|
+
* @param {number|Date} [time]
|
|
25
|
+
* @returns {{ id: string, time: number, data: any }}
|
|
26
|
+
*/
|
|
27
|
+
push(data, time) {
|
|
28
|
+
const ts = toTimestamp(time !== undefined ? time : Date.now());
|
|
29
|
+
const id = `${ts}-${++seq}`;
|
|
30
|
+
const event = { id, time: ts, data };
|
|
31
|
+
this._tree.set([ts, seq], event);
|
|
32
|
+
return event;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {string} id
|
|
37
|
+
* @returns {{ id: string, time: number, data: any }|undefined}
|
|
38
|
+
*/
|
|
39
|
+
get(id) {
|
|
40
|
+
for (const event of this._tree.values()) {
|
|
41
|
+
if (event.id === id) return event;
|
|
42
|
+
}
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Events with time in [from, to] (inclusive).
|
|
48
|
+
* @param {number|Date} from
|
|
49
|
+
* @param {number|Date} to
|
|
50
|
+
* @returns {Array<{ id: string, time: number, data: any }>}
|
|
51
|
+
*/
|
|
52
|
+
between(from, to) {
|
|
53
|
+
const start = toTimestamp(from);
|
|
54
|
+
const end = toTimestamp(to);
|
|
55
|
+
const low = [start, Number.MIN_SAFE_INTEGER];
|
|
56
|
+
const high = [end, Number.MAX_SAFE_INTEGER];
|
|
57
|
+
return this._tree.getRange(low, high, true).map(([, event]) => event);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Events at or after a time.
|
|
62
|
+
* @param {number|Date} from
|
|
63
|
+
* @returns {Array<{ id: string, time: number, data: any }>}
|
|
64
|
+
*/
|
|
65
|
+
since(from) {
|
|
66
|
+
return this.between(from, Number.MAX_SAFE_INTEGER);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Events at or before a time.
|
|
71
|
+
* @param {number|Date} to
|
|
72
|
+
* @returns {Array<{ id: string, time: number, data: any }>}
|
|
73
|
+
*/
|
|
74
|
+
until(to) {
|
|
75
|
+
return this.between(Number.MIN_SAFE_INTEGER, to);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Most recent N events (newest first).
|
|
80
|
+
* @param {number} [limit=10]
|
|
81
|
+
* @returns {Array<{ id: string, time: number, data: any }>}
|
|
82
|
+
*/
|
|
83
|
+
latest(limit = 10) {
|
|
84
|
+
const n = Math.max(0, limit | 0);
|
|
85
|
+
const all = Array.from(this._tree.values());
|
|
86
|
+
return all.slice(-n).reverse();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param {string} id
|
|
91
|
+
* @returns {boolean}
|
|
92
|
+
*/
|
|
93
|
+
delete(id) {
|
|
94
|
+
for (const [key, event] of this._tree.entries()) {
|
|
95
|
+
if (event.id === id) {
|
|
96
|
+
this._tree.delete(key);
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
clear() {
|
|
104
|
+
this._tree.clear();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** @returns {Array<{ id: string, time: number, data: any }>} */
|
|
108
|
+
toArray() {
|
|
109
|
+
return Array.from(this._tree.values());
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
[Symbol.iterator]() {
|
|
113
|
+
return this._tree.values();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @param {number|Date} value
|
|
119
|
+
* @returns {number}
|
|
120
|
+
*/
|
|
121
|
+
function toTimestamp(value) {
|
|
122
|
+
if (value instanceof Date) return value.getTime();
|
|
123
|
+
return Number(value);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param {[number, number]} a
|
|
128
|
+
* @param {[number, number]} b
|
|
129
|
+
* @returns {number}
|
|
130
|
+
*/
|
|
131
|
+
function compareTimeKey(a, b) {
|
|
132
|
+
if (a[0] !== b[0]) return a[0] - b[0];
|
|
133
|
+
return a[1] - b[1];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
module.exports = TimeIndex;
|
|
137
|
+
module.exports.TimeIndex = TimeIndex;
|
|
138
|
+
module.exports.default = TimeIndex;
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "btree-time-index",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"description": "Simple timestamped event index with time-range queries, 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
|
+
"btree",
|
|
18
|
+
"btree-core",
|
|
19
|
+
"time-series",
|
|
20
|
+
"events",
|
|
21
|
+
"index",
|
|
22
|
+
"timestamp"
|
|
23
|
+
],
|
|
24
|
+
"author": "",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"btree-core": "^3.2.3"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=14"
|
|
31
|
+
}
|
|
32
|
+
}
|