-
Notifications
You must be signed in to change notification settings - Fork 202
/
identifiers.go
69 lines (55 loc) · 1.79 KB
/
identifiers.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
package process
import (
"fmt"
"strconv"
"strings"
)
// ShardCacherIdentifier generates a string identifier between 2 shards
func ShardCacherIdentifier(senderShardID uint32, destinationShardID uint32) string {
if senderShardID == destinationShardID {
return fmt.Sprintf("%d", senderShardID)
}
return fmt.Sprintf("%d_%d", senderShardID, destinationShardID)
}
// ParseShardCacherIdentifier parses an identifier into its components: sender shard, destination shard
func ParseShardCacherIdentifier(cacheID string) (uint32, uint32, error) {
parts := strings.Split(cacheID, "_")
isIntraShard := len(parts) == 1
isCrossShard := len(parts) == 2
isBad := !isIntraShard && !isCrossShard
if isBad {
return parseErrorOfShardCacherIdentifier()
}
if isIntraShard {
shardID, err := parseCacherIdentifierPart(parts[0])
if err != nil {
return parseErrorOfShardCacherIdentifier()
}
return shardID, shardID, nil
}
// Is cross-shard
sourceShardID, err := parseCacherIdentifierPart(parts[0])
if err != nil {
return parseErrorOfShardCacherIdentifier()
}
destinationShardID, err := parseCacherIdentifierPart(parts[1])
if err != nil {
return parseErrorOfShardCacherIdentifier()
}
return sourceShardID, destinationShardID, nil
}
func parseErrorOfShardCacherIdentifier() (uint32, uint32, error) {
return 0, 0, ErrInvalidShardCacherIdentifier
}
func parseCacherIdentifierPart(cacheID string) (uint32, error) {
part, err := strconv.ParseUint(cacheID, 10, 64)
return uint32(part), err
}
// IsShardCacherIdentifierIntraShard checks whether the specified cache is for the pair (sender = self, destination = self)
func IsShardCacherIdentifierIntraShard(cacheID string, shardID uint32) bool {
parsed, err := parseCacherIdentifierPart(cacheID)
if err != nil {
return false
}
return parsed == shardID
}