2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
2024-03-05 04:54:42 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-11-01 03:32:09 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
{.push raises: [].}
|
2022-04-06 05:28:19 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
std/os,
|
2024-03-05 04:54:42 +00:00
|
|
|
stew/results,
|
|
|
|
rocksdb,
|
2022-04-06 05:28:19 +00:00
|
|
|
eth/db/kvstore
|
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
export kvstore
|
2022-04-06 05:28:19 +00:00
|
|
|
|
|
|
|
const maxOpenFiles = 512
|
|
|
|
|
|
|
|
type
|
|
|
|
RocksStoreRef* = ref object of RootObj
|
2024-03-05 04:54:42 +00:00
|
|
|
db: RocksDbRef
|
|
|
|
backupEngine: BackupEngineRef
|
|
|
|
readOnly: bool
|
2022-04-06 05:28:19 +00:00
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
proc readOnly*(store: RocksStoreRef): bool =
|
|
|
|
store.readOnly
|
2022-04-06 05:28:19 +00:00
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
proc readOnlyDb*(store: RocksStoreRef): RocksDbReadOnlyRef =
|
|
|
|
doAssert store.readOnly
|
|
|
|
store.db.RocksDbReadOnlyRef
|
|
|
|
|
|
|
|
proc readWriteDb*(store: RocksStoreRef): RocksDbReadWriteRef =
|
|
|
|
doAssert not store.readOnly
|
|
|
|
store.db.RocksDbReadWriteRef
|
|
|
|
|
|
|
|
template validateCanWriteAndGet(store: RocksStoreRef): RocksDbReadWriteRef =
|
|
|
|
if store.readOnly:
|
|
|
|
raiseAssert "Unimplemented"
|
|
|
|
store.db.RocksDbReadWriteRef
|
|
|
|
|
|
|
|
proc get*(store: RocksStoreRef, key: openArray[byte], onData: kvstore.DataProc): KvResult[bool] =
|
|
|
|
store.db.get(key, onData)
|
|
|
|
|
|
|
|
proc find*(store: RocksStoreRef, prefix: openArray[byte], onFind: kvstore.KeyValueProc): KvResult[int] =
|
2022-04-06 05:28:19 +00:00
|
|
|
raiseAssert "Unimplemented"
|
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
proc put*(store: RocksStoreRef, key, value: openArray[byte]): KvResult[void] =
|
|
|
|
store.validateCanWriteAndGet().put(key, value)
|
|
|
|
|
|
|
|
proc contains*(store: RocksStoreRef, key: openArray[byte]): KvResult[bool] =
|
|
|
|
store.db.keyExists(key)
|
2022-04-06 05:28:19 +00:00
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
proc del*(store: RocksStoreRef, key: openArray[byte]): KvResult[bool] =
|
|
|
|
let db = store.validateCanWriteAndGet()
|
2022-04-06 05:28:19 +00:00
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
let exists = ? db.keyExists(key)
|
|
|
|
if not exists:
|
|
|
|
return ok(false)
|
2022-04-06 05:28:19 +00:00
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
let res = db.delete(key)
|
|
|
|
if res.isErr():
|
|
|
|
return err(res.error())
|
2023-01-27 14:57:48 +00:00
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
ok(true)
|
|
|
|
|
|
|
|
proc clear*(store: RocksStoreRef): KvResult[bool] =
|
|
|
|
raiseAssert "Unimplemented"
|
|
|
|
|
|
|
|
proc close*(store: RocksStoreRef) =
|
|
|
|
store.db.close()
|
|
|
|
store.backupEngine.close()
|
2022-04-06 05:28:19 +00:00
|
|
|
|
|
|
|
proc init*(
|
2024-03-05 04:54:42 +00:00
|
|
|
T: type RocksStoreRef,
|
|
|
|
basePath: string,
|
|
|
|
name: string,
|
2022-04-06 05:28:19 +00:00
|
|
|
readOnly = false): KvResult[T] =
|
2024-03-05 04:54:42 +00:00
|
|
|
|
2022-04-06 05:28:19 +00:00
|
|
|
let
|
|
|
|
dataDir = basePath / name / "data"
|
|
|
|
backupsDir = basePath / name / "backups"
|
|
|
|
|
|
|
|
try:
|
|
|
|
createDir(dataDir)
|
|
|
|
createDir(backupsDir)
|
|
|
|
except OSError, IOError:
|
|
|
|
return err("rocksdb: cannot create database directory")
|
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
let backupEngine = ? openBackupEngine(backupsDir)
|
|
|
|
|
|
|
|
let dbOpts = defaultDbOptions()
|
|
|
|
dbOpts.setMaxOpenFiles(maxOpenFiles)
|
2022-04-06 05:28:19 +00:00
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
if readOnly:
|
|
|
|
let readOnlyDb = ? openRocksDbReadOnly(dataDir, dbOpts)
|
|
|
|
ok(T(db: readOnlyDb, backupEngine: backupEngine, readOnly: true))
|
|
|
|
else:
|
|
|
|
let readWriteDb = ? openRocksDb(dataDir, dbOpts)
|
|
|
|
ok(T(db: readWriteDb, backupEngine: backupEngine, readOnly: false))
|