-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
60 lines (56 loc) · 1.18 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// @ts-ignore
import { RootDatabase, clearKeptObjects, open } from "lmdb"
const MAXIMUM_KEY = 9007199254740991
export class LMDBArray {
_nextKey: number
db: RootDatabase
length: number
constructor() {
this.db = open({
compression: true,
cache: true,
noMemInit: true,
useWritemap: true,
noSync: true,
})
this.length = 0
this._nextKey = 0
}
getElementAtIndex(index: number) {
return this.db.get(index)
}
push(element: any) {
const index = this._nextKey
this.db.put(index, element)
this._nextKey++
this.length++
return this.length
}
pop(): any {
const index: number = this._nextKey - 1
const item = this.db.get(index)
this.db.remove(index)
this._nextKey--
this.length--
return item
}
deleteAt(index: number) {
const exists = this.db.get(index)
this.db.remove(index)
if (exists) {
this.length--
}
return this
}
insertAt(item: any, index: number) {
const exists = this.db.get(index)
this.db.put(index, item)
if (!exists) {
this.length++
}
return this
}
toJSON() {
return this.db.getRange({ end: MAXIMUM_KEY }).asArray
}
}