Use progress callback for compacting (#197)

This commit is contained in:
Jaco Greeff
2018-08-28 14:07:00 +02:00
committed by GitHub
parent f0af13d0c7
commit dbb064fd2c
4 changed files with 9 additions and 11 deletions
+2 -4
View File
@@ -31,12 +31,10 @@ class DiskDown extends AbstractLevelDOWN {
this._store = new LRUMap(LRU_SIZE); this._store = new LRUMap(LRU_SIZE);
} }
compact (callback: Function): void { compact (progress: (message: string) => void): void {
l.debug(() => ['compact']); l.debug(() => ['compact']);
this._disk.compact(); this._disk.compact(progress);
process.nextTick(callback);
} }
_open (options: any, callback: Function) { _open (options: any, callback: Function) {
+5 -5
View File
@@ -84,7 +84,7 @@ export default class Combined implements DiskStore {
fs.closeSync(this._fd); fs.closeSync(this._fd);
} }
compact (): void { compact (progress: (message: string) => void): void {
assert(this._fd === -1, 'Database cannot be open for compacting'); assert(this._fd === -1, 'Database cannot be open for compacting');
l.log('compacting database'); l.log('compacting database');
@@ -93,7 +93,7 @@ export default class Combined implements DiskStore {
const newFile = `${this._file}.compacted`; const newFile = `${this._file}.compacted`;
const newFd = this._open(newFile, true); const newFd = this._open(newFile, true);
const oldFd = this._open(this._file); const oldFd = this._open(this._file);
const count = this._compact(newFd, oldFd); const count = this._compact(progress, newFd, oldFd);
fs.closeSync(oldFd); fs.closeSync(oldFd);
fs.closeSync(newFd); fs.closeSync(newFd);
@@ -191,7 +191,7 @@ export default class Combined implements DiskStore {
return at; return at;
} }
private _compact (newFd: number, oldFd: number, newAt: number = 0, oldAt: number = 0, depth: number = 0): number { private _compact (progress: (message: string) => void, newFd: number, oldFd: number, newAt: number = 0, oldAt: number = 0, depth: number = 0): number {
// l.debug(() => ['_compact', debug({ newFd, oldFd, newAt, oldAt })]); // l.debug(() => ['_compact', debug({ newFd, oldFd, newAt, oldAt })]);
let count = 0; let count = 0;
@@ -218,7 +218,7 @@ export default class Combined implements DiskStore {
const headerAt = this._compactWriteHeader(newFd, newAt, index); const headerAt = this._compactWriteHeader(newFd, newAt, index);
indexCount += this._compact(newFd, oldFd, headerAt, dataAt, depth + 1); indexCount += this._compact(progress, newFd, oldFd, headerAt, dataAt, depth + 1);
} else { } else {
throw new Error(`Unknown entry type, ${entryType}`); throw new Error(`Unknown entry type, ${entryType}`);
} }
@@ -228,7 +228,7 @@ export default class Combined implements DiskStore {
if (oldAt === 0) { if (oldAt === 0) {
const percentage = ` ${(100 * (index + 1) / ENTRY_NUM).toFixed(2)}`; const percentage = ` ${(100 * (index + 1) / ENTRY_NUM).toFixed(2)}`;
l.log(`${percentage.slice(-6)}% compacted, ${count} keys`); progress(`${percentage.slice(-6)}% compacted, ${count} keys`);
} }
} }
+1 -1
View File
@@ -54,7 +54,7 @@ export default class Scatter implements DiskStore {
// noop // noop
} }
compact (): void { compact (progress: (message: string) => void): void {
// noop // noop
} }
+1 -1
View File
@@ -4,7 +4,7 @@
export interface DiskStore { export interface DiskStore {
close (): void; close (): void;
compact (): void, compact (progress: (message: string) => void): void,
delete (key: Buffer): void; delete (key: Buffer): void;
get (key: Buffer): Buffer | undefined; get (key: Buffer): Buffer | undefined;
open (): void, open (): void,