2023-06-20 13:26:25 +00:00
|
|
|
# nimbus-eth1
|
2024-03-05 04:54:42 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-06-20 13:26:25 +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,
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
2024-06-10 12:04:22 +00:00
|
|
|
stew/keyed_queue,
|
|
|
|
../../[aristo_blobify, aristo_desc],
|
2023-08-25 22:53:59 +00:00
|
|
|
../init_common,
|
2023-06-20 13:26:25 +00:00
|
|
|
./rdb_desc
|
|
|
|
|
|
|
|
const
|
2024-04-16 20:39:11 +00:00
|
|
|
extraTraceMessages = false
|
2023-06-20 13:26:25 +00:00
|
|
|
## Enable additional logging noise
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
import chronicles
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
logScope:
|
|
|
|
topics = "aristo-rocksdb"
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-04-16 20:39:11 +00:00
|
|
|
# Private helpers
|
2023-06-20 13:26:25 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
proc disposeSession(rdb: var RdbInst) =
|
|
|
|
rdb.session.close()
|
|
|
|
rdb.session = WriteBatchRef(nil)
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
proc begin*(rdb: var RdbInst) =
|
|
|
|
if rdb.session.isNil:
|
2024-06-10 12:04:22 +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():
|
2024-04-22 19:02:22 +00:00
|
|
|
rdb.rdKeyLru.clear() # Flush caches
|
|
|
|
rdb.rdVtxLru.clear() # Flush caches
|
2024-04-16 20:39:11 +00:00
|
|
|
rdb.disposeSession()
|
|
|
|
|
|
|
|
proc commit*(rdb: var RdbInst): Result[void,(AristoError,string)] =
|
|
|
|
if not rdb.session.isClosed():
|
|
|
|
defer: rdb.disposeSession()
|
2024-06-10 12:04:22 +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()
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2024-06-10 12:04:22 +00:00
|
|
|
|
|
|
|
proc putAdm*(
|
2024-04-22 19:02:22 +00:00
|
|
|
rdb: var RdbInst;
|
2024-06-10 12:04:22 +00:00
|
|
|
xid: AdminTabID;
|
2024-07-02 18:25:06 +00:00
|
|
|
data: openArray[byte];
|
2024-06-10 12:04:22 +00:00
|
|
|
): Result[void,(AdminTabID,AristoError,string)] =
|
|
|
|
let dsc = rdb.session
|
|
|
|
if data.len == 0:
|
2024-06-27 08:51:43 +00:00
|
|
|
dsc.delete(xid.toOpenArray, rdb.admCol.handle()).isOkOr:
|
2024-06-10 12:04:22 +00:00
|
|
|
const errSym = RdbBeDriverDelAdmError
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "putAdm()", xid, error=errSym, info=error
|
|
|
|
return err((xid,errSym,error))
|
|
|
|
else:
|
2024-06-27 08:51:43 +00:00
|
|
|
dsc.put(xid.toOpenArray, data, rdb.admCol.handle()).isOkOr:
|
2024-06-10 12:04:22 +00:00
|
|
|
const errSym = RdbBeDriverPutAdmError
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "putAdm()", xid, error=errSym, info=error
|
|
|
|
return err((xid,errSym,error))
|
2024-04-22 19:02:22 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
proc putKey*(
|
|
|
|
rdb: var RdbInst;
|
2024-06-25 11:39:53 +00:00
|
|
|
vid: VertexID, key: HashKey;
|
2024-06-10 12:04:22 +00:00
|
|
|
): Result[void,(VertexID,AristoError,string)] =
|
|
|
|
let dsc = rdb.session
|
2024-06-25 11:39:53 +00:00
|
|
|
if key.isValid:
|
2024-07-02 18:25:06 +00:00
|
|
|
dsc.put(vid.blobify().data(), key.data, rdb.keyCol.handle()).isOkOr:
|
2024-06-25 11:39:53 +00:00
|
|
|
# Caller must `rollback()` which will flush the `rdKeyLru` cache
|
|
|
|
const errSym = RdbBeDriverPutKeyError
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "putKey()", vid, error=errSym, info=error
|
|
|
|
return err((vid,errSym,error))
|
|
|
|
|
|
|
|
# Update cache
|
|
|
|
if not rdb.rdKeyLru.lruUpdate(vid, key):
|
|
|
|
discard rdb.rdKeyLru.lruAppend(vid, key, RdKeyLruMaxSize)
|
|
|
|
|
|
|
|
else:
|
2024-07-02 18:25:06 +00:00
|
|
|
dsc.delete(vid.blobify().data(), rdb.keyCol.handle()).isOkOr:
|
2024-06-25 11:39:53 +00:00
|
|
|
# Caller must `rollback()` which will flush the `rdKeyLru` cache
|
|
|
|
const errSym = RdbBeDriverDelKeyError
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "putKey()", vid, error=errSym, info=error
|
|
|
|
return err((vid,errSym,error))
|
|
|
|
|
|
|
|
# Update cache, vertex will most probably never be visited anymore
|
|
|
|
rdb.rdKeyLru.del vid
|
2024-06-10 12:04:22 +00:00
|
|
|
|
2024-04-22 19:02:22 +00:00
|
|
|
ok()
|
|
|
|
|
2024-06-10 12:04:22 +00:00
|
|
|
|
2024-04-22 19:02:22 +00:00
|
|
|
proc putVtx*(
|
|
|
|
rdb: var RdbInst;
|
2024-06-25 11:39:53 +00:00
|
|
|
vid: VertexID; vtx: VertexRef
|
2024-06-10 12:04:22 +00:00
|
|
|
): Result[void,(VertexID,AristoError,string)] =
|
|
|
|
let dsc = rdb.session
|
2024-06-25 11:39:53 +00:00
|
|
|
if vtx.isValid:
|
|
|
|
let rc = vtx.blobify()
|
|
|
|
if rc.isErr:
|
|
|
|
# Caller must `rollback()` which will flush the `rdVtxLru` cache
|
|
|
|
return err((vid,rc.error,""))
|
|
|
|
|
2024-07-02 18:25:06 +00:00
|
|
|
dsc.put(vid.blobify().data(), rc.value, rdb.vtxCol.handle()).isOkOr:
|
2024-06-25 11:39:53 +00:00
|
|
|
# Caller must `rollback()` which will flush the `rdVtxLru` cache
|
|
|
|
const errSym = RdbBeDriverPutVtxError
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "putVtx()", vid, error=errSym, info=error
|
|
|
|
return err((vid,errSym,error))
|
|
|
|
|
|
|
|
# Update cache
|
|
|
|
if not rdb.rdVtxLru.lruUpdate(vid, vtx):
|
|
|
|
discard rdb.rdVtxLru.lruAppend(vid, vtx, RdVtxLruMaxSize)
|
|
|
|
|
|
|
|
else:
|
2024-07-02 18:25:06 +00:00
|
|
|
dsc.delete(vid.blobify().data(), rdb.vtxCol.handle()).isOkOr:
|
2024-06-25 11:39:53 +00:00
|
|
|
# Caller must `rollback()` which will flush the `rdVtxLru` cache
|
|
|
|
const errSym = RdbBeDriverDelVtxError
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "putVtx()", vid, error=errSym, info=error
|
|
|
|
return err((vid,errSym,error))
|
|
|
|
|
|
|
|
# Update cache, vertex will most probably never be visited anymore
|
|
|
|
rdb.rdVtxLru.del vid
|
2024-06-10 12:04:22 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|