-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathshardpreftable.go
192 lines (169 loc) · 4.32 KB
/
shardpreftable.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"fmt"
"sync"
peer "github.com/libp2p/go-libp2p-peer"
)
const byteSize = 8 // in bits
type ListeningShards struct {
shardBits []byte
}
func NewListeningShards() *ListeningShards {
return &ListeningShards{
shardBits: make([]byte, (numShards/8)+1),
}
}
func shardIDToBitIndex(shardID ShardIDType) (byte, byte, error) {
if shardID >= numShards {
return 0, 0, fmt.Errorf("Invalid shardID: %v", shardID)
}
byteIndex := byte(shardID / byteSize)
bitIndex := byte(shardID % byteSize)
return byteIndex, bitIndex, nil
}
func (ls *ListeningShards) unsetShard(shardID ShardIDType) error {
byteIndex, bitIndex, err := shardIDToBitIndex(shardID)
if err != nil {
return err
}
if int(byteIndex) >= len(ls.shardBits) {
return fmt.Errorf(
"(byteIndex=%v) >= (len(shardBits)=%v)",
byteIndex,
len(ls.shardBits),
)
}
ls.shardBits[byteIndex] &= (^(1 << bitIndex))
return nil
}
func (ls *ListeningShards) setShard(shardID ShardIDType) error {
byteIndex, bitIndex, err := shardIDToBitIndex(shardID)
if err != nil {
return err
}
if int(byteIndex) >= len(ls.shardBits) {
return fmt.Errorf(
"(byteIndex=%v) >= (len(shardBits)=%v)",
byteIndex,
len(ls.shardBits),
)
}
ls.shardBits[byteIndex] |= (1 << bitIndex)
return nil
}
func (ls *ListeningShards) isShardSet(shardID ShardIDType) bool {
byteIndex, bitIndex, err := shardIDToBitIndex(shardID)
if err != nil {
return false
}
index := ls.shardBits[byteIndex] & (1 << bitIndex)
return index != 0
}
func (ls *ListeningShards) getShards() []ShardIDType {
shards := []ShardIDType{}
for shardID := ShardIDType(0); shardID < numShards; shardID++ {
if ls.isShardSet(shardID) {
shards = append(shards, shardID)
}
}
return shards
}
func (ls *ListeningShards) fromSlice(shards []ShardIDType) *ListeningShards {
for _, shardID := range shards {
ls.setShard(shardID)
}
return ls
}
// TODO: need checks against the format of bytes
func (ls *ListeningShards) fromBytes(bytes []byte) *ListeningShards {
ls.shardBits = bytes
return ls
}
func (ls *ListeningShards) toBytes() []byte {
return ls.shardBits
}
// ShardPrefTable manages peers' shard preference
type ShardPrefTable struct {
shardPrefMap map[peer.ID]*ListeningShards
lock sync.RWMutex
}
func NewShardPrefTable() *ShardPrefTable {
return &ShardPrefTable{
shardPrefMap: make(map[peer.ID]*ListeningShards),
lock: sync.RWMutex{},
}
}
func (n *ShardPrefTable) isPeerRecorded(peerID peer.ID) bool {
n.lock.RLock()
defer n.lock.RUnlock()
_, prs := n.shardPrefMap[peerID]
return prs
}
func (n *ShardPrefTable) IsPeerListeningShard(peerID peer.ID, shardID ShardIDType) bool {
if !n.isPeerRecorded(peerID) {
return false
}
n.lock.RLock()
shardPref := n.shardPrefMap[peerID]
n.lock.RUnlock()
return shardPref.isShardSet(shardID)
}
func (n *ShardPrefTable) AddPeerListeningShard(peerID peer.ID, shardID ShardIDType) error {
n.lock.RLock()
shardPref, prs := n.shardPrefMap[peerID]
n.lock.RUnlock()
if !prs {
shardPref = NewListeningShards()
}
if err := shardPref.setShard(shardID); err != nil {
return err
}
n.lock.Lock()
defer n.lock.Unlock()
n.shardPrefMap[peerID] = shardPref
return nil
}
func (n *ShardPrefTable) RemovePeerListeningShard(peerID peer.ID, shardID ShardIDType) error {
if !n.isPeerRecorded(peerID) {
return nil
}
if !n.IsPeerListeningShard(peerID, shardID) {
return nil
}
n.lock.Lock()
defer n.lock.Unlock()
return n.shardPrefMap[peerID].unsetShard(shardID)
}
func (n *ShardPrefTable) GetPeerListeningShard(peerID peer.ID) *ListeningShards {
if !n.isPeerRecorded(peerID) {
return NewListeningShards()
}
n.lock.RLock()
defer n.lock.RUnlock()
return n.shardPrefMap[peerID]
}
func (n *ShardPrefTable) GetPeerListeningShardSlice(peerID peer.ID) []ShardIDType {
if !n.isPeerRecorded(peerID) {
return []ShardIDType{}
}
n.lock.RLock()
defer n.lock.RUnlock()
return n.shardPrefMap[peerID].getShards()
}
func (n *ShardPrefTable) SetPeerListeningShard(peerID peer.ID, ls *ListeningShards) {
n.lock.Lock()
defer n.lock.Unlock()
n.shardPrefMap[peerID] = ls
return
}
func (n *ShardPrefTable) GetPeersInShard(shardID ShardIDType) []peer.ID {
n.lock.RLock()
defer n.lock.RUnlock()
peers := []peer.ID{}
for peerID, listeningShards := range n.shardPrefMap {
if listeningShards.isShardSet(shardID) {
peers = append(peers, peerID)
}
}
return peers
}