From 2d75751960e0a0f68f82e66e679b4497edd78389 Mon Sep 17 00:00:00 2001 From: andri lim Date: Mon, 1 Apr 2019 15:11:18 +0700 Subject: [PATCH] add readonly option to lmdb and rocksdb backend --- eth/trie/backends/lmdb_backend.nim | 12 ++++++++---- eth/trie/backends/rocksdb_backend.nim | 8 ++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/eth/trie/backends/lmdb_backend.nim b/eth/trie/backends/lmdb_backend.nim index 7c8ba16..bbf5a15 100644 --- a/eth/trie/backends/lmdb_backend.nim +++ b/eth/trie/backends/lmdb_backend.nim @@ -10,6 +10,7 @@ else: const MDB_NOSUBDIR = 0x4000 + MDB_RDONLY = 0x20000 MDB_NOTFOUND = -30798 when defined(cpu64): @@ -147,7 +148,7 @@ proc del*(db: ChainDB, key: openarray[byte]) = proc close*(db: ChainDB) = mdb_env_close(db.env) -proc newChainDB*(basePath: string): ChainDB = +proc newChainDB*(basePath: string, readOnly = false): ChainDB = result.new() let dataDir = basePath / "nimbus.db" @@ -156,9 +157,12 @@ proc newChainDB*(basePath: string): ChainDB = ok = mdb_env_set_mapsize(result.env, LMDB_MAP_SIZE) == 0 if not ok: raiseStorageInitError() - + + var openFlags = MDB_NOSUBDIR + if readOnly: openFlags = openFlags or MDB_RDONLY # file mode ignored on windows - ok = mdb_env_open(result.env, dataDir, MDB_NOSUBDIR.cuint, 0o664.cint) == 0 + ok = mdb_env_open(result.env, dataDir, openFlags.cuint, 0o664.cint) == 0 if not ok: raiseStorageInitError() - result.put(emptyRlpHash.data, emptyRlp) + if not readOnly: + result.put(emptyRlpHash.data, emptyRlp) diff --git a/eth/trie/backends/rocksdb_backend.nim b/eth/trie/backends/rocksdb_backend.nim index 2353e32..b0f97ba 100644 --- a/eth/trie/backends/rocksdb_backend.nim +++ b/eth/trie/backends/rocksdb_backend.nim @@ -35,7 +35,7 @@ proc del*(db: ChainDB, key: openarray[byte]) = proc close*(db: ChainDB) = db.store.close -proc newChainDB*(basePath: string): ChainDB = +proc newChainDB*(basePath: string, readOnly = false): ChainDB = result.new() let dataDir = basePath / "data" @@ -44,8 +44,8 @@ proc newChainDB*(basePath: string): ChainDB = createDir(dataDir) createDir(backupsDir) - let s = result.store.init(dataDir, backupsDir) + let s = result.store.init(dataDir, backupsDir, readOnly) if not s.ok: raiseStorageInitError() - put(result, emptyRlpHash.data, emptyRlp) - + if not readOnly: + put(result, emptyRlpHash.data, emptyRlp)