From ccc1529a73d6b8dc8fdfe4a7feae2f24e78deb05 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 4 Mar 2019 18:04:26 +0100 Subject: [PATCH] Auto-grow lmdb mapSize (>75% use) (#350) --- packages/db/src/engines/LmDb.ts | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/db/src/engines/LmDb.ts b/packages/db/src/engines/LmDb.ts index 94f41da66..2492f7af7 100644 --- a/packages/db/src/engines/LmDb.ts +++ b/packages/db/src/engines/LmDb.ts @@ -4,12 +4,13 @@ import { BaseDb, BaseDbOptions, ProgressCb } from '../types'; +import fs from 'fs'; import mkdirp from 'mkdirp'; import lmdb from 'node-lmdb'; import path from 'path'; import { bufferToU8a, u8aToBuffer } from '@polkadot/util/index'; -const MAPSIZE = 2 * 1024 * 1024 * 1024; // 2GB +const BASE_MAPSIZE = 2 * 1024 * 1024 * 1024; // 2GB export default class LmDb implements BaseDb { private _env: any; @@ -26,10 +27,30 @@ export default class LmDb implements BaseDb { this._env.open({ path: this._path, - mapSize: MAPSIZE + mapSize: this.getMapSize() * BASE_MAPSIZE }); } + private getMapSize (): number { + const size = this.size(); + const max = Math.ceil(size / BASE_MAPSIZE); + const use = size / (max * BASE_MAPSIZE); + + return (use > 0.75) + ? max + 1 + : max; + } + + private growMapSize (): void { + const info = this._env.info(); + const next = this.getMapSize(); + const current = Math.ceil(info.mapSize / BASE_MAPSIZE); + + if (current < next) { + this._env.resize(next * BASE_MAPSIZE); + } + } + close (): void { this._dbi.close(); this._dbi = null; @@ -64,7 +85,7 @@ export default class LmDb implements BaseDb { } size (): number { - return 0; + return fs.statSync(path.join(this._path, 'data.mdb')).size; } txCommit (): void { @@ -78,6 +99,8 @@ export default class LmDb implements BaseDb { } txStart (): void { + this.growMapSize(); + this._txn = this._env.beginTxn(); }