2023-09-12 18:44:45 +00:00
|
|
|
# nimbus-eth1
|
2024-03-05 04:54:42 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-09-12 18:44:45 +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.
|
|
|
|
|
|
|
|
## Rocks DB store data record
|
|
|
|
## ==========================
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
eth/common,
|
|
|
|
rocksdb,
|
|
|
|
results,
|
|
|
|
../../kvt_desc,
|
|
|
|
./rdb_desc
|
|
|
|
|
|
|
|
const
|
2024-04-16 20:39:11 +00:00
|
|
|
extraTraceMessages = false
|
2023-09-12 18:44:45 +00:00
|
|
|
## Enable additional logging noise
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
2024-04-17 18:09:55 +00:00
|
|
|
import
|
|
|
|
chronicles,
|
|
|
|
stew/byteutils
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
logScope:
|
|
|
|
topics = "kvt-rocksdb"
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-04-16 20:39:11 +00:00
|
|
|
# Private helpers
|
2023-09-12 18:44:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
proc disposeSession(rdb: var RdbInst) =
|
|
|
|
rdb.session.close()
|
|
|
|
rdb.session = WriteBatchRef(nil)
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-04-17 18:09:55 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
proc `$`(a: Blob): string =
|
|
|
|
a.toHex
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
proc begin*(rdb: var RdbInst) =
|
|
|
|
if rdb.session.isNil:
|
2024-06-13 18:15:11 +00:00
|
|
|
rdb.session = rdb.baseDb.openWriteBatch()
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
proc rollback*(rdb: var RdbInst) =
|
|
|
|
if not rdb.session.isClosed():
|
|
|
|
rdb.disposeSession()
|
|
|
|
|
|
|
|
proc commit*(rdb: var RdbInst): Result[void,(KvtError,string)] =
|
|
|
|
if not rdb.session.isClosed():
|
|
|
|
defer: rdb.disposeSession()
|
2024-06-13 18:15:11 +00:00
|
|
|
rdb.baseDb.write(rdb.session).isOkOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
const errSym = RdbBeDriverWriteError
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "commit", error=errSym, info=error
|
|
|
|
return err((errSym,error))
|
|
|
|
ok()
|
2024-03-05 04:54:42 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
proc put*(
|
|
|
|
rdb: RdbInst;
|
|
|
|
data: openArray[(Blob,Blob)];
|
|
|
|
): Result[void,(Blob,KvtError,string)] =
|
|
|
|
for (key,val) in data:
|
|
|
|
if val.len == 0:
|
2024-06-13 18:15:11 +00:00
|
|
|
rdb.session.delete(key, $KvtGeneric).isOkOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
const errSym = RdbBeDriverDelError
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "del", key, error=errSym, info=error
|
|
|
|
return err((key,errSym,error))
|
2023-09-12 18:44:45 +00:00
|
|
|
else:
|
2024-06-13 18:15:11 +00:00
|
|
|
rdb.session.put(key, val, $KvtGeneric).isOkOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
const errSym = RdbBeDriverPutError
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "put", key, error=errSym, info=error
|
|
|
|
return err((key,errSym,error))
|
2023-09-12 18:44:45 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|