Skip to content

Commit 024b84a

Browse files
committed
api: support IPROTO_FEATURE_SPACE_AND_INDEX_NAMES
Support `IPROTO_FEATURE_SPACE_AND_INDEX_NAMES` for Tarantool version >= 3.0.0-alpha. It allows to use space and index names in requests instead of their IDs. `ResolveSpaceIndex` function for `SchemaResolver` interface split into two: `ResolveSpace` and `ResolveIndex`. `NamesUseSupported` function added into the interface to get information if usage of space and index names is supported. Closes #338
1 parent a664c6b commit 024b84a

12 files changed

+670
-139
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
2222
- Support `crud.schema` request (#336)
2323
- Support `IPROTO_WATCH_ONCE` request type for Tarantool
2424
version >= 3.0.0-alpha1 (#337)
25+
- Support `IPROTO_FEATURE_SPACE_AND_INDEX_NAMES` for Tarantool
26+
version >= 3.0.0-alpha1 (#338). It allows to use space and index names
27+
in requests instead of their IDs
2528

2629
### Changed
2730

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ and user may cancel it in process.
239239
* `iproto.Feature` type used instead of `ProtocolFeature`.
240240
* `iproto.IPROTO_FEATURE_` constants used instead of local ones.
241241

242+
#### Schema changes
243+
244+
`ResolveSpaceIndex` function for `SchemaResolver` interface split into two:
245+
`ResolveSpace` and `ResolveIndex`. `NamesUseSupported` function added into the
246+
interface to get information if usage of space and index names in requests
247+
is supported.
248+
242249
## Contributing
243250

244251
See [the contributing guide](CONTRIBUTING.md) for detailed instructions on how

crud/request_test.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/tarantool/go-tarantool/v2"
1414
"github.com/tarantool/go-tarantool/v2/crud"
15+
"github.com/tarantool/go-tarantool/v2/test_helpers"
1516
)
1617

1718
const invalidSpaceMsg = "invalid space"
@@ -72,25 +73,38 @@ var expectedOpts = map[string]interface{}{
7273
type ValidSchemeResolver struct {
7374
}
7475

75-
func (*ValidSchemeResolver) ResolveSpaceIndex(s, i interface{}) (uint32, uint32, error) {
76-
var spaceNo, indexNo uint32
76+
func (*ValidSchemeResolver) ResolveSpace(s interface{}) (uint32, error) {
77+
var spaceNo uint32
7778
if s != nil {
7879
spaceNo = uint32(s.(int))
7980
} else {
8081
spaceNo = defaultSpace
8182
}
83+
if spaceNo == invalidSpace {
84+
return 0, errors.New(invalidSpaceMsg)
85+
}
86+
return spaceNo, nil
87+
}
88+
89+
func (*ValidSchemeResolver) ResolveIndex(i interface{}, spaceNo uint32) (uint32, error) {
90+
var indexNo uint32
8291
if i != nil {
8392
indexNo = uint32(i.(int))
8493
} else {
8594
indexNo = defaultIndex
8695
}
87-
if spaceNo == invalidSpace {
88-
return 0, 0, errors.New(invalidSpaceMsg)
89-
}
9096
if indexNo == invalidIndex {
91-
return 0, 0, errors.New(invalidIndexMsg)
97+
return 0, errors.New(invalidIndexMsg)
98+
}
99+
return indexNo, nil
100+
}
101+
102+
func (*ValidSchemeResolver) NamesUseSupported() bool {
103+
isLess, err := test_helpers.IsTarantoolVersionLess(3, 0, 0)
104+
if err != nil {
105+
return false
92106
}
93-
return spaceNo, indexNo, nil
107+
return !isLess
94108
}
95109

96110
var resolver ValidSchemeResolver

example_test.go

+118
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,26 @@ func ExampleSelectRequest() {
231231
// response is [{{} 1111 hello world}]
232232
}
233233

234+
func ExampleSelectRequest_spaceAndIndexNames() {
235+
isLess, err := test_helpers.IsTarantoolVersionLess(3, 0, 0)
236+
if err != nil || isLess {
237+
return
238+
}
239+
240+
conn := exampleConnect(opts)
241+
defer conn.Close()
242+
243+
req := tarantool.NewSelectRequest(spaceName)
244+
req.Index(indexName)
245+
resp, err := conn.Do(req).Get()
246+
247+
if err != nil {
248+
fmt.Printf("Failed to execute the request: %s\n", err)
249+
} else {
250+
fmt.Println(resp.Data)
251+
}
252+
}
253+
234254
func ExampleInsertRequest() {
235255
conn := exampleConnect(opts)
236256
defer conn.Close()
@@ -273,6 +293,25 @@ func ExampleInsertRequest() {
273293
// Data [[32 test one]]
274294
}
275295

296+
func ExampleInsertRequest_spaceAndIndexNames() {
297+
isLess, err := test_helpers.IsTarantoolVersionLess(3, 0, 0)
298+
if err != nil || isLess {
299+
return
300+
}
301+
302+
conn := exampleConnect(opts)
303+
defer conn.Close()
304+
305+
req := tarantool.NewInsertRequest(spaceName)
306+
resp, err := conn.Do(req).Get()
307+
308+
if err != nil {
309+
fmt.Printf("Failed to execute the request: %s\n", err)
310+
} else {
311+
fmt.Println(resp.Data)
312+
}
313+
}
314+
276315
func ExampleDeleteRequest() {
277316
conn := exampleConnect(opts)
278317
defer conn.Close()
@@ -316,6 +355,26 @@ func ExampleDeleteRequest() {
316355
// Data [[36 test one]]
317356
}
318357

358+
func ExampleDeleteRequest_spaceAndIndexNames() {
359+
isLess, err := test_helpers.IsTarantoolVersionLess(3, 0, 0)
360+
if err != nil || isLess {
361+
return
362+
}
363+
364+
conn := exampleConnect(opts)
365+
defer conn.Close()
366+
367+
req := tarantool.NewDeleteRequest(spaceName)
368+
req.Index(indexName)
369+
resp, err := conn.Do(req).Get()
370+
371+
if err != nil {
372+
fmt.Printf("Failed to execute the request: %s\n", err)
373+
} else {
374+
fmt.Println(resp.Data)
375+
}
376+
}
377+
319378
func ExampleReplaceRequest() {
320379
conn := exampleConnect(opts)
321380
defer conn.Close()
@@ -375,6 +434,25 @@ func ExampleReplaceRequest() {
375434
// Data [[13 test twelve]]
376435
}
377436

437+
func ExampleReplaceRequest_spaceAndIndexNames() {
438+
isLess, err := test_helpers.IsTarantoolVersionLess(3, 0, 0)
439+
if err != nil || isLess {
440+
return
441+
}
442+
443+
conn := exampleConnect(opts)
444+
defer conn.Close()
445+
446+
req := tarantool.NewReplaceRequest(spaceName)
447+
resp, err := conn.Do(req).Get()
448+
449+
if err != nil {
450+
fmt.Printf("Failed to execute the request: %s\n", err)
451+
} else {
452+
fmt.Println(resp.Data)
453+
}
454+
}
455+
378456
func ExampleUpdateRequest() {
379457
conn := exampleConnect(opts)
380458
defer conn.Close()
@@ -411,6 +489,26 @@ func ExampleUpdateRequest() {
411489
// response is []interface {}{[]interface {}{0x457, "hello", "world"}}
412490
}
413491

492+
func ExampleUpdateRequest_spaceAndIndexNames() {
493+
isLess, err := test_helpers.IsTarantoolVersionLess(3, 0, 0)
494+
if err != nil || isLess {
495+
return
496+
}
497+
498+
conn := exampleConnect(opts)
499+
defer conn.Close()
500+
501+
req := tarantool.NewUpdateRequest(spaceName)
502+
req.Index(indexName)
503+
resp, err := conn.Do(req).Get()
504+
505+
if err != nil {
506+
fmt.Printf("Failed to execute the request: %s\n", err)
507+
} else {
508+
fmt.Println(resp.Data)
509+
}
510+
}
511+
414512
func ExampleUpsertRequest() {
415513
conn := exampleConnect(opts)
416514
defer conn.Close()
@@ -452,6 +550,25 @@ func ExampleUpsertRequest() {
452550
// response is []interface {}{[]interface {}{0x459, "first", "updated"}}
453551
}
454552

553+
func ExampleUpsertRequest_spaceAndIndexNames() {
554+
isLess, err := test_helpers.IsTarantoolVersionLess(3, 0, 0)
555+
if err != nil || isLess {
556+
return
557+
}
558+
559+
conn := exampleConnect(opts)
560+
defer conn.Close()
561+
562+
req := tarantool.NewUpsertRequest(spaceName)
563+
resp, err := conn.Do(req).Get()
564+
565+
if err != nil {
566+
fmt.Printf("Failed to execute the request: %s\n", err)
567+
} else {
568+
fmt.Println(resp.Data)
569+
}
570+
}
571+
455572
func ExampleCallRequest() {
456573
conn := exampleConnect(opts)
457574
defer conn.Close()
@@ -634,6 +751,7 @@ func ExampleProtocolVersion() {
634751
// Connector client protocol feature: IPROTO_FEATURE_ERROR_EXTENSION
635752
// Connector client protocol feature: IPROTO_FEATURE_WATCHERS
636753
// Connector client protocol feature: IPROTO_FEATURE_PAGINATION
754+
// Connector client protocol feature: IPROTO_FEATURE_SPACE_AND_INDEX_NAMES
637755
// Connector client protocol feature: IPROTO_FEATURE_WATCH_ONCE
638756
}
639757

export_test.go

+38-13
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,24 @@ import (
55
"net"
66
"time"
77

8+
"github.com/tarantool/go-iproto"
89
"github.com/vmihailenco/msgpack/v5"
910
)
1011

12+
func getSpaceKey(space interface{}) iproto.Key {
13+
if _, ok := space.(string); ok {
14+
return iproto.IPROTO_SPACE_NAME
15+
}
16+
return iproto.IPROTO_SPACE_ID
17+
}
18+
19+
func getIndexKey(index interface{}) iproto.Key {
20+
if _, ok := index.(string); ok {
21+
return iproto.IPROTO_INDEX_NAME
22+
}
23+
return iproto.IPROTO_INDEX_ID
24+
}
25+
1126
func SslDialContext(ctx context.Context, network, address string,
1227
opts SslOpts) (connection net.Conn, err error) {
1328
return sslDialContext(ctx, network, address, opts)
@@ -25,39 +40,49 @@ func RefImplPingBody(enc *msgpack.Encoder) error {
2540

2641
// RefImplSelectBody is reference implementation for filling of a select
2742
// request's body.
28-
func RefImplSelectBody(enc *msgpack.Encoder, space, index, offset, limit uint32, iterator Iter,
29-
key, after interface{}, fetchPos bool) error {
30-
return fillSelect(enc, space, index, offset, limit, iterator, key, after, fetchPos)
43+
func RefImplSelectBody(enc *msgpack.Encoder, space, index interface{},
44+
offset, limit uint32, iterator Iter, key, after interface{}, fetchPos bool) error {
45+
iprotoSpaceKey := getSpaceKey(space)
46+
iprotoIndexKey := getIndexKey(index)
47+
return fillSelect(enc, space, index, offset, limit, iterator, key, after,
48+
fetchPos, iprotoSpaceKey, iprotoIndexKey)
3149
}
3250

3351
// RefImplInsertBody is reference implementation for filling of an insert
3452
// request's body.
35-
func RefImplInsertBody(enc *msgpack.Encoder, space uint32, tuple interface{}) error {
36-
return fillInsert(enc, space, tuple)
53+
func RefImplInsertBody(enc *msgpack.Encoder, space, tuple interface{}) error {
54+
iprotoSpaceKey := getSpaceKey(space)
55+
return fillInsert(enc, space, tuple, iprotoSpaceKey)
3756
}
3857

3958
// RefImplReplaceBody is reference implementation for filling of a replace
4059
// request's body.
41-
func RefImplReplaceBody(enc *msgpack.Encoder, space uint32, tuple interface{}) error {
42-
return fillInsert(enc, space, tuple)
60+
func RefImplReplaceBody(enc *msgpack.Encoder, space, tuple interface{}) error {
61+
iprotoSpaceKey := getSpaceKey(space)
62+
return fillInsert(enc, space, tuple, iprotoSpaceKey)
4363
}
4464

4565
// RefImplDeleteBody is reference implementation for filling of a delete
4666
// request's body.
47-
func RefImplDeleteBody(enc *msgpack.Encoder, space, index uint32, key interface{}) error {
48-
return fillDelete(enc, space, index, key)
67+
func RefImplDeleteBody(enc *msgpack.Encoder, space, index, key interface{}) error {
68+
iprotoSpaceKey := getSpaceKey(space)
69+
iprotoIndexKey := getIndexKey(index)
70+
return fillDelete(enc, space, index, key, iprotoSpaceKey, iprotoIndexKey)
4971
}
5072

5173
// RefImplUpdateBody is reference implementation for filling of an update
5274
// request's body.
53-
func RefImplUpdateBody(enc *msgpack.Encoder, space, index uint32, key, ops interface{}) error {
54-
return fillUpdate(enc, space, index, key, ops)
75+
func RefImplUpdateBody(enc *msgpack.Encoder, space, index, key, ops interface{}) error {
76+
iprotoSpaceKey := getSpaceKey(space)
77+
iprotoIndexKey := getIndexKey(index)
78+
return fillUpdate(enc, space, index, key, ops, iprotoSpaceKey, iprotoIndexKey)
5579
}
5680

5781
// RefImplUpsertBody is reference implementation for filling of an upsert
5882
// request's body.
59-
func RefImplUpsertBody(enc *msgpack.Encoder, space uint32, tuple, ops interface{}) error {
60-
return fillUpsert(enc, space, tuple, ops)
83+
func RefImplUpsertBody(enc *msgpack.Encoder, space, tuple, ops interface{}) error {
84+
iprotoSpaceKey := getSpaceKey(space)
85+
return fillUpsert(enc, space, tuple, ops, iprotoSpaceKey)
6186
}
6287

6388
// RefImplCallBody is reference implementation for filling of a call or call17

protocol.go

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var clientProtocolInfo ProtocolInfo = ProtocolInfo{
5656
iproto.IPROTO_FEATURE_ERROR_EXTENSION,
5757
iproto.IPROTO_FEATURE_WATCHERS,
5858
iproto.IPROTO_FEATURE_PAGINATION,
59+
iproto.IPROTO_FEATURE_SPACE_AND_INDEX_NAMES,
5960
iproto.IPROTO_FEATURE_WATCH_ONCE,
6061
},
6162
}

0 commit comments

Comments
 (0)