Commit 02fca42a authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Juan Batiz-Benet

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: default avatarBrian Tiger Chow <brian@perfmode.com>
parent b527a68b
......@@ -172,7 +172,7 @@ func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.Peer) e
}
message := bsmsg.New()
for _, wanted := range bs.wantlist.Entries() {
message.AddEntry(wanted.Value, wanted.Priority)
message.AddEntry(wanted.Key, wanted.Priority)
}
wg := sync.WaitGroup{}
for peerToQuery := range peers {
......@@ -210,7 +210,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan
message := bsmsg.New()
message.SetFull(true)
for _, e := range bs.wantlist.Entries() {
message.AddEntry(e.Value, e.Priority)
message.AddEntry(e.Key, e.Priority)
}
ps := pset.NewPeerSet()
......@@ -229,7 +229,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan
bs.send(ctx, prov, message)
}
}
}(e.Value)
}(e.Key)
}
wg.Wait()
}
......
......@@ -5,6 +5,7 @@ import (
blocks "github.com/jbenet/go-ipfs/blocks"
pb "github.com/jbenet/go-ipfs/exchange/bitswap/message/internal/pb"
wantlist "github.com/jbenet/go-ipfs/exchange/bitswap/wantlist"
inet "github.com/jbenet/go-ipfs/net"
u "github.com/jbenet/go-ipfs/util"
......@@ -64,9 +65,8 @@ func newMsg() *impl {
}
type Entry struct {
Key u.Key
Priority int
Cancel bool
wantlist.Entry
Cancel bool
}
func newMessageFromProto(pbm pb.Message) BitSwapMessage {
......@@ -121,9 +121,11 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
e.Cancel = cancel
} else {
m.wantlist[k] = &Entry{
Key: k,
Priority: priority,
Cancel: cancel,
Entry: wantlist.Entry{
Key: k,
Priority: priority,
},
Cancel: cancel,
}
}
}
......
......@@ -61,7 +61,7 @@ func (lm *LedgerManager) taskWorker(ctx context.Context) {
}
continue
}
block, err := lm.bs.Get(nextTask.Key)
block, err := lm.bs.Get(nextTask.Entry.Key)
if err != nil {
continue // TODO maybe return an error
}
......
package strategy
import (
wantlist "github.com/jbenet/go-ipfs/exchange/bitswap/wantlist"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
)
......@@ -20,9 +21,8 @@ func newTaskQueue() *taskQueue {
}
type task struct {
Key u.Key
Target peer.Peer
theirPriority int
Entry wantlist.Entry
Target peer.Peer
}
// 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) {
if task, ok := tl.taskmap[taskKey(to, block)]; ok {
// TODO: when priority queue is implemented,
// rearrange this task
task.theirPriority = priority
task.Entry.Priority = priority
return
}
task := &task{
Key: block,
Target: to,
theirPriority: priority,
Entry: wantlist.Entry{
Key: block,
Priority: priority,
},
Target: to,
}
tl.tasks = append(tl.tasks, task)
tl.taskmap[taskKey(to, block)] = task
......@@ -52,9 +54,9 @@ func (tl *taskQueue) Pop() *task {
// the same block from multiple peers
out = tl.tasks[0]
tl.tasks = tl.tasks[1:]
delete(tl.taskmap, taskKey(out.Target, out.Key))
delete(tl.taskmap, taskKey(out.Target, out.Entry.Key))
// Filter out blocks that have been cancelled
if out.theirPriority >= 0 {
if out.Entry.Priority >= 0 { // FIXME separate the "cancel" signal from priority
break
}
}
......@@ -66,7 +68,7 @@ func (tl *taskQueue) Pop() *task {
func (tl *taskQueue) Remove(k u.Key, p peer.Peer) {
t, ok := tl.taskmap[taskKey(p, k)]
if ok {
t.theirPriority = -1
t.Entry.Priority = -1
}
}
......
......@@ -18,7 +18,7 @@ func New() *Wantlist {
}
type Entry struct {
Value u.Key
Key u.Key
Priority int
}
......@@ -29,7 +29,7 @@ func (w *Wantlist) Add(k u.Key, priority int) {
return
}
w.set[k] = &Entry{
Value: k,
Key: k,
Priority: priority,
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment