-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseStorage.ts
159 lines (130 loc) · 4.26 KB
/
baseStorage.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
Level,
IteratorOptions,
KeyIteratorOptions,
ValueIteratorOptions,
} from 'level'
import { AbstractLevel, AbstractChainedBatch } from 'abstract-level'
import { StorageEntry } from './types.js'
// @todo: define an abstract interface
export type StorageCommonOptions = { sublevel?: string; debug?: boolean }
export type StorageGetOptions<K, V> = IteratorOptions<K, V> &
StorageCommonOptions
export type StoragePutOptions<K, V> = StorageCommonOptions & {
batch?: AbstractChainedBatch<StorageLevel<K, V>, K, V>
}
export type StorageBatch<K, V> = AbstractChainedBatch<Level<K, V>, K, V>
export type StorageIteratorOptions<K, V> = IteratorOptions<K, V> &
StorageCommonOptions
export type StorageKeyIteratorOptions<K> = KeyIteratorOptions<K> &
StorageCommonOptions
export type StorageValueIteratorOptions<K, V> = ValueIteratorOptions<K, V> &
StorageCommonOptions
export type StorageLevel<K, V> = AbstractLevel<
string | Buffer | Uint8Array,
K,
V
>
export abstract class BaseStorage<K extends string, V> {
static defaultOpts = { keyEncoding: 'utf8', valueEncoding: 'json' }
protected sublevels: Record<string, StorageLevel<K, V>> = {}
constructor(
protected path: string,
protected db: Level<K, V> = new Level(path, BaseStorage.defaultOpts),
) {
process.on('beforeExit', this.close.bind(this))
}
close(): Promise<void> {
return this.db.close()
}
protected getDb(sublevel?: string): StorageLevel<K, V> {
if (!sublevel) return this.db
let db = this.sublevels[sublevel]
if (!db) {
db = this.db.sublevel<K, V>(sublevel, BaseStorage.defaultOpts)
this.sublevels[sublevel] = db
}
return db
}
async get(key: K, options?: StorageCommonOptions): Promise<V | undefined> {
try {
const db = this.getDb(options?.sublevel)
return await db.get(key)
} catch (e) {
const isNotFound = (e as any)?.code === 'LEVEL_NOT_FOUND'
if (!isNotFound) throw e
}
}
async getMany(keys: K[], options?: StorageCommonOptions): Promise<V[]> {
const db = this.getDb(options?.sublevel)
return await db.getMany(keys)
}
async exists(key: K, options?: StorageCommonOptions): Promise<boolean> {
return (await this.get(key, options)) !== undefined
}
async put(key: K, value: V, options?: StorageCommonOptions): Promise<void> {
const db = this.getDb(options?.sublevel)
return db.put(key, value)
}
async del(key: K, options?: StorageCommonOptions): Promise<void> {
const db = this.getDb(options?.sublevel)
return db.del(key)
}
async clear(options?: StorageCommonOptions): Promise<void> {
const db = this.getDb(options?.sublevel)
return db.clear()
}
async getLastEntry(): Promise<StorageEntry<K, V> | undefined> {
return this.findBoundingEntry({ reverse: true })
}
async getFirstEntry(): Promise<StorageEntry<K, V> | undefined> {
return this.findBoundingEntry({ reverse: false })
}
async getLastKey(): Promise<K | undefined> {
return this.findBoundingKey({ reverse: true })
}
async getFirstKey(): Promise<K | undefined> {
return this.findBoundingKey({ reverse: false })
}
async getLastValue(): Promise<V | undefined> {
return this.findBoundingValue({ reverse: true })
}
async getFirstValue(): Promise<V | undefined> {
return this.findBoundingValue({ reverse: false })
}
async findBoundingEntry(
options?: StorageIteratorOptions<K, V>,
): Promise<StorageEntry<K, V> | undefined> {
const db = this.getDb(options?.sublevel)
const items = db.iterator({
...options,
limit: 1,
keys: true,
values: true,
})
for await (const item of items) {
if (item) {
const [key, value] = item
return { key, value }
}
}
}
async findBoundingKey(
options?: StorageKeyIteratorOptions<K>,
): Promise<K | undefined> {
const db = this.getDb(options?.sublevel)
const items = db.keys({ ...options, limit: 1 })
for await (const item of items) {
if (item) return item as K
}
}
async findBoundingValue(
options?: StorageValueIteratorOptions<K, V>,
): Promise<V | undefined> {
const db = this.getDb(options?.sublevel)
const items = db.values({ ...options, limit: 1 })
for await (const item of items) {
if (item) return item as V
}
}
}