Skip to content

Commit 5fa0ab8

Browse files
authored
Merge pull request #974 from Elbehery/20250609-revert-drop-unused-txs-list
[release-1.4] Revert "Merge pull request #969 from nspcc-dev/drop-unused-txs"
2 parents f33bb13 + 2b74b9b commit 5fa0ab8

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

db.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ type DB struct {
132132
pageSize int
133133
opened bool
134134
rwtx *Tx
135+
txs []*Tx
135136

136137
freelist fl.Interface
137138
freelistLoad sync.Once
@@ -793,6 +794,9 @@ func (db *DB) beginTx() (*Tx, error) {
793794
t := &Tx{}
794795
t.init(db)
795796

797+
// Keep track of transaction until it closes.
798+
db.txs = append(db.txs, t)
799+
n := len(db.txs)
796800
if db.freelist != nil {
797801
db.freelist.AddReadonlyTXID(t.meta.Txid())
798802
}
@@ -803,7 +807,7 @@ func (db *DB) beginTx() (*Tx, error) {
803807
// Update the transaction stats.
804808
db.statlock.Lock()
805809
db.stats.TxN++
806-
db.stats.OpenTxN++
810+
db.stats.OpenTxN = n
807811
db.statlock.Unlock()
808812

809813
return t, nil
@@ -852,6 +856,17 @@ func (db *DB) removeTx(tx *Tx) {
852856
// Use the meta lock to restrict access to the DB object.
853857
db.metalock.Lock()
854858

859+
// Remove the transaction.
860+
for i, t := range db.txs {
861+
if t == tx {
862+
last := len(db.txs) - 1
863+
db.txs[i] = db.txs[last]
864+
db.txs[last] = nil
865+
db.txs = db.txs[:last]
866+
break
867+
}
868+
}
869+
n := len(db.txs)
855870
if db.freelist != nil {
856871
db.freelist.RemoveReadonlyTXID(tx.meta.Txid())
857872
}
@@ -861,7 +876,7 @@ func (db *DB) removeTx(tx *Tx) {
861876

862877
// Merge statistics.
863878
db.statlock.Lock()
864-
db.stats.OpenTxN--
879+
db.stats.OpenTxN = n
865880
db.stats.TxStats.add(&tx.stats)
866881
db.statlock.Unlock()
867882
}

0 commit comments

Comments
 (0)