Add drop() to BaseDb

This commit is contained in:
Jaco Greeff
2018-09-07 09:29:01 +02:00
parent 5746d1eba9
commit 61357fb289
7 changed files with 46 additions and 15 deletions
+17 -5
View File
@@ -51,6 +51,8 @@ export default class FileFlatDb implements BaseDb {
}
open (): void {
this.assertOpen(false);
this._fd = this._open(this._path);
this._fileSize = fs.fstatSync(this._fd).size;
@@ -67,8 +69,14 @@ export default class FileFlatDb implements BaseDb {
this._lruData.clear();
}
drop (): void {
this.assertOpen(false);
fs.unlinkSync(this._path);
}
empty (): void {
this.close();
this.assertOpen(false);
this._fd = this._open(this._path, true);
this._fileSize = fs.fstatSync(this._fd).size;
@@ -79,7 +87,7 @@ export default class FileFlatDb implements BaseDb {
}
rename (base: string, file: string): void {
assert(this._fd === -1, 'Database cannot be open for renaming');
this.assertOpen(false);
const oldPath = this._path;
@@ -91,7 +99,7 @@ export default class FileFlatDb implements BaseDb {
}
maintain (fn: ProgressCb): void {
assert(this._fd === -1, 'Database cannot be open for compacting');
this.assertOpen(false);
const compactor = new Compact(this._file);
@@ -457,8 +465,12 @@ export default class FileFlatDb implements BaseDb {
return this._writeNewBuffer(Buffer.concat(buffers), false);
}
private assertOpen (): void {
assert(this._fd !== -1, 'Expected an open database');
private assertOpen (open: boolean = true): void {
const test = open
? this._fd !== -1
: this._fd === -1;
assert(test, `Expected ${open ? 'an open' : 'a closed'} database`);
}
private _open (file: string, startEmpty: boolean = false): number {
+4
View File
@@ -50,6 +50,10 @@ export default class FileTreeDb implements BaseDb {
// noop
}
drop (): void {
l.error('drop() is not implemented');
}
empty (): void {
l.error('empty() is not implemented');
}
+4
View File
@@ -38,6 +38,10 @@ export default class LruDb implements BaseDb {
this.backing.open();
}
drop (): void {
this.backing.drop();
}
empty (): void {
l.debug(() => ['empty']);
+4
View File
@@ -27,6 +27,10 @@ export default class MemoryDb implements BaseDb {
this.empty();
}
drop (): void {
this.empty();
}
empty (): void {
this.storage = {};
}
+4
View File
@@ -58,6 +58,10 @@ export default class TransactionDb implements TxDb {
this.backing.open();
}
drop (): void {
this.backing.drop();
}
empty (): void {
this.backing.empty();
}
+1
View File
@@ -17,6 +17,7 @@ export type BaseDbOptions = {
export interface BaseDb {
close (): void;
open (): void;
drop (): void;
empty (): void;
maintain (fn: ProgressCb): void;
rename (base: string, file: string): void;
+12 -10
View File
@@ -79,6 +79,10 @@ export default class Trie implements TrieDb {
this.db.empty();
}
drop (): void {
this.db.drop();
}
maintain (fn: ProgressCb): void {
this.db.maintain(fn);
}
@@ -176,20 +180,18 @@ export default class Trie implements TrieDb {
return keys;
}
const filtered = node.filter((node) =>
node && node.length === 32
) as Array<Uint8Array>;
filtered.forEach((root) => {
keys = this._snapshot(dest, fn, root, keys, percent, depth + 1);
percent += (100 / filtered.length) / Math.pow(filtered.length, depth);
});
keys++;
dest.db.put(root, encodeNode(node));
fn({ keys, percent });
node.forEach((u8a) => {
if (u8a && u8a.length === 32) {
keys = this._snapshot(dest, fn, u8a, keys, percent, depth + 1);
}
percent += (100 / node.length) / Math.pow(100, depth);
});
return keys;
}