Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
767d6ca6
Commit
767d6ca6
authored
Sep 21, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refac(bitswap, util) extract KeySet
parent
faee10ef
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
49 additions
and
57 deletions
+49
-57
exchange/bitswap/bitswap.go
exchange/bitswap/bitswap.go
+2
-36
exchange/bitswap/bitswap_test.go
exchange/bitswap/bitswap_test.go
+1
-3
exchange/bitswap/strategy/interface.go
exchange/bitswap/strategy/interface.go
+0
-18
util/key_set.go
util/key_set.go
+46
-0
No files found.
exchange/bitswap/bitswap.go
View file @
767d6ca6
...
...
@@ -2,7 +2,6 @@ package bitswap
import
(
"errors"
"sync"
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"
...
...
@@ -29,9 +28,7 @@ func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageS
strategy
:
strategy
.
New
(),
routing
:
directory
,
sender
:
networkAdapter
,
wantlist
:
WantList
{
data
:
make
(
map
[
u
.
Key
]
struct
{}),
},
wantlist
:
u
.
NewKeySet
(),
}
networkAdapter
.
SetDelegate
(
bs
)
...
...
@@ -58,38 +55,7 @@ type bitswap struct {
// TODO(brian): save the strategy's state to the datastore
strategy
strategy
.
Strategy
wantlist
WantList
}
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
wantlist
u
.
KeySet
}
// GetBlock attempts to retrieve a particular block from peers within the
...
...
exchange/bitswap/bitswap_test.go
View file @
767d6ca6
...
...
@@ -289,9 +289,7 @@ func session(net tn.Network, rs tn.RoutingServer, id peer.ID) instance {
strategy
:
strategy
.
New
(),
routing
:
htc
,
sender
:
adapter
,
wantlist
:
WantList
{
data
:
make
(
map
[
util
.
Key
]
struct
{}),
},
wantlist
:
util
.
NewKeySet
(),
}
adapter
.
SetDelegate
(
bs
)
return
instance
{
...
...
exchange/bitswap/strategy/interface.go
View file @
767d6ca6
...
...
@@ -30,21 +30,3 @@ type Strategy interface {
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
}
util/key_set.go
0 → 100644
View file @
767d6ca6
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment