Adjust initial lmdb mapsize (#345)

* Adjust initial lmdb mapsize

* Make transaction return generic result
This commit is contained in:
Jaco Greeff
2019-03-04 14:38:27 +01:00
committed by GitHub
parent 10e36c95fd
commit 9a3468dfaa
2 changed files with 24 additions and 12 deletions
+23 -11
View File
@@ -9,20 +9,24 @@ import lmdb from 'node-lmdb';
import path from 'path';
import { bufferToU8a, u8aToBuffer } from '@polkadot/util/index';
const MAPSIZE = 2 * 1024 * 1024 * 1024; // 2GB
export default class LmDb implements BaseDb {
private _env: any;
private _dbi: any | null;
private _path: string;
private _txn: any;
private _txn: any | null;
constructor (base: string, name: string, options?: BaseDbOptions) {
this._env = new lmdb.Env();
this._path = path.join(base, name);
this._txn = null;
mkdirp.sync(this._path);
this._env.open({
path: this._path
path: this._path,
mapSize: MAPSIZE
});
}
@@ -34,11 +38,9 @@ export default class LmDb implements BaseDb {
open (): void {
this._dbi = this._env.openDbi({
create: true,
name: null,
name: 'lmdb',
keyIsBuffer: true
});
this.txStart();
}
drop (): void {
@@ -67,14 +69,12 @@ export default class LmDb implements BaseDb {
txCommit (): void {
this._txn.commit();
this.txStart();
this._txn = null;
}
txRevert (): void {
this._txn.abort();
this.txStart();
this._txn = null;
}
txStart (): void {
@@ -85,14 +85,26 @@ export default class LmDb implements BaseDb {
this._txn.del(this._dbi, u8aToBuffer(key));
}
get (key: Uint8Array): Uint8Array | null {
const value = this._txn.getBinary(this._dbi, u8aToBuffer(key));
get (_key: Uint8Array): Uint8Array | null {
const key = u8aToBuffer(_key);
const value = this._txn
? this._txn.getBinary(this._dbi, key)
: this.txnGet(key);
return value
? bufferToU8a(value)
: null;
}
private txnGet (key: Buffer): Buffer {
const txn = this._env.beginTxn();
const value = txn.getBinary(this._dbi, key);
txn.abort();
return value;
}
put (key: Uint8Array, value: Uint8Array): void {
this._txn.putBinary(this._dbi, u8aToBuffer(key), u8aToBuffer(value));
}
+1 -1
View File
@@ -26,7 +26,7 @@ export default class TransactionDb implements TxDb {
this.txStarted = false;
}
transaction (fn: () => boolean): boolean {
transaction<T = boolean> (fn: () => T): T {
l.debug(() => ['transaction']);
try {