forked from absolute8511/go-zanredisdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpk.go
56 lines (48 loc) · 1.01 KB
/
pk.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
package zanredisdb
import (
"bytes"
"errors"
"fmt"
"strings"
)
var ErrInvalidKey = errors.New("invalid key format")
func ValidPrefix(k string) bool {
fields := strings.SplitN(k, ":", 3)
if len(fields) < 3 || len(fields[2]) <= 0 {
return false
}
return true
}
func ParsePKey(k string) (*PKey, error) {
fields := strings.SplitN(k, ":", 3)
if len(fields) < 3 || len(fields[2]) <= 0 {
return nil, ErrInvalidKey
}
return NewPKey(fields[0], fields[1], []byte(fields[2])), nil
}
type PKey struct {
Namespace string
Set string
PK []byte
RawKey []byte
}
func NewPKey(ns string, set string, pk []byte) *PKey {
var tmp bytes.Buffer
tmp.WriteString(ns)
tmp.WriteString(":")
tmp.WriteString(set)
tmp.WriteString(":")
tmp.Write(pk)
return &PKey{
Namespace: ns,
Set: set,
PK: pk,
RawKey: tmp.Bytes(),
}
}
func (self *PKey) ShardingKey() []byte {
return self.RawKey[len(self.Namespace)+1:]
}
func (self *PKey) String() string {
return fmt.Sprintf("%s", string(self.RawKey))
}