Ver código fonte

Resolve code review issues

- Add reset timer to benchmark
- use const block for more than one consts
- Change embedded big.Int within RandomToken to RandomToken being a type def of big.Int directly
Justin Corpron 10 anos atrás
pai
commit
9f0924d352
2 arquivos alterados com 21 adições e 14 exclusões
  1. 17 11
      token.go
  2. 4 3
      token_test.go

+ 17 - 11
token.go

@@ -42,8 +42,10 @@ func murmur3H1(data []byte) uint64 {
 
 	var h1, h2, k1, k2 uint64
 
-	const c1 = 0x87c37b91114253d5
-	const c2 = 0x4cf5ad432745937f
+	const (
+		c1 = 0x87c37b91114253d5
+		c2 = 0x4cf5ad432745937f
+	)
 
 	// body
 	nBlocks := length / 16
@@ -141,8 +143,10 @@ func murmur3H1(data []byte) uint64 {
 	h2 += h1
 
 	// finalizer
-	const fmix1 = 0xff51afd7ed558ccd
-	const fmix2 = 0xc4ceb9fe1a85ec53
+	const (
+		fmix1 = 0xff51afd7ed558ccd
+		fmix2 = 0xc4ceb9fe1a85ec53
+	)
 
 	// fmix64(h1)
 	h1 ^= h1 >> 33
@@ -201,9 +205,7 @@ func (o OrderPreservingToken) Less(token Token) bool {
 
 // random partitioner and token
 type RandomPartitioner struct{}
-type RandomToken struct {
-	*big.Int
-}
+type RandomToken big.Int
 
 func (p RandomPartitioner) Hash(partitionKey []byte) Token {
 	hash := md5.New()
@@ -213,17 +215,21 @@ func (p RandomPartitioner) Hash(partitionKey []byte) Token {
 	val = val.SetBytes(sum)
 	val = val.Abs(val)
 
-	return RandomToken{val}
+	return (*RandomToken)(val)
 }
 
 func (p RandomPartitioner) ParseString(str string) Token {
 	val := new(big.Int)
 	val.SetString(str, 10)
-	return RandomToken{val}
+	return (*RandomToken)(val)
+}
+
+func (r *RandomToken) String() string {
+	return (*big.Int)(r).String()
 }
 
-func (r RandomToken) Less(token Token) bool {
-	return -1 == r.Int.Cmp(token.(RandomToken).Int)
+func (r *RandomToken) Less(token Token) bool {
+	return -1 == (*big.Int)(r).Cmp((*big.Int)(token.(*RandomToken)))
 }
 
 // a data structure for organizing the relationship between tokens and hosts

+ 4 - 3
token_test.go

@@ -32,6 +32,7 @@ func BenchmarkMurmur3H1(b *testing.B) {
 		data[i] = byte(i)
 	}
 	for i := 0; i < b.N; i++ {
+		b.ResetTimer()
 		h1 = murmur3H1(data[:])
 		_ = Murmur3Token(int64(h1))
 	}
@@ -98,13 +99,13 @@ func TestRandomPartitioner(t *testing.T) {
 }
 
 func TestRandomToken(t *testing.T) {
-	if (RandomToken{big.NewInt(42)}).Less(RandomToken{big.NewInt(42)}) {
+	if ((*RandomToken)(big.NewInt(42))).Less((*RandomToken)(big.NewInt(42))) {
 		t.Errorf("Expected Less to return false, but was true")
 	}
-	if !(RandomToken{big.NewInt(41)}).Less(RandomToken{big.NewInt(42)}) {
+	if !((*RandomToken)(big.NewInt(41))).Less((*RandomToken)(big.NewInt(42))) {
 		t.Errorf("Expected Less to return true, but was false")
 	}
-	if (RandomToken{big.NewInt(42)}).Less(RandomToken{big.NewInt(41)}) {
+	if ((*RandomToken)(big.NewInt(42))).Less((*RandomToken)(big.NewInt(41))) {
 		t.Errorf("Expected Less to return false, but was true")
 	}
 }