-
Notifications
You must be signed in to change notification settings - Fork 198
/
key_helper.go
154 lines (133 loc) · 3.49 KB
/
key_helper.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
// Copyright 2014-2022 Aerospike, 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 aerospike
import (
"encoding/binary"
"math"
"github.com/aerospike/aerospike-client-go/v7/pkg/ripemd160"
"github.com/aerospike/aerospike-client-go/v7/types"
)
type keyWriter struct {
buffer [8]byte
hash ripemd160.Digest
}
// WriteInt64 writes a int64 to the key
func (vb *keyWriter) WriteInt64(num int64) int {
return vb.WriteUint64(uint64(num))
}
// WriteUint64 writes a uint64 to the key
func (vb *keyWriter) WriteUint64(num uint64) int {
binary.BigEndian.PutUint64(vb.buffer[:8], num)
vb.hash.Write(vb.buffer[:8])
return 8
}
// WriteInt32 writes a int32 to the key
func (vb *keyWriter) WriteInt32(num int32) int {
return vb.WriteUint32(uint32(num))
}
// WriteUint32 writes a uint32 to the key
func (vb *keyWriter) WriteUint32(num uint32) int {
binary.BigEndian.PutUint32(vb.buffer[:4], num)
vb.hash.Write(vb.buffer[:4])
return 4
}
// WriteInt16 writes a int16 to the key
func (vb *keyWriter) WriteInt16(num int16) int {
return vb.WriteUint16(uint16(num))
}
// WriteUint16 writes a uint16 to the key
func (vb *keyWriter) WriteUint16(num uint16) int {
binary.BigEndian.PutUint16(vb.buffer[:2], num)
vb.hash.Write(vb.buffer[:2])
return 2
}
// WriteFloat32 writes a float32 to the key
func (vb *keyWriter) WriteFloat32(float float32) int {
bits := math.Float32bits(float)
binary.BigEndian.PutUint32(vb.buffer[:4], bits)
vb.hash.Write(vb.buffer[:4])
return 4
}
// WriteFloat64 writes a float64 to the key
func (vb *keyWriter) WriteFloat64(float float64) int {
bits := math.Float64bits(float)
binary.BigEndian.PutUint64(vb.buffer[:8], bits)
vb.hash.Write(vb.buffer[:8])
return 8
}
// WriteBool writes a bool to the key
func (vb *keyWriter) WriteBool(b bool) int {
if b {
vb.hash.Write([]byte{1})
} else {
vb.hash.Write([]byte{0})
}
return 1
}
// WriteByte writes a byte to the key
func (vb *keyWriter) WriteByte(b byte) {
vb.hash.Write([]byte{b})
}
// WriteString writes a string to the key
func (vb *keyWriter) WriteString(s string) (int, Error) {
// To avoid allocating memory, write the strings in small chunks
l := len(s)
const size = 128
b := [size]byte{}
cnt := 0
sz := 0
for i := 0; i < l; i++ {
b[cnt] = s[i]
cnt++
if cnt == size {
n, err := vb.Write(b[:])
if err != nil {
return sz + n, err
}
sz += n
cnt = 0
}
}
if cnt > 0 {
n, err := vb.Write(b[:cnt])
if err != nil {
return sz + n, err
}
}
return len(s), nil
}
func (vb *keyWriter) Write(b []byte) (int, Error) {
n, err := vb.hash.Write(b)
if err != nil {
return n, newCommonError(err)
}
return n, nil
}
func (vb *keyWriter) writeKey(val Value) Error {
switch v := val.(type) {
case IntegerValue:
vb.WriteInt64(int64(v))
return nil
case LongValue:
vb.WriteInt64(int64(v))
return nil
case StringValue:
vb.WriteString(string(v))
return nil
case BytesValue:
vb.Write(v)
return nil
}
return newError(types.PARAMETER_ERROR, "Key Generation Error. Value not supported: "+val.String())
}