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
|
2024-03-12 03:04:46 +00:00
|
|
|
std/[os, sequtils],
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2024-03-05 04:54:42 +00:00
|
|
|
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
|
|
|
|
|
|
|
type
|
|
|
|
RocksStoreRef* = ref object of RootObj
|
2024-03-12 03:04:46 +00:00
|
|
|
db: RocksDbReadWriteRef
|
2022-04-06 05:28:19 +00:00
|
|
|
|
2024-03-12 03:04:46 +00:00
|
|
|
RocksNamespaceRef* = ref object of RootObj
|
|
|
|
colFamily: ColFamilyReadWrite
|
2022-04-06 05:28:19 +00:00
|
|
|
|
2024-03-12 03:04:46 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# RocksStoreRef procs
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-03-05 04:54:42 +00:00
|
|
|
|
2024-03-12 03:04:46 +00:00
|
|
|
proc rocksDb*(store: RocksStoreRef): RocksDbReadWriteRef =
|
|
|
|
store.db
|
2024-03-05 04:54:42 +00:00
|
|
|
|
2024-03-12 03:04:46 +00:00
|
|
|
proc get*(
|
|
|
|
store: RocksStoreRef,
|
|
|
|
key: openArray[byte],
|
|
|
|
onData: kvstore.DataProc): KvResult[bool] =
|
2024-03-05 04:54:42 +00:00
|
|
|
store.db.get(key, onData)
|
|
|
|
|
2024-03-12 03:04:46 +00:00
|
|
|
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] =
|
2024-03-12 03:04:46 +00:00
|
|
|
store.db.put(key, value)
|
2024-03-05 04:54:42 +00:00
|
|
|
|
|
|
|
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] =
|
2022-04-06 05:28:19 +00:00
|
|
|
|
2024-04-17 18:09:55 +00:00
|
|
|
let rc = store.db.keyExists(key)
|
|
|
|
if rc.isErr:
|
|
|
|
return rc
|
|
|
|
if not rc.value:
|
2024-03-05 04:54:42 +00:00
|
|
|
return ok(false)
|
2022-04-06 05:28:19 +00:00
|
|
|
|
2024-03-12 03:04:46 +00:00
|
|
|
let res = store.db.delete(key)
|
2024-03-05 04:54:42 +00:00
|
|
|
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()
|
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,
|
2024-03-12 03:04:46 +00:00
|
|
|
namespaces = @["default"]): KvResult[T] =
|
2024-03-05 04:54:42 +00:00
|
|
|
|
2024-03-12 03:04:46 +00:00
|
|
|
let dataDir = basePath / name / "data"
|
2022-04-06 05:28:19 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
createDir(dataDir)
|
|
|
|
except OSError, IOError:
|
2024-03-12 03:04:46 +00:00
|
|
|
return err("RocksStoreRef: cannot create database directory")
|
2024-03-05 04:54:42 +00:00
|
|
|
|
|
|
|
let dbOpts = defaultDbOptions()
|
2022-04-06 05:28:19 +00:00
|
|
|
|
2024-03-12 03:04:46 +00:00
|
|
|
let db = ? openRocksDb(dataDir, dbOpts,
|
|
|
|
columnFamilies = namespaces.mapIt(initColFamilyDescriptor(it)))
|
|
|
|
ok(T(db: db))
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# RocksNamespaceRef procs
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc name*(store: RocksNamespaceRef): string =
|
|
|
|
store.colFamily.name
|
|
|
|
|
|
|
|
proc get*(
|
|
|
|
ns: RocksNamespaceRef,
|
|
|
|
key: openArray[byte],
|
|
|
|
onData: kvstore.DataProc): KvResult[bool] =
|
|
|
|
ns.colFamily.get(key, onData)
|
|
|
|
|
|
|
|
proc find*(
|
|
|
|
ns: RocksNamespaceRef,
|
|
|
|
prefix: openArray[byte],
|
|
|
|
onFind: kvstore.KeyValueProc): KvResult[int] =
|
|
|
|
raiseAssert "Unimplemented"
|
|
|
|
|
|
|
|
proc put*(ns: RocksNamespaceRef, key, value: openArray[byte]): KvResult[void] =
|
|
|
|
ns.colFamily.put(key, value)
|
|
|
|
|
|
|
|
proc contains*(ns: RocksNamespaceRef, key: openArray[byte]): KvResult[bool] =
|
|
|
|
ns.colFamily.keyExists(key)
|
|
|
|
|
|
|
|
proc del*(ns: RocksNamespaceRef, key: openArray[byte]): KvResult[bool] =
|
|
|
|
let exists = ? ns.colFamily.keyExists(key)
|
|
|
|
if not exists:
|
|
|
|
return ok(false)
|
|
|
|
|
|
|
|
let res = ns.colFamily.delete(key)
|
|
|
|
if res.isErr():
|
|
|
|
return err(res.error())
|
|
|
|
|
|
|
|
ok(true)
|
|
|
|
|
|
|
|
proc clear*(ns: RocksNamespaceRef): KvResult[bool] =
|
|
|
|
raiseAssert "Unimplemented"
|
|
|
|
|
|
|
|
proc close*(ns: RocksNamespaceRef) =
|
|
|
|
# To close the database, call close on RocksStoreRef.
|
|
|
|
raiseAssert "Unimplemented"
|
|
|
|
|
|
|
|
proc openNamespace*(
|
|
|
|
store: RocksStoreRef,
|
|
|
|
name: string): KvResult[RocksNamespaceRef] =
|
|
|
|
doAssert not store.db.isClosed()
|
|
|
|
|
2024-06-27 08:51:43 +00:00
|
|
|
ok(RocksNamespaceRef(colFamily: ?store.db.getColFamily(name)))
|