-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
179 lines (129 loc) · 3.28 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package tempdb
import (
"bytes"
"sync"
"github.com/btcsuite/btcwallet/walletdb"
)
type Transaction struct {
lock *sync.Mutex
State *State
cursor *State
ID int
listeners []func()
Rolledback bool
}
func (tx *Transaction) ReadBucket(key []byte) walletdb.ReadBucket {
return tx.ReadWriteBucket(key)
}
func (tx *Transaction) ReadWriteBucket(key []byte) walletdb.ReadWriteBucket {
Logger.Debug("read/write top-level bucket", "bucket key", key, "transaction ID", tx.ID)
for _, bkt := range tx.State.Buckets {
if bkt.Parent != RootBucketID {
continue
}
if bytes.Equal(bkt.Key, key) {
return &bkt
}
}
return nil
}
func (tx *Transaction) ForEachBucket(f func(key []byte) error) error {
Logger.Debug("for each top-level bucket", "transaction ID", tx.ID)
for _, bkt := range tx.State.Buckets {
if bkt.Parent != RootBucketID {
continue
}
err := f(bkt.Key)
if err != nil {
return err
}
}
return nil
}
func (tx *Transaction) CreateTopLevelBucket(key []byte) (walletdb.ReadWriteBucket, error) {
Logger.Debug("creating top-level bucket", "bucket key", key, "transaction ID", tx.ID)
bkt := tx.ReadWriteBucket(key)
if bkt != nil {
return bkt, nil
}
return tx.createBucket(key, RootBucketID), nil
}
func (tx *Transaction) DeleteTopLevelBucket(key []byte) error {
Logger.Debug("delete top-level bucket", "bucket key", key, "transaction ID", tx.ID)
for i, bkt := range tx.State.Buckets {
if bkt.Parent != RootBucketID {
continue
}
if bytes.Equal(bkt.Key, key) {
tx.State.Buckets = append(tx.State.Buckets[:i], tx.State.Buckets[i+1:]...)
break
}
}
return nil
}
func (tx *Transaction) unlock() {
if tx.lock != nil {
tx.lock.Unlock()
// prevent duplicate unlocks.
tx.lock = nil
}
}
func (tx *Transaction) Commit() error {
defer tx.unlock()
Logger.Debug("transaction commit", "transaction", tx, "transaction ID", tx.ID)
if tx.Rolledback {
return walletdb.ErrTxClosed
}
// copy the transaction state then update the database state.
*tx.cursor = *tx.State.Copy()
for _, f := range tx.listeners {
f()
}
return nil
}
func (tx *Transaction) OnCommit(f func()) {
tx.listeners = append(tx.listeners, f)
}
func (tx *Transaction) Rollback() error {
defer tx.unlock()
Logger.Debug("transaction rollback", "transaction", tx, "transaction ID", tx.ID)
if tx.Rolledback {
return walletdb.ErrTxClosed
}
tx.Rolledback = true
return nil
}
func (tx *Transaction) createBucket(key []byte, parent BucketID) *Bucket {
bkt := Bucket{
tx: tx,
Key: key,
Value: make(map[string][]byte),
Parent: parent,
}
// create the bucket and use the allocated ID.
bkt.ID = tx.State.Add(bkt)
Logger.Debug("create bucket", "bucket key", key, "bucket ID", bkt.ID, "parent ID", parent, "transaction ID", tx.ID)
return &bkt
}
func newTransaction(state *State, lock *sync.Mutex) *Transaction {
if lock != nil {
lock.Lock()
}
// create a new transaction.
tx := &Transaction{
lock: lock,
cursor: state,
ID: state.nextTX,
}
// increment to next transaction ID.
state.nextTX += 1
Logger.Debug("create transaction", "transaction ID", tx.ID)
// deep copy the state.
tx.State = state.Copy()
// update the underlying transaction in each bucket.
for i, bkt := range tx.State.Buckets {
bkt.tx = tx
tx.State.Buckets[i] = bkt
}
return tx
}