Revert LRU removal (#199)

* Revert "Remove db-diskdown LRU (#198)"

This reverts commit 219f12a65b.

* Re-add LRU for db-diskdown

* Re-add depth check
This commit is contained in:
Jaco Greeff
2018-08-28 21:10:47 +02:00
committed by GitHub
parent 81738e212a
commit 372e2fc903
4 changed files with 14 additions and 3 deletions
+1
View File
@@ -32,6 +32,7 @@
"@polkadot/util": "^0.28.17",
"@types/mkdirp": "^0.5.2",
"abstract-leveldown": "^5.0.0",
"lru_map": "^0.3.3",
"mkdirp": "^0.5.1"
},
"devDependencies": {
+7 -1
View File
@@ -5,11 +5,13 @@
import { DiskStore } from './types';
import { AbstractLevelDOWN } from 'abstract-leveldown';
import { LRUMap } from 'lru_map';
import isUndefined from '@polkadot/util/is/undefined';
import logger from '@polkadot/util/logger';
import Combined from './store/Combined';
const LRU_SIZE = 16384;
const KEY_LENGTH = 32;
const l = logger('disk/scatter');
@@ -18,11 +20,13 @@ const noop = () =>
class DiskDown extends AbstractLevelDOWN {
_disk: DiskStore;
_store: LRUMap<string, Buffer>;
constructor (location: string) {
super(location);
this._disk = new Combined(location);
this._store = new LRUMap(LRU_SIZE);
}
compact (progress: (message: string) => void): void {
@@ -69,6 +73,7 @@ class DiskDown extends AbstractLevelDOWN {
_del (key: Buffer, options: any, callback: Function) {
l.debug(() => ['_del', key]);
this._store.delete(key.toString());
this._disk.delete(key);
process.nextTick(callback);
@@ -77,7 +82,7 @@ class DiskDown extends AbstractLevelDOWN {
_get (key: Buffer, options: any, callback: Function) {
l.debug(() => ['_get', key.toString('hex')]);
const value = this._disk.get(key);
const value = this._store.get(key.toString()) || this._disk.get(key);
if (isUndefined(value)) {
process.nextTick(callback, new Error('NotFound'));
@@ -90,6 +95,7 @@ class DiskDown extends AbstractLevelDOWN {
_put (key: Buffer, value: Buffer, options: any, callback: Function) {
l.debug(() => ['_put', key.toString('hex'), value]);
this._store.set(key.toString(), value);
this._disk.set(key, value);
process.nextTick(callback);
+2 -2
View File
@@ -101,14 +101,14 @@ export default class Combined implements DiskStore {
const newStat = fs.lstatSync(newFile);
const oldStat = fs.lstatSync(this._file);
const percentage = 100 * (newStat.size / oldStat.size);
const sizeMb = newStat.size / (1024 * 1024);
const sizeMB = newStat.size / (1024 * 1024);
const elapsed = (Date.now() - start) / 1000;
fs.unlinkSync(this._file);
// fs.renameSync(this._file, `${this._file}.old`);
fs.renameSync(newFile, this._file);
l.log(`compacted in ${elapsed.toFixed(2)}s, ${(count / 1000).toFixed(2)}k keys, ${sizeMb.toFixed(2)}MB (${percentage.toFixed(2)}%)`);
l.log(`compacted in ${elapsed.toFixed(2)}s, ${(count / 1000).toFixed(2)}k keys, ${sizeMB.toFixed(2)}MB (${percentage.toFixed(2)}%)`);
}
delete (key: Buffer): void {