4
4
"errors"
5
5
"fmt"
6
6
7
- "github.com/tarantool/go-iproto"
8
7
"github.com/vmihailenco/msgpack/v5"
9
8
"github.com/vmihailenco/msgpack/v5/msgpcode"
10
9
)
@@ -58,9 +57,9 @@ type SchemaResolver interface {
58
57
type Schema struct {
59
58
Version uint
60
59
// Spaces is map from space names to spaces.
61
- Spaces map [string ]* Space
60
+ Spaces map [string ]Space
62
61
// SpacesById is map from space numbers to spaces.
63
- SpacesById map [uint32 ]* Space
62
+ SpacesById map [uint32 ]Space
64
63
}
65
64
66
65
// Space contains information about Tarantool's space.
@@ -72,12 +71,12 @@ type Space struct {
72
71
Temporary bool // Is this space temporary?
73
72
// Field configuration is not mandatory and not checked by Tarantool.
74
73
FieldsCount uint32
75
- Fields map [string ]* Field
76
- FieldsById map [uint32 ]* Field
74
+ Fields map [string ]Field
75
+ FieldsById map [uint32 ]Field
77
76
// Indexes is map from index names to indexes.
78
- Indexes map [string ]* Index
77
+ Indexes map [string ]Index
79
78
// IndexesById is map from index numbers to indexes.
80
- IndexesById map [uint32 ]* Index
79
+ IndexesById map [uint32 ]Index
81
80
}
82
81
83
82
func (space * Space ) DecodeMsgpack (d * msgpack.Decoder ) error {
@@ -135,17 +134,17 @@ func (space *Space) DecodeMsgpack(d *msgpack.Decoder) error {
135
134
return errors .New ("unexpected schema format (space flags)" )
136
135
}
137
136
}
138
- space .FieldsById = make (map [uint32 ]* Field )
139
- space .Fields = make (map [string ]* Field )
140
- space .IndexesById = make (map [uint32 ]* Index )
141
- space .Indexes = make (map [string ]* Index )
137
+ space .FieldsById = make (map [uint32 ]Field )
138
+ space .Fields = make (map [string ]Field )
139
+ space .IndexesById = make (map [uint32 ]Index )
140
+ space .Indexes = make (map [string ]Index )
142
141
if arrayLen >= vspaceSpFormatFieldNum {
143
142
fieldCount , err := d .DecodeArrayLen ()
144
143
if err != nil {
145
144
return err
146
145
}
147
146
for i := 0 ; i < fieldCount ; i ++ {
148
- field := & Field {}
147
+ field := Field {}
149
148
if err := field .DecodeMsgpack (d ); err != nil {
150
149
return err
151
150
}
@@ -206,7 +205,7 @@ type Index struct {
206
205
Name string
207
206
Type string
208
207
Unique bool
209
- Fields []* IndexField
208
+ Fields []IndexField
210
209
}
211
210
212
211
func (index * Index ) DecodeMsgpack (d * msgpack.Decoder ) error {
@@ -261,9 +260,9 @@ func (index *Index) DecodeMsgpack(d *msgpack.Decoder) error {
261
260
if err != nil {
262
261
return err
263
262
}
264
- index .Fields = make ([]* IndexField , fieldCount )
263
+ index .Fields = make ([]IndexField , fieldCount )
265
264
for i := 0 ; i < int (fieldCount ); i ++ {
266
- index .Fields [i ] = new ( IndexField )
265
+ index .Fields [i ] = IndexField {}
267
266
if index .Fields [i ].Id , err = d .DecodeUint32 (); err != nil {
268
267
return err
269
268
}
@@ -340,51 +339,40 @@ func (indexField *IndexField) DecodeMsgpack(d *msgpack.Decoder) error {
340
339
return errors .New ("unexpected schema format (index fields)" )
341
340
}
342
341
343
- func (conn * Connection ) loadSchema () (err error ) {
344
- schema := new (Schema )
345
- schema .SpacesById = make (map [uint32 ]* Space )
346
- schema .Spaces = make (map [string ]* Space )
342
+ // GetSchema returns the actual schema for the connection.
343
+ func GetSchema (conn Connector ) (Schema , error ) {
344
+ schema := Schema {}
345
+ schema .SpacesById = make (map [uint32 ]Space )
346
+ schema .Spaces = make (map [string ]Space )
347
347
348
348
// Reload spaces.
349
- var spaces []* Space
350
- err = conn .SelectTyped (vspaceSpId , 0 , 0 , maxSchemas , IterAll , []interface {}{}, & spaces )
349
+ var spaces []Space
350
+ err : = conn .SelectTyped (vspaceSpId , 0 , 0 , maxSchemas , IterAll , []interface {}{}, & spaces )
351
351
if err != nil {
352
- return err
352
+ return Schema {}, err
353
353
}
354
354
for _ , space := range spaces {
355
355
schema .SpacesById [space .Id ] = space
356
356
schema .Spaces [space .Name ] = space
357
357
}
358
358
359
359
// Reload indexes.
360
- var indexes []* Index
360
+ var indexes []Index
361
361
err = conn .SelectTyped (vindexSpId , 0 , 0 , maxSchemas , IterAll , []interface {}{}, & indexes )
362
362
if err != nil {
363
- return err
363
+ return Schema {}, err
364
364
}
365
365
for _ , index := range indexes {
366
366
spaceId := index .SpaceId
367
367
if _ , ok := schema .SpacesById [spaceId ]; ok {
368
368
schema .SpacesById [spaceId ].IndexesById [index .Id ] = index
369
369
schema .SpacesById [spaceId ].Indexes [index .Name ] = index
370
370
} else {
371
- return errors .New ("concurrent schema update" )
371
+ return Schema {}, errors .New ("concurrent schema update" )
372
372
}
373
373
}
374
374
375
- spaceAndIndexNamesSupported :=
376
- isFeatureInSlice (iproto .IPROTO_FEATURE_SPACE_AND_INDEX_NAMES ,
377
- conn .serverProtocolInfo .Features )
378
-
379
- conn .lockShards ()
380
- conn .Schema = schema
381
- conn .schemaResolver = & loadedSchemaResolver {
382
- Schema : schema ,
383
- SpaceAndIndexNamesSupported : spaceAndIndexNamesSupported ,
384
- }
385
- conn .unlockShards ()
386
-
387
- return nil
375
+ return schema , nil
388
376
}
389
377
390
378
// resolveSpaceNumber tries to resolve a space number.
@@ -462,7 +450,7 @@ func resolveIndexNumber(i interface{}) (uint32, error) {
462
450
}
463
451
464
452
type loadedSchemaResolver struct {
465
- Schema * Schema
453
+ Schema Schema
466
454
// SpaceAndIndexNamesSupported shows if a current Tarantool version supports
467
455
// iproto.IPROTO_FEATURE_SPACE_AND_INDEX_NAMES.
468
456
SpaceAndIndexNamesSupported bool
0 commit comments