forked from pingcap/tidb
/
codec.go
179 lines (166 loc) · 5.04 KB
/
codec.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2015 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package codec
import (
"bytes"
"github.com/juju/errors"
mysql "github.com/pingcap/tidb/mysqldef"
)
var (
// InfiniteValue is the greatest than any other encoded value.
InfiniteValue = []byte{0xFF, 0xFF}
// NilValue is the smallest than any other encoded value.
NilValue = []byte{0x00, 0x00}
// SmallestNoneNilValue is smaller than any other encoded value except nil value.
SmallestNoneNilValue = []byte{0x00, 0x01}
)
const (
formatNilFlag = 'n'
formatIntFlag = 'd'
formatUintFlag = 'u'
formatFloatFlag = 'f'
formatStringFlag = 's'
formatBytesFlag = 'b'
)
var sepKey = []byte{0x00, 0x00}
// EncodeKey encodes args to a slice which can be sorted lexicographically later.
// EncodeKey guarantees the encoded slice is in ascending order for comparison.
// TODO: we may add more test to check its valiadation, especially for null type and multi indices.
func EncodeKey(args ...interface{}) ([]byte, error) {
var b []byte
format := make([]byte, 0, len(args))
for _, arg := range args {
switch v := arg.(type) {
case bool:
if v {
b = EncodeInt(b, int64(1))
} else {
b = EncodeInt(b, int64(0))
}
format = append(format, formatIntFlag)
case int:
b = EncodeInt(b, int64(v))
format = append(format, formatIntFlag)
case int8:
b = EncodeInt(b, int64(v))
format = append(format, formatIntFlag)
case int16:
b = EncodeInt(b, int64(v))
format = append(format, formatIntFlag)
case int32:
b = EncodeInt(b, int64(v))
format = append(format, formatIntFlag)
case int64:
b = EncodeInt(b, int64(v))
format = append(format, formatIntFlag)
case uint:
b = EncodeUint(b, uint64(v))
format = append(format, formatUintFlag)
case uint8:
b = EncodeUint(b, uint64(v))
format = append(format, formatUintFlag)
case uint16:
b = EncodeUint(b, uint64(v))
format = append(format, formatUintFlag)
case uint32:
b = EncodeUint(b, uint64(v))
format = append(format, formatUintFlag)
case uint64:
b = EncodeUint(b, uint64(v))
format = append(format, formatUintFlag)
case float32:
b = EncodeFloat(b, float64(v))
format = append(format, formatFloatFlag)
case float64:
b = EncodeFloat(b, float64(v))
format = append(format, formatFloatFlag)
case string:
b = EncodeBytes(b, []byte(v))
format = append(format, formatStringFlag)
case []byte:
b = EncodeBytes(b, v)
format = append(format, formatBytesFlag)
case mysql.Time:
b = EncodeBytes(b, []byte(v.String()))
format = append(format, formatStringFlag)
case nil:
// We will 0x00, 0x00 for nil.
// The []byte{} will be encoded as 0x00, 0x01.
// The []byte{0x00} will be encode as 0x00, 0xFF, 0x00, 0x01.
// And any integer and float encoded values are greater than 0x00, 0x01.
// So maybe the smallest none null value is []byte{} and we can use it to skip null values.
b = append(b, sepKey...)
format = append(format, formatNilFlag)
default:
return nil, errors.Errorf("unsupport encode type %T", arg)
}
}
// The comma is the seperator,
// e.g: 0x00, 0x00
// We need more tests to check its validation.
b = append(b, sepKey...)
b = append(b, format...)
return b, nil
}
// StripEnd splits a slice b into two substrings separated by sepKey
// and returns a slice byte of the previous substrings.
func StripEnd(b []byte) ([]byte, error) {
n := bytes.LastIndex(b, sepKey)
if n == -1 || n+2 >= len(b) {
// No seperator or no proper format.
return nil, errors.Errorf("invalid encoded key")
}
return b[:n], nil
}
// DecodeKey decodes values from a byte slice generated with EncodeKey before.
func DecodeKey(b []byte) ([]interface{}, error) {
// At first read the format.
n := bytes.LastIndex(b, sepKey)
if n == -1 || n+2 >= len(b) {
// No seperator or no proper format.
return nil, errors.Errorf("invalid encoded key")
}
format := b[n+2:]
b = b[0:n]
v := make([]interface{}, len(format))
var err error
for i, flag := range format {
switch flag {
case formatIntFlag:
b, v[i], err = DecodeInt(b)
case formatUintFlag:
b, v[i], err = DecodeUint(b)
case formatFloatFlag:
b, v[i], err = DecodeFloat(b)
case formatStringFlag:
var r []byte
b, r, err = DecodeBytes(b)
if err == nil {
v[i] = string(r)
}
case formatBytesFlag:
b, v[i], err = DecodeBytes(b)
case formatNilFlag:
if len(b) < 2 || (b[0] != 0x00 && b[1] != 0x00) {
return nil, errors.Errorf("malformed encoded nil")
}
b, v[i] = b[2:], nil
default:
return nil, errors.Errorf("invalid encoded key format %v in %s", flag, format)
}
if err != nil {
return nil, errors.Trace(err)
}
}
return v, nil
}