-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchash.go
120 lines (105 loc) · 2.46 KB
/
chash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 werbenhu
// SPDX-FileContributor: werbenhu
package chash
import (
"encoding/json"
"sync"
)
// CHash a warpper of Consistent hashing
type CHash struct {
sync.RWMutex
groups map[string]*Group
}
func New() *CHash {
return &CHash{
groups: make(map[string]*Group),
}
}
// GetGroup retrieves a group by name
func (c *CHash) GetGroup(groupName string) (*Group, error) {
c.RLock()
defer c.RUnlock()
group, ok := c.groups[groupName]
if !ok {
return nil, ErrGroupNotFound
}
return group, nil
}
// CreateGroup creates a new group with the given name and the number of replicas
func (c *CHash) CreateGroup(groupName string, replicas int) (*Group, error) {
c.Lock()
defer c.Unlock()
if existing, ok := c.groups[groupName]; ok {
return existing, ErrGroupExisted
}
group := NewGroup(groupName, replicas)
c.groups[groupName] = group
return group, nil
}
// RemoveGroup removes a group by name
func (c *CHash) RemoveGroup(groupName string) {
c.Lock()
defer c.Unlock()
delete(c.groups, groupName)
}
// RemoveAllGroup removes all groups
func (c *CHash) RemoveAllGroup() {
c.Lock()
defer c.Unlock()
for k := range c.groups {
delete(c.groups, k)
}
}
// Insert inserts a new key-value pair into a group
func (c *CHash) Insert(groupName string, key string, payload []byte) error {
c.Lock()
group, ok := c.groups[groupName]
c.Unlock()
if !ok {
return ErrGroupNotFound
}
return group.Insert(key, payload)
}
// Delete removes a key from a group
func (c *CHash) Delete(groupName string, key string) error {
c.Lock()
group, ok := c.groups[groupName]
c.Unlock()
if !ok {
return ErrGroupNotFound
}
group.Delete(key)
return nil
}
// Match returns the key-value pair closest to the given key in a group
func (c *CHash) Match(groupName string, key string) (string, []byte, error) {
c.RLock()
group, ok := c.groups[groupName]
c.RUnlock()
if !ok {
return "", nil, ErrGroupNotFound
}
return group.Match(key)
}
// Serialize serializes the CHash structure to JSON
func (c *CHash) Serialize() ([]byte, error) {
c.RLock()
defer c.RUnlock()
return json.Marshal(c.groups)
}
// Restore deserializes a JSON representation of the CHash structure
func (c *CHash) Restore(data []byte) error {
c.Lock()
defer c.Unlock()
if err := json.Unmarshal(data, &c.groups); err != nil {
return err
}
for _, group := range c.groups {
group.Init()
for _, node := range group.Elements {
group.hashElement(node)
}
}
return nil
}