This repository has been archived by the owner on Jun 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
index.ts
102 lines (84 loc) · 2.57 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
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
import { AbstractBatch } from 'abstract-leveldown';
import EncodingDown from 'encoding-down';
// tslint:disable-next-line match-default-export-name
import levelup, { LevelUp } from 'levelup';
import RocksDB from 'rocksdb';
import { debug } from 'debug';
const logger = debug('db');
const delimitor = ':';
export interface BatchCommand {
readonly type: 'put' | 'del';
readonly bucket: string;
readonly key: string | number;
// tslint:disable-next-line no-any
readonly value?: any;
}
export interface ReadStreamOption {
// tslint:disable-next-line no-any
readonly gt?: any;
// tslint:disable-next-line no-any
readonly gte?: any;
// tslint:disable-next-line no-any
readonly lt?: any;
// tslint:disable-next-line no-any
readonly lte?: any;
readonly reverse?: boolean;
readonly limit?: number;
readonly keys?: boolean;
readonly values?: boolean;
}
export class DB {
private readonly _db: LevelUp<RocksDB>;
public constructor(file: string) {
logger('opening file', { file });
this._db = levelup(EncodingDown(RocksDB(file), { valueEncoding: 'json' }));
}
public async close(): Promise<void> {
return this._db.close();
}
// tslint:disable-next-line no-any
public async get(bucket: string, key: string | number): Promise<any> {
const fullKey = `${bucket}${delimitor}${key}`;
logger('get', { key: fullKey });
return this._db.get(fullKey);
}
// tslint:disable-next-line no-any
public async exists(bucket: string, key: string | number): Promise<boolean> {
const fullKey = `${bucket}${delimitor}${key}`;
try {
logger('exists', { key: fullKey });
await this._db.get(fullKey);
return true;
} catch (error) {
if (error.notFound) {
return false;
}
throw error;
}
}
// tslint:disable-next-line no-any
public async put(bucket: string, key: string, val: any): Promise<void> {
const fullKey = `${bucket}${delimitor}${key}`;
logger('put', { key: fullKey });
return this._db.put(fullKey, val);
}
public async del(bucket: string, key: string): Promise<void> {
const fullKey = `${bucket}${delimitor}${key}`;
logger('del', { key: fullKey });
return this._db.del(fullKey);
}
public createReadStream(options?: ReadStreamOption): NodeJS.ReadableStream {
logger('readStream', { options });
return this._db.createReadStream(options);
}
public async batch(tasks: ReadonlyArray<BatchCommand>): Promise<void> {
const execTasks = tasks.map(t => ({
type: t.type,
key: `${t.bucket}${delimitor}${
typeof t.key === 'string' ? t.key : t.key.toString()
}`,
value: t.value,
}));
return this._db.batch(execTasks as AbstractBatch[]);
}
}