-
Notifications
You must be signed in to change notification settings - Fork 4
/
hash.go
146 lines (124 loc) · 3.45 KB
/
hash.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
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vindexes
import (
"bytes"
"crypto/cipher"
"crypto/des"
"encoding/binary"
"encoding/hex"
"fmt"
"strconv"
"gopkg.in/src-d/go-vitess.v1/sqltypes"
"gopkg.in/src-d/go-vitess.v1/vt/key"
)
var (
_ Vindex = (*Hash)(nil)
_ Reversible = (*Hash)(nil)
)
// Hash defines vindex that hashes an int64 to a KeyspaceId
// by using null-key 3DES hash. It's Unique, Reversible and
// Functional.
type Hash struct {
name string
}
// NewHash creates a new Hash.
func NewHash(name string, m map[string]string) (Vindex, error) {
return &Hash{name: name}, nil
}
// String returns the name of the vindex.
func (vind *Hash) String() string {
return vind.name
}
// Cost returns the cost of this index as 1.
func (vind *Hash) Cost() int {
return 1
}
// IsUnique returns true since the Vindex is unique.
func (vind *Hash) IsUnique() bool {
return true
}
// IsFunctional returns true since the Vindex is functional.
func (vind *Hash) IsFunctional() bool {
return true
}
// Map can map ids to key.Destination objects.
func (vind *Hash) Map(cursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
out := make([]key.Destination, len(ids))
for i, id := range ids {
var num uint64
var err error
if id.IsSigned() {
// This is ToUint64 with no check on negative values.
str := id.ToString()
var ival int64
ival, err = strconv.ParseInt(str, 10, 64)
num = uint64(ival)
} else {
num, err = sqltypes.ToUint64(id)
}
if err != nil {
out[i] = key.DestinationNone{}
continue
}
out[i] = key.DestinationKeyspaceID(vhash(num))
}
return out, nil
}
// Verify returns true if ids maps to ksids.
func (vind *Hash) Verify(_ VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) {
out := make([]bool, len(ids))
for i := range ids {
num, err := sqltypes.ToUint64(ids[i])
if err != nil {
return nil, fmt.Errorf("hash.Verify: %v", err)
}
out[i] = (bytes.Compare(vhash(num), ksids[i]) == 0)
}
return out, nil
}
// ReverseMap returns the ids from ksids.
func (vind *Hash) ReverseMap(_ VCursor, ksids [][]byte) ([]sqltypes.Value, error) {
reverseIds := make([]sqltypes.Value, 0, len(ksids))
for _, keyspaceID := range ksids {
val, err := vunhash(keyspaceID)
if err != nil {
return reverseIds, err
}
reverseIds = append(reverseIds, sqltypes.NewUint64(val))
}
return reverseIds, nil
}
var block3DES cipher.Block
func init() {
var err error
block3DES, err = des.NewTripleDESCipher(make([]byte, 24))
if err != nil {
panic(err)
}
Register("hash", NewHash)
}
func vhash(shardKey uint64) []byte {
var keybytes, hashed [8]byte
binary.BigEndian.PutUint64(keybytes[:], shardKey)
block3DES.Encrypt(hashed[:], keybytes[:])
return []byte(hashed[:])
}
func vunhash(k []byte) (uint64, error) {
if len(k) != 8 {
return 0, fmt.Errorf("invalid keyspace id: %v", hex.EncodeToString(k))
}
var unhashed [8]byte
block3DES.Decrypt(unhashed[:], k)
return binary.BigEndian.Uint64(unhashed[:]), nil
}