2018-06-24 15:39:03 +00:00
|
|
|
import os, rocksdb, ranges
|
|
|
|
import ../storage_types
|
|
|
|
|
|
|
|
type
|
2018-07-26 20:08:43 +00:00
|
|
|
RocksChainDB* = ref object of RootObj
|
2018-06-24 15:39:03 +00:00
|
|
|
store: RocksDBInstance
|
|
|
|
|
|
|
|
ChainDB* = RocksChainDB
|
|
|
|
|
2018-07-26 20:08:43 +00:00
|
|
|
proc newChainDB*(basePath: string): ChainDB =
|
|
|
|
result.new()
|
2018-06-24 15:39:03 +00:00
|
|
|
let
|
|
|
|
dataDir = basePath / "data"
|
|
|
|
backupsDir = basePath / "backups"
|
|
|
|
|
|
|
|
createDir(dataDir)
|
|
|
|
createDir(backupsDir)
|
|
|
|
|
|
|
|
let s = result.store.init(dataDir, backupsDir)
|
|
|
|
if not s.ok: raiseStorageInitError()
|
|
|
|
|
2018-07-26 20:08:43 +00:00
|
|
|
proc get*(db: ChainDB, key: openarray[byte]): seq[byte] =
|
|
|
|
let s = db.store.getBytes(key)
|
2018-06-24 15:39:03 +00:00
|
|
|
if not s.ok: raiseKeyReadError(key)
|
2018-07-26 20:08:43 +00:00
|
|
|
return s.value
|
2018-06-24 15:39:03 +00:00
|
|
|
|
2018-07-26 20:08:43 +00:00
|
|
|
proc put*(db: ChainDB, key, value: openarray[byte]) =
|
|
|
|
let s = db.store.put(key, value)
|
2018-06-24 15:39:03 +00:00
|
|
|
if not s.ok: raiseKeyWriteError(key)
|
|
|
|
|
2018-07-26 20:08:43 +00:00
|
|
|
proc contains*(db: ChainDB, key: openarray[byte]): bool =
|
|
|
|
let s = db.store.contains(key)
|
2018-06-24 15:39:03 +00:00
|
|
|
if not s.ok: raiseKeySearchError(key)
|
|
|
|
return s.value
|
|
|
|
|
2018-07-26 20:08:43 +00:00
|
|
|
proc del*(db: ChainDB, key: openarray[byte]) =
|
|
|
|
let s = db.store.del(key)
|
2018-06-24 15:39:03 +00:00
|
|
|
if not s.ok: raiseKeyDeletionError(key)
|
|
|
|
|
2018-07-26 20:08:43 +00:00
|
|
|
proc close*(db: ChainDB) =
|
2018-06-24 15:39:03 +00:00
|
|
|
db.store.close
|