@@ -108,7 +108,7 @@ func (db *DB) GetKVStores(rule string, groupID session.ID,
108108 feature string ) KVStores {
109109
110110 return & kvStores {
111- DB : db ,
111+ db : db . DB ,
112112 ruleName : rule ,
113113 groupID : groupID ,
114114 featureName : feature ,
@@ -117,25 +117,12 @@ func (db *DB) GetKVStores(rule string, groupID session.ID,
117117
118118// kvStores implements the rules.KVStores interface.
119119type kvStores struct {
120- * DB
120+ db * bbolt. DB
121121 ruleName string
122122 groupID session.ID
123123 featureName string
124124}
125125
126- // beginTx starts db transaction. The transaction will be a read or read-write
127- // transaction depending on the value of the `writable` parameter.
128- func (s * kvStores ) beginTx (writable bool ) (* kvStoreTx , error ) {
129- boltTx , err := s .Begin (writable )
130- if err != nil {
131- return nil , err
132- }
133- return & kvStoreTx {
134- kvStores : s ,
135- boltTx : boltTx ,
136- }, nil
137- }
138-
139126// Update opens a database read/write transaction and executes the function f
140127// with the transaction passed as a parameter. After f exits, if f did not
141128// error, the transaction is committed. Otherwise, if f did error, the
@@ -144,30 +131,17 @@ func (s *kvStores) beginTx(writable bool) (*kvStoreTx, error) {
144131// returned.
145132//
146133// NOTE: this is part of the KVStores interface.
147- func (s * kvStores ) Update (ctx context.Context , f func (ctx context.Context ,
134+ func (s * kvStores ) Update (ctx context.Context , fn func (ctx context.Context ,
148135 tx KVStoreTx ) error ) error {
149136
150- tx , err := s .beginTx (true )
151- if err != nil {
152- return err
153- }
154-
155- // Make sure the transaction rolls back in the event of a panic.
156- defer func () {
157- if tx != nil {
158- _ = tx .boltTx .Rollback ()
137+ return s .db .Update (func (tx * bbolt.Tx ) error {
138+ boltTx := & kvStoreTx {
139+ boltTx : tx ,
140+ kvStores : s ,
159141 }
160- }()
161142
162- err = f (ctx , tx )
163- if err != nil {
164- // Want to return the original error, not a rollback error if
165- // any occur.
166- _ = tx .boltTx .Rollback ()
167- return err
168- }
169-
170- return tx .boltTx .Commit ()
143+ return fn (ctx , boltTx )
144+ })
171145}
172146
173147// View opens a database read transaction and executes the function f with the
@@ -176,31 +150,17 @@ func (s *kvStores) Update(ctx context.Context, f func(ctx context.Context,
176150// occur).
177151//
178152// NOTE: this is part of the KVStores interface.
179- func (s * kvStores ) View (ctx context.Context , f func (ctx context.Context ,
153+ func (s * kvStores ) View (ctx context.Context , fn func (ctx context.Context ,
180154 tx KVStoreTx ) error ) error {
181155
182- tx , err := s .beginTx (false )
183- if err != nil {
184- return err
185- }
186-
187- // Make sure the transaction rolls back in the event of a panic.
188- defer func () {
189- if tx != nil {
190- _ = tx .boltTx .Rollback ()
156+ return s .db .View (func (tx * bbolt.Tx ) error {
157+ boltTx := & kvStoreTx {
158+ boltTx : tx ,
159+ kvStores : s ,
191160 }
192- }()
193161
194- err = f (ctx , tx )
195- rollbackErr := tx .boltTx .Rollback ()
196- if err != nil {
197- return err
198- }
199-
200- if rollbackErr != nil {
201- return rollbackErr
202- }
203- return nil
162+ return fn (ctx , boltTx )
163+ })
204164}
205165
206166// getBucketFunc defines the signature of the bucket creation/fetching function
0 commit comments