2023-01-31 01:32:17 +00:00
|
|
|
{.push raises: [].}
|
2022-04-06 05:28:19 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
std/os,
|
|
|
|
rocksdb, stew/results,
|
|
|
|
eth/db/kvstore
|
|
|
|
|
|
|
|
export results, kvstore
|
|
|
|
|
|
|
|
const maxOpenFiles = 512
|
|
|
|
|
|
|
|
type
|
|
|
|
RocksStoreRef* = ref object of RootObj
|
2022-08-12 15:42:07 +00:00
|
|
|
store*: RocksDBInstance
|
|
|
|
tmpDir*: string
|
2022-04-06 05:28:19 +00:00
|
|
|
|
|
|
|
proc get*(db: RocksStoreRef, key: openArray[byte], onData: kvstore.DataProc): KvResult[bool] =
|
|
|
|
db.store.get(key, onData)
|
|
|
|
|
|
|
|
proc find*(db: RocksStoreRef, prefix: openArray[byte], onFind: kvstore.KeyValueProc): KvResult[int] =
|
|
|
|
raiseAssert "Unimplemented"
|
|
|
|
|
|
|
|
proc put*(db: RocksStoreRef, key, value: openArray[byte]): KvResult[void] =
|
|
|
|
db.store.put(key, value)
|
|
|
|
|
|
|
|
proc contains*(db: RocksStoreRef, key: openArray[byte]): KvResult[bool] =
|
|
|
|
db.store.contains(key)
|
|
|
|
|
2023-01-27 14:57:48 +00:00
|
|
|
proc del*(db: RocksStoreRef, key: openArray[byte]): KvResult[bool] =
|
2022-04-06 05:28:19 +00:00
|
|
|
db.store.del(key)
|
|
|
|
|
2023-01-27 14:57:48 +00:00
|
|
|
proc clear*(db: RocksStoreRef): KvResult[bool] =
|
|
|
|
db.store.clear()
|
|
|
|
|
2022-04-06 05:28:19 +00:00
|
|
|
proc close*(db: RocksStoreRef) =
|
|
|
|
db.store.close
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: type RocksStoreRef, basePath: string, name: string,
|
|
|
|
readOnly = false): KvResult[T] =
|
|
|
|
let
|
|
|
|
dataDir = basePath / name / "data"
|
2023-01-31 01:32:17 +00:00
|
|
|
# tmpDir = basePath / name / "tmp" -- notused
|
2022-04-06 05:28:19 +00:00
|
|
|
backupsDir = basePath / name / "backups"
|
|
|
|
|
|
|
|
try:
|
|
|
|
createDir(dataDir)
|
|
|
|
createDir(backupsDir)
|
|
|
|
except OSError, IOError:
|
|
|
|
return err("rocksdb: cannot create database directory")
|
|
|
|
|
|
|
|
var store: RocksDBInstance
|
|
|
|
if (let v = store.init(
|
|
|
|
dataDir, backupsDir, readOnly, maxOpenFiles = maxOpenFiles); v.isErr):
|
|
|
|
return err(v.error)
|
|
|
|
|
|
|
|
ok(T(store: store))
|