mirror of https://github.com/status-im/nim-eth.git
add readonly option to lmdb and rocksdb backend
This commit is contained in:
parent
7954c0c7ed
commit
2d75751960
|
@ -10,6 +10,7 @@ else:
|
||||||
|
|
||||||
const
|
const
|
||||||
MDB_NOSUBDIR = 0x4000
|
MDB_NOSUBDIR = 0x4000
|
||||||
|
MDB_RDONLY = 0x20000
|
||||||
MDB_NOTFOUND = -30798
|
MDB_NOTFOUND = -30798
|
||||||
|
|
||||||
when defined(cpu64):
|
when defined(cpu64):
|
||||||
|
@ -147,7 +148,7 @@ proc del*(db: ChainDB, key: openarray[byte]) =
|
||||||
proc close*(db: ChainDB) =
|
proc close*(db: ChainDB) =
|
||||||
mdb_env_close(db.env)
|
mdb_env_close(db.env)
|
||||||
|
|
||||||
proc newChainDB*(basePath: string): ChainDB =
|
proc newChainDB*(basePath: string, readOnly = false): ChainDB =
|
||||||
result.new()
|
result.new()
|
||||||
|
|
||||||
let dataDir = basePath / "nimbus.db"
|
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
|
ok = mdb_env_set_mapsize(result.env, LMDB_MAP_SIZE) == 0
|
||||||
if not ok: raiseStorageInitError()
|
if not ok: raiseStorageInitError()
|
||||||
|
|
||||||
|
var openFlags = MDB_NOSUBDIR
|
||||||
|
if readOnly: openFlags = openFlags or MDB_RDONLY
|
||||||
# file mode ignored on windows
|
# 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()
|
if not ok: raiseStorageInitError()
|
||||||
|
|
||||||
result.put(emptyRlpHash.data, emptyRlp)
|
if not readOnly:
|
||||||
|
result.put(emptyRlpHash.data, emptyRlp)
|
||||||
|
|
|
@ -35,7 +35,7 @@ proc del*(db: ChainDB, key: openarray[byte]) =
|
||||||
proc close*(db: ChainDB) =
|
proc close*(db: ChainDB) =
|
||||||
db.store.close
|
db.store.close
|
||||||
|
|
||||||
proc newChainDB*(basePath: string): ChainDB =
|
proc newChainDB*(basePath: string, readOnly = false): ChainDB =
|
||||||
result.new()
|
result.new()
|
||||||
let
|
let
|
||||||
dataDir = basePath / "data"
|
dataDir = basePath / "data"
|
||||||
|
@ -44,8 +44,8 @@ proc newChainDB*(basePath: string): ChainDB =
|
||||||
createDir(dataDir)
|
createDir(dataDir)
|
||||||
createDir(backupsDir)
|
createDir(backupsDir)
|
||||||
|
|
||||||
let s = result.store.init(dataDir, backupsDir)
|
let s = result.store.init(dataDir, backupsDir, readOnly)
|
||||||
if not s.ok: raiseStorageInitError()
|
if not s.ok: raiseStorageInitError()
|
||||||
|
|
||||||
put(result, emptyRlpHash.data, emptyRlp)
|
if not readOnly:
|
||||||
|
put(result, emptyRlpHash.data, emptyRlp)
|
||||||
|
|
Loading…
Reference in New Issue