forked from tarantool/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
264 lines (248 loc) · 5.9 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
package tarantool
import (
"fmt"
)
// Schema contains information about spaces and indexes.
type Schema struct {
Version uint
// 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 // Is this space temporaray?
// 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
spaceSpId = 280
vspaceSpId = 281
indexSpId = 288
vindexSpId = 289
)
func (conn *Connection) loadSchema() (err error) {
var resp *Response
schema := new(Schema)
schema.SpacesById = make(map[uint32]*Space)
schema.Spaces = make(map[string]*Space)
// reload spaces
resp, err = conn.Select(vspaceSpId, 0, 0, maxSchemas, IterAll, []interface{}{})
if err != nil {
return err
}
for _, row := range resp.Data {
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].(uint64))
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
resp, err = conn.Select(vindexSpId, 0, 0, maxSchemas, IterAll, []interface{}{})
if err != nil {
return err
}
for _, row := range resp.Data {
row := row.([]interface{})
index := new(Index)
index.Id = uint32(row[1].(uint64))
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 row[5].(type) {
case uint64:
cnt := int(row[5].(uint64))
for i := 0; i < cnt; i++ {
field := new(IndexField)
field.Id = uint32(row[6+i*2].(uint64))
field.Type = row[7+i*2].(string)
index.Fields = append(index.Fields, field)
}
case []interface{}:
for _, f := range row[5].([]interface{}) {
f := f.([]interface{})
field := new(IndexField)
field.Id = uint32(f[0].(uint64))
field.Type = f[1].(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 = uint32(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 = uint32(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
}