nimbus-eth1/execution_chain/db/kvt/kvt_tx_frame.nim
andri lim a1a9c6b027
Merge AristoTxRef/KvtTxRef with their LayerRef (#3078)
* Merge AristoTxRef/KvtTxRef with their LayerRef

also clean up unused files

* rm unused test_rocksdb

* Fix tests

* lint
2025-02-17 10:30:43 +07:00

87 lines
2.5 KiB
Nim

# nimbus-eth1
# Copyright (c) 2023-2025 Status Research & Development GmbH
# 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.
## Kvt DB -- Transaction frames helper
## ===================================
##
{.push raises: [].}
import
./[kvt_desc, kvt_layers]
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
proc txFrameBegin*(db: KvtDbRef, parent: KvtTxRef): KvtTxRef =
## Starts a new transaction.
##
## Example:
## ::
## proc doSomething(db: KvtDbRef) =
## let tx = db.begin
## defer: tx.rollback()
## ... continue using db ...
## tx.commit()
##
let parent = if parent == nil: db.txRef else: parent
KvtTxRef(
db: db,
parent: parent,
)
proc baseTxFrame*(db: KvtDbRef): KvtTxRef =
db.txRef
proc dispose*(
tx: KvtTxRef;
) =
tx[].reset()
proc txFramePersist*(
db: KvtDbRef;
batch: PutHdlRef;
txFrame: KvtTxRef;
) =
let be = db.backend
doAssert not be.isNil, "Persisting to backend requires ... a backend!"
if txFrame != db.txRef:
# Consolidate the changes from the old to the new base going from the
# bottom of the stack to avoid having to cascade each change through
# the full stack
assert txFrame.parent != nil
for frame in txFrame.stack():
if frame == db.txRef:
continue
mergeAndReset(db.txRef, frame)
frame.dispose()
# Put the now-merged contents in txFrame and make it the new base
swap(db.txRef[], txFrame[])
db.txRef = txFrame
# Store structural single trie entries
for k,v in txFrame.sTab:
be.putKvpFn(batch, k, v)
# TODO above, we only prepare the changes to the database but don't actually
# write them to disk - the code below that updates the frame should
# really run after things have been written (to maintain sync betweeen
# in-memory and on-disk state)
# Done with txRef, all saved to backend
txFrame.sTab.clear()
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------