Skip to content

Commit 87fa2f8

Browse files
committed
draft: Refactoring of SpaceRequest
1 parent c0802b2 commit 87fa2f8

File tree

3 files changed

+105
-77
lines changed

3 files changed

+105
-77
lines changed

arrow/arrow_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestInsert_invalid(t *testing.T) {
8080
require.ErrorContains(t, err, a.expected)
8181
return
8282
}
83-
req := tarantool.NewInsertArrowRequest(space).Arrow(arr)
83+
req := arrow.NewInsertRequest(space, arr)
8484

8585
_, err = conn.Do(req).Get()
8686
require.ErrorContains(t, err, a.expected)

arrow/request.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package arrow
2+
3+
import (
4+
"context"
5+
6+
"github.com/tarantool/go-iproto"
7+
"github.com/tarantool/go-tarantool/v2"
8+
"github.com/vmihailenco/msgpack/v5"
9+
)
10+
11+
// INSERT Arrow request.
12+
//
13+
// FIXME: replace with iproto.IPROTO_INSERT_ARROW when iproto will released.
14+
// https://github.com/tarantool/go-replica/issues/30
15+
const iprotoInsertArrowType = iproto.Type(17)
16+
17+
// The data in Arrow format.
18+
//
19+
// FIXME: replace with iproto.IPROTO_ARROW when iproto will released.
20+
// https://github.com/tarantool/go-replica/issues/30
21+
const iprotoArrowKey = iproto.Key(0x36)
22+
23+
// InsertRequest helps you to create an insert request object for execution
24+
// by a Connection.
25+
type InsertRequest struct {
26+
tarantool.SpaceRequest
27+
arrow interface{}
28+
}
29+
30+
// NewInsertRequest returns a new empty InsertArrowRequest.
31+
func NewInsertRequest(space interface{}, arrow interface{}) *InsertRequest {
32+
return &InsertRequest{
33+
SpaceRequest: tarantool.NewSpaceRequest(iprotoInsertArrowType, space),
34+
arrow: arrow,
35+
}
36+
}
37+
38+
// Arrow sets the arrow for insertion the insert arrow request.
39+
// Note: default value is nil.
40+
func (req *InsertRequest) Arrow(arrow interface{}) *InsertRequest {
41+
req.arrow = arrow
42+
return req
43+
}
44+
45+
func (req *InsertRequest) fillInsertArrow(res tarantool.SchemaResolver, enc *msgpack.Encoder) error {
46+
if err := enc.EncodeMapLen(2); err != nil {
47+
return err
48+
}
49+
if err := req.SpaceRequest.Encode(res, enc); err != nil {
50+
return err
51+
}
52+
53+
if err := enc.EncodeUint(uint64(iprotoArrowKey)); err != nil {
54+
return err
55+
}
56+
return enc.Encode(req.arrow)
57+
}
58+
59+
// Body fills an msgpack.Encoder with the insert arrow request body.
60+
func (req *InsertRequest) Body(res tarantool.SchemaResolver, enc *msgpack.Encoder) error {
61+
62+
return req.fillInsertArrow(res, enc)
63+
}
64+
65+
// Context sets a passed context to the request.
66+
//
67+
// Pay attention that when using context with request objects,
68+
// the timeout option for Connection does not affect the lifetime
69+
// of the request. For those purposes use context.WithTimeout() as
70+
// the root context.
71+
func (req *InsertRequest) Context(ctx context.Context) *InsertRequest {
72+
req.SpaceRequest.Context(ctx)
73+
return req
74+
}

request.go

Lines changed: 30 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@ import (
1313
"github.com/vmihailenco/msgpack/v5"
1414
)
1515

16-
// INSERT Arrow request.
17-
//
18-
// FIXME: replace with iproto.IPROTO_INSERT_ARROW when iproto will released.
19-
// https://github.com/tarantool/go-replica/issues/30
20-
const iprotoInsertArrowType = iproto.Type(17)
21-
22-
// The data in Arrow format.
23-
//
24-
// FIXME: replace with iproto.IPROTO_ARROW when iproto will released.
25-
// https://github.com/tarantool/go-replica/issues/30
26-
const iprotoArrowKey = iproto.Key(0x36)
27-
2816
type spaceEncoder struct {
2917
Id uint32
3018
Name string
@@ -148,20 +136,6 @@ func fillInsert(enc *msgpack.Encoder, spaceEnc spaceEncoder, tuple interface{})
148136
return enc.Encode(tuple)
149137
}
150138

