change TransactionFlags to TransactionState

This commit is contained in:
andri lim 2019-02-18 16:12:01 +07:00 committed by zah
parent 96f29a971d
commit c36b3e54cc
1 changed files with 10 additions and 16 deletions

View File

@ -34,7 +34,8 @@ type
containsProc: ContainsProc containsProc: ContainsProc
mostInnerTransaction: DbTransaction mostInnerTransaction: DbTransaction
TransactionFlags = enum TransactionState = enum
Pending
Committed Committed
RolledBack RolledBack
@ -42,7 +43,7 @@ type
db: TrieDatabaseRef db: TrieDatabaseRef
parentTransaction: DbTransaction parentTransaction: DbTransaction
modifications: MemoryLayer modifications: MemoryLayer
flags: set[TransactionFlags] state: TransactionState
proc put*(db: TrieDatabaseRef, key, val: openarray[byte]) {.gcsafe.} proc put*(db: TrieDatabaseRef, key, val: openarray[byte]) {.gcsafe.}
proc get*(db: TrieDatabaseRef, key: openarray[byte]): Bytes {.gcsafe.} proc get*(db: TrieDatabaseRef, key: openarray[byte]): Bytes {.gcsafe.}
@ -133,7 +134,7 @@ proc beginTransaction*(db: TrieDatabaseRef): DbTransaction =
new result new result
result.db = db result.db = db
init result.modifications init result.modifications
result.state = Pending
result.parentTransaction = db.mostInnerTransaction result.parentTransaction = db.mostInnerTransaction
db.mostInnerTransaction = result db.mostInnerTransaction = result
@ -141,32 +142,25 @@ proc rollback*(t: DbTransaction) =
# Transactions should be handled in a strictly nested fashion. # Transactions should be handled in a strictly nested fashion.
# Any child transaction must be committed or rolled-back before # Any child transaction must be committed or rolled-back before
# its parent transactions: # its parent transactions:
doAssert t.db.mostInnerTransaction == t and doAssert t.db.mostInnerTransaction == t and t.state == Pending
Committed notin t.flags and
RolledBack notin t.flags
t.db.mostInnerTransaction = t.parentTransaction t.db.mostInnerTransaction = t.parentTransaction
t.flags.incl RolledBack t.state = RolledBack
proc commit*(t: DbTransaction) = proc commit*(t: DbTransaction) =
# Transactions should be handled in a strictly nested fashion. # Transactions should be handled in a strictly nested fashion.
# Any child transaction must be committed or rolled-back before # Any child transaction must be committed or rolled-back before
# its parent transactions: # its parent transactions:
doAssert t.db.mostInnerTransaction == t and doAssert t.db.mostInnerTransaction == t and t.state == Pending
Committed notin t.flags and
RolledBack notin t.flags
t.db.mostInnerTransaction = t.parentTransaction t.db.mostInnerTransaction = t.parentTransaction
t.modifications.commit(t.db) t.modifications.commit(t.db)
t.flags.incl Committed t.state = Committed
proc dispose*(t: DbTransaction) {.inline.} = proc dispose*(t: DbTransaction) {.inline.} =
if Committed notin t.flags and if t.state == Pending:
RolledBack notin t.flags:
t.rollback() t.rollback()
proc safeDispose*(t: DbTransaction) {.inline.} = proc safeDispose*(t: DbTransaction) {.inline.} =
if t != nil and if t != nil and t.state == Pending:
Committed notin t.flags and
RolledBack notin t.flags:
t.rollback() t.rollback()
proc putImpl[T](db: RootRef, key, val: openarray[byte]) = proc putImpl[T](db: RootRef, key, val: openarray[byte]) =