2018-10-16 03:10:01 +03:00
|
|
|
import os, rocksdb, ranges, eth_trie/[defs, db_tracing]
|
2018-06-24 18:39:03 +03:00
|
|
|
import ../storage_types
|
|
|
|
|
|
|
|
type
|
2018-07-26 23:08:43 +03:00
|
|
|
RocksChainDB* = ref object of RootObj
|
2018-06-24 18:39:03 +03:00
|
|
|
store: RocksDBInstance
|
|
|
|
|
|
|
|
ChainDB* = RocksChainDB
|
|
|
|
|
2018-07-26 23:08:43 +03:00
|
|
|
proc get*(db: ChainDB, key: openarray[byte]): seq[byte] =
|
|
|
|
let s = db.store.getBytes(key)
|
2018-09-06 20:05:22 +03:00
|
|
|
if s.ok:
|
2018-09-16 00:20:20 +03:00
|
|
|
result = s.value
|
|
|
|
traceGet key, result
|
2018-09-06 20:05:22 +03:00
|
|
|
elif s.error.len == 0:
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
raiseKeyReadError(key)
|
2018-06-24 18:39:03 +03:00
|
|
|
|
2018-07-26 23:08:43 +03:00
|
|
|
proc put*(db: ChainDB, key, value: openarray[byte]) =
|
2018-09-16 00:20:20 +03:00
|
|
|
tracePut key, value
|
2018-07-26 23:08:43 +03:00
|
|
|
let s = db.store.put(key, value)
|
2018-06-24 18:39:03 +03:00
|
|
|
if not s.ok: raiseKeyWriteError(key)
|
|
|
|
|
2018-07-26 23:08:43 +03:00
|
|
|
proc contains*(db: ChainDB, key: openarray[byte]): bool =
|
|
|
|
let s = db.store.contains(key)
|
2018-06-24 18:39:03 +03:00
|
|
|
if not s.ok: raiseKeySearchError(key)
|
|
|
|
return s.value
|
|
|
|
|
2018-07-26 23:08:43 +03:00
|
|
|
proc del*(db: ChainDB, key: openarray[byte]) =
|
2018-09-16 00:20:20 +03:00
|
|
|
traceDel key
|
2018-07-26 23:08:43 +03:00
|
|
|
let s = db.store.del(key)
|
2018-06-24 18:39:03 +03:00
|
|
|
if not s.ok: raiseKeyDeletionError(key)
|
|
|
|
|
2018-07-26 23:08:43 +03:00
|
|
|
proc close*(db: ChainDB) =
|
2018-06-24 18:39:03 +03:00
|
|
|
db.store.close
|
2018-10-05 03:20:12 +03:00
|
|
|
|
|
|
|
proc newChainDB*(basePath: string): ChainDB =
|
|
|
|
result.new()
|
|
|
|
let
|
|
|
|
dataDir = basePath / "data"
|
|
|
|
backupsDir = basePath / "backups"
|
|
|
|
|
|
|
|
createDir(dataDir)
|
|
|
|
createDir(backupsDir)
|
|
|
|
|
|
|
|
let s = result.store.init(dataDir, backupsDir)
|
|
|
|
if not s.ok: raiseStorageInitError()
|
|
|
|
|
|
|
|
put(result, emptyRlpHash.data, emptyRlp)
|
|
|
|
|