forked from vitessio/vitess
/
binary.go
72 lines (63 loc) · 1.74 KB
/
binary.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
package vindexes
import (
"bytes"
"fmt"
)
// Binary is a vindex that converts binary bits to a keyspace id.
type Binary struct {
name string
}
// NewBinary creates a new Binary.
func NewBinary(name string, _ map[string]string) (Vindex, error) {
return &Binary{name: name}, nil
}
// String returns the name of the vindex.
func (vind *Binary) String() string {
return vind.name
}
// Cost returns the cost as 1.
func (vind *Binary) Cost() int {
return 1
}
// Verify returns true if ids maps to ksids.
func (vind *Binary) Verify(_ VCursor, ids []interface{}, ksids [][]byte) (bool, error) {
if len(ids) != len(ksids) {
return false, fmt.Errorf("Binary.Verify: length of ids %v doesn't match length of ksids %v", len(ids), len(ksids))
}
for rowNum := range ids {
data, err := getBytes(ids[rowNum])
if err != nil {
return false, fmt.Errorf("Binary.Verify: %v", err)
}
if bytes.Compare(data, ksids[rowNum]) != 0 {
return false, nil
}
}
return true, nil
}
// Map returns the corresponding keyspace id values for the given ids.
func (vind *Binary) Map(_ VCursor, ids []interface{}) ([][]byte, error) {
out := make([][]byte, 0, len(ids))
for _, id := range ids {
data, err := getBytes(id)
if err != nil {
return nil, fmt.Errorf("Binary.Map :%v", err)
}
out = append(out, data)
}
return out, nil
}
// ReverseMap returns the associated ids for the ksids.
func (*Binary) ReverseMap(_ VCursor, ksids [][]byte) ([]interface{}, error) {
var reverseIds = make([]interface{}, len(ksids))
for rownum, keyspaceID := range ksids {
if keyspaceID == nil {
return nil, fmt.Errorf("Binary.ReverseMap: keyspaceId is nil")
}
reverseIds[rownum] = []byte(keyspaceID)
}
return reverseIds, nil
}
func init() {
Register("binary", NewBinary)
}