Skip to content

Commit

Permalink
missioncontrolstore: switch to generic list implementation
Browse files Browse the repository at this point in the history
This removes the need for type assertions.
  • Loading branch information
matheusd committed Apr 22, 2024
1 parent 242ac9f commit a49df7d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
decred.org/dcrwallet/v3 v3.1.1-0.20240303162647-7352f6fb06da
github.com/NebulousLabs/go-upnp v0.0.0-20181203152547-b32978b8ccbf
github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344
github.com/bahlo/generic-list-go v0.2.0
github.com/btcsuite/btcwallet/walletdb v1.4.0
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
github.com/davecgh/go-spew v1.1.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
26 changes: 13 additions & 13 deletions routing/missioncontrol_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package routing

import (
"bytes"
"container/list"
"encoding/binary"
"fmt"
"sync"
"time"

list "github.com/bahlo/generic-list-go"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrlnd/channeldb"
"github.com/decred/dcrlnd/kvdb"
Expand Down Expand Up @@ -43,7 +43,7 @@ type missionControlStore struct {
queueMx sync.Mutex

// queue stores all pending payment results not yet added to the store.
queue *list.List
queue *list.List[*paymentResult]

// queueChan is signalled when the first item is put into queue after
// a storeResult().
Expand All @@ -52,7 +52,7 @@ type missionControlStore struct {
// keys holds the stored MC store item keys in the order of storage.
// We use this list when adding/deleting items from the database to
// avoid cursor use which may be slow in the remote DB case.
keys *list.List
keys *list.List[string]

// keysMap holds the stored MC store item keys. We use this map to check
// if a new payment result has already been stored.
Expand All @@ -70,7 +70,7 @@ func newMissionControlStore(db kvdb.Backend, maxRecords int,
flushInterval time.Duration) (*missionControlStore, error) {

var (
keys *list.List
keys *list.List[string]
keysMap map[string]struct{}
)

Expand All @@ -92,7 +92,7 @@ func newMissionControlStore(db kvdb.Backend, maxRecords int,

return nil
}, func() {
keys = list.New()
keys = list.New[string]()
keysMap = make(map[string]struct{})
})
if err != nil {
Expand All @@ -104,7 +104,7 @@ func newMissionControlStore(db kvdb.Backend, maxRecords int,
return &missionControlStore{
done: make(chan struct{}),
db: db,
queue: list.New(),
queue: list.New[*paymentResult](),
queueChan: make(chan struct{}, 1),
keys: keys,
keysMap: keysMap,
Expand All @@ -131,7 +131,7 @@ func (b *missionControlStore) clear() error {
return err
}

b.queue = list.New()
b.queue = list.New[*paymentResult]()
return nil
}

Expand Down Expand Up @@ -328,7 +328,7 @@ func (b *missionControlStore) storeResults() error {
// return early from the func without running any updates.
isEmpty := l.Len() == 0
if !isEmpty {
b.queue = list.New()
b.queue = list.New[*paymentResult]()
}
b.queueMx.Unlock()

Expand All @@ -340,7 +340,7 @@ func (b *missionControlStore) storeResults() error {
// Create a deduped list of new entries.
newKeys := make(map[string]*paymentResult, l.Len())
for e := l.Front(); e != nil; {
pr := e.Value.(*paymentResult)
pr := e.Value
key := string(getResultKey(pr))
if _, ok := b.keysMap[key]; ok {
e, _ = e.Next(), l.Remove(e)
Expand All @@ -362,15 +362,15 @@ func (b *missionControlStore) storeResults() error {

// Delete as many as needed from old keys.
for e := b.keys.Front(); len(delKeys) < toDelete && e != nil; {
delKeys = append(delKeys, e.Value.(string))
delKeys = append(delKeys, e.Value)
e = e.Next()
}

// If more deletions are needed, simply do not add from the
// list of new keys.
for e := l.Front(); len(delKeys) < toDelete && e != nil; {
toDelete--
pr := e.Value.(*paymentResult)
pr := e.Value
key := string(getResultKey(pr))
delete(newKeys, key)
l.Remove(e)
Expand All @@ -384,7 +384,7 @@ func (b *missionControlStore) storeResults() error {
bucket := tx.ReadWriteBucket(resultsKey)

for e := l.Front(); e != nil; e = e.Next() {
pr := e.Value.(*paymentResult)
pr := e.Value
// Serialize result into key and value byte slices.
k, v, err := serializeResult(pr)
if err != nil {
Expand Down Expand Up @@ -425,7 +425,7 @@ func (b *missionControlStore) storeResults() error {
b.keys.Remove(b.keys.Front())
}
for e := l.Front(); e != nil; e = e.Next() {
pr := e.Value.(*paymentResult)
pr := e.Value
key := string(getResultKey(pr))
b.keys.PushBack(key)
}
Expand Down

0 comments on commit a49df7d

Please sign in to comment.