Commit 2f8828de authored by Jeromy's avatar Jeromy

more work implementing coral type lookups

parent 534dbca4
...@@ -3,11 +3,16 @@ package dht ...@@ -3,11 +3,16 @@ package dht
import ( import (
"bytes" "bytes"
"crypto/sha256" "crypto/sha256"
"errors"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
) )
// Returned if a routing table query returns no results. This is NOT expected
// behaviour
var ErrLookupFailure = errors.New("failed to find any peer in table")
// ID for IpfsDHT should be a byte slice, to allow for simpler operations // ID for IpfsDHT should be a byte slice, to allow for simpler operations
// (xor). DHT ids are based on the peer.IDs. // (xor). DHT ids are based on the peer.IDs.
// //
...@@ -19,8 +24,8 @@ func (id ID) Equal(other ID) bool { ...@@ -19,8 +24,8 @@ func (id ID) Equal(other ID) bool {
return bytes.Equal(id, other) return bytes.Equal(id, other)
} }
func (id ID) Less(other interface{}) bool { func (id ID) Less(other ID) bool {
a, b := equalizeSizes(id, other.(ID)) a, b := equalizeSizes(id, other)
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
if a[i] != b[i] { if a[i] != b[i] {
return a[i] < b[i] return a[i] < b[i]
...@@ -80,3 +85,14 @@ func ConvertKey(id u.Key) ID { ...@@ -80,3 +85,14 @@ func ConvertKey(id u.Key) ID {
hash := sha256.Sum256([]byte(id)) hash := sha256.Sum256([]byte(id))
return hash[:] return hash[:]
} }
// Returns true if a is closer to key than b is
func Closer(a, b peer.ID, key u.Key) bool {
aid := ConvertPeerID(a)
bid := ConvertPeerID(b)
tgt := ConvertKey(key)
adist := xor(aid, tgt)
bdist := xor(bid, tgt)
return adist.Less(bdist)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment