forked from viciious/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack_data.go
160 lines (138 loc) · 3.62 KB
/
pack_data.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
package tarantool
import (
"fmt"
"github.com/tinylib/msgp/msgp"
)
// cache precompiled
type packData struct {
defaultSpace interface{}
packedDefaultSpace []byte
packedDefaultIndex []byte
packedIterEq []byte
packedDefaultLimit []byte
packedDefaultOffset []byte
packedSingleKey []byte
spaceMap map[string]uint64
indexMap map[uint64]map[string]uint64
primaryKeyMap map[uint64][]int
}
func encodeValues2(v1, v2 interface{}) []byte {
o := make([]byte, 0)
o, _ = msgp.AppendIntf(o, v1)
o, _ = msgp.AppendIntf(o, v2)
return o[:]
}
func packSelectSingleKey() []byte {
o := make([]byte, 0)
o = msgp.AppendUint(o, KeyKey)
o = msgp.AppendArrayHeader(o, 1)
return o[:]
}
func newPackData(defaultSpace interface{}) *packData {
var packedDefaultSpace []byte
if spaceNo, ok := defaultSpace.(uint64); ok {
packedDefaultSpace = encodeValues2(KeySpaceNo, spaceNo)
}
return &packData{
defaultSpace: defaultSpace,
packedDefaultSpace: packedDefaultSpace,
packedDefaultIndex: encodeValues2(KeyIndexNo, uint32(0)),
packedIterEq: encodeValues2(KeyIterator, IterEq),
packedDefaultLimit: encodeValues2(KeyLimit, DefaultLimit),
packedDefaultOffset: encodeValues2(KeyOffset, 0),
packedSingleKey: packSelectSingleKey(),
spaceMap: make(map[string]uint64),
indexMap: make(map[uint64]map[string]uint64),
primaryKeyMap: make(map[uint64][]int),
}
}
func (data *packData) spaceNo(space interface{}) (uint64, error) {
if space == nil {
space = data.defaultSpace
}
switch value := space.(type) {
case string:
spaceNo, exists := data.spaceMap[value]
if exists {
return spaceNo, nil
}
return 0, fmt.Errorf("Unknown space %#v", space)
}
return numberToUint64(space)
}
func (data *packData) packSpace(space interface{}, o []byte) ([]byte, error) {
if space == nil && data.packedDefaultSpace != nil {
o = append(o, data.packedDefaultSpace...)
return o, nil
}
spaceNo, err := data.spaceNo(space)
if err != nil {
return o, err
}
o = msgp.AppendUint(o, KeySpaceNo)
o = msgp.AppendUint64(o, spaceNo)
return o, nil
}
func numberToUint64(number interface{}) (uint64, error) {
switch value := number.(type) {
default:
return 0, fmt.Errorf("Bad number %#v", number)
case int:
return uint64(value), nil
case uint:
return uint64(value), nil
case int8:
return uint64(value), nil
case uint8:
return uint64(value), nil
case int16:
return uint64(value), nil
case uint16:
return uint64(value), nil
case int32:
return uint64(value), nil
case uint32:
return uint64(value), nil
case int64:
return uint64(value), nil
case uint64:
return value, nil
}
}
func (data *packData) fieldNo(field interface{}) (uint64, error) {
return numberToUint64(field)
}
func (data *packData) indexNo(space interface{}, index interface{}) (uint64, error) {
if index == nil {
return 0, nil
}
if value, ok := index.(string); ok {
spaceNo, err := data.spaceNo(space)
if err != nil {
return 0, nil
}
spaceData, exists := data.indexMap[spaceNo]
if !exists {
return 0, fmt.Errorf("No indexes defined for space %#v", space)
}
indexNo, exists := spaceData[value]
if exists {
return indexNo, nil
}
return 0, fmt.Errorf("Unknown index %#v", index)
}
return numberToUint64(index)
}
func (data *packData) packIndex(space interface{}, index interface{}, o []byte) ([]byte, error) {
if index == nil {
o = append(o, data.packedDefaultIndex...)
return o, nil
}
indexNo, err := data.indexNo(space, index)
if err != nil {
return o, err
}
o = msgp.AppendUint(o, KeyIndexNo)
o = msgp.AppendUint64(o, indexNo)
return o, nil
}