-
Notifications
You must be signed in to change notification settings - Fork 16
/
memory.ts
51 lines (39 loc) · 1.19 KB
/
memory.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
import { Key } from 'interface-datastore/key'
import { BaseDatastore } from './base.js'
import * as Errors from './errors.js'
import type { Pair } from 'interface-datastore'
import type { Await, AwaitIterable } from 'interface-store'
export class MemoryDatastore extends BaseDatastore {
private readonly data: Map<string, Uint8Array>
constructor () {
super()
this.data = new Map()
}
put (key: Key, val: Uint8Array): Await<Key> { // eslint-disable-line require-await
this.data.set(key.toString(), val)
return key
}
get (key: Key): Await<Uint8Array> {
const result = this.data.get(key.toString())
if (result == null) {
throw Errors.notFoundError()
}
return result
}
has (key: Key): Await<boolean> { // eslint-disable-line require-await
return this.data.has(key.toString())
}
delete (key: Key): Await<void> { // eslint-disable-line require-await
this.data.delete(key.toString())
}
* _all (): AwaitIterable<Pair> {
for (const [key, value] of this.data.entries()) {
yield { key: new Key(key), value }
}
}
* _allKeys (): AwaitIterable<Key> {
for (const key of this.data.keys()) {
yield new Key(key)
}
}
}