@@ -32,6 +32,21 @@ const (
3232 unknownFailureSourceIdx = - 1
3333)
3434
35+ // missionControlDB is an interface that defines the database methods that a
36+ // single missionControlStore has access to. It allows the missionControlStore
37+ // to be unaware of the overall DB structure and restricts its access to the DB
38+ // by only providing it the bucket that it needs to care about.
39+ type missionControlDB interface {
40+ // update can be used to perform reads and writes on the given bucket.
41+ update (f func (bkt kvdb.RwBucket ) error , reset func ()) error
42+
43+ // view can be used to perform reads on the given bucket.
44+ view (f func (bkt kvdb.RBucket ) error , reset func ()) error
45+
46+ // purge will delete all the contents in this store.
47+ purge () error
48+ }
49+
3550// missionControlStore is a bolt db based implementation of a mission control
3651// store. It stores the raw payment attempt data from which the internal mission
3752// controls state can be rederived on startup. This allows the mission control
@@ -41,7 +56,7 @@ const (
4156type missionControlStore struct {
4257 done chan struct {}
4358 wg sync.WaitGroup
44- db kvdb. Backend
59+ db missionControlDB
4560
4661 // queueCond is signalled when items are put into the queue.
4762 queueCond * sync.Cond
@@ -67,7 +82,7 @@ type missionControlStore struct {
6782 flushInterval time.Duration
6883}
6984
70- func newMissionControlStore (db kvdb. Backend , maxRecords int ,
85+ func newMissionControlStore (db missionControlDB , maxRecords int ,
7186 flushInterval time.Duration ) (* missionControlStore , error ) {
7287
7388 var (
@@ -76,13 +91,7 @@ func newMissionControlStore(db kvdb.Backend, maxRecords int,
7691 )
7792
7893 // Create buckets if not yet existing.
79- err := kvdb .Update (db , func (tx kvdb.RwTx ) error {
80- resultsBucket , err := tx .CreateTopLevelBucket (resultsKey )
81- if err != nil {
82- return fmt .Errorf ("cannot create results bucket: %w" ,
83- err )
84- }
85-
94+ err := db .update (func (resultsBucket kvdb.RwBucket ) error {
8695 // Collect all keys to be able to quickly calculate the
8796 // difference when updating the DB state.
8897 c := resultsBucket .ReadCursor ()
@@ -119,29 +128,20 @@ func (b *missionControlStore) clear() error {
119128 b .queueCond .L .Lock ()
120129 defer b .queueCond .L .Unlock ()
121130
122- err := kvdb .Update (b .db , func (tx kvdb.RwTx ) error {
123- if err := tx .DeleteTopLevelBucket (resultsKey ); err != nil {
124- return err
125- }
126-
127- _ , err := tx .CreateTopLevelBucket (resultsKey )
128- return err
129- }, func () {})
130-
131- if err != nil {
131+ if err := b .db .purge (); err != nil {
132132 return err
133133 }
134134
135135 b .queue = list .New ()
136+
136137 return nil
137138}
138139
139140// fetchAll returns all results currently stored in the database.
140141func (b * missionControlStore ) fetchAll () ([]* paymentResult , error ) {
141142 var results []* paymentResult
142143
143- err := kvdb .View (b .db , func (tx kvdb.RTx ) error {
144- resultBucket := tx .ReadBucket (resultsKey )
144+ err := b .db .view (func (resultBucket kvdb.RBucket ) error {
145145 results = make ([]* paymentResult , 0 )
146146
147147 return resultBucket .ForEach (func (k , v []byte ) error {
@@ -511,9 +511,7 @@ func (b *missionControlStore) storeResults() error {
511511 }
512512 }
513513
514- err := kvdb .Update (b .db , func (tx kvdb.RwTx ) error {
515- bucket := tx .ReadWriteBucket (resultsKey )
516-
514+ err := b .db .update (func (bucket kvdb.RwBucket ) error {
517515 for e := l .Front (); e != nil ; e = e .Next () {
518516 pr , ok := e .Value .(* paymentResult )
519517 if ! ok {
0 commit comments