* Start of trie-db re-implementation * WIP commit * More WIP * WIP, seems complete, untested * Ported trie-db operational * Add Ethereum trie tests * Use u8aConcat to construct u8a * Basic RocksDb interface * Combine engines into DB, expand tests * Update Trie with transaction wrapper * LevelDb (sync) interface * Remove unused (for now) package (compatibility) * trie-d -> trie-db-old, trie-db-ext -> trie-db * Version bump * Skip old trie tests (package to be removed) * Skip tests (fixed) * Remove non-used RocksDb spec * Skip FileFlat tests (deprecated?)
79 lines
1.6 KiB
TypeScript
79 lines
1.6 KiB
TypeScript
// Copyright 2017-2018 @polkadot/db authors & contributors
|
|
// This software may be modified and distributed under the terms
|
|
// of the ISC license. See the LICENSE file for details.
|
|
|
|
import { BaseDb, ProgressCb } from '../types';
|
|
|
|
import { LRUMap } from 'lru_map';
|
|
import logger from '@polkadot/util/logger';
|
|
|
|
type CachedValue = {
|
|
value: Uint8Array | null
|
|
};
|
|
|
|
const DEFAULT_ITEM_COUNT = 4096;
|
|
|
|
const l = logger('db/lru');
|
|
|
|
export default class LruDb implements BaseDb {
|
|
private backing: BaseDb;
|
|
private lru: LRUMap<string, CachedValue>;
|
|
|
|
constructor (backing: BaseDb, itemCount: number = DEFAULT_ITEM_COUNT) {
|
|
this.backing = backing;
|
|
this.lru = new LRUMap(itemCount);
|
|
}
|
|
|
|
close (): void {
|
|
l.debug(() => ['close']);
|
|
|
|
this.lru.clear();
|
|
this.backing.close();
|
|
}
|
|
|
|
open (): void {
|
|
l.debug(() => ['open']);
|
|
|
|
this.lru.clear();
|
|
this.backing.open();
|
|
}
|
|
|
|
maintain (fn: ProgressCb): void {
|
|
this.backing.maintain(fn);
|
|
}
|
|
|
|
del (key: Uint8Array): void {
|
|
const keyStr = key.toString();
|
|
|
|
this.backing.del(key);
|
|
this.lru.set(keyStr, { value: null });
|
|
}
|
|
|
|
get (key: Uint8Array): Uint8Array | null {
|
|
const keyStr = key.toString();
|
|
const cached = this.lru.get(keyStr);
|
|
|
|
if (cached) {
|
|
return cached.value;
|
|
}
|
|
|
|
const value = this.backing.get(key);
|
|
|
|
this.lru.set(keyStr, { value });
|
|
|
|
return value;
|
|
}
|
|
|
|
// Convenience methods used in tests
|
|
_getLru (key: Uint8Array): CachedValue | undefined {
|
|
return this.lru.get(key.toString());
|
|
}
|
|
|
|
put (key: Uint8Array, value: Uint8Array): void {
|
|
const keyStr = key.toString();
|
|
|
|
this.backing.put(key, value);
|
|
this.lru.set(keyStr, { value });
|
|
}
|
|
}
|