151-
func fillInsertArrow(enc *msgpack.Encoder, spaceEnc spaceEncoder, arrow interface{}) error {
152-
if err := enc.EncodeMapLen(2); err != nil {
153-
return err
154-
}
155-
if err := spaceEnc.Encode(enc); err != nil {
156-
return err
157-
}
158-
159-
if err := enc.EncodeUint(uint64(iprotoArrowKey)); err != nil {
160-
return err
161-
}
162-
return enc.Encode(arrow)
163-
}
164-
165139
func fillSelect(enc *msgpack.Encoder, spaceEnc spaceEncoder, indexEnc indexEncoder,
166140
offset, limit uint32, iterator Iter, key, after interface{}, fetchPos bool) error {
167141
mapLen := 6
@@ -888,17 +862,41 @@ func (req *baseRequest) Response(header Header, body io.Reader) (Response, error
888862
return &resp, nil
889863
}
890864

891-
type spaceRequest struct {
865+
type SpaceRequest struct {
892866
baseRequest
893867
space interface{}
894868
}
895869

896-
func (req *spaceRequest) setSpace(space interface{}) {
870+
func (r *SpaceRequest) Context(ctx context.Context) {
871+
r.ctx = ctx
872+
}
873+
874+
func NewSpaceRequest(rtype iproto.Type, space interface{}) SpaceRequest {
875+
return SpaceRequest{
876+
baseRequest: baseRequest{
877+
rtype: rtype,
878+
},
879+
space: space,
880+
}
881+
}
882+
883+
func (req *SpaceRequest) setSpace(space interface{}) {
897884
req.space = space
898885
}
899886

887+
func (req *SpaceRequest) Encode(res SchemaResolver, enc *msgpack.Encoder) error {
888+
spaceEnc, err := newSpaceEncoder(res, req.space)
889+
if err != nil {
890+
return err
891+
}
892+
if err := spaceEnc.Encode(enc); err != nil {
893+
return err
894+
}
895+
return nil
896+
}
897+
900898
type spaceIndexRequest struct {
901-
spaceRequest
899+
SpaceRequest
902900
index interface{}
903901
}
904902

@@ -1141,7 +1139,7 @@ func (req *SelectRequest) Response(header Header, body io.Reader) (Response, err
11411139
// InsertRequest helps you to create an insert request object for execution
11421140
// by a Connection.
11431141
type InsertRequest struct {
1144-
spaceRequest
1142+
SpaceRequest
11451143
tuple interface{}
11461144
}
11471145

@@ -1182,54 +1180,10 @@ func (req *InsertRequest) Context(ctx context.Context) *InsertRequest {
11821180
return req
11831181
}
11841182

1185-
// InsertArrowRequest helps you to create an insert request object for execution
1186-
// by a Connection.
1187-
type InsertArrowRequest struct {
1188-
spaceRequest
1189-
arrow interface{}
1190-
}
1191-
1192-
// NewInsertArrowRequest returns a new empty InsertArrowRequest.
1193-
func NewInsertArrowRequest(space interface{}) *InsertArrowRequest {
1194-
req := new(InsertArrowRequest)
1195-
req.rtype = iprotoInsertArrowType
1196-
req.setSpace(space)
1197-
req.arrow = []interface{}{}
1198-
return req
1199-
}
1200-
1201-
// Arrow sets the arrow for insertion the insert arrow request.
1202-
// Note: default value is nil.
1203-
func (req *InsertArrowRequest) Arrow(arrow interface{}) *InsertArrowRequest {
1204-
req.arrow = arrow
1205-
return req
1206-
}
1207-
1208-
// Body fills an msgpack.Encoder with the insert arrow request body.
1209-
func (req *InsertArrowRequest) Body(res SchemaResolver, enc *msgpack.Encoder) error {
1210-
spaceEnc, err := newSpaceEncoder(res, req.space)
1211-
if err != nil {
1212-
return err
1213-
}
1214-
1215-
return fillInsertArrow(enc, spaceEnc, req.arrow)
1216-
}
1217-
1218-
// Context sets a passed context to the request.
1219-
//
1220-
// Pay attention that when using context with request objects,
1221-
// the timeout option for Connection does not affect the lifetime
1222-
// of the request. For those purposes use context.WithTimeout() as
1223-
// the root context.
1224-
func (req *InsertArrowRequest) Context(ctx context.Context) *InsertArrowRequest {
1225-
req.ctx = ctx
1226-
return req
1227-
}
1228-
12291183
// ReplaceRequest helps you to create a replace request object for execution
12301184
// by a Connection.
12311185
type ReplaceRequest struct {
1232-
spaceRequest
1186+
SpaceRequest
12331187
tuple interface{}
12341188
}
12351189

@@ -1391,7 +1345,7 @@ func (req *UpdateRequest) Context(ctx context.Context) *UpdateRequest {
13911345
// UpsertRequest helps you to create an upsert request object for execution
13921346
// by a Connection.
13931347
type UpsertRequest struct {
1394-
spaceRequest
1348+
SpaceRequest
13951349
tuple interface{}
13961350
ops *Operations
13971351
}

0 commit comments

Comments
 (0)