@cryptotaxi247 / kubo / commits / fedcebf67

refactor: re-use wantlist.Entry type wherever it makes sense

it seems to make sense since, in each place, the Key and Priority represent the same information b/c you know the saying... "It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures." License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>

Brian Tiger Chow committed Dec 16, 2014 at 22:08 UTC fedcebf67a6b01339bcd57397dcee2484a45cf97
5 files changed +26 -22
exchange/bitswap/bitswap.go
+3 -3
@@ -172,7 +172,7 @@ func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.Peer) e
172 }
173 message := bsmsg.New()
174 for _, wanted := range bs.wantlist.Entries() {
175 - message.AddEntry(wanted.Value, wanted.Priority)
175 + message.AddEntry(wanted.Key, wanted.Priority)
176 }
177 wg := sync.WaitGroup{}
178 for peerToQuery := range peers {
@@ -210,7 +210,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan
210 message := bsmsg.New()
211 message.SetFull(true)
212 for _, e := range bs.wantlist.Entries() {
213 - message.AddEntry(e.Value, e.Priority)
213 + message.AddEntry(e.Key, e.Priority)
214 }
215
216 ps := pset.NewPeerSet()
@@ -229,7 +229,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan
229 bs.send(ctx, prov, message)
230 }
231 }
232 - }(e.Value)
232 + }(e.Key)
233 }
234 wg.Wait()
235 }
exchange/bitswap/message/message.go
+8 -6
@@ -5,6 +5,7 @@ import (
5
6 blocks "github.com/jbenet/go-ipfs/blocks"
7 pb "github.com/jbenet/go-ipfs/exchange/bitswap/message/internal/pb"
8 + wantlist "github.com/jbenet/go-ipfs/exchange/bitswap/wantlist"
9 inet "github.com/jbenet/go-ipfs/net"
10 u "github.com/jbenet/go-ipfs/util"
11
@@ -64,9 +65,8 @@ func newMsg() *impl {
65 }
66
67 type Entry struct {
67 - Key u.Key
68 - Priority int
69 - Cancel bool
68 + wantlist.Entry
69 + Cancel bool
70 }
71
72 func newMessageFromProto(pbm pb.Message) BitSwapMessage {
@@ -121,9 +121,11 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
121 e.Cancel = cancel
122 } else {
123 m.wantlist[k] = &Entry{
124 - Key: k,
125 - Priority: priority,
126 - Cancel: cancel,
124 + Entry: wantlist.Entry{
125 + Key: k,
126 + Priority: priority,
127 + },
128 + Cancel: cancel,
129 }
130 }
131 }
exchange/bitswap/strategy/ledgermanager.go
+1 -1
@@ -61,7 +61,7 @@ func (lm *LedgerManager) taskWorker(ctx context.Context) {
61 }
62 continue
63 }
64 - block, err := lm.bs.Get(nextTask.Key)
64 + block, err := lm.bs.Get(nextTask.Entry.Key)
65 if err != nil {
66 continue // TODO maybe return an error
67 }
exchange/bitswap/strategy/taskqueue.go
+12 -10
@@ -1,6 +1,7 @@
1 package strategy
2
3 import (
4 + wantlist "github.com/jbenet/go-ipfs/exchange/bitswap/wantlist"
5 peer "github.com/jbenet/go-ipfs/peer"
6 u "github.com/jbenet/go-ipfs/util"
7 )
@@ -20,9 +21,8 @@ func newTaskQueue() *taskQueue {
21 }
22
23 type task struct {
23 - Key u.Key
24 - Target peer.Peer
25 - theirPriority int
24 + Entry wantlist.Entry
25 + Target peer.Peer
26 }
27
28 // Push currently adds a new task to the end of the list
@@ -31,13 +31,15 @@ func (tl *taskQueue) Push(block u.Key, priority int, to peer.Peer) {
31 if task, ok := tl.taskmap[taskKey(to, block)]; ok {
32 // TODO: when priority queue is implemented,
33 // rearrange this task
34 - task.theirPriority = priority
34 + task.Entry.Priority = priority
35 return
36 }
37 task := &task{
38 - Key: block,
39 - Target: to,
40 - theirPriority: priority,
38 + Entry: wantlist.Entry{
39 + Key: block,
40 + Priority: priority,
41 + },
42 + Target: to,
43 }
44 tl.tasks = append(tl.tasks, task)
45 tl.taskmap[taskKey(to, block)] = task
@@ -52,9 +54,9 @@ func (tl *taskQueue) Pop() *task {
54 // the same block from multiple peers
55 out = tl.tasks[0]
56 tl.tasks = tl.tasks[1:]
55 - delete(tl.taskmap, taskKey(out.Target, out.Key))
57 + delete(tl.taskmap, taskKey(out.Target, out.Entry.Key))
58 // Filter out blocks that have been cancelled
57 - if out.theirPriority >= 0 {
59 + if out.Entry.Priority >= 0 { // FIXME separate the "cancel" signal from priority
60 break
61 }
62 }
@@ -66,7 +68,7 @@ func (tl *taskQueue) Pop() *task {
68 func (tl *taskQueue) Remove(k u.Key, p peer.Peer) {
69 t, ok := tl.taskmap[taskKey(p, k)]
70 if ok {
69 - t.theirPriority = -1
71 + t.Entry.Priority = -1
72 }
73 }
74
exchange/bitswap/wantlist/wantlist.go
+2 -2
@@ -18,7 +18,7 @@ func New() *Wantlist {
18 }
19
20 type Entry struct {
21 - Value u.Key
21 + Key u.Key
22 Priority int
23 }
24
@@ -29,7 +29,7 @@ func (w *Wantlist) Add(k u.Key, priority int) {
29 return
30 }
31 w.set[k] = &Entry{
32 - Value: k,
32 + Key: k,
33 Priority: priority,
34 }
35 }