@cryptotaxi247 / kubo / commits / e62b82227

refactored keyspace Adjusted -> Bytes

Juan Batiz-Benet committed Sep 17, 2014 at 02:46 UTC e62b8222777f8901b8861cb85b851ac2465a1191
4 files changed +18 -18
routing/kbucket/util.go
+2 -2
@@ -25,8 +25,8 @@ func (id ID) equal(other ID) bool {
25 }
26
27 func (id ID) less(other ID) bool {
28 - a := ks.Key{Space: ks.XORKeySpace, Adjusted: id}
29 - b := ks.Key{Space: ks.XORKeySpace, Adjusted: other}
28 + a := ks.Key{Space: ks.XORKeySpace, Bytes: id}
29 + b := ks.Key{Space: ks.XORKeySpace, Bytes: other}
30 return a.Less(b)
31 }
32
routing/keyspace/keyspace.go
+3 -3
@@ -8,7 +8,7 @@ import (
8
9 // Key represents an identifier in a KeySpace. It holds a reference to the
10 // associated KeySpace, as well references to both the Original identifier,
11 -// as well as the new, KeySpace Adjusted one.
11 +// as well as the new, KeySpace Bytes one.
12 type Key struct {
13
14 // Space is the KeySpace this Key is related to.
@@ -17,8 +17,8 @@ type Key struct {
17 // Original is the original value of the identifier
18 Original []byte
19
20 - // Adjusted is the new value of the identifier, in the KeySpace.
21 - Adjusted []byte
20 + // Bytes is the new value of the identifier, in the KeySpace.
21 + Bytes []byte
22 }
23
24 // Equal returns whether this key is equal to another.
routing/keyspace/xor.go
+5 -5
@@ -21,19 +21,19 @@ func (s *xorKeySpace) Key(id []byte) Key {
21 return Key{
22 Space: s,
23 Original: id,
24 - Adjusted: key,
24 + Bytes: key,
25 }
26 }
27
28 // Equal returns whether keys are equal in this key space
29 func (s *xorKeySpace) Equal(k1, k2 Key) bool {
30 - return bytes.Equal(k1.Adjusted, k2.Adjusted)
30 + return bytes.Equal(k1.Bytes, k2.Bytes)
31 }
32
33 // Distance returns the distance metric in this key space
34 func (s *xorKeySpace) Distance(k1, k2 Key) *big.Int {
35 // XOR the keys
36 - k3 := XOR(k1.Adjusted, k2.Adjusted)
36 + k3 := XOR(k1.Bytes, k2.Bytes)
37
38 // interpret it as an integer
39 dist := big.NewInt(0).SetBytes(k3)
@@ -42,8 +42,8 @@ func (s *xorKeySpace) Distance(k1, k2 Key) *big.Int {
42
43 // Less returns whether the first key is smaller than the second.
44 func (s *xorKeySpace) Less(k1, k2 Key) bool {
45 - a := k1.Adjusted
46 - b := k2.Adjusted
45 + a := k1.Bytes
46 + b := k2.Bytes
47 for i := 0; i < len(a); i++ {
48 if a[i] != b[i] {
49 return a[i] < b[i]
routing/keyspace/xor_test.go
+8 -8
@@ -69,16 +69,16 @@ func TestXorKeySpace(t *testing.T) {
69 t.Errorf("Key not eq. %v != %v", set[0], set[1])
70 }
71
72 - if !bytes.Equal(set[0].Adjusted, set[1].Adjusted) {
73 - t.Errorf("Key gen failed. %v != %v", set[0].Adjusted, set[1].Adjusted)
72 + if !bytes.Equal(set[0].Bytes, set[1].Bytes) {
73 + t.Errorf("Key gen failed. %v != %v", set[0].Bytes, set[1].Bytes)
74 }
75
76 if !bytes.Equal(set[0].Original, ids[i]) {
77 t.Errorf("ptrs to original. %v != %v", set[0].Original, ids[i])
78 }
79
80 - if len(set[0].Adjusted) != 32 {
81 - t.Errorf("key length incorrect. 32 != %d", len(set[0].Adjusted))
80 + if len(set[0].Bytes) != 32 {
81 + t.Errorf("key length incorrect. 32 != %d", len(set[0].Bytes))
82 }
83 }
84
@@ -110,7 +110,7 @@ func TestDistancesAndCenterSorting(t *testing.T) {
110
111 keys := make([]Key, len(adjs))
112 for i, a := range adjs {
113 - keys[i] = Key{Space: XORKeySpace, Adjusted: a}
113 + keys[i] = Key{Space: XORKeySpace, Bytes: a}
114 }
115
116 cmp := func(a int, b *big.Int) int {
@@ -126,8 +126,8 @@ func TestDistancesAndCenterSorting(t *testing.T) {
126 }
127
128 d1 := keys[2].Distance(keys[5])
129 - d2 := XOR(keys[2].Adjusted, keys[5].Adjusted)
130 - d2 = d2[len(keys[2].Adjusted)-len(d1.Bytes()):] // skip empty space for big
129 + d2 := XOR(keys[2].Bytes, keys[5].Bytes)
130 + d2 = d2[len(keys[2].Bytes)-len(d1.Bytes()):] // skip empty space for big
131 if !bytes.Equal(d1.Bytes(), d2) {
132 t.Errorf("bytes should be the same. %v == %v", d1.Bytes(), d2)
133 }
@@ -139,7 +139,7 @@ func TestDistancesAndCenterSorting(t *testing.T) {
139 keys2 := SortByDistance(XORKeySpace, keys[2], keys)
140 order := []int{2, 3, 4, 5, 1, 0}
141 for i, o := range order {
142 - if !bytes.Equal(keys[o].Adjusted, keys2[i].Adjusted) {
142 + if !bytes.Equal(keys[o].Bytes, keys2[i].Bytes) {
143 t.Errorf("order is wrong. %d?? %v == %v", o, keys[o], keys2[i])
144 }
145 }