Skip to content

Commit

Permalink
code optimization
Browse files Browse the repository at this point in the history
code optimization
  • Loading branch information
werbenhu committed Apr 5, 2023
1 parent 5b04602 commit 9c7698e
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 102 deletions.
4 changes: 2 additions & 2 deletions chash.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (c *CHash) Restore(data []byte) error {
}
for _, group := range c.groups {
group.Init()
for _, node := range group.Agents {
group.hashAgent(node)
for _, node := range group.Elements {
group.hashElement(node)
}
}
return nil
Expand Down
22 changes: 11 additions & 11 deletions chash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestCHashInsert(t *testing.T) {
group1, err := hash.GetGroup("werbenhu1")
assert.Nil(t, err)
assert.NotNil(t, group1)
assert.Equal(t, group1.Agents, map[string]*Agent{
assert.Equal(t, group1.Elements, map[string]*Element{
"192.168.1.101:8080": {
Key: "192.168.1.101:8080",
Payload: []byte("werbenhu101"),
Expand All @@ -117,7 +117,7 @@ func TestCHashInsert(t *testing.T) {
group2, err := hash.GetGroup("werbenhu2")
assert.Nil(t, err)
assert.NotNil(t, group2)
assert.Equal(t, group2.Agents, map[string]*Agent{
assert.Equal(t, group2.Elements, map[string]*Element{
"192.168.2.101:8080": {
Key: "192.168.2.101:8080",
Payload: []byte("werbenhu201"),
Expand All @@ -136,7 +136,7 @@ func TestCHashInsert(t *testing.T) {
assert.Equal(t, ErrGroupNotFound, err)
}

func TestCHashDeleteAgent(t *testing.T) {
func TestCHashDeleteElement(t *testing.T) {
hash := New()
hash.CreateGroup("werbenhu1", 10000)
hash.Insert("werbenhu1", "192.168.1.101:8080", []byte("werbenhu101"))
Expand All @@ -146,18 +146,18 @@ func TestCHashDeleteAgent(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, group)

assert.Equal(t, 2, len(group.Agents))
assert.Equal(t, 2, len(group.Elements))
err = hash.Delete("werbenhu1", "192.168.1.101:8080")
assert.Equal(t, 1, len(group.Agents))
assert.Equal(t, 1, len(group.Elements))
assert.Nil(t, err)

err = hash.Delete("werbenhu1", "192.168.1.102:8080")
assert.Nil(t, err)
assert.Equal(t, 0, len(group.Agents))
assert.Equal(t, 0, len(group.Elements))

err = hash.Delete("werbenhu1", "192.168.1.101:8080")
assert.Nil(t, err)
assert.Equal(t, 0, len(group.Agents))
assert.Equal(t, 0, len(group.Elements))
}

func TestCHashSerialize(t *testing.T) {
Expand All @@ -172,23 +172,23 @@ func TestCHashSerialize(t *testing.T) {
hash.Insert("werbenhu2", "192.168.2.102:8080", []byte("werbenhu202"))

bs, err := hash.Serialize()
expert := `{"werbenhu1":{"name":"werbenhu1","numberOfReplicas":2000,"agents":{"192.168.1.101:8080":{"key":"192.168.1.101:8080","payload":"d2VyYmVuaHUxMDE="},"192.168.1.102:8080":{"key":"192.168.1.102:8080","payload":"d2VyYmVuaHUxMDI="}}},"werbenhu2":{"name":"werbenhu2","numberOfReplicas":1000,"agents":{"192.168.2.101:8080":{"key":"192.168.2.101:8080","payload":"d2VyYmVuaHUyMDE="},"192.168.2.102:8080":{"key":"192.168.2.102:8080","payload":"d2VyYmVuaHUyMDI="}}}}`
expert := `{"werbenhu1":{"name":"werbenhu1","numberOfReplicas":2000,"elements":{"192.168.1.101:8080":{"key":"192.168.1.101:8080","payload":"d2VyYmVuaHUxMDE="},"192.168.1.102:8080":{"key":"192.168.1.102:8080","payload":"d2VyYmVuaHUxMDI="}}},"werbenhu2":{"name":"werbenhu2","numberOfReplicas":1000,"elements":{"192.168.2.101:8080":{"key":"192.168.2.101:8080","payload":"d2VyYmVuaHUyMDE="},"192.168.2.102:8080":{"key":"192.168.2.102:8080","payload":"d2VyYmVuaHUyMDI="}}}}`

assert.Nil(t, err)
assert.Equal(t, expert, string(bs))
}

func TestCHashRestore(t *testing.T) {
hash := New()
data := []byte(`{"werbenhu1":{"name":"werbenhu1","numberOfReplicas":2000,"agents":{"192.168.1.101:8080":{"key":"192.168.1.101:8080","payload":"d2VyYmVuaHUxMDE="},"192.168.1.102:8080":{"key":"192.168.1.102:8080","payload":"d2VyYmVuaHUxMDI="}}},"werbenhu2":{"name":"werbenhu2","numberOfReplicas":1000,"agents":{"192.168.2.101:8080":{"key":"192.168.2.101:8080","payload":"d2VyYmVuaHUyMDE="},"192.168.2.102:8080":{"key":"192.168.2.102:8080","payload":"d2VyYmVuaHUyMDI="}}}}`)
data := []byte(`{"werbenhu1":{"name":"werbenhu1","numberOfReplicas":2000,"elements":{"192.168.1.101:8080":{"key":"192.168.1.101:8080","payload":"d2VyYmVuaHUxMDE="},"192.168.1.102:8080":{"key":"192.168.1.102:8080","payload":"d2VyYmVuaHUxMDI="}}},"werbenhu2":{"name":"werbenhu2","numberOfReplicas":1000,"elements":{"192.168.2.101:8080":{"key":"192.168.2.101:8080","payload":"d2VyYmVuaHUyMDE="},"192.168.2.102:8080":{"key":"192.168.2.102:8080","payload":"d2VyYmVuaHUyMDI="}}}}`)
err := hash.Restore(data)
assert.Nil(t, err)

group1, err := hash.GetGroup("werbenhu1")
assert.Nil(t, err)
assert.NotNil(t, group1)
assert.Equal(t, 4000, len(group1.rows))
assert.Equal(t, group1.Agents, map[string]*Agent{
assert.Equal(t, group1.Elements, map[string]*Element{
"192.168.1.101:8080": {
Key: "192.168.1.101:8080",
Payload: []byte("werbenhu101"),
Expand All @@ -203,7 +203,7 @@ func TestCHashRestore(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, group2)
assert.Equal(t, 2000, len(group2.rows))
assert.Equal(t, group2.Agents, map[string]*Agent{
assert.Equal(t, group2.Elements, map[string]*Element{
"192.168.2.101:8080": {
Key: "192.168.2.101:8080",
Payload: []byte("werbenhu201"),
Expand Down
14 changes: 7 additions & 7 deletions indexes.go → circle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ import (
"sort"
)

type Indexes []uint32
type Circle []uint32

func (idx Indexes) Len() int {
func (idx Circle) Len() int {
return len(idx)
}

func (idx Indexes) Swap(i, j int) {
func (idx Circle) Swap(i, j int) {
idx[i], idx[j] = idx[j], idx[i]
}

func (idx Indexes) Less(i, j int) bool {
func (idx Circle) Less(i, j int) bool {
return idx[i] < (idx[j])
}

func (idx Indexes) Sort() {
func (idx Circle) Sort() {
sort.Sort(idx)
}

func (idx Indexes) Search(target uint32) (int, bool) {
func (idx Circle) Search(target uint32) (int, bool) {
if len(idx) == 0 {
return 0, false
}
Expand All @@ -44,7 +44,7 @@ func (idx Indexes) Search(target uint32) (int, bool) {
}

// Match returns an element close to where key hashes to in the circle.
func (idx Indexes) Match(target uint32) (int, bool) {
func (idx Circle) Match(target uint32) (int, bool) {
if len(idx) == 0 {
return 0, false
}
Expand Down
Loading

0 comments on commit 9c7698e

Please sign in to comment.