Skip to content

Commit ab65888

Browse files
committed
WIP
1 parent 8e0d789 commit ab65888

File tree

6 files changed

+185
-142
lines changed

6 files changed

+185
-142
lines changed

src/DB.ts

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { AbstractBatch, AbstractIteratorOptions } from 'abstract-leveldown';
1+
import type {
2+
AbstractBatch,
3+
AbstractIteratorOptions,
4+
} from 'abstract-leveldown';
25
import type { LevelDB } from 'level';
36
import type { ResourceAcquire } from '@matrixai/resources';
47
import type {
@@ -209,14 +212,8 @@ class DB {
209212
* Get from root level
210213
* @internal
211214
*/
212-
public async _get<T>(
213-
keyPath: KeyPath,
214-
raw?: false,
215-
): Promise<T | undefined>;
216-
public async _get(
217-
keyPath: KeyPath,
218-
raw: true,
219-
): Promise<Buffer | undefined>;
215+
public async _get<T>(keyPath: KeyPath, raw?: false): Promise<T | undefined>;
216+
public async _get(keyPath: KeyPath, raw: true): Promise<Buffer | undefined>;
220217
public async _get<T>(
221218
keyPath: KeyPath,
222219
raw: boolean = false,
@@ -268,16 +265,8 @@ class DB {
268265
* Put from root level
269266
* @internal
270267
*/
271-
public async _put(
272-
keyPath: KeyPath,
273-
value: any,
274-
raw?: false,
275-
): Promise<void>;
276-
public async _put(
277-
keyPath: KeyPath,
278-
value: Buffer,
279-
raw: true,
280-
): Promise<void>;
268+
public async _put(keyPath: KeyPath, value: any, raw?: false): Promise<void>;
269+
public async _put(keyPath: KeyPath, value: Buffer, raw: true): Promise<void>;
281270
public async _put(
282271
keyPath: KeyPath,
283272
value: any,
@@ -443,13 +432,15 @@ class DB {
443432
if (options.gt != null) {
444433
options.gt = Buffer.concat([
445434
levelKeyStart,
446-
(typeof options.gt === 'string') ? Buffer.from(options.gt) : options.gt
435+
typeof options.gt === 'string' ? Buffer.from(options.gt) : options.gt,
447436
]);
448437
}
449438
if (options.gte != null) {
450439
options.gte = Buffer.concat([
451440
levelKeyStart,
452-
(typeof options.gte === 'string') ? Buffer.from(options.gte) : options.gte
441+
typeof options.gte === 'string'
442+
? Buffer.from(options.gte)
443+
: options.gte,
453444
]);
454445
}
455446
if (options.gt == null && options.gte == null) {
@@ -458,13 +449,15 @@ class DB {
458449
if (options?.lt != null) {
459450
options.lt = Buffer.concat([
460451
levelKeyStart,
461-
(typeof options.lt === 'string') ? Buffer.from(options.lt) : options.lt
452+
typeof options.lt === 'string' ? Buffer.from(options.lt) : options.lt,
462453
]);
463454
}
464455
if (options?.lte != null) {
465456
options.lte = Buffer.concat([
466457
levelKeyStart,
467-
(typeof options.lte === 'string') ? Buffer.from(options.lte) : options.lte
458+
typeof options.lte === 'string'
459+
? Buffer.from(options.lte)
460+
: options.lte,
468461
]);
469462
}
470463
if (options.lt == null && options.lte == null) {
@@ -477,11 +470,7 @@ class DB {
477470
const next = iterator.next.bind(iterator);
478471
// @ts-ignore AbstractIterator type is outdated
479472
iterator.seek = (k: Buffer | string): void => {
480-
seek(
481-
utils.keyPathToKey(
482-
[...levelPath, k] as unknown as KeyPath
483-
)
484-
);
473+
seek(utils.keyPathToKey([...levelPath, k] as unknown as KeyPath));
485474
};
486475
// @ts-ignore AbstractIterator type is outdated
487476
iterator.next = async () => {
@@ -492,9 +481,7 @@ class DB {
492481
if (kv[0] != null) {
493482
// Truncate level path so the returned key is relative to the level path
494483
const keyPath = utils.parseKey(kv[0]).slice(levelPath.length);
495-
kv[0] = utils.keyPathToKey(
496-
keyPath as unknown as KeyPath
497-
);
484+
kv[0] = utils.keyPathToKey(keyPath as unknown as KeyPath);
498485
}
499486
// Handle values: false
500487
if (kv[1] != null) {
@@ -511,9 +498,7 @@ class DB {
511498
* This is not atomic, it will iterate over a snapshot of the DB
512499
*/
513500
@ready(new errors.ErrorDBNotRunning())
514-
public async clear(
515-
levelPath: LevelPath = [],
516-
): Promise<void> {
501+
public async clear(levelPath: LevelPath = []): Promise<void> {
517502
levelPath = ['data', ...levelPath];
518503
if (utils.checkSepLevelPath(levelPath)) {
519504
throw new errors.ErrorDBLevelSep();
@@ -525,10 +510,7 @@ class DB {
525510
* Clear from root level
526511
* @internal
527512
*/
528-
public async _clear(
529-
db: LevelDB,
530-
levelPath: LevelPath = [],
531-
): Promise<void> {
513+
public async _clear(db: LevelDB, levelPath: LevelPath = []): Promise<void> {
532514
for await (const [k] of this._iterator(db, { values: false }, levelPath)) {
533515
await db.del(utils.keyPathToKey([...levelPath, k] as unknown as KeyPath));
534516
}
@@ -547,10 +529,19 @@ class DB {
547529
* Dump from root level
548530
* It is intended for diagnostics
549531
*/
550-
public async dump(levelPath?: LevelPath, raw?: false): Promise<Array<[string, any]>>;
551-
public async dump(levelPath: LevelPath | undefined, raw: true): Promise<Array<[Buffer, Buffer]>>;
532+
public async dump(
533+
levelPath?: LevelPath,
534+
raw?: false,
535+
): Promise<Array<[string, any]>>;
536+
public async dump(
537+
levelPath: LevelPath | undefined,
538+
raw: true,
539+
): Promise<Array<[Buffer, Buffer]>>;
552540
@ready(new errors.ErrorDBNotRunning())
553-
public async dump(levelPath: LevelPath = [], raw: boolean = false): Promise<Array<[string | Buffer, any]>> {
541+
public async dump(
542+
levelPath: LevelPath = [],
543+
raw: boolean = false,
544+
): Promise<Array<[string | Buffer, any]>> {
554545
if (utils.checkSepLevelPath(levelPath)) {
555546
throw new errors.ErrorDBLevelSep();
556547
}

src/DBTransaction.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class DBTransaction {
117117
}
118118
let value = await this.db._get<T>(
119119
[...this.transactionPath, ...keyPath] as unknown as KeyPath,
120-
raw as any
120+
raw as any,
121121
);
122122
if (value === undefined) {
123123
value = await this.db.get<T>(keyPath, raw as any);
@@ -152,7 +152,7 @@ class DBTransaction {
152152
await this.db._put(
153153
[...this.transactionPath, ...keyPath] as unknown as KeyPath,
154154
value,
155-
raw as any
155+
raw as any,
156156
);
157157
this._ops.push({
158158
type: 'put',
@@ -170,10 +170,13 @@ class DBTransaction {
170170
if (utils.checkSepKeyPath(keyPath as KeyPath)) {
171171
throw new errors.ErrorDBLevelSep();
172172
}
173-
await this.db._del([...this.transactionPath, ...keyPath] as unknown as KeyPath);
173+
await this.db._del([
174+
...this.transactionPath,
175+
...keyPath,
176+
] as unknown as KeyPath);
174177
this._ops.push({
175178
type: 'del',
176-
keyPath
179+
keyPath,
177180
});
178181
}
179182

@@ -196,17 +199,17 @@ class DBTransaction {
196199
...options,
197200
keys: true,
198201
keyAsBuffer: true,
199-
valueAsBuffer: true
202+
valueAsBuffer: true,
200203
},
201-
['data', ...levelPath]
204+
['data', ...levelPath],
202205
);
203206
const tranIterator = this.db._iterator(
204207
this.db.db,
205208
{
206209
...options,
207210
keys: true,
208211
keyAsBuffer: true,
209-
valueAsBuffer: true
212+
valueAsBuffer: true,
210213
},
211214
[...this.transactionPath, ...levelPath],
212215
);
@@ -307,9 +310,7 @@ class DBTransaction {
307310
}
308311

309312
@ready(new errors.ErrorDBTransactionDestroyed())
310-
public async clear(
311-
levelPath: LevelPath = [],
312-
): Promise<void> {
313+
public async clear(levelPath: LevelPath = []): Promise<void> {
313314
for await (const [k] of await this.iterator({ values: false }, levelPath)) {
314315
await this.del([...levelPath, k] as unknown as KeyPath);
315316
}
@@ -328,14 +329,23 @@ class DBTransaction {
328329
* Dump from transaction level
329330
* It is intended for diagnostics
330331
*/
331-
public async dump(levelPath?: LevelPath, raw?: false): Promise<Array<[string, any]>>;
332-
public async dump(levelPath: LevelPath | undefined, raw: true): Promise<Array<[Buffer, Buffer]>>;
332+
public async dump(
333+
levelPath?: LevelPath,
334+
raw?: false,
335+
): Promise<Array<[string, any]>>;
336+
public async dump(
337+
levelPath: LevelPath | undefined,
338+
raw: true,
339+
): Promise<Array<[Buffer, Buffer]>>;
333340
@ready(new errors.ErrorDBTransactionDestroyed())
334-
public async dump(levelPath: LevelPath = [], raw: boolean = false): Promise<Array<[string | Buffer, any]>> {
335-
return await this.db.dump([
336-
...this.transactionPath,
337-
...levelPath
338-
], raw as any);
341+
public async dump(
342+
levelPath: LevelPath = [],
343+
raw: boolean = false,
344+
): Promise<Array<[string | Buffer, any]>> {
345+
return await this.db.dump(
346+
[...this.transactionPath, ...levelPath],
347+
raw as any,
348+
);
339349
}
340350

341351
@ready(new errors.ErrorDBTransactionDestroyed())

src/utils.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function keyPathToKey(keyPath: KeyPath): Buffer {
2020
const levelPath = keyPath.slice(0, -1);
2121
return Buffer.concat([
2222
levelPathToKey(levelPath),
23-
(typeof keyPart === 'string') ? Buffer.from(keyPart, 'utf-8') : keyPart
23+
typeof keyPart === 'string' ? Buffer.from(keyPart, 'utf-8') : keyPart,
2424
]);
2525
}
2626

@@ -31,11 +31,13 @@ function keyPathToKey(keyPath: KeyPath): Buffer {
3131
*/
3232
function levelPathToKey(levelPath: LevelPath): Buffer {
3333
return Buffer.concat(
34-
levelPath.map((p) => Buffer.concat([
35-
sep,
36-
(typeof p === 'string') ? Buffer.from(p, 'utf-8') : p,
37-
sep
38-
]))
34+
levelPath.map((p) =>
35+
Buffer.concat([
36+
sep,
37+
typeof p === 'string' ? Buffer.from(p, 'utf-8') : p,
38+
sep,
39+
]),
40+
),
3941
);
4042
}
4143

@@ -53,7 +55,7 @@ function levelPathToKey(levelPath: LevelPath): Buffer {
5355
*/
5456
function parseKey(key: Buffer): KeyPath {
5557
const [bufs] = parsePath(key);
56-
if (!isNonEmptyArray(bufs)) {
58+
if (!isNonEmptyArray(bufs)) {
5759
throw new TypeError('Buffer is not a key');
5860
}
5961
return bufs;
@@ -73,13 +75,16 @@ function parsePath(input: Buffer): [Array<Buffer>, Buffer] {
7375
let output: Array<Buffer> = [];
7476
let input_: Buffer = input;
7577
let output_: Array<Buffer>;
78+
// eslint-disable-next-line prefer-const
7679
[output_, input_] = parseKeyActual(input_);
7780
output = output.concat(output_);
7881
return [output, input_];
7982
}
80-
};
83+
}
8184

82-
function parseLevels(input: Buffer): [output: Array<Buffer>, remaining: Buffer] {
85+
function parseLevels(
86+
input: Buffer,
87+
): [output: Array<Buffer>, remaining: Buffer] {
8388
let output: Array<Buffer> = [];
8489
try {
8590
let input_: Buffer = input;
@@ -93,7 +98,7 @@ function parseLevels(input: Buffer): [output: Array<Buffer>, remaining: Buffer]
9398
} catch (e) {
9499
return [[], input];
95100
}
96-
};
101+
}
97102

98103
function parseLevel(input: Buffer): [Array<Buffer>, Buffer] {
99104
const sepStart = input.indexOf(sep);
@@ -108,14 +113,14 @@ function parseLevel(input: Buffer): [Array<Buffer>, Buffer] {
108113
const remaining = input.subarray(sepEnd + 1);
109114

110115
return [[level], remaining];
111-
};
116+
}
112117

113118
function parseKeyActual(input: Buffer): [Array<Buffer>, Buffer] {
114119
if (input.byteLength < 1) {
115120
throw new errors.ErrorDBParseKey('Key cannot be empty');
116121
}
117122
return [[input], input.subarray(input.byteLength)];
118-
};
123+
}
119124

120125
/**
121126
* Checks if the KeyPath contains the separator
@@ -137,7 +142,7 @@ function checkSepLevelPath(levelPath: LevelPath): boolean {
137142
* Checks if the separator exists in a string or buffer
138143
* This only needs to applied to the LevelPath, not the final key
139144
*/
140-
function sepExists (data: string | Buffer): boolean {
145+
function sepExists(data: string | Buffer): boolean {
141146
if (typeof data === 'string') {
142147
return data.includes(sep.toString('utf-8'));
143148
} else {

0 commit comments

Comments
 (0)