Skip to content

Commit

Permalink
api: write a connection schema getter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
DerekBum committed Nov 18, 2023
1 parent 6225ec4 commit 48e65ed
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
- Support `IPROTO_FEATURE_SPACE_AND_INDEX_NAMES` for Tarantool
version >= 3.0.0-alpha1 (#338). It allows to use space and index names
in requests instead of their IDs.
- `GetSchema` method to the `Connection` struct (#7)

### Changed

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

### Fixed

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ now does not attempt to reconnect and tries to establish a connection only once.
Function might be canceled via context. Context accepted as first argument,
and user may cancel it in process.

#### Connection Schema

Removed `Schema` field from the `Connection` struct. Instead, new `GetSchema()`
metod was added to get the current connection schema on demand.

#### Protocol changes

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

## Contributing

Expand Down
16 changes: 13 additions & 3 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ type Connection struct {
c Conn
mutex sync.Mutex
cond *sync.Cond
// Schema contains schema loaded on connection.
Schema *Schema
// schemaResolver contains a SchemaResolver implementation.
schemaResolver SchemaResolver
// requestId contains the last request ID for requests with nil context.
Expand Down Expand Up @@ -1302,15 +1300,27 @@ func (conn *Connection) ConfiguredTimeout() time.Duration {
return conn.opts.Timeout
}

// GetSchema returns the current connection Schema.
func (conn *Connection) GetSchema() *Schema {
return conn.schemaResolver.GetSchema()
}

// OverrideSchema sets Schema for the connection.
func (conn *Connection) OverrideSchema(s *Schema) {
if s != nil {
spaceAndIndexNamesSupported :=
isFeatureInSlice(iproto.IPROTO_FEATURE_SPACE_AND_INDEX_NAMES,
conn.serverProtocolInfo.Features)

conn.mutex.Lock()
defer conn.mutex.Unlock()
conn.lockShards()
defer conn.unlockShards()

conn.Schema = s
conn.schemaResolver = &loadedSchemaResolver{
Schema: s,
SpaceAndIndexNamesSupported: spaceAndIndexNamesSupported,
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ func ExampleSchema() {
conn := exampleConnect(opts)
defer conn.Close()

schema := conn.Schema
schema := conn.GetSchema()
if schema.SpacesById == nil {
fmt.Println("schema.SpacesById is nil")
}
Expand All @@ -1086,7 +1086,7 @@ func ExampleSpace() {
defer conn.Close()

// Save Schema to a local variable to avoid races
schema := conn.Schema
schema := conn.GetSchema()
if schema.SpacesById == nil {
fmt.Println("schema.SpacesById is nil")
}
Expand Down
4 changes: 4 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (r *ValidSchemeResolver) NamesUseSupported() bool {
return r.nameUseSupported
}

func (r *ValidSchemeResolver) GetSchema() *Schema {
return nil
}

var resolver ValidSchemeResolver

func assertBodyCall(t testing.TB, requests []Request, errorMsg string) {
Expand Down
11 changes: 10 additions & 1 deletion schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type SchemaResolver interface {
// IDs, is supported. It must return true if
// iproto.IPROTO_FEATURE_SPACE_AND_INDEX_NAMES is supported.
NamesUseSupported() bool
// GetSchema returns inner Schema.
GetSchema() *Schema
}

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

conn.lockShards()
conn.Schema = schema
conn.schemaResolver = &loadedSchemaResolver{
Schema: schema,
SpaceAndIndexNamesSupported: spaceAndIndexNamesSupported,
Expand Down Expand Up @@ -502,6 +503,10 @@ func (r *loadedSchemaResolver) NamesUseSupported() bool {
return r.SpaceAndIndexNamesSupported
}

func (r *loadedSchemaResolver) GetSchema() *Schema {
return r.Schema
}

type noSchemaResolver struct {
// SpaceAndIndexNamesSupported shows if a current Tarantool version supports
// iproto.IPROTO_FEATURE_SPACE_AND_INDEX_NAMES.
Expand All @@ -527,3 +532,7 @@ func (*noSchemaResolver) ResolveIndex(i interface{}, spaceNo uint32) (uint32, er
func (r *noSchemaResolver) NamesUseSupported() bool {
return r.SpaceAndIndexNamesSupported
}

func (r *noSchemaResolver) GetSchema() *Schema {
return nil
}
4 changes: 4 additions & 0 deletions settings/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (r *ValidSchemeResolver) NamesUseSupported() bool {
return false
}

func (r *ValidSchemeResolver) GetSchema() *tarantool.Schema {
return nil
}

var resolver ValidSchemeResolver

func TestRequestsAPI(t *testing.T) {
Expand Down
29 changes: 27 additions & 2 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,31 @@ func TestConnection_DoWithStrangerConn(t *testing.T) {
}
}

func TestConnection_GetSchema(t *testing.T) {
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

s := conn.GetSchema()
if s.Version != 0 || s.Spaces[spaceName].Id != spaceNo {
t.Errorf("GetSchema() returns incorrect schema")
}
}

func TestConnection_OverrideSchema(t *testing.T) {
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

newSchema := Schema{
Version: 2,
}
conn.OverrideSchema(&newSchema)

s := conn.GetSchema()
if s.Version != 2 || len(s.Spaces) != 0 || len(s.SpacesById) != 0 {
t.Errorf("schema did not get updated")
}
}

func TestNewPreparedFromResponse(t *testing.T) {
var (
ErrNilResponsePassed = fmt.Errorf("passed nil response")
Expand Down Expand Up @@ -1882,7 +1907,7 @@ func TestSchema(t *testing.T) {
defer conn.Close()

// Schema
schema := conn.Schema
schema := conn.GetSchema()
if schema.SpacesById == nil {
t.Errorf("schema.SpacesById is nil")
}
Expand Down Expand Up @@ -2028,7 +2053,7 @@ func TestSchema_IsNullable(t *testing.T) {
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

schema := conn.Schema
schema := conn.GetSchema()
if schema.Spaces == nil {
t.Errorf("schema.Spaces is nil")
}
Expand Down

0 comments on commit 48e65ed

Please sign in to comment.