-
Notifications
You must be signed in to change notification settings - Fork 4
/
flash-store.ts
251 lines (216 loc) · 6.3 KB
/
flash-store.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import * as path from 'path'
import {
path as appRoot,
} from 'app-root-path'
import rimraf from 'rimraf'
import { SnapDB } from 'snap-db'
import {
log,
VERSION,
} from './config'
import {
AsyncMap,
} from './async-map'
export interface IteratorOptions<K> {
gt? : K
gte? : K
lt? : K
lte? : K
reverse? : boolean
keys? : boolean
values? : boolean
limit? : number
prefix? : K
}
export class FlashStore<K = string, V = any> implements AsyncMap<K, V> {
private snapDb: SnapDB<K>
/**
* FlashStore is a Key-Value database tool and makes using leveldb more easy for Node.js
*
* Creates an instance of FlashStore.
* @param {string} [workdir=path.join(appRoot, 'flash-store.workdir')]
* @example
* import { FlashStore } from 'flash-store'
* const flashStore = new FlashStore('flashstore.workdir')
*/
constructor (
public workdir = path.join(appRoot, '.flash-store'),
) {
log.verbose('FlashStore', 'constructor(%s)', workdir)
// we use seperate workdir for snapdb, leveldb, and rocksdb etc.
const snapdbWorkdir = path.join(workdir, 'snapdb')
this.snapDb = new SnapDB({
dir: snapdbWorkdir, // database folder
key: 'string', // key type, can be "int", "string" or "float"
})
}
public version (): string {
return VERSION
}
/**
* Set data in database
*
* @param {K} key
* @param {V} value
* @returns {Promise<void>}
* @example
* await flashStore.set(1, 1)
*/
public async set (key: K, value: V): Promise<void> {
log.verbose('FlashStore', 'set(%s, %s) value type: %s', key, value, typeof value)
// FIXME(huan): string for SnapDB only
if (typeof key !== 'string') {
throw new Error('only support string as key')
}
await this.snapDb.put(key, JSON.stringify(value))
}
/**
* Get value from database by key
*
* @param {K} key
* @returns {(Promise<V | null>)}
* @example
* console.log(await flashStore.get(1))
*/
public async get (key: K): Promise<V | undefined> {
log.verbose('FlashStore', 'get(%s)', key)
try {
// FIXME(huan): string for SnapDB only
if (typeof key !== 'string') {
throw new Error('only support string as key')
}
const val = await this.snapDb.get(key)
return val && JSON.parse(val)
} catch (e) {
if (/^NotFoundError/.test(e)) {
// The leveldb will throw NotFoundError for non-exist keys
return undefined
}
throw e
}
}
/**
* Del data by key
*
* @param {K} key
* @returns {Promise<void>}
* @example
* await flashStore.delete(1)
*/
public async delete (key: K): Promise<void> {
log.verbose('FlashStore', 'delete(%s)', key)
// FIXME(huan): string for SnapDB only
if (typeof key !== 'string') {
throw new Error('only support string as key')
}
await this.snapDb.delete(key)
}
/**
* @typedef IteratorOptions
*
* @property { K } gt - Matches values that are greater than a specified value
* @property { K } gte - Matches values that are greater than or equal to a specified value.
* @property { K } lt - Matches values that are less than a specified value.
* @property { K } lte - Matches values that are less than or equal to a specified value.
* @property { boolean } reverse - Reverse the result set
* @property { number } limit - Limits the number in the result set.
* @property { K } prefix - Make the same prefix key get together.
*/
/**
* Find keys by IteratorOptions
*
* @param {IteratorOptions} [options={}]
* @returns {AsyncIterableIterator<K>}
* @example
* const flashStore = new FlashStore('flashstore.workdir')
* for await(const key of flashStore.keys({gte: 1})) {
* console.log(key)
* }
*/
public async * keys (options: IteratorOptions<K> = {}): AsyncIterableIterator<K> {
log.verbose('FlashStore', 'keys()')
const keysOptions = {
...options,
values: false, // do not include values
}
for await (const [key] of this.entries(keysOptions)) {
yield key
}
}
/**
* Find all values
*
* @returns {AsyncIterableIterator<V>}
* @example
* const flashStore = new FlashStore('flashstore.workdir')
* for await(const value of flashStore.values()) {
* console.log(value)
* }
*/
public async * values (options: IteratorOptions<K> = {}): AsyncIterableIterator<V> {
log.verbose('FlashStore', 'values()')
const valuesOptions = {
...options,
keys: false, // do not include the keys
}
for await (const [, value] of this.entries(valuesOptions)) {
yield value
}
}
/**
* Get the size of the database
* @returns {Promise<number>}
* @example
* const size = await flashStore.size
* console.log(`database size: ${size}`)
*/
public get size (): Promise<number> {
log.verbose('FlashStore', 'size()')
return this.snapDb.getCount()
}
/**
* FIXME(huan): use better way to do this
*/
public async has (key: K): Promise<boolean> {
const val = await this.get(key)
return !!val
}
/**
*
*/
public async clear (): Promise<void> {
log.verbose('FlashStore', 'clear()')
await this.snapDb.empty()
}
/**
* @private
*/
public async * entries (options?: IteratorOptions<K>): AsyncIterableIterator<[K, V]> {
log.verbose('FlashStore', '*entries(%s)', JSON.stringify(options))
const iterator = await this.snapDb.queryIt(options || {})
for await (const [key, val] of iterator) {
const valObj = val === undefined ? undefined : JSON.parse(val)
// FIXME(huan): key has to be string for SnapDB
yield [key as any, valObj]
}
}
public async * [Symbol.asyncIterator] (): AsyncIterableIterator<[K, V]> {
log.verbose('FlashStore', '*[Symbol.asyncIterator]()')
yield * this.entries()
}
public async close (): Promise<void> {
log.verbose('FlashStore', 'close()')
await this.snapDb.close()
}
/**
* Destroy the database
*
* @returns {Promise<void>}
*/
public async destroy (): Promise<void> {
log.verbose('FlashStore', 'destroy()')
await this.snapDb.close()
await new Promise(resolve => rimraf(this.workdir, resolve))
}
}
export default FlashStore