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
ae1f7688
Commit
ae1f7688
authored
Sep 17, 2014
by
Juan Batiz-Benet
Committed by
Brian Tiger Chow
Sep 22, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
separate to ensure sync safety
parent
9e2c3fb8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
34 deletions
+42
-34
peer/queue/distance.go
peer/queue/distance.go
+42
-34
No files found.
peer/queue/distance.go
View file @
ae1f7688
...
...
@@ -10,61 +10,69 @@ import (
u
"github.com/jbenet/go-ipfs/util"
)
// peer
Distance
tracks a peer and its distance to something else.
type
peer
Distance
struct
{
// peer
Metric
tracks a peer and its distance to something else.
type
peer
Metric
struct
{
// the peer
peer
*
peer
.
Peer
// big.Int for XOR metric
distance
*
big
.
Int
metric
*
big
.
Int
}
// distancePQ implements heap.Interface and PeerQueue
type
distancePQ
struct
{
// from is the Key this PQ measures against
from
ks
.
Key
// peers is a heap of peerDistance items
peers
[]
*
peerDistance
sync
.
RWMutex
}
// peerMetricHeap implements a heap of peerDistances
type
peerMetricHeap
[]
*
peerMetric
func
(
p
q
*
distancePQ
)
Len
()
int
{
return
len
(
p
q
.
peers
)
func
(
p
h
peerMetricHeap
)
Len
()
int
{
return
len
(
p
h
)
}
func
(
p
q
*
distancePQ
)
Less
(
i
,
j
int
)
bool
{
return
-
1
==
p
q
.
peers
[
i
]
.
distance
.
Cmp
(
pq
.
peers
[
j
]
.
distance
)
func
(
p
h
peerMetricHeap
)
Less
(
i
,
j
int
)
bool
{
return
-
1
==
p
h
[
i
]
.
metric
.
Cmp
(
ph
[
j
]
.
metric
)
}
func
(
pq
*
distancePQ
)
Swap
(
i
,
j
int
)
{
p
:=
pq
.
peers
p
[
i
],
p
[
j
]
=
p
[
j
],
p
[
i
]
func
(
ph
peerMetricHeap
)
Swap
(
i
,
j
int
)
{
ph
[
i
],
ph
[
j
]
=
ph
[
j
],
ph
[
i
]
}
func
(
p
q
*
distancePQ
)
Push
(
x
interface
{})
{
item
:=
x
.
(
*
peer
Distance
)
pq
.
peers
=
append
(
pq
.
peers
,
item
)
func
(
p
h
*
peerMetricHeap
)
Push
(
x
interface
{})
{
item
:=
x
.
(
*
peer
Metric
)
*
ph
=
append
(
*
ph
,
item
)
}
func
(
p
q
*
distancePQ
)
Pop
()
interface
{}
{
old
:=
pq
.
peers
func
(
p
h
*
peerMetricHeap
)
Pop
()
interface
{}
{
old
:=
*
ph
n
:=
len
(
old
)
item
:=
old
[
n
-
1
]
pq
.
peers
=
old
[
0
:
n
-
1
]
*
ph
=
old
[
0
:
n
-
1
]
return
item
}
// distancePQ implements heap.Interface and PeerQueue
type
distancePQ
struct
{
// from is the Key this PQ measures against
from
ks
.
Key
// heap is a heap of peerDistance items
heap
peerMetricHeap
sync
.
RWMutex
}
func
(
pq
*
distancePQ
)
Len
()
int
{
pq
.
Lock
()
defer
pq
.
Unlock
()
return
len
(
pq
.
heap
)
}
func
(
pq
*
distancePQ
)
Enqueue
(
p
*
peer
.
Peer
)
{
pq
.
Lock
()
defer
pq
.
Unlock
()
distance
:=
ks
.
XORKeySpace
.
Key
(
p
.
ID
)
.
Distance
(
pq
.
from
)
heap
.
Push
(
pq
,
&
peer
Distance
{
peer
:
p
,
distance
:
distance
,
heap
.
Push
(
&
pq
.
heap
,
&
peer
Metric
{
peer
:
p
,
metric
:
distance
,
})
}
...
...
@@ -72,13 +80,13 @@ func (pq *distancePQ) Dequeue() *peer.Peer {
pq
.
Lock
()
defer
pq
.
Unlock
()
if
len
(
pq
.
peers
)
<
1
{
if
len
(
pq
.
heap
)
<
1
{
panic
(
"called Dequeue on an empty PeerQueue"
)
// will panic internally anyway, but we can help debug here
}
o
:=
heap
.
Pop
(
pq
)
p
:=
o
.
(
*
peer
Distance
)
o
:=
heap
.
Pop
(
&
pq
.
heap
)
p
:=
o
.
(
*
peer
Metric
)
return
p
.
peer
}
...
...
@@ -87,7 +95,7 @@ func (pq *distancePQ) Dequeue() *peer.Peer {
// XOR as a metric of distance).
func
NewXORDistancePQ
(
fromKey
u
.
Key
)
PeerQueue
{
return
&
distancePQ
{
from
:
ks
.
XORKeySpace
.
Key
([]
byte
(
fromKey
)),
peers
:
[]
*
peerDistance
{},
from
:
ks
.
XORKeySpace
.
Key
([]
byte
(
fromKey
)),
heap
:
peerMetricHeap
{},
}
}
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