Skip to content

Commit 48e65ed

Browse files
committed
api: write a connection schema getter
Write a helper function to load the actual schema for the user. Previously we stored actual schema in a private `schemaResolver` field and `Schema` field was used only to get a current schema. But now because of the new method, we don't need to store the `Schema` as a different field. So `Schema` was also removed. Closes #7
1 parent 6225ec4 commit 48e65ed

8 files changed

+68
-8
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
2626
- Support `IPROTO_FEATURE_SPACE_AND_INDEX_NAMES` for Tarantool
2727
version >= 3.0.0-alpha1 (#338). It allows to use space and index names
2828
in requests instead of their IDs.
29+
- `GetSchema` method to the `Connection` struct (#7)
2930

3031
### Changed
3132

@@ -70,6 +71,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
7071
- UUID_extId (#158)
7172
- IPROTO constants (#158)
7273
- Code() method from the Request interface (#158)
74+
- `Schema` field from the `Connection` struct (#7)
7375

7476
### Fixed
7577

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ now does not attempt to reconnect and tries to establish a connection only once.
248248
Function might be canceled via context. Context accepted as first argument,
249249
and user may cancel it in process.
250250

251+
#### Connection Schema
252+
253+
Removed `Schema` field from the `Connection` struct. Instead, new `GetSchema()`
254+
metod was added to get the current connection schema on demand.
255+
251256
#### Protocol changes
252257

253258
* `iproto.Feature` type used instead of `ProtocolFeature`.
@@ -260,6 +265,7 @@ and user may cancel it in process.
260265
interface to get information if the usage of space and index names in requests
261266
is supported.
262267
* `Schema` structure no longer implements `SchemaResolver` interface.
268+
* `GetSchema` method added to the `SchemaResolver` interface.
263269

264270
## Contributing
265271

Diff for: connection.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ type Connection struct {
160160
c Conn
161161
mutex sync.Mutex
162162
cond *sync.Cond
163-
// Schema contains schema loaded on connection.
164-
Schema *Schema
165163
// schemaResolver contains a SchemaResolver implementation.
166164
schemaResolver SchemaResolver
167165
// requestId contains the last request ID for requests with nil context.
@@ -1302,15 +1300,27 @@ func (conn *Connection) ConfiguredTimeout() time.Duration {
13021300
return conn.opts.Timeout
13031301
}
13041302

1303+
// GetSchema returns the current connection Schema.
1304+
func (conn *Connection) GetSchema() *Schema {
1305+
return conn.schemaResolver.GetSchema()
1306+
}
1307+
13051308
// OverrideSchema sets Schema for the connection.
13061309
func (conn *Connection) OverrideSchema(s *Schema) {
13071310
if s != nil {
1311+
spaceAndIndexNamesSupported :=
1312+
isFeatureInSlice(iproto.IPROTO_FEATURE_SPACE_AND_INDEX_NAMES,
1313+
conn.serverProtocolInfo.Features)
1314+
13081315
conn.mutex.Lock()
13091316
defer conn.mutex.Unlock()
13101317
conn.lockShards()
13111318
defer conn.unlockShards()
13121319

1313-
conn.Schema = s
1320+
conn.schemaResolver = &loadedSchemaResolver{
1321+
Schema: s,
1322+
SpaceAndIndexNamesSupported: spaceAndIndexNamesSupported,
1323+
}
13141324
}
13151325
}
13161326

Diff for: example_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ func ExampleSchema() {
10631063
conn := exampleConnect(opts)
10641064
defer conn.Close()
10651065

1066-
schema := conn.Schema
1066+
schema := conn.GetSchema()
10671067
if schema.SpacesById == nil {
10681068
fmt.Println("schema.SpacesById is nil")
10691069
}
@@ -1086,7 +1086,7 @@ func ExampleSpace() {
10861086
defer conn.Close()
10871087

10881088
// Save Schema to a local variable to avoid races
1089-
schema := conn.Schema
1089+
schema := conn.GetSchema()
10901090
if schema.SpacesById == nil {
10911091
fmt.Println("schema.SpacesById is nil")
10921092
}

Diff for: request_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func (r *ValidSchemeResolver) NamesUseSupported() bool {
7878
return r.nameUseSupported
7979
}
8080

81+
func (r *ValidSchemeResolver) GetSchema() *Schema {
82+
return nil
83+
}
84+
8185
var resolver ValidSchemeResolver
8286

8387
func assertBodyCall(t testing.TB, requests []Request, errorMsg string) {

Diff for: schema.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type SchemaResolver interface {
5252
// IDs, is supported. It must return true if
5353
// iproto.IPROTO_FEATURE_SPACE_AND_INDEX_NAMES is supported.
5454
NamesUseSupported() bool
55+
// GetSchema returns inner Schema.
56+
GetSchema() *Schema
5557
}
5658

5759
// Schema contains information about spaces and indexes.
@@ -377,7 +379,6 @@ func (conn *Connection) loadSchema() (err error) {
377379
conn.serverProtocolInfo.Features)
378380

379381
conn.lockShards()
380-
conn.Schema = schema
381382
conn.schemaResolver = &loadedSchemaResolver{
382383
Schema: schema,
383384
SpaceAndIndexNamesSupported: spaceAndIndexNamesSupported,
@@ -502,6 +503,10 @@ func (r *loadedSchemaResolver) NamesUseSupported() bool {
502503
return r.SpaceAndIndexNamesSupported
503504
}
504505

506+
func (r *loadedSchemaResolver) GetSchema() *Schema {
507+
return r.Schema
508+
}
509+
505510
type noSchemaResolver struct {
506511
// SpaceAndIndexNamesSupported shows if a current Tarantool version supports
507512
// iproto.IPROTO_FEATURE_SPACE_AND_INDEX_NAMES.
@@ -527,3 +532,7 @@ func (*noSchemaResolver) ResolveIndex(i interface{}, spaceNo uint32) (uint32, er
527532
func (r *noSchemaResolver) NamesUseSupported() bool {
528533
return r.SpaceAndIndexNamesSupported
529534
}
535+
536+
func (r *noSchemaResolver) GetSchema() *Schema {
537+
return nil
538+
}

Diff for: settings/request_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func (r *ValidSchemeResolver) NamesUseSupported() bool {
2828
return false
2929
}
3030

31+
func (r *ValidSchemeResolver) GetSchema() *tarantool.Schema {
32+
return nil
33+
}
34+
3135
var resolver ValidSchemeResolver
3236

3337
func TestRequestsAPI(t *testing.T) {

Diff for: tarantool_test.go

+27-2
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,31 @@ func TestConnection_DoWithStrangerConn(t *testing.T) {
18511851
}
18521852
}
18531853

1854+
func TestConnection_GetSchema(t *testing.T) {
1855+
conn := test_helpers.ConnectWithValidation(t, server, opts)
1856+
defer conn.Close()
1857+
1858+
s := conn.GetSchema()
1859+
if s.Version != 0 || s.Spaces[spaceName].Id != spaceNo {
1860+
t.Errorf("GetSchema() returns incorrect schema")
1861+
}
1862+
}
1863+
1864+
func TestConnection_OverrideSchema(t *testing.T) {
1865+
conn := test_helpers.ConnectWithValidation(t, server, opts)
1866+
defer conn.Close()
1867+
1868+
newSchema := Schema{
1869+
Version: 2,
1870+
}
1871+
conn.OverrideSchema(&newSchema)
1872+
1873+
s := conn.GetSchema()
1874+
if s.Version != 2 || len(s.Spaces) != 0 || len(s.SpacesById) != 0 {
1875+
t.Errorf("schema did not get updated")
1876+
}
1877+
}
1878+
18541879
func TestNewPreparedFromResponse(t *testing.T) {
18551880
var (
18561881
ErrNilResponsePassed = fmt.Errorf("passed nil response")
@@ -1882,7 +1907,7 @@ func TestSchema(t *testing.T) {
18821907
defer conn.Close()
18831908

18841909
// Schema
1885-
schema := conn.Schema
1910+
schema := conn.GetSchema()
18861911
if schema.SpacesById == nil {
18871912
t.Errorf("schema.SpacesById is nil")
18881913
}
@@ -2028,7 +2053,7 @@ func TestSchema_IsNullable(t *testing.T) {
20282053
conn := test_helpers.ConnectWithValidation(t, server, opts)
20292054
defer conn.Close()
20302055

2031-
schema := conn.Schema
2056+
schema := conn.GetSchema()
20322057
if schema.Spaces == nil {
20332058
t.Errorf("schema.Spaces is nil")
20342059
}

0 commit comments

Comments
 (0)