mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-02 03:28:25 +08:00
refac(bitswap, util) extract KeySet
This commit is contained in:
@ -2,7 +2,6 @@ package bitswap
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
|
||||||
|
|
||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
|
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
|
|||||||
strategy: strategy.New(),
|
strategy: strategy.New(),
|
||||||
routing: directory,
|
routing: directory,
|
||||||
sender: networkAdapter,
|
sender: networkAdapter,
|
||||||
wantlist: WantList{
|
wantlist: u.NewKeySet(),
|
||||||
data: make(map[u.Key]struct{}),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
networkAdapter.SetDelegate(bs)
|
networkAdapter.SetDelegate(bs)
|
||||||
|
|
||||||
@ -58,38 +55,7 @@ type bitswap struct {
|
|||||||
// TODO(brian): save the strategy's state to the datastore
|
// TODO(brian): save the strategy's state to the datastore
|
||||||
strategy strategy.Strategy
|
strategy strategy.Strategy
|
||||||
|
|
||||||
wantlist WantList
|
wantlist u.KeySet
|
||||||
}
|
|
||||||
|
|
||||||
type WantList struct {
|
|
||||||
lock sync.RWMutex
|
|
||||||
data map[u.Key]struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wl *WantList) Add(k u.Key) {
|
|
||||||
u.DOut("Adding %v to Wantlist\n", k.Pretty())
|
|
||||||
wl.lock.Lock()
|
|
||||||
defer wl.lock.Unlock()
|
|
||||||
|
|
||||||
wl.data[k] = struct{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wl *WantList) Remove(k u.Key) {
|
|
||||||
u.DOut("Removing %v from Wantlist\n", k.Pretty())
|
|
||||||
wl.lock.Lock()
|
|
||||||
defer wl.lock.Unlock()
|
|
||||||
|
|
||||||
delete(wl.data, k)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wl *WantList) Keys() []u.Key {
|
|
||||||
wl.lock.RLock()
|
|
||||||
defer wl.lock.RUnlock()
|
|
||||||
keys := make([]u.Key, 0)
|
|
||||||
for k, _ := range wl.data {
|
|
||||||
keys = append(keys, k)
|
|
||||||
}
|
|
||||||
return keys
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBlock attempts to retrieve a particular block from peers within the
|
// GetBlock attempts to retrieve a particular block from peers within the
|
||||||
|
@ -289,9 +289,7 @@ func session(net tn.Network, rs tn.RoutingServer, id peer.ID) instance {
|
|||||||
strategy: strategy.New(),
|
strategy: strategy.New(),
|
||||||
routing: htc,
|
routing: htc,
|
||||||
sender: adapter,
|
sender: adapter,
|
||||||
wantlist: WantList{
|
wantlist: util.NewKeySet(),
|
||||||
data: make(map[util.Key]struct{}),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
adapter.SetDelegate(bs)
|
adapter.SetDelegate(bs)
|
||||||
return instance{
|
return instance{
|
||||||
|
@ -30,21 +30,3 @@ type Strategy interface {
|
|||||||
|
|
||||||
NumBytesReceivedFrom(*peer.Peer) uint64
|
NumBytesReceivedFrom(*peer.Peer) uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
type WantList interface {
|
|
||||||
// Peer returns the owner of the WantList
|
|
||||||
Peer() *peer.Peer
|
|
||||||
|
|
||||||
// Intersection returns the keys common to both WantLists
|
|
||||||
Intersection(WantList) WantList
|
|
||||||
|
|
||||||
KeySet
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(brian): potentially move this somewhere more generic. For now, it's
|
|
||||||
// useful in BitSwap operations.
|
|
||||||
|
|
||||||
type KeySet interface {
|
|
||||||
Contains(u.Key) bool
|
|
||||||
Keys() []u.Key
|
|
||||||
}
|
|
||||||
|
46
util/key_set.go
Normal file
46
util/key_set.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type KeySet interface {
|
||||||
|
Add(Key)
|
||||||
|
Remove(Key)
|
||||||
|
Keys() []Key
|
||||||
|
}
|
||||||
|
|
||||||
|
type ks struct {
|
||||||
|
lock sync.RWMutex
|
||||||
|
data map[Key]struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewKeySet() KeySet {
|
||||||
|
return &ks{
|
||||||
|
data: make(map[Key]struct{}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wl *ks) Add(k Key) {
|
||||||
|
wl.lock.Lock()
|
||||||
|
defer wl.lock.Unlock()
|
||||||
|
|
||||||
|
wl.data[k] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wl *ks) Remove(k Key) {
|
||||||
|
wl.lock.Lock()
|
||||||
|
defer wl.lock.Unlock()
|
||||||
|
|
||||||
|
delete(wl.data, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wl *ks) Keys() []Key {
|
||||||
|
wl.lock.RLock()
|
||||||
|
defer wl.lock.RUnlock()
|
||||||
|
keys := make([]Key, 0)
|
||||||
|
for k, _ := range wl.data {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
return keys
|
||||||
|
}
|
Reference in New Issue
Block a user