-
Notifications
You must be signed in to change notification settings - Fork 4
/
schema.go
271 lines (255 loc) · 6.14 KB
/
schema.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package tarantool
import (
"context"
"fmt"
"time"
)
// Schema contains information about spaces and indexes.
type Schema struct {
// Spaces is map from space names to spaces.
Spaces map[string]*Space
// SpacesByID is map from space numbers to spaces.
SpacesByID map[uint32]*Space
}
// Space contains information about tarantool space.
type Space struct {
ID uint32
Name string
Engine string
Temporary bool
// Field configuration is not mandatory and not checked by tarantool.
FieldsCount uint32
Fields map[string]*Field
FieldsByID map[uint32]*Field
// Indexes is map from index names to indexes.
Indexes map[string]*Index
// IndexesByID is map from index numbers to indexes.
IndexesByID map[uint32]*Index
}
type Field struct {
ID uint32
Name string
Type string
}
// Index contains information about index.
type Index struct {
ID uint32
Name string
Type string
Unique bool
Fields []*IndexField
}
type IndexField struct {
ID uint32
Type string
}
const (
maxSchemas = 10000
vSpaceSpID = 281
vIndexSpID = 289
)
func (conn *Connection) loadSchema() (err error) {
schema := new(Schema)
schema.SpacesByID = make(map[uint32]*Space)
schema.Spaces = make(map[string]*Space)
// Reload spaces.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := conn.ExecContext(ctx, Select(vSpaceSpID, 0, 0, maxSchemas, IterAll, []interface{}{}))
if err != nil {
return err
}
for _, row := range resp {
row := row.([]interface{})
space := new(Space)
space.ID = uint32(row[0].(uint64))
space.Name = row[2].(string)
space.Engine = row[3].(string)
space.FieldsCount = uint32(row[4].(int64))
if len(row) >= 6 {
switch row5 := row[5].(type) {
case string:
space.Temporary = row5 == "temporary"
case map[interface{}]interface{}:
if temp, ok := row5["temporary"]; ok {
space.Temporary = temp.(bool)
}
default:
panic("unexpected schema format (space flags)")
}
}
space.FieldsByID = make(map[uint32]*Field)
space.Fields = make(map[string]*Field)
space.IndexesByID = make(map[uint32]*Index)
space.Indexes = make(map[string]*Index)
if len(row) >= 7 {
for i, f := range row[6].([]interface{}) {
if f == nil {
continue
}
f := f.(map[interface{}]interface{})
field := new(Field)
field.ID = uint32(i)
if name, ok := f["name"]; ok && name != nil {
field.Name = name.(string)
}
if type1, ok := f["type"]; ok && type1 != nil {
field.Type = type1.(string)
}
space.FieldsByID[field.ID] = field
if field.Name != "" {
space.Fields[field.Name] = field
}
}
}
schema.SpacesByID[space.ID] = space
schema.Spaces[space.Name] = space
}
// Reload indexes.
ctx, cancel2 := context.WithTimeout(context.Background(), time.Second)
defer cancel2()
resp, err = conn.ExecContext(ctx, Select(vIndexSpID, 0, 0, maxSchemas, IterAll, []interface{}{}))
if err != nil {
return err
}
for _, row := range resp {
row := row.([]interface{})
index := new(Index)
index.ID = uint32(row[1].(int64))
index.Name = row[2].(string)
index.Type = row[3].(string)
switch row[4].(type) {
case uint64:
index.Unique = row[4].(uint64) > 0
case map[interface{}]interface{}:
opts := row[4].(map[interface{}]interface{})
var ok bool
if index.Unique, ok = opts["unique"].(bool); !ok {
/* see bug https://github.com/tarantool/tarantool/issues/2060 */
index.Unique = true
}
default:
panic("unexpected schema format (index flags)")
}
switch fields := row[5].(type) {
case uint64:
cnt := int(fields)
for i := 0; i < cnt; i++ {
field := new(IndexField)
field.ID = uint32(row[6+i*2].(int64))
field.Type = row[7+i*2].(string)
index.Fields = append(index.Fields, field)
}
case []interface{}:
for _, f := range fields {
field := new(IndexField)
switch f := f.(type) {
case []interface{}:
field.ID = uint32(f[0].(int64))
field.Type = f[1].(string)
case map[interface{}]interface{}:
field.ID = uint32(f["field"].(int64))
field.Type = f["type"].(string)
}
index.Fields = append(index.Fields, field)
}
default:
panic("unexpected schema format (index fields)")
}
spaceID := uint32(row[0].(uint64))
schema.SpacesByID[spaceID].IndexesByID[index.ID] = index
schema.SpacesByID[spaceID].Indexes[index.Name] = index
}
conn.schema = schema
return nil
}
func (schema *Schema) resolveSpaceIndex(s interface{}, i interface{}) (spaceNo, indexNo uint32, err error) {
var space *Space
var index *Index
var ok bool
switch s := s.(type) {
case string:
if schema == nil {
err = fmt.Errorf("schema is not loaded")
return
}
if space, ok = schema.Spaces[s]; !ok {
err = fmt.Errorf("there is no space with name %s", s)
return
}
spaceNo = space.ID
case uint:
spaceNo = uint32(s)
case uint64:
spaceNo = uint32(s)
case uint32:
spaceNo = s
case uint16:
spaceNo = uint32(s)
case uint8:
spaceNo = uint32(s)
case int:
spaceNo = uint32(s)
case int64:
spaceNo = uint32(s)
case int32:
spaceNo = uint32(s)
case int16:
spaceNo = uint32(s)
case int8:
spaceNo = uint32(s)
case Space:
spaceNo = s.ID
case *Space:
spaceNo = s.ID
default:
panic("unexpected type of space param")
}
if i != nil {
switch i := i.(type) {
case string:
if schema == nil {
err = fmt.Errorf("schema is not loaded")
return
}
if space == nil {
if space, ok = schema.SpacesByID[spaceNo]; !ok {
err = fmt.Errorf("there is no space with id %d", spaceNo)
return
}
}
if index, ok = space.Indexes[i]; !ok {
err = fmt.Errorf("space %s has not index with name %s", space.Name, i)
return
}
indexNo = index.ID
case uint:
indexNo = uint32(i)
case uint64:
indexNo = uint32(i)
case uint32:
indexNo = i
case uint16:
indexNo = uint32(i)
case uint8:
indexNo = uint32(i)
case int:
indexNo = uint32(i)
case int64:
indexNo = uint32(i)
case int32:
indexNo = uint32(i)
case int16:
indexNo = uint32(i)
case int8:
indexNo = uint32(i)
case Index:
indexNo = i.ID
case *Index:
indexNo = i.ID
default:
panic("unexpected type of index param")
}
}
return
}