2019-07-07 09:55:17 +00:00
|
|
|
import os, rocksdb, stew/ranges, eth/trie/[trie_defs, db_tracing]
|
2019-02-05 12:01:10 +00:00
|
|
|
import backend_defs
|
|
|
|
|
|
|
|
type
|
|
|
|
RocksChainDB* = ref object of RootObj
|
|
|
|
store: RocksDBInstance
|
|
|
|
|
|
|
|
ChainDB* = RocksChainDB
|
|
|
|
|
2019-07-16 13:57:28 +00:00
|
|
|
# Maximum open files for rocksdb, set to 512 to be safe for usual 1024 Linux
|
|
|
|
# limit per application
|
|
|
|
const maxOpenFiles = 512
|
|
|
|
|
2019-02-05 12:01:10 +00:00
|
|
|
proc get*(db: ChainDB, key: openarray[byte]): seq[byte] =
|
|
|
|
let s = db.store.getBytes(key)
|
2020-04-18 05:21:13 +00:00
|
|
|
if s.isOk:
|
2019-02-05 12:01:10 +00:00
|
|
|
result = s.value
|
|
|
|
traceGet key, result
|
|
|
|
elif s.error.len == 0:
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
raiseKeyReadError(key)
|
|
|
|
|
|
|
|
proc put*(db: ChainDB, key, value: openarray[byte]) =
|
|
|
|
tracePut key, value
|
|
|
|
let s = db.store.put(key, value)
|
2020-04-18 05:21:13 +00:00
|
|
|
if not s.isOk: raiseKeyWriteError(key)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
proc contains*(db: ChainDB, key: openarray[byte]): bool =
|
|
|
|
let s = db.store.contains(key)
|
2020-04-18 05:21:13 +00:00
|
|
|
if not s.isOk: raiseKeySearchError(key)
|
2019-02-05 12:01:10 +00:00
|
|
|
return s.value
|
|
|
|
|
|
|
|
proc del*(db: ChainDB, key: openarray[byte]) =
|
|
|
|
traceDel key
|
|
|
|
let s = db.store.del(key)
|
2020-04-18 05:21:13 +00:00
|
|
|
if not s.isOk: raiseKeyDeletionError(key)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
proc close*(db: ChainDB) =
|
|
|
|
db.store.close
|
|
|
|
|
2019-04-01 08:11:18 +00:00
|
|
|
proc newChainDB*(basePath: string, readOnly = false): ChainDB =
|
2019-02-05 12:01:10 +00:00
|
|
|
result.new()
|
|
|
|
let
|
|
|
|
dataDir = basePath / "data"
|
|
|
|
backupsDir = basePath / "backups"
|
|
|
|
|
|
|
|
createDir(dataDir)
|
|
|
|
createDir(backupsDir)
|
|
|
|
|
2019-07-16 13:57:28 +00:00
|
|
|
let s = result.store.init(dataDir, backupsDir, readOnly,
|
|
|
|
maxOpenFiles = maxOpenFiles)
|
2020-04-18 05:21:13 +00:00
|
|
|
if not s.isOk: raiseStorageInitError()
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2019-04-01 08:11:18 +00:00
|
|
|
if not readOnly:
|
|
|
|
put(result, emptyRlpHash.data, emptyRlp)
|