@cryptotaxi247 / kubo / commits / 767d6ca63

refac(bitswap, util) extract KeySet

Brian Tiger Chow committed Sep 21, 2014 at 23:04 UTC 767d6ca633e37bfd9c9a23429a5fce44052852c9
4 files changed +49 -57
exchange/bitswap/bitswap.go
+2 -36
@@ -2,7 +2,6 @@ package bitswap
2
3 import (
4 "errors"
5 - "sync"
5
6 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7 ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
@@ -29,9 +28,7 @@ func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageS
28 strategy: strategy.New(),
29 routing: directory,
30 sender: networkAdapter,
32 - wantlist: WantList{
33 - data: make(map[u.Key]struct{}),
34 - },
31 + wantlist: u.NewKeySet(),
32 }
33 networkAdapter.SetDelegate(bs)
34
@@ -58,38 +55,7 @@ type bitswap struct {
55 // TODO(brian): save the strategy's state to the datastore
56 strategy strategy.Strategy
57
61 - wantlist WantList
62 -}
63 -
64 -type WantList struct {
65 - lock sync.RWMutex
66 - data map[u.Key]struct{}
67 -}
68 -
69 -func (wl *WantList) Add(k u.Key) {
70 - u.DOut("Adding %v to Wantlist\n", k.Pretty())
71 - wl.lock.Lock()
72 - defer wl.lock.Unlock()
73 -
74 - wl.data[k] = struct{}{}
75 -}
76 -
77 -func (wl *WantList) Remove(k u.Key) {
78 - u.DOut("Removing %v from Wantlist\n", k.Pretty())
79 - wl.lock.Lock()
80 - defer wl.lock.Unlock()
81 -
82 - delete(wl.data, k)
83 -}
84 -
85 -func (wl *WantList) Keys() []u.Key {
86 - wl.lock.RLock()
87 - defer wl.lock.RUnlock()
88 - keys := make([]u.Key, 0)
89 - for k, _ := range wl.data {
90 - keys = append(keys, k)
91 - }
92 - return keys
58 + wantlist u.KeySet
59 }
60
61 // GetBlock attempts to retrieve a particular block from peers within the
exchange/bitswap/bitswap_test.go
+1 -3
@@ -289,9 +289,7 @@ func session(net tn.Network, rs tn.RoutingServer, id peer.ID) instance {
289 strategy: strategy.New(),
290 routing: htc,
291 sender: adapter,
292 - wantlist: WantList{
293 - data: make(map[util.Key]struct{}),
294 - },
292 + wantlist: util.NewKeySet(),
293 }
294 adapter.SetDelegate(bs)
295 return instance{
exchange/bitswap/strategy/interface.go
-18
@@ -30,21 +30,3 @@ type Strategy interface {
30
31 NumBytesReceivedFrom(*peer.Peer) uint64
32 }
33 -
34 -type WantList interface {
35 - // Peer returns the owner of the WantList
36 - Peer() *peer.Peer
37 -
38 - // Intersection returns the keys common to both WantLists
39 - Intersection(WantList) WantList
40 -
41 - KeySet
42 -}
43 -
44 -// TODO(brian): potentially move this somewhere more generic. For now, it's
45 -// useful in BitSwap operations.
46 -
47 -type KeySet interface {
48 - Contains(u.Key) bool
49 - Keys() []u.Key
50 -}
util/key_set.go new
+46
@@ -0,0 +1,46 @@
1 +package util
2 +
3 +import (
4 + "sync"
5 +)
6 +
7 +type KeySet interface {
8 + Add(Key)
9 + Remove(Key)
10 + Keys() []Key
11 +}
12 +
13 +type ks struct {
14 + lock sync.RWMutex
15 + data map[Key]struct{}
16 +}
17 +
18 +func NewKeySet() KeySet {
19 + return &ks{
20 + data: make(map[Key]struct{}),
21 + }
22 +}
23 +
24 +func (wl *ks) Add(k Key) {
25 + wl.lock.Lock()
26 + defer wl.lock.Unlock()
27 +
28 + wl.data[k] = struct{}{}
29 +}
30 +
31 +func (wl *ks) Remove(k Key) {
32 + wl.lock.Lock()
33 + defer wl.lock.Unlock()
34 +
35 + delete(wl.data, k)
36 +}
37 +
38 +func (wl *ks) Keys() []Key {
39 + wl.lock.RLock()
40 + defer wl.lock.RUnlock()
41 + keys := make([]Key, 0)
42 + for k, _ := range wl.data {
43 + keys = append(keys, k)
44 + }
45 + return keys
46 +}