Skip to content

Commit

Permalink
crud: support schema opts
Browse files Browse the repository at this point in the history
Follows #336
  • Loading branch information
DifferentialOrange committed Nov 8, 2023
1 parent 9a58dc8 commit c461e79
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
- Support `operation_data` in `crud.Error` (#330)
- Support `fetch_latest_metadata` option for crud requests with metadata (#335)
- Support `noreturn` option for data change crud requests (#335)
- Support `crud.schema` request (#336)
- Support `crud.schema` request (#336, #351)
- Support `IPROTO_WATCH_ONCE` request type for Tarantool
version >= 3.0.0-alpha1 (#337)
- Support `yield_every` option for crud select requests (#350)
Expand Down
1 change: 1 addition & 0 deletions crud/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
batchSizeOptName = "batch_size"
fetchLatestMetadataOptName = "fetch_latest_metadata"
noreturnOptName = "noreturn"
cachedOptName = "cached"
)

// OptUint is an optional uint.
Expand Down
16 changes: 16 additions & 0 deletions crud/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func TestRequestsCodes(t *testing.T) {
{req: crud.MakeCountRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeStorageInfoRequest(), rtype: CrudRequestType},
{req: crud.MakeStatsRequest(), rtype: CrudRequestType},
{req: crud.MakeSchemaRequest(), rtype: CrudRequestType},
}

for _, test := range tests {
Expand Down Expand Up @@ -231,6 +232,7 @@ func TestRequestsAsync(t *testing.T) {
{req: crud.MakeCountRequest(validSpace), async: false},
{req: crud.MakeStorageInfoRequest(), async: false},
{req: crud.MakeStatsRequest(), async: false},
{req: crud.MakeSchemaRequest(), async: false},
}

for _, test := range tests {
Expand Down Expand Up @@ -268,6 +270,7 @@ func TestRequestsCtx_default(t *testing.T) {
{req: crud.MakeCountRequest(validSpace), expected: nil},
{req: crud.MakeStorageInfoRequest(), expected: nil},
{req: crud.MakeStatsRequest(), expected: nil},
{req: crud.MakeSchemaRequest(), expected: nil},
}

for _, test := range tests {
Expand Down Expand Up @@ -306,6 +309,7 @@ func TestRequestsCtx_setter(t *testing.T) {
{req: crud.MakeCountRequest(validSpace).Context(ctx), expected: ctx},
{req: crud.MakeStorageInfoRequest().Context(ctx), expected: ctx},
{req: crud.MakeStatsRequest().Context(ctx), expected: ctx},
{req: crud.MakeSchemaRequest().Context(ctx), expected: ctx},
}

for _, test := range tests {
Expand Down Expand Up @@ -462,6 +466,12 @@ func TestRequestsDefaultValues(t *testing.T) {
[]interface{}{}),
target: crud.MakeStatsRequest(),
},
{
name: "SchemaRequest",
ref: tarantool.NewCall17Request("crud.schema").Args(
[]interface{}{}),
target: crud.MakeSchemaRequest(),
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -624,6 +634,12 @@ func TestRequestsSetters(t *testing.T) {
[]interface{}{spaceName}),
target: crud.MakeStatsRequest().Space(spaceName),
},
{
name: "SchemaRequest",
ref: tarantool.NewCall17Request("crud.schema").Args(
[]interface{}{spaceName}),
target: crud.MakeSchemaRequest().Space(spaceName),
},
}

for _, tc := range testCases {
Expand Down
35 changes: 35 additions & 0 deletions crud/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,39 @@ func msgpackIsMap(code byte) bool {
return code == msgpcode.Map16 || code == msgpcode.Map32 || msgpcode.IsFixedMap(code)
}

// SchemaOpts describes options for `crud.schema` method.
type SchemaOpts struct {
// Timeout is a `vshard.call` timeout and vshard
// master discovery timeout (in seconds).
Timeout OptFloat64
// VshardRouter is cartridge vshard group name or
// vshard router instance.
VshardRouter OptString
// Cached defines whether router should reload storage schema on call.
Cached OptBool
}

// EncodeMsgpack provides custom msgpack encoder.
func (opts SchemaOpts) EncodeMsgpack(enc *msgpack.Encoder) error {
const optsCnt = 3

names := [optsCnt]string{timeoutOptName, vshardRouterOptName,
cachedOptName}
values := [optsCnt]interface{}{}
exists := [optsCnt]bool{}
values[0], exists[0] = opts.Timeout.Get()
values[1], exists[1] = opts.VshardRouter.Get()
values[2], exists[2] = opts.Cached.Get()

return encodeOptions(enc, names[:], values[:], exists[:])
}

// SchemaRequest helps you to create request object to call `crud.schema`
// for execution by a Connection.
type SchemaRequest struct {
baseRequest
space OptString
opts SchemaOpts
}

// MakeSchemaRequest returns a new empty SchemaRequest.
Expand All @@ -35,6 +63,13 @@ func (req SchemaRequest) Space(space string) SchemaRequest {
return req
}

// Opts sets the options for the SelectRequest request.
// Note: default value is nil.
func (req SchemaRequest) Opts(opts SchemaOpts) SchemaRequest {
req.opts = opts
return req
}

// Body fills an encoder with the call request body.
func (req SchemaRequest) Body(res tarantool.SchemaResolver, enc *msgpack.Encoder) error {
if value, ok := req.space.Get(); ok {
Expand Down
10 changes: 10 additions & 0 deletions crud/tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ var opObjManyOpts = crud.OperationObjectManyOpts{
Timeout: crud.MakeOptFloat64(timeout),
}

var schemaOpts = crud.SchemaOpts{
Timeout: crud.MakeOptFloat64(timeout),
Cached: crud.MakeOptBool(false),
}

var conditions = []crud.Condition{
{
Operator: crud.Lt,
Expand Down Expand Up @@ -216,6 +221,11 @@ var testProcessDataCases = []struct {
1,
crud.MakeStorageInfoRequest().Opts(baseOpts),
},
{
"Schema",
1,
crud.MakeSchemaRequest().Opts(schemaOpts),
},
}

var testResultWithErrCases = []struct {
Expand Down

0 comments on commit c461e79

Please sign in to comment.