Skip to content

Commit 696e6c5

Browse files
committed
cache the snapshot key
1 parent 36bfe7c commit 696e6c5

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

storage/badger_queue.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Queue struct {
1818
}
1919

2020
type PeerSnapshot struct {
21+
key crypto.Hash
2122
PeerId crypto.Hash
2223
Snapshot *common.Snapshot
2324
}
@@ -55,11 +56,10 @@ func (q *Queue) PutFinal(ps *PeerSnapshot) error {
5556
q.mutex.Lock()
5657
defer q.mutex.Unlock()
5758

58-
hash := ps.finalKey()
59-
if q.finalSet[hash] {
59+
if q.finalSet[ps.key] {
6060
return nil
6161
}
62-
q.finalSet[hash] = true
62+
q.finalSet[ps.key] = true
6363

6464
for {
6565
put, err := q.finalRing.Offer(ps)
@@ -79,19 +79,18 @@ func (q *Queue) PopFinal() (*PeerSnapshot, error) {
7979
return nil, err
8080
}
8181
ps := item.(*PeerSnapshot)
82-
delete(q.finalSet, ps.finalKey())
82+
delete(q.finalSet, ps.key)
8383
return ps, nil
8484
}
8585

8686
func (q *Queue) PutCache(ps *PeerSnapshot) error {
8787
q.mutex.Lock()
8888
defer q.mutex.Unlock()
8989

90-
hash := ps.cacheKey()
91-
if q.cacheSet[hash] {
90+
if q.cacheSet[ps.key] {
9291
return nil
9392
}
94-
q.cacheSet[hash] = true
93+
q.cacheSet[ps.key] = true
9594

9695
for {
9796
put, err := q.cacheRing.Offer(ps)
@@ -111,7 +110,7 @@ func (q *Queue) PopCache() (*PeerSnapshot, error) {
111110
return nil, err
112111
}
113112
ps := item.(*PeerSnapshot)
114-
delete(q.cacheSet, ps.cacheKey())
113+
delete(q.cacheSet, ps.key)
115114
return ps, nil
116115
}
117116

@@ -137,8 +136,10 @@ func (s *BadgerStore) QueueAppendSnapshot(peerId crypto.Hash, snap *common.Snaps
137136
Snapshot: snap,
138137
}
139138
if finalized {
139+
ps.key = ps.finalKey()
140140
return s.queue.PutFinal(ps)
141141
}
142+
ps.key = ps.cacheKey()
142143
return s.queue.PutCache(ps)
143144
}
144145

0 commit comments

Comments
 (0)