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 ...@@ -172,7 +172,7 @@ func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.Peer) e
} }
message := bsmsg.New() message := bsmsg.New()
for _, wanted := range bs.wantlist.Entries() { for _, wanted := range bs.wantlist.Entries() {
message.AddEntry(wanted.Value, wanted.Priority) message.AddEntry(wanted.Key, wanted.Priority)
} }
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
for peerToQuery := range peers { for peerToQuery := range peers {
...@@ -210,7 +210,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan ...@@ -210,7 +210,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan
message := bsmsg.New() message := bsmsg.New()
message.SetFull(true) message.SetFull(true)
for _, e := range bs.wantlist.Entries() { for _, e := range bs.wantlist.Entries() {
message.AddEntry(e.Value, e.Priority) message.AddEntry(e.Key, e.Priority)
} }
ps := pset.NewPeerSet() ps := pset.NewPeerSet()
...@@ -229,7 +229,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan ...@@ -229,7 +229,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan
bs.send(ctx, prov, message) bs.send(ctx, prov, message)
} }
} }
}(e.Value) }(e.Key)
} }
wg.Wait() wg.Wait()
} }
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
blocks "github.com/jbenet/go-ipfs/blocks" blocks "github.com/jbenet/go-ipfs/blocks"
pb "github.com/jbenet/go-ipfs/exchange/bitswap/message/internal/pb" 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" inet "github.com/jbenet/go-ipfs/net"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
...@@ -64,8 +65,7 @@ func newMsg() *impl { ...@@ -64,8 +65,7 @@ func newMsg() *impl {
} }
type Entry struct { type Entry struct {
Key u.Key wantlist.Entry
Priority int
Cancel bool Cancel bool
} }
...@@ -121,8 +121,10 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) { ...@@ -121,8 +121,10 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
e.Cancel = cancel e.Cancel = cancel
} else { } else {
m.wantlist[k] = &Entry{ m.wantlist[k] = &Entry{
Entry: wantlist.Entry{
Key: k, Key: k,
Priority: priority, Priority: priority,
},
Cancel: cancel, Cancel: cancel,
} }
} }
......
...@@ -61,7 +61,7 @@ func (lm *LedgerManager) taskWorker(ctx context.Context) { ...@@ -61,7 +61,7 @@ func (lm *LedgerManager) taskWorker(ctx context.Context) {
} }
continue continue
} }
block, err := lm.bs.Get(nextTask.Key) block, err := lm.bs.Get(nextTask.Entry.Key)
if err != nil { if err != nil {
continue // TODO maybe return an error continue // TODO maybe return an error
} }
......
package strategy package strategy
import ( import (
wantlist "github.com/jbenet/go-ipfs/exchange/bitswap/wantlist"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
) )
...@@ -20,9 +21,8 @@ func newTaskQueue() *taskQueue { ...@@ -20,9 +21,8 @@ func newTaskQueue() *taskQueue {
} }
type task struct { type task struct {
Key u.Key Entry wantlist.Entry
Target peer.Peer Target peer.Peer
theirPriority int
} }
// Push currently adds a new task to the end of the list // 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,13 +31,15 @@ func (tl *taskQueue) Push(block u.Key, priority int, to peer.Peer) {
if task, ok := tl.taskmap[taskKey(to, block)]; ok { if task, ok := tl.taskmap[taskKey(to, block)]; ok {
// TODO: when priority queue is implemented, // TODO: when priority queue is implemented,
// rearrange this task // rearrange this task
task.theirPriority = priority task.Entry.Priority = priority
return return
} }
task := &task{ task := &task{
Entry: wantlist.Entry{
Key: block, Key: block,
Priority: priority,
},
Target: to, Target: to,
theirPriority: priority,
} }
tl.tasks = append(tl.tasks, task) tl.tasks = append(tl.tasks, task)
tl.taskmap[taskKey(to, block)] = task tl.taskmap[taskKey(to, block)] = task
...@@ -52,9 +54,9 @@ func (tl *taskQueue) Pop() *task { ...@@ -52,9 +54,9 @@ func (tl *taskQueue) Pop() *task {
// the same block from multiple peers // the same block from multiple peers
out = tl.tasks[0] out = tl.tasks[0]
tl.tasks = tl.tasks[1:] 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 // Filter out blocks that have been cancelled
if out.theirPriority >= 0 { if out.Entry.Priority >= 0 { // FIXME separate the "cancel" signal from priority
break break
} }
} }
...@@ -66,7 +68,7 @@ func (tl *taskQueue) Pop() *task { ...@@ -66,7 +68,7 @@ func (tl *taskQueue) Pop() *task {
func (tl *taskQueue) Remove(k u.Key, p peer.Peer) { func (tl *taskQueue) Remove(k u.Key, p peer.Peer) {
t, ok := tl.taskmap[taskKey(p, k)] t, ok := tl.taskmap[taskKey(p, k)]
if ok { if ok {
t.theirPriority = -1 t.Entry.Priority = -1
} }
} }
......
...@@ -18,7 +18,7 @@ func New() *Wantlist { ...@@ -18,7 +18,7 @@ func New() *Wantlist {
} }
type Entry struct { type Entry struct {
Value u.Key Key u.Key
Priority int Priority int
} }
...@@ -29,7 +29,7 @@ func (w *Wantlist) Add(k u.Key, priority int) { ...@@ -29,7 +29,7 @@ func (w *Wantlist) Add(k u.Key, priority int) {
return return
} }
w.set[k] = &Entry{ w.set[k] = &Entry{
Value: k, Key: k,
Priority: priority, 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