Export rename(base, file) on BaseDb
This commit is contained in:
@@ -29,6 +29,8 @@ const l = logger('db/flat');
|
||||
export default class FileFlatDb implements BaseDb {
|
||||
private _fd: number;
|
||||
private _fileSize: number = 0;
|
||||
private _base: string;
|
||||
private _path: string;
|
||||
private _file: string;
|
||||
private _isCompressed: boolean;
|
||||
private _lruBranch: LRUMap<number, Buffer>;
|
||||
@@ -36,7 +38,9 @@ export default class FileFlatDb implements BaseDb {
|
||||
|
||||
constructor (base: string, file: string = defaults.DEFAULT_FILE, options: BaseDbOptions = {}) {
|
||||
this._fd = -1;
|
||||
this._file = path.join(base, file);
|
||||
this._base = base;
|
||||
this._file = file;
|
||||
this._path = path.join(base, file);
|
||||
this._lruBranch = new LRUMap(LRU_BRANCH_COUNT);
|
||||
this._lruData = new LRUMap(LRU_DATA_COUNT);
|
||||
this._isCompressed = options && !isUndefined(options.isCompressed)
|
||||
@@ -47,7 +51,7 @@ export default class FileFlatDb implements BaseDb {
|
||||
}
|
||||
|
||||
open (): void {
|
||||
this._fd = this._open(this._file);
|
||||
this._fd = this._open(this._path);
|
||||
this._fileSize = fs.fstatSync(this._fd).size;
|
||||
|
||||
this._lruBranch.clear();
|
||||
@@ -66,10 +70,22 @@ export default class FileFlatDb implements BaseDb {
|
||||
empty (): void {
|
||||
this.close();
|
||||
|
||||
this._fd = this._open(this._file, true);
|
||||
this._fd = this._open(this._path, true);
|
||||
this._fileSize = fs.fstatSync(this._fd).size;
|
||||
}
|
||||
|
||||
rename (base: string, file: string): void {
|
||||
assert(this._fd === -1, 'Database cannot be open for renaming');
|
||||
|
||||
const oldPath = this._path;
|
||||
|
||||
this._base = base;
|
||||
this._file = file;
|
||||
this._path = path.join(base, file);
|
||||
|
||||
fs.renameSync(oldPath, this._path);
|
||||
}
|
||||
|
||||
maintain (fn: ProgressCb): void {
|
||||
assert(this._fd === -1, 'Database cannot be open for compacting');
|
||||
|
||||
|
||||
@@ -51,7 +51,11 @@ export default class FileTreeDb implements BaseDb {
|
||||
}
|
||||
|
||||
empty (): void {
|
||||
throw new Error('error is not implemented');
|
||||
l.error('empty() is not implemented');
|
||||
}
|
||||
|
||||
rename (base: string, file: string): void {
|
||||
l.error('rename() is not implemented');
|
||||
}
|
||||
|
||||
maintain (fn: ProgressCb): void {
|
||||
|
||||
@@ -45,6 +45,10 @@ export default class LruDb implements BaseDb {
|
||||
this.backing.empty();
|
||||
}
|
||||
|
||||
rename (base: string, file: string): void {
|
||||
this.backing.rename(base, file);
|
||||
}
|
||||
|
||||
maintain (fn: ProgressCb): void {
|
||||
this.backing.maintain(fn);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,10 @@ export default class MemoryDb implements BaseDb {
|
||||
this.storage = {};
|
||||
}
|
||||
|
||||
rename (base: string, file: string): void {
|
||||
l.error('rename() is not implemented');
|
||||
}
|
||||
|
||||
maintain (fn: ProgressCb): void {
|
||||
fn({
|
||||
isCompleted: true,
|
||||
|
||||
@@ -62,6 +62,10 @@ export default class TransactionDb implements TxDb {
|
||||
this.backing.empty();
|
||||
}
|
||||
|
||||
rename (base: string, file: string): void {
|
||||
this.backing.rename(base, file);
|
||||
}
|
||||
|
||||
maintain (fn: ProgressCb): void {
|
||||
assert(this.txStarted === false, 'Cannot maintain inside an open transaction');
|
||||
|
||||
|
||||
Vendored
+1
@@ -18,6 +18,7 @@ export interface BaseDb {
|
||||
close (): void;
|
||||
open (): void;
|
||||
empty (): void;
|
||||
rename (base: string, file: string): void;
|
||||
|
||||
maintain (fn: ProgressCb): void;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { TxDb, ProgressCb } from '@polkadot/db/types';
|
||||
import { TrieDb, Node, NodeBranch, NodeEncodedOrEmpty, NodeKv, NodeNotEmpty, NodeType, SnapshotValue } from './types';
|
||||
import { TrieDb, Node, NodeBranch, NodeEncodedOrEmpty, NodeKv, NodeNotEmpty, NodeType } from './types';
|
||||
|
||||
import MemoryDb from '@polkadot/db/Memory';
|
||||
import isNull from '@polkadot/util/is/null';
|
||||
@@ -21,7 +21,7 @@ import { EMPTY_HASH, EMPTY_U8A } from './constants';
|
||||
const l = logger('trie/db');
|
||||
|
||||
export default class Trie implements TrieDb {
|
||||
private db: TxDb;
|
||||
readonly db: TxDb;
|
||||
private txRoot: Uint8Array;
|
||||
private rootHash: Uint8Array;
|
||||
|
||||
@@ -83,6 +83,10 @@ export default class Trie implements TrieDb {
|
||||
this.db.maintain(fn);
|
||||
}
|
||||
|
||||
rename (base: string, file: string): void {
|
||||
this.db.rename(base, file);
|
||||
}
|
||||
|
||||
del (key: Uint8Array) {
|
||||
l.debug(() => ['del', { key }]);
|
||||
|
||||
@@ -134,12 +138,12 @@ export default class Trie implements TrieDb {
|
||||
// return this._setRootNode(rootNode);
|
||||
}
|
||||
|
||||
createSnapshot (dest: Trie, fn: ProgressCb): number {
|
||||
snapshot (dest: TrieDb, fn: ProgressCb): number {
|
||||
const start = Date.now();
|
||||
|
||||
l.log('creating current state snapshot');
|
||||
|
||||
const keys = this._createSnapshot(dest, fn, this.rootHash, 0, 0, 0);
|
||||
const keys = this._snapshot(dest, fn, this.rootHash, 0, 0, 0);
|
||||
const elapsed = (Date.now() - start) / 1000;
|
||||
|
||||
dest.setRoot(this.rootHash);
|
||||
@@ -155,10 +159,8 @@ export default class Trie implements TrieDb {
|
||||
return keys;
|
||||
}
|
||||
|
||||
private _createSnapshot (dest: Trie, fn: ProgressCb, hash: Uint8Array, keys: number, percent: number, depth: number): number {
|
||||
const root = hash || this.rootHash || EMPTY_HASH;
|
||||
|
||||
l.debug(() => ['createSnapshot', { hash }]);
|
||||
private _snapshot (dest: TrieDb, fn: ProgressCb, root: Uint8Array, keys: number, percent: number, depth: number): number {
|
||||
l.debug(() => ['snapshot', { root }]);
|
||||
|
||||
const node = this._getNode(root);
|
||||
|
||||
@@ -167,33 +169,22 @@ export default class Trie implements TrieDb {
|
||||
}
|
||||
|
||||
keys++;
|
||||
dest.db.put(root, encodeNode(node));
|
||||
fn({ keys, percent });
|
||||
|
||||
fn({
|
||||
keys,
|
||||
percent
|
||||
});
|
||||
dest.restoreSnapshot({
|
||||
key: root,
|
||||
value: encodeNode(node)
|
||||
});
|
||||
const filtered = node.filter((node) =>
|
||||
node && node.length === 32
|
||||
) as Array<Uint8Array>;
|
||||
|
||||
node.forEach((node) => {
|
||||
if (isNull(node) || node.length !== 32) {
|
||||
return;
|
||||
}
|
||||
filtered.forEach((root) => {
|
||||
keys = this._snapshot(dest, fn, root, keys, percent, depth + 1);
|
||||
|
||||
keys = this._createSnapshot(dest, fn, node, keys, percent, depth + 1);
|
||||
|
||||
percent += (1 / node.length) / Math.pow(node.length, depth);
|
||||
percent += (100 / filtered.length) / Math.pow(filtered.length, depth);
|
||||
});
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
restoreSnapshot ({ key, value }: SnapshotValue): void {
|
||||
this.db.put(key, value);
|
||||
}
|
||||
|
||||
private _getNode (hash: Uint8Array | null): Node {
|
||||
if (!hash || hash.length === 0 || keyEquals(hash, EMPTY_HASH)) {
|
||||
return null;
|
||||
|
||||
@@ -33,14 +33,10 @@ export type NodeNotEmpty = NodeKv | NodeBranch;
|
||||
|
||||
export type Node = NodeEmpty | NodeNotEmpty;
|
||||
|
||||
export type SnapshotValue = {
|
||||
key: Uint8Array,
|
||||
value: Uint8Array
|
||||
};
|
||||
|
||||
export interface TrieDb extends TxDb {
|
||||
readonly db: TxDb;
|
||||
|
||||
getRoot (): Uint8Array;
|
||||
setRoot (rootHash: Uint8Array): void;
|
||||
createSnapshot (dest: TrieDb, fn: ProgressCb): number;
|
||||
restoreSnapshot (value: SnapshotValue): void;
|
||||
snapshot (dest: TrieDb, fn: ProgressCb): number;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user