2023-06-20 14:26:25 +01:00
|
|
|
# nimbus-eth1
|
2024-04-16 20:39:11 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-06-20 14:26:25 +01: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 fetch data record
|
|
|
|
## ==========================
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
eth/common,
|
|
|
|
rocksdb,
|
2023-09-12 19:45:12 +01:00
|
|
|
results,
|
2024-04-22 19:02:22 +00:00
|
|
|
stew/keyed_queue,
|
2024-06-10 12:04:22 +00:00
|
|
|
../../[aristo_blobify, aristo_desc],
|
2024-04-16 20:39:11 +00:00
|
|
|
../init_common,
|
2023-06-20 14:26:25 +01:00
|
|
|
./rdb_desc
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
const
|
|
|
|
extraTraceMessages = false
|
|
|
|
## Enable additional logging noise
|
|
|
|
|
|
|
|
when extraTraceMessages:
|
|
|
|
import
|
|
|
|
chronicles
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "aristo-rocksdb"
|
|
|
|
|
2024-06-10 12:04:22 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc getAdm*(rdb: RdbInst; xid: AdminTabID): Result[Blob,(AristoError,string)] =
|
2024-04-22 19:02:22 +00:00
|
|
|
var res: Blob
|
|
|
|
let onData = proc(data: openArray[byte]) =
|
|
|
|
res = @data
|
|
|
|
|
2024-06-10 12:04:22 +00:00
|
|
|
let gotData = rdb.admCol.get(xid.toOpenArray, onData).valueOr:
|
|
|
|
const errSym = RdbBeDriverGetAdmError
|
2024-04-22 19:02:22 +00:00
|
|
|
when extraTraceMessages:
|
2024-06-10 12:04:22 +00:00
|
|
|
trace logTxt "getAdm", xid, error=errSym, info=error
|
2024-04-22 19:02:22 +00:00
|
|
|
return err((errSym,error))
|
|
|
|
|
|
|
|
# Correct result if needed
|
|
|
|
if not gotData:
|
|
|
|
res = EmptyBlob
|
2024-05-24 11:27:17 +02:00
|
|
|
ok move(res)
|
2024-04-22 19:02:22 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
|
2024-06-10 12:04:22 +00:00
|
|
|
proc getKey*(
|
|
|
|
rdb: var RdbInst;
|
|
|
|
vid: VertexID;
|
|
|
|
): Result[HashKey,(AristoError,string)] =
|
2024-04-22 19:02:22 +00:00
|
|
|
# Try LRU cache first
|
2024-06-10 12:04:22 +00:00
|
|
|
var rc = rdb.rdKeyLru.lruFetch(vid)
|
2024-04-22 19:02:22 +00:00
|
|
|
if rc.isOK:
|
2024-05-24 11:27:17 +02:00
|
|
|
return ok(move(rc.value))
|
2024-04-16 20:39:11 +00:00
|
|
|
|
2024-04-22 19:02:22 +00:00
|
|
|
# Otherwise fetch from backend database
|
2024-06-11 11:38:58 +02:00
|
|
|
var res: Result[HashKey,(AristoError,string)]
|
2024-06-10 12:04:22 +00:00
|
|
|
let onData = proc(data: openArray[byte]) =
|
2024-06-11 11:38:58 +02:00
|
|
|
res = HashKey.fromBytes(data).mapErr(proc(): auto =
|
|
|
|
(RdbHashKeyExpected,""))
|
2024-06-10 12:04:22 +00:00
|
|
|
|
|
|
|
let gotData = rdb.keyCol.get(vid.toOpenArray, onData).valueOr:
|
|
|
|
const errSym = RdbBeDriverGetKeyError
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "getKey", vid, error=errSym, info=error
|
|
|
|
return err((errSym,error))
|
|
|
|
|
|
|
|
# Correct result if needed
|
2024-06-11 11:38:58 +02:00
|
|
|
if not gotData:
|
|
|
|
res = ok(VOID_HASH_KEY)
|
|
|
|
elif res.isErr():
|
|
|
|
return res # Parsing failed
|
2024-04-22 19:02:22 +00:00
|
|
|
|
|
|
|
# Update cache and return
|
2024-06-11 11:38:58 +02:00
|
|
|
ok rdb.rdKeyLru.lruAppend(vid, res.value(), RdKeyLruMaxSize)
|
2024-04-22 19:02:22 +00:00
|
|
|
|
2024-06-10 12:04:22 +00:00
|
|
|
proc getVtx*(
|
|
|
|
rdb: var RdbInst;
|
|
|
|
vid: VertexID;
|
|
|
|
): Result[VertexRef,(AristoError,string)] =
|
2024-04-22 19:02:22 +00:00
|
|
|
# Try LRU cache first
|
2024-06-10 12:04:22 +00:00
|
|
|
var rc = rdb.rdVtxLru.lruFetch(vid)
|
2024-04-22 19:02:22 +00:00
|
|
|
if rc.isOK:
|
2024-05-24 11:27:17 +02:00
|
|
|
return ok(move(rc.value))
|
2024-04-22 19:02:22 +00:00
|
|
|
|
|
|
|
# Otherwise fetch from backend database
|
2024-06-11 11:38:58 +02:00
|
|
|
var res: Result[VertexRef,(AristoError,string)]
|
2024-06-10 12:04:22 +00:00
|
|
|
let onData = proc(data: openArray[byte]) =
|
2024-06-11 11:38:58 +02:00
|
|
|
res = data.deblobify(VertexRef).mapErr(proc(error: AristoError): auto =
|
|
|
|
(error,""))
|
2024-06-10 12:04:22 +00:00
|
|
|
|
|
|
|
let gotData = rdb.vtxCol.get(vid.toOpenArray, onData).valueOr:
|
|
|
|
const errSym = RdbBeDriverGetVtxError
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "getVtx", vid, error=errSym, info=error
|
|
|
|
return err((errSym,error))
|
|
|
|
|
2024-06-11 11:38:58 +02:00
|
|
|
if not gotData:
|
|
|
|
res = ok(VertexRef(nil))
|
|
|
|
elif res.isErr():
|
|
|
|
return res # Parsing failed
|
2024-04-22 19:02:22 +00:00
|
|
|
|
|
|
|
# Update cache and return
|
2024-06-11 11:38:58 +02:00
|
|
|
ok rdb.rdVtxLru.lruAppend(vid, res.value(), RdVtxLruMaxSize)
|
2023-06-20 14:26:25 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|