@cryptotaxi247 / kubo / commits / 86c438b6e

refactor() message API

performing CR in the form of a PR. Let me know what you think. License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>

Brian Tiger Chow committed Dec 15, 2014 at 22:38 UTC 86c438b6e1bafa60406409b4099545b4a9d44878
4 files changed +32 -18
exchange/bitswap/bitswap.go
+3 -3
@@ -169,7 +169,7 @@ func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.Peer) e
169 }
170 message := bsmsg.New()
171 for _, wanted := range bs.wantlist.Entries() {
172 - message.AddEntry(wanted.Value, wanted.Priority, false)
172 + message.AddEntry(wanted.Value, wanted.Priority)
173 }
174 wg := sync.WaitGroup{}
175 for peerToQuery := range peers {
@@ -207,7 +207,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan
207 message := bsmsg.New()
208 message.SetFull(true)
209 for _, e := range bs.wantlist.Entries() {
210 - message.AddEntry(e.Value, e.Priority, false)
210 + message.AddEntry(e.Value, e.Priority)
211 }
212
213 ps := pset.NewPeerSet()
@@ -335,7 +335,7 @@ func (bs *bitswap) cancelBlocks(ctx context.Context, bkeys []u.Key) {
335 message := bsmsg.New()
336 message.SetFull(false)
337 for _, k := range bkeys {
338 - message.AddEntry(k, 0, true)
338 + message.Cancel(k)
339 }
340 for _, p := range bs.ledgermanager.Peers() {
341 err := bs.send(ctx, p, message)
exchange/bitswap/message/message.go
+18 -4
@@ -24,7 +24,9 @@ type BitSwapMessage interface {
24 Blocks() []*blocks.Block
25
26 // AddEntry adds an entry to the Wantlist.
27 - AddEntry(key u.Key, priority int, cancel bool)
27 + AddEntry(key u.Key, priority int)
28 +
29 + Cancel(key u.Key)
30
31 // Sets whether or not the contained wantlist represents the entire wantlist
32 // true = full wantlist
@@ -50,6 +52,10 @@ type impl struct {
52 }
53
54 func New() BitSwapMessage {
55 + return newMsg()
56 +}
57 +
58 +func newMsg() *impl {
59 return &impl{
60 blocks: make(map[u.Key]*blocks.Block),
61 wantlist: make(map[u.Key]*Entry),
@@ -64,10 +70,10 @@ type Entry struct {
70 }
71
72 func newMessageFromProto(pbm pb.Message) BitSwapMessage {
67 - m := New()
73 + m := newMsg()
74 m.SetFull(pbm.GetWantlist().GetFull())
75 for _, e := range pbm.GetWantlist().GetEntries() {
70 - m.AddEntry(u.Key(e.GetBlock()), int(e.GetPriority()), e.GetCancel())
76 + m.addEntry(u.Key(e.GetBlock()), int(e.GetPriority()), e.GetCancel())
77 }
78 for _, d := range pbm.GetBlocks() {
79 b := blocks.NewBlock(d)
@@ -100,7 +106,15 @@ func (m *impl) Blocks() []*blocks.Block {
106 return bs
107 }
108
103 -func (m *impl) AddEntry(k u.Key, priority int, cancel bool) {
109 +func (m *impl) Cancel(k u.Key) {
110 + m.addEntry(k, 0, true)
111 +}
112 +
113 +func (m *impl) AddEntry(k u.Key, priority int) {
114 + m.addEntry(k, priority, false)
115 +}
116 +
117 +func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
118 e, exists := m.wantlist[k]
119 if exists {
120 e.Priority = priority
exchange/bitswap/message/message_test.go
+10 -10
@@ -14,7 +14,7 @@ import (
14 func TestAppendWanted(t *testing.T) {
15 const str = "foo"
16 m := New()
17 - m.AddEntry(u.Key(str), 1, false)
17 + m.AddEntry(u.Key(str), 1)
18
19 if !wantlistContains(m.ToProto().GetWantlist(), str) {
20 t.Fail()
@@ -63,7 +63,7 @@ func TestWantlist(t *testing.T) {
63 keystrs := []string{"foo", "bar", "baz", "bat"}
64 m := New()
65 for _, s := range keystrs {
66 - m.AddEntry(u.Key(s), 1, false)
66 + m.AddEntry(u.Key(s), 1)
67 }
68 exported := m.Wantlist()
69
@@ -86,7 +86,7 @@ func TestCopyProtoByValue(t *testing.T) {
86 const str = "foo"
87 m := New()
88 protoBeforeAppend := m.ToProto()
89 - m.AddEntry(u.Key(str), 1, false)
89 + m.AddEntry(u.Key(str), 1)
90 if wantlistContains(protoBeforeAppend.GetWantlist(), str) {
91 t.Fail()
92 }
@@ -94,11 +94,11 @@ func TestCopyProtoByValue(t *testing.T) {
94
95 func TestToNetFromNetPreservesWantList(t *testing.T) {
96 original := New()
97 - original.AddEntry(u.Key("M"), 1, false)
98 - original.AddEntry(u.Key("B"), 1, false)
99 - original.AddEntry(u.Key("D"), 1, false)
100 - original.AddEntry(u.Key("T"), 1, false)
101 - original.AddEntry(u.Key("F"), 1, false)
97 + original.AddEntry(u.Key("M"), 1)
98 + original.AddEntry(u.Key("B"), 1)
99 + original.AddEntry(u.Key("D"), 1)
100 + original.AddEntry(u.Key("T"), 1)
101 + original.AddEntry(u.Key("F"), 1)
102
103 var buf bytes.Buffer
104 if err := original.ToNet(&buf); err != nil {
@@ -174,8 +174,8 @@ func TestDuplicates(t *testing.T) {
174 b := blocks.NewBlock([]byte("foo"))
175 msg := New()
176
177 - msg.AddEntry(b.Key(), 1, false)
178 - msg.AddEntry(b.Key(), 1, false)
177 + msg.AddEntry(b.Key(), 1)
178 + msg.AddEntry(b.Key(), 1)
179 if len(msg.Wantlist()) != 1 {
180 t.Fatal("Duplicate in BitSwapMessage")
181 }
exchange/bitswap/strategy/ledgermanager_test.go
+1 -1
@@ -64,7 +64,7 @@ func TestBlockRecordedAsWantedAfterMessageReceived(t *testing.T) {
64 block := blocks.NewBlock([]byte("data wanted by beggar"))
65
66 messageFromBeggarToChooser := message.New()
67 - messageFromBeggarToChooser.AddEntry(block.Key(), 1, false)
67 + messageFromBeggarToChooser.AddEntry(block.Key(), 1)
68
69 chooser.ls.MessageReceived(beggar.Peer, messageFromBeggarToChooser)
70 // for this test, doesn't matter if you record that beggar sent