From 56a13bc71168c14d41bfe0825e3c54c7306b10d2 Mon Sep 17 00:00:00 2001 From: Oleg Jukovec Date: Fri, 24 Mar 2023 16:27:30 +0300 Subject: [PATCH] api: add separate types for constants Part of #158 --- CHANGELOG.md | 3 ++ connection.go | 28 ++++++++----- connector.go | 6 +-- const.go | 45 +------------------- crud/common.go | 2 +- crud/request_test.go | 50 +++++++++++----------- crud/tarantool_test.go | 2 +- errors.go | 2 +- example_test.go | 4 +- export_test.go | 4 +- future.go | 2 +- future_test.go | 20 ++++----- iterator.go | 19 +++++++++ pool/connection_pool.go | 9 ++-- pool/connection_pool_test.go | 22 +++++----- pool/connector.go | 7 ++-- pool/connector_test.go | 23 ++++++----- pool/pooler.go | 9 ++-- prepared.go | 6 +-- protocol.go | 2 +- request.go | 80 +++++++++++++++++++++++++----------- request_test.go | 44 ++++++++++---------- response.go | 20 ++++++--- settings/request.go | 4 +- settings/request_test.go | 44 ++++++++++---------- stream.go | 6 +-- tarantool_test.go | 34 +++++++-------- test_helpers/request_mock.go | 2 +- watch.go | 6 +-- 29 files changed, 271 insertions(+), 234 deletions(-) create mode 100644 iterator.go diff --git a/CHANGELOG.md b/CHANGELOG.md index a96fd42b8..d161655b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,12 +15,15 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release. - Support CRUD API (#108) - An ability to replace a base network connection to a Tarantool instance (#265) +- Enumeration types for response codes and iterators (#158) +- A separate type for a request code (#158) ### Changed - connection_pool renamed to pool (#239) - msgpack/v5 is default at now (#236) - Call/NewCallRequest = Call17/NewCall17Request (#235) +- Constants RequestCode renamed to RequestCode (#158) ### Removed diff --git a/connection.go b/connection.go index ad5aa76bd..768cda4bd 100644 --- a/connection.go +++ b/connection.go @@ -230,6 +230,19 @@ type connShard struct { enc *msgpack.Encoder } +// RLimitActions is an enumeration type for an action to do when a rate limit +// is riched. +type RLimitAction int + +const ( + // RLimitDrop immediately aborts request, + RLimitDrop RLimitAction = iota + // RLimitWait waits during timeout period for some request to be answered. + // If no request answered during timeout period, this request is aborted. + // If no timeout period is set, it will wait forever. + RLimitWait +) + // Opts is a way to configure Connection type Opts struct { // Auth is an authentication method. @@ -268,14 +281,9 @@ type Opts struct { // It is disabled by default. // See RLimitAction for possible actions when RateLimit.reached. RateLimit uint - // RLimitAction tells what to do when RateLimit reached: - // RLimitDrop - immediately abort request, - // RLimitWait - wait during timeout period for some request to be answered. - // If no request answered during timeout period, this request - // is aborted. - // If no timeout period is set, it will wait forever. + // RLimitAction tells what to do when RateLimit is reached. // It is required if RateLimit is specified. - RLimitAction uint + RLimitAction RLimitAction // Concurrency is amount of separate mutexes for request // queues and buffers inside of connection. // It is rounded up to nearest power of 2. @@ -845,14 +853,14 @@ func (conn *Connection) reader(r io.Reader, c Conn) { } var fut *Future = nil - if resp.Code == EventCode { + if resp.Code == keyWatchEvent { if event, err := readWatchEvent(&resp.buf); err == nil { events <- event } else { conn.opts.Logger.Report(LogWatchEventReadFailed, conn, err) } continue - } else if resp.Code == PushCode { + } else if resp.Code == CodePush { if fut = conn.peekFuture(resp.RequestId); fut != nil { fut.AppendPush(resp) } @@ -1072,7 +1080,7 @@ func (conn *Connection) putFuture(fut *Future, req Request, streamId uint64) { if fut = conn.fetchFuture(reqid); fut != nil { resp := &Response{ RequestId: reqid, - Code: OkCode, + Code: CodeOk, } fut.SetResponse(resp) conn.markDone(fut) diff --git a/connector.go b/connector.go index d93c69ec8..3aa2f9542 100644 --- a/connector.go +++ b/connector.go @@ -8,7 +8,7 @@ type Connector interface { Ping() (resp *Response, err error) ConfiguredTimeout() time.Duration - Select(space, index interface{}, offset, limit, iterator uint32, key interface{}) (resp *Response, err error) + Select(space, index interface{}, offset, limit uint32, iterator Iter, key interface{}) (resp *Response, err error) Insert(space interface{}, tuple interface{}) (resp *Response, err error) Replace(space interface{}, tuple interface{}) (resp *Response, err error) Delete(space, index interface{}, key interface{}) (resp *Response, err error) @@ -21,7 +21,7 @@ type Connector interface { Execute(expr string, args interface{}) (resp *Response, err error) GetTyped(space, index interface{}, key interface{}, result interface{}) (err error) - SelectTyped(space, index interface{}, offset, limit, iterator uint32, key interface{}, result interface{}) (err error) + SelectTyped(space, index interface{}, offset, limit uint32, iterator Iter, key interface{}, result interface{}) (err error) InsertTyped(space interface{}, tuple interface{}, result interface{}) (err error) ReplaceTyped(space interface{}, tuple interface{}, result interface{}) (err error) DeleteTyped(space, index interface{}, key interface{}, result interface{}) (err error) @@ -32,7 +32,7 @@ type Connector interface { EvalTyped(expr string, args interface{}, result interface{}) (err error) ExecuteTyped(expr string, args interface{}, result interface{}) (SQLInfo, []ColumnMetaData, error) - SelectAsync(space, index interface{}, offset, limit, iterator uint32, key interface{}) *Future + SelectAsync(space, index interface{}, offset, limit uint32, iterator Iter, key interface{}) *Future InsertAsync(space interface{}, tuple interface{}) *Future ReplaceAsync(space interface{}, tuple interface{}) *Future DeleteAsync(space, index interface{}, key interface{}) *Future diff --git a/const.go b/const.go index 65d0d3b59..f24cc4b8b 100644 --- a/const.go +++ b/const.go @@ -31,6 +31,7 @@ const ( keySQLBind = 0x41 keySQLInfo = 0x42 keyStmtID = 0x43 + keyWatchEvent = 0x4c keyError = 0x52 /* Extended error in >= 2.4 format. */ keyVersion = 0x54 keyFeatures = 0x55 @@ -40,47 +41,3 @@ const ( keyTxnIsolation = 0x59 keyAuthType = 0x5b ) - -const ( - SelectRequestCode = 1 - InsertRequestCode = 2 - ReplaceRequestCode = 3 - UpdateRequestCode = 4 - DeleteRequestCode = 5 - Call16RequestCode = 6 /* call in 1.6 format */ - AuthRequestCode = 7 - EvalRequestCode = 8 - UpsertRequestCode = 9 - Call17RequestCode = 10 /* call in >= 1.7 format */ - ExecuteRequestCode = 11 - PrepareRequestCode = 13 - BeginRequestCode = 14 - CommitRequestCode = 15 - RollbackRequestCode = 16 - PingRequestCode = 64 - SubscribeRequestCode = 66 - IdRequestCode = 73 - WatchRequestCode = 74 - UnwatchRequestCode = 75 - CallRequestCode = Call17RequestCode - - // https://github.com/fl00r/go-tarantool-1.6/issues/2 - - IterEq = uint32(0) // key == x ASC order - IterReq = uint32(1) // key == x DESC order - IterAll = uint32(2) // all tuples - IterLt = uint32(3) // key < x - IterLe = uint32(4) // key <= x - IterGe = uint32(5) // key >= x - IterGt = uint32(6) // key > x - IterBitsAllSet = uint32(7) // all bits from x are set in key - IterBitsAnySet = uint32(8) // at least one x's bit is set - IterBitsAllNotSet = uint32(9) // all bits are not set - - RLimitDrop = 1 - RLimitWait = 2 - - OkCode = uint32(0) - EventCode = uint32(0x4c) - PushCode = uint32(0x80) -) diff --git a/crud/common.go b/crud/common.go index bb882fb8b..42b78f7f6 100644 --- a/crud/common.go +++ b/crud/common.go @@ -68,7 +68,7 @@ func newCall(method string) *tarantool.CallRequest { } // Code returns IPROTO code for CRUD request. -func (req baseRequest) Code() int32 { +func (req baseRequest) Code() tarantool.RequestCode { return req.impl.Code() } diff --git a/crud/request_test.go b/crud/request_test.go index 5a81e0d1e..d5a5c3148 100644 --- a/crud/request_test.go +++ b/crud/request_test.go @@ -22,7 +22,7 @@ const validSpace = "test" // Any valid value != default. const defaultSpace = 0 // And valid too. const defaultIndex = 0 // And valid too. -const CrudRequestCode = tarantool.Call17RequestCode +const RequestCodeCrud = tarantool.RequestCodeCall17 var reqObject = crud.MapObject{ "id": uint(24), @@ -167,31 +167,31 @@ func BenchmarkSelectRequest(b *testing.B) { func TestRequestsCodes(t *testing.T) { tests := []struct { req tarantool.Request - code int32 + code tarantool.RequestCode }{ - {req: crud.MakeInsertRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeInsertObjectRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeInsertManyRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeInsertObjectManyRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeGetRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeUpdateRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeDeleteRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeReplaceRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeReplaceObjectRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeReplaceManyRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeReplaceObjectManyRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeUpsertRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeUpsertObjectRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeUpsertManyRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeUpsertObjectManyRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeMinRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeMaxRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeSelectRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeTruncateRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeLenRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeCountRequest(validSpace), code: CrudRequestCode}, - {req: crud.MakeStorageInfoRequest(), code: CrudRequestCode}, - {req: crud.MakeStatsRequest(), code: CrudRequestCode}, + {req: crud.MakeInsertRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeInsertObjectRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeInsertManyRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeInsertObjectManyRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeGetRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeUpdateRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeDeleteRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeReplaceRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeReplaceObjectRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeReplaceManyRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeReplaceObjectManyRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeUpsertRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeUpsertObjectRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeUpsertManyRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeUpsertObjectManyRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeMinRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeMaxRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeSelectRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeTruncateRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeLenRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeCountRequest(validSpace), code: RequestCodeCrud}, + {req: crud.MakeStorageInfoRequest(), code: RequestCodeCrud}, + {req: crud.MakeStatsRequest(), code: RequestCodeCrud}, } for _, test := range tests { diff --git a/crud/tarantool_test.go b/crud/tarantool_test.go index 65cf72d10..90dd97887 100644 --- a/crud/tarantool_test.go +++ b/crud/tarantool_test.go @@ -374,7 +374,7 @@ func getCrudError(req tarantool.Request, crudError interface{}) (interface{}, er code := req.Code() if crudError != nil { - if code == tarantool.Call17RequestCode { + if code == tarantool.RequestCodeCall17 { return crudError, nil } diff --git a/errors.go b/errors.go index 980e4e5bd..e96ef5c01 100644 --- a/errors.go +++ b/errors.go @@ -4,7 +4,7 @@ import "fmt" // Error is wrapper around error returned by Tarantool. type Error struct { - Code uint32 + Code Code Msg string ExtendedInfo *BoxError } diff --git a/example_test.go b/example_test.go index a7582647a..174455025 100644 --- a/example_test.go +++ b/example_test.go @@ -591,10 +591,10 @@ func ExampleFuture_GetIterator() { var it tarantool.ResponseIterator for it = fut.GetIterator().WithTimeout(timeout); it.Next(); { resp := it.Value() - if resp.Code == tarantool.PushCode { + if resp.Code == tarantool.CodePush { // It is a push message. fmt.Printf("push message: %v\n", resp.Data[0]) - } else if resp.Code == tarantool.OkCode { + } else if resp.Code == tarantool.CodeOk { // It is a regular response. fmt.Printf("response: %v", resp.Data[0]) } else { diff --git a/export_test.go b/export_test.go index 879d345ac..23e17bae6 100644 --- a/export_test.go +++ b/export_test.go @@ -24,8 +24,8 @@ func RefImplPingBody(enc *msgpack.Encoder) error { // RefImplSelectBody is reference implementation for filling of a select // request's body. -func RefImplSelectBody(enc *msgpack.Encoder, space, index, offset, limit, iterator uint32, - key, after interface{}, fetchPos bool) error { +func RefImplSelectBody(enc *msgpack.Encoder, space, index, offset, limit uint32, + iterator Iter, key, after interface{}, fetchPos bool) error { return fillSelect(enc, space, index, offset, limit, iterator, key, after, fetchPos) } diff --git a/future.go b/future.go index a92a4c091..db87a0dbc 100644 --- a/future.go +++ b/future.go @@ -135,7 +135,7 @@ func (fut *Future) AppendPush(resp *Response) { if fut.isDone() { return } - resp.Code = PushCode + resp.Code = CodePush fut.pushes = append(fut.pushes, resp) fut.ready <- struct{}{} diff --git a/future_test.go b/future_test.go index 274bee7d5..cd6050399 100644 --- a/future_test.go +++ b/future_test.go @@ -10,7 +10,7 @@ import ( ) func assertResponseIteratorValue(t testing.TB, it ResponseIterator, - code uint32, resp *Response) { + code Code, resp *Response) { t.Helper() if it.Err() != nil { @@ -56,7 +56,7 @@ func TestFutureGetIteratorNoResponse(t *testing.T) { fut.AppendPush(push) if it := fut.GetIterator(); it.Next() { - assertResponseIteratorValue(t, it, PushCode, push) + assertResponseIteratorValue(t, it, CodePush, push) if it.Next() == true { t.Errorf("An unexpected next value.") } @@ -72,7 +72,7 @@ func TestFutureGetIteratorNoResponseTimeout(t *testing.T) { fut.AppendPush(push) if it := fut.GetIterator().WithTimeout(1 * time.Nanosecond); it.Next() { - assertResponseIteratorValue(t, it, PushCode, push) + assertResponseIteratorValue(t, it, CodePush, push) if it.Next() == true { t.Errorf("An unexpected next value.") } @@ -99,10 +99,10 @@ func TestFutureGetIteratorResponseOnTimeout(t *testing.T) { var it ResponseIterator var cnt = 0 for it = fut.GetIterator().WithTimeout(5 * time.Second); it.Next(); { - code := PushCode + code := CodePush r := push if cnt == 1 { - code = OkCode + code = CodeOk r = resp } assertResponseIteratorValue(t, it, code, r) @@ -131,7 +131,7 @@ func TestFutureGetIteratorFirstResponse(t *testing.T) { fut.SetResponse(resp2) if it := fut.GetIterator(); it.Next() { - assertResponseIteratorValue(t, it, OkCode, resp1) + assertResponseIteratorValue(t, it, CodeOk, resp1) if it.Next() == true { t.Errorf("An unexpected next value.") } @@ -163,7 +163,7 @@ func TestFutureGetIteratorResponse(t *testing.T) { responses := []*Response{ {}, {}, - {Code: OkCode}, + {Code: CodeOk}, } fut := NewFuture() for i, resp := range responses { @@ -181,9 +181,9 @@ func TestFutureGetIteratorResponse(t *testing.T) { for _, it := range its { var cnt = 0 for it.Next() { - code := PushCode + code := CodePush if cnt == len(responses)-1 { - code = OkCode + code = CodeOk } assertResponseIteratorValue(t, it, code, responses[cnt]) cnt += 1 @@ -216,7 +216,7 @@ func TestFutureGetIteratorError(t *testing.T) { for _, it := range its { var cnt = 0 for it.Next() { - code := PushCode + code := CodePush assertResponseIteratorValue(t, it, code, responses[cnt]) cnt += 1 } diff --git a/iterator.go b/iterator.go new file mode 100644 index 000000000..353b74cd2 --- /dev/null +++ b/iterator.go @@ -0,0 +1,19 @@ +package tarantool + +// Iter is an enumeration type of a select iterator. +type Iter uint32 + +// https://github.com/fl00r/go-tarantool-1.6/issues/2 + +const ( + IterEq Iter = 0 // key == x ASC order + IterReq Iter = 1 // key == x DESC order + IterAll Iter = 2 // all tuples + IterLt Iter = 3 // key < x + IterLe Iter = 4 // key <= x + IterGe Iter = 5 // key >= x + IterGt Iter = 6 // key > x + IterBitsAllSet Iter = 7 // all bits from x are set in key + IterBitsAnySet Iter = 8 // at least one x's bit is set + IterBitsAllNotSet Iter = 9 // all bits are not set +) diff --git a/pool/connection_pool.go b/pool/connection_pool.go index 53dc3b6bf..4f4e21998 100644 --- a/pool/connection_pool.go +++ b/pool/connection_pool.go @@ -277,7 +277,8 @@ func (connPool *ConnectionPool) Ping(userMode Mode) (*tarantool.Response, error) } // Select performs select to box space. -func (connPool *ConnectionPool) Select(space, index interface{}, offset, limit, iterator uint32, key interface{}, userMode ...Mode) (resp *tarantool.Response, err error) { +func (connPool *ConnectionPool) Select(space, index interface{}, offset, limit uint32, + iterator tarantool.Iter, key interface{}, userMode ...Mode) (resp *tarantool.Response, err error) { conn, err := connPool.getConnByMode(ANY, userMode) if err != nil { return nil, err @@ -409,7 +410,8 @@ func (connPool *ConnectionPool) GetTyped(space, index interface{}, key interface } // SelectTyped performs select to box space and fills typed result. -func (connPool *ConnectionPool) SelectTyped(space, index interface{}, offset, limit, iterator uint32, key interface{}, result interface{}, userMode ...Mode) (err error) { +func (connPool *ConnectionPool) SelectTyped(space, index interface{}, offset, limit uint32, + iterator tarantool.Iter, key interface{}, result interface{}, userMode ...Mode) (err error) { conn, err := connPool.getConnByMode(ANY, userMode) if err != nil { return err @@ -517,7 +519,8 @@ func (connPool *ConnectionPool) ExecuteTyped(expr string, args interface{}, resu } // SelectAsync sends select request to Tarantool and returns Future. -func (connPool *ConnectionPool) SelectAsync(space, index interface{}, offset, limit, iterator uint32, key interface{}, userMode ...Mode) *tarantool.Future { +func (connPool *ConnectionPool) SelectAsync(space, index interface{}, offset, limit uint32, + iterator tarantool.Iter, key interface{}, userMode ...Mode) *tarantool.Future { conn, err := connPool.getConnByMode(ANY, userMode) if err != nil { return newErrorFuture(err) diff --git a/pool/connection_pool_test.go b/pool/connection_pool_test.go index 1d68e3f48..40dfbd2b2 100644 --- a/pool/connection_pool_test.go +++ b/pool/connection_pool_test.go @@ -1809,7 +1809,7 @@ func TestNewPrepared(t *testing.T) { if resp == nil { t.Fatalf("nil response") } - if resp.Code != tarantool.OkCode { + if resp.Code != tarantool.CodeOk { t.Fatalf("failed to execute prepared: code %d", resp.Code) } if reflect.DeepEqual(resp.Data[0], []interface{}{1, "test"}) { @@ -1827,7 +1827,7 @@ func TestNewPrepared(t *testing.T) { if err != nil { t.Errorf("failed to unprepare prepared statement: %v", err) } - if resp.Code != tarantool.OkCode { + if resp.Code != tarantool.CodeOk { t.Errorf("failed to unprepare prepared statement: code %d", resp.Code) } @@ -1895,7 +1895,7 @@ func TestStream_Commit(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Begin") require.NotNilf(t, resp, "response is nil after Begin") - require.Equalf(t, tarantool.OkCode, resp.Code, "failed to Begin: wrong code returned") + require.Equalf(t, tarantool.CodeOk, resp.Code, "failed to Begin: wrong code returned") // Insert in stream req = tarantool.NewInsertRequest(spaceName). @@ -1903,7 +1903,7 @@ func TestStream_Commit(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Insert") require.NotNilf(t, resp, "response is nil after Insert") - require.Equalf(t, tarantool.OkCode, resp.Code, "failed to Insert: wrong code returned") + require.Equalf(t, tarantool.CodeOk, resp.Code, "failed to Insert: wrong code returned") // Connect to servers[2] to check if tuple // was inserted outside of stream on RW instance @@ -1947,7 +1947,7 @@ func TestStream_Commit(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Commit") require.NotNilf(t, resp, "response is nil after Commit") - require.Equalf(t, tarantool.OkCode, resp.Code, "failed to Commit: wrong code returned") + require.Equalf(t, tarantool.CodeOk, resp.Code, "failed to Commit: wrong code returned") // Select outside of transaction resp, err = conn.Do(selectReq).Get() @@ -1994,7 +1994,7 @@ func TestStream_Rollback(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Begin") require.NotNilf(t, resp, "response is nil after Begin") - require.Equalf(t, tarantool.OkCode, resp.Code, "failed to Begin: wrong code returned") + require.Equalf(t, tarantool.CodeOk, resp.Code, "failed to Begin: wrong code returned") // Insert in stream req = tarantool.NewInsertRequest(spaceName). @@ -2002,7 +2002,7 @@ func TestStream_Rollback(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Insert") require.NotNilf(t, resp, "response is nil after Insert") - require.Equalf(t, tarantool.OkCode, resp.Code, "failed to Insert: wrong code returned") + require.Equalf(t, tarantool.CodeOk, resp.Code, "failed to Insert: wrong code returned") // Connect to servers[2] to check if tuple // was not inserted outside of stream on RW instance @@ -2045,7 +2045,7 @@ func TestStream_Rollback(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Rollback") require.NotNilf(t, resp, "response is nil after Rollback") - require.Equalf(t, tarantool.OkCode, resp.Code, "failed to Rollback: wrong code returned") + require.Equalf(t, tarantool.CodeOk, resp.Code, "failed to Rollback: wrong code returned") // Select outside of transaction resp, err = conn.Do(selectReq).Get() @@ -2099,7 +2099,7 @@ func TestStream_TxnIsolationLevel(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Begin") require.NotNilf(t, resp, "response is nil after Begin") - require.Equalf(t, tarantool.OkCode, resp.Code, "wrong code returned") + require.Equalf(t, tarantool.CodeOk, resp.Code, "wrong code returned") // Insert in stream req = tarantool.NewInsertRequest(spaceName). @@ -2107,7 +2107,7 @@ func TestStream_TxnIsolationLevel(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Insert") require.NotNilf(t, resp, "response is nil after Insert") - require.Equalf(t, tarantool.OkCode, resp.Code, "wrong code returned") + require.Equalf(t, tarantool.CodeOk, resp.Code, "wrong code returned") // Select not related to the transaction // while transaction is not committed @@ -2145,7 +2145,7 @@ func TestStream_TxnIsolationLevel(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Rollback") require.NotNilf(t, resp, "response is nil after Rollback") - require.Equalf(t, tarantool.OkCode, resp.Code, "wrong code returned") + require.Equalf(t, tarantool.CodeOk, resp.Code, "wrong code returned") // Select outside of transaction resp, err = conn.Do(selectReq).Get() diff --git a/pool/connector.go b/pool/connector.go index 2cf094858..ffa3edd26 100644 --- a/pool/connector.go +++ b/pool/connector.go @@ -63,7 +63,7 @@ func (c *ConnectorAdapter) ConfiguredTimeout() time.Duration { // Select performs select to box space. func (c *ConnectorAdapter) Select(space, index interface{}, - offset, limit, iterator uint32, + offset, limit uint32, iterator tarantool.Iter, key interface{}) (*tarantool.Response, error) { return c.pool.Select(space, index, offset, limit, iterator, key, c.mode) } @@ -143,7 +143,7 @@ func (c *ConnectorAdapter) GetTyped(space, index interface{}, // SelectTyped performs select to box space and fills typed result. func (c *ConnectorAdapter) SelectTyped(space, index interface{}, - offset, limit, iterator uint32, + offset, limit uint32, iterator tarantool.Iter, key interface{}, result interface{}) error { return c.pool.SelectTyped(space, index, offset, limit, iterator, key, result, c.mode) } @@ -210,7 +210,8 @@ func (c *ConnectorAdapter) ExecuteTyped(expr string, args interface{}, // SelectAsync sends select request to Tarantool and returns Future. func (c *ConnectorAdapter) SelectAsync(space, index interface{}, - offset, limit, iterator uint32, key interface{}) *tarantool.Future { + offset, limit uint32, iterator tarantool.Iter, + key interface{}) *tarantool.Future { return c.pool.SelectAsync(space, index, offset, limit, iterator, key, c.mode) } diff --git a/pool/connector_test.go b/pool/connector_test.go index d4d197607..34b18f584 100644 --- a/pool/connector_test.go +++ b/pool/connector_test.go @@ -125,13 +125,14 @@ func TestConnectorConfiguredTimeoutWithError(t *testing.T) { type baseRequestMock struct { Pooler - called int - functionName string - offset, limit, iterator uint32 - space, index interface{} - args, tuple, key, ops interface{} - result interface{} - mode Mode + called int + functionName string + offset, limit uint32 + iterator tarantool.Iter + space, index interface{} + args, tuple, key, ops interface{} + result interface{} + mode Mode } var reqResp *tarantool.Response = &tarantool.Response{} @@ -141,7 +142,7 @@ var reqFuture *tarantool.Future = &tarantool.Future{} var reqFunctionName string = "any_name" var reqOffset uint32 = 1 var reqLimit uint32 = 2 -var reqIterator uint32 = 3 +var reqIterator tarantool.Iter = tarantool.IterEq var reqSpace interface{} = []interface{}{1} var reqIndex interface{} = []interface{}{2} var reqArgs interface{} = []interface{}{3} @@ -188,7 +189,7 @@ type selectMock struct { } func (m *selectMock) Select(space, index interface{}, - offset, limit, iterator uint32, key interface{}, + offset, limit uint32, iterator tarantool.Iter, key interface{}, mode ...Mode) (*tarantool.Response, error) { m.called++ m.space = space @@ -224,7 +225,7 @@ type selectTypedMock struct { } func (m *selectTypedMock) SelectTyped(space, index interface{}, - offset, limit, iterator uint32, key interface{}, + offset, limit uint32, iterator tarantool.Iter, key interface{}, result interface{}, mode ...Mode) error { m.called++ m.space = space @@ -262,7 +263,7 @@ type selectAsyncMock struct { } func (m *selectAsyncMock) SelectAsync(space, index interface{}, - offset, limit, iterator uint32, key interface{}, + offset, limit uint32, iterator tarantool.Iter, key interface{}, mode ...Mode) *tarantool.Future { m.called++ m.space = space diff --git a/pool/pooler.go b/pool/pooler.go index 626c5af93..4ac9088b6 100644 --- a/pool/pooler.go +++ b/pool/pooler.go @@ -13,7 +13,8 @@ type Pooler interface { Ping(mode Mode) (*tarantool.Response, error) ConfiguredTimeout(mode Mode) (time.Duration, error) - Select(space, index interface{}, offset, limit, iterator uint32, + Select(space, index interface{}, offset, limit uint32, + iterator tarantool.Iter, key interface{}, mode ...Mode) (*tarantool.Response, error) Insert(space interface{}, tuple interface{}, mode ...Mode) (*tarantool.Response, error) @@ -38,7 +39,8 @@ type Pooler interface { GetTyped(space, index interface{}, key interface{}, result interface{}, mode ...Mode) error - SelectTyped(space, index interface{}, offset, limit, iterator uint32, + SelectTyped(space, index interface{}, offset, limit uint32, + iterator tarantool.Iter, key interface{}, result interface{}, mode ...Mode) error InsertTyped(space interface{}, tuple interface{}, result interface{}, mode ...Mode) error @@ -59,7 +61,8 @@ type Pooler interface { ExecuteTyped(expr string, args interface{}, result interface{}, mode Mode) (tarantool.SQLInfo, []tarantool.ColumnMetaData, error) - SelectAsync(space, index interface{}, offset, limit, iterator uint32, + SelectAsync(space, index interface{}, offset, limit uint32, + iterator tarantool.Iter, key interface{}, mode ...Mode) *tarantool.Future InsertAsync(space interface{}, tuple interface{}, mode ...Mode) *tarantool.Future diff --git a/prepared.go b/prepared.go index ce90fbeb6..320d35638 100644 --- a/prepared.go +++ b/prepared.go @@ -69,7 +69,7 @@ type PrepareRequest struct { // NewPrepareRequest returns a new empty PrepareRequest. func NewPrepareRequest(expr string) *PrepareRequest { req := new(PrepareRequest) - req.requestCode = PrepareRequestCode + req.requestCode = RequestCodePrepare req.expr = expr return req } @@ -100,7 +100,7 @@ type UnprepareRequest struct { // NewUnprepareRequest returns a new empty UnprepareRequest. func NewUnprepareRequest(stmt *Prepared) *UnprepareRequest { req := new(UnprepareRequest) - req.requestCode = PrepareRequestCode + req.requestCode = RequestCodePrepare req.stmt = stmt return req } @@ -137,7 +137,7 @@ type ExecutePreparedRequest struct { // NewExecutePreparedRequest returns a new empty preparedExecuteRequest. func NewExecutePreparedRequest(stmt *Prepared) *ExecutePreparedRequest { req := new(ExecutePreparedRequest) - req.requestCode = ExecuteRequestCode + req.requestCode = RequestCodeExecute req.stmt = stmt req.args = []interface{}{} return req diff --git a/protocol.go b/protocol.go index ead0edec4..5e0b001d2 100644 --- a/protocol.go +++ b/protocol.go @@ -130,7 +130,7 @@ func fillId(enc *msgpack.Encoder, protocolInfo ProtocolInfo) error { // NewIdRequest returns a new IdRequest. func NewIdRequest(protocolInfo ProtocolInfo) *IdRequest { req := new(IdRequest) - req.requestCode = IdRequestCode + req.requestCode = RequestCodeId req.protocolInfo = protocolInfo.Clone() return req } diff --git a/request.go b/request.go index 4df5e059e..4f6efb0b7 100644 --- a/request.go +++ b/request.go @@ -30,7 +30,7 @@ func fillSearch(enc *msgpack.Encoder, spaceNo, indexNo uint32, key interface{}) return enc.Encode(key) } -func fillIterator(enc *msgpack.Encoder, offset, limit, iterator uint32) error { +func fillIterator(enc *msgpack.Encoder, offset, limit uint32, iterator Iter) error { if err := enc.EncodeUint(keyIterator); err != nil { return err } @@ -65,7 +65,8 @@ func fillInsert(enc *msgpack.Encoder, spaceNo uint32, tuple interface{}) error { return enc.Encode(tuple) } -func fillSelect(enc *msgpack.Encoder, spaceNo, indexNo, offset, limit, iterator uint32, +func fillSelect(enc *msgpack.Encoder, spaceNo, indexNo, offset, limit uint32, + iterator Iter, key, after interface{}, fetchPos bool) error { mapLen := 6 if fetchPos { @@ -173,7 +174,8 @@ func (conn *Connection) Ping() (resp *Response, err error) { // Select performs select to box space. // // It is equal to conn.SelectAsync(...).Get(). -func (conn *Connection) Select(space, index interface{}, offset, limit, iterator uint32, key interface{}) (resp *Response, err error) { +func (conn *Connection) Select(space, index interface{}, offset, limit uint32, + iterator Iter, key interface{}) (resp *Response, err error) { return conn.SelectAsync(space, index, offset, limit, iterator, key).Get() } @@ -293,7 +295,8 @@ func (conn *Connection) GetTyped(space, index interface{}, key interface{}, resu // SelectTyped performs select to box space and fills typed result. // // It is equal to conn.SelectAsync(space, index, offset, limit, iterator, key).GetTyped(&result) -func (conn *Connection) SelectTyped(space, index interface{}, offset, limit, iterator uint32, key interface{}, result interface{}) (err error) { +func (conn *Connection) SelectTyped(space, index interface{}, offset, limit uint32, + iterator Iter, key interface{}, result interface{}) (err error) { return conn.SelectAsync(space, index, offset, limit, iterator, key).GetTyped(result) } @@ -372,7 +375,8 @@ func (conn *Connection) ExecuteTyped(expr string, args interface{}, result inter } // SelectAsync sends select request to Tarantool and returns Future. -func (conn *Connection) SelectAsync(space, index interface{}, offset, limit, iterator uint32, key interface{}) *Future { +func (conn *Connection) SelectAsync(space, index interface{}, offset, limit uint32, + iterator Iter, key interface{}) *Future { req := NewSelectRequest(space). Index(index). Offset(offset). @@ -597,11 +601,38 @@ func encodeSQLBind(enc *msgpack.Encoder, from interface{}) error { return nil } +// RequestCode is a code of a request. +type RequestCode int32 + +const ( + RequestCodeSelect RequestCode = 1 + RequestCodeInsert RequestCode = 2 + RequestCodeReplace RequestCode = 3 + RequestCodeUpdate RequestCode = 4 + RequestCodeDelete RequestCode = 5 + RequestCodeCall16 RequestCode = 6 /* call in 1.6 format */ + RequestCodeAuth RequestCode = 7 + RequestCodeEval RequestCode = 8 + RequestCodeUpsert RequestCode = 9 + RequestCodeCall17 RequestCode = 10 /* call in >= 1.7 format */ + RequestCodeExecute RequestCode = 11 + RequestCodePrepare RequestCode = 13 + RequestCodeBegin RequestCode = 14 + RequestCodeCommit RequestCode = 15 + RequestCodeRollback RequestCode = 16 + RequestCodePing RequestCode = 64 + RequestCodeSubscribe RequestCode = 66 + RequestCodeId RequestCode = 73 + RequestCodeWatch RequestCode = 74 + RequestCodeUnwatch RequestCode = 75 + RequestCodeCall RequestCode = RequestCodeCall17 +) + // Request is an interface that provides the necessary data to create a request // that will be sent to a tarantool instance. type Request interface { // Code returns a IPROTO code for the request. - Code() int32 + Code() RequestCode // Body fills an msgpack.Encoder with a request body. Body(resolver SchemaResolver, enc *msgpack.Encoder) error // Ctx returns a context of the request. @@ -619,13 +650,13 @@ type ConnectedRequest interface { } type baseRequest struct { - requestCode int32 + requestCode RequestCode async bool ctx context.Context } // Code returns a IPROTO code for the request. -func (req *baseRequest) Code() int32 { +func (req *baseRequest) Code() RequestCode { return req.requestCode } @@ -689,8 +720,8 @@ func newPapSha256AuthRequest(user, password string) authRequest { } // Code returns a IPROTO code for the request. -func (req authRequest) Code() int32 { - return AuthRequestCode +func (req authRequest) Code() RequestCode { + return RequestCodeAuth } // Async returns true if the request does not require a response. @@ -720,7 +751,7 @@ type PingRequest struct { // NewPingRequest returns a new PingRequest. func NewPingRequest() *PingRequest { req := new(PingRequest) - req.requestCode = PingRequestCode + req.requestCode = RequestCodePing return req } @@ -745,14 +776,15 @@ func (req *PingRequest) Context(ctx context.Context) *PingRequest { type SelectRequest struct { spaceIndexRequest isIteratorSet, fetchPos bool - offset, limit, iterator uint32 + offset, limit uint32 + iterator Iter key, after interface{} } // NewSelectRequest returns a new empty SelectRequest. func NewSelectRequest(space interface{}) *SelectRequest { req := new(SelectRequest) - req.requestCode = SelectRequestCode + req.requestCode = RequestCodeSelect req.setSpace(space) req.isIteratorSet = false req.fetchPos = false @@ -786,7 +818,7 @@ func (req *SelectRequest) Limit(limit uint32) *SelectRequest { // Iterator set the iterator for the select request. // Note: default value is IterAll if key is not set or IterEq otherwise. -func (req *SelectRequest) Iterator(iterator uint32) *SelectRequest { +func (req *SelectRequest) Iterator(iterator Iter) *SelectRequest { req.iterator = iterator req.isIteratorSet = true return req @@ -858,7 +890,7 @@ type InsertRequest struct { // NewInsertRequest returns a new empty InsertRequest. func NewInsertRequest(space interface{}) *InsertRequest { req := new(InsertRequest) - req.requestCode = InsertRequestCode + req.requestCode = RequestCodeInsert req.setSpace(space) req.tuple = []interface{}{} return req @@ -902,7 +934,7 @@ type ReplaceRequest struct { // NewReplaceRequest returns a new empty ReplaceRequest. func NewReplaceRequest(space interface{}) *ReplaceRequest { req := new(ReplaceRequest) - req.requestCode = ReplaceRequestCode + req.requestCode = RequestCodeReplace req.setSpace(space) req.tuple = []interface{}{} return req @@ -946,7 +978,7 @@ type DeleteRequest struct { // NewDeleteRequest returns a new empty DeleteRequest. func NewDeleteRequest(space interface{}) *DeleteRequest { req := new(DeleteRequest) - req.requestCode = DeleteRequestCode + req.requestCode = RequestCodeDelete req.setSpace(space) req.key = []interface{}{} return req @@ -998,7 +1030,7 @@ type UpdateRequest struct { // NewUpdateRequest returns a new empty UpdateRequest. func NewUpdateRequest(space interface{}) *UpdateRequest { req := new(UpdateRequest) - req.requestCode = UpdateRequestCode + req.requestCode = RequestCodeUpdate req.setSpace(space) req.key = []interface{}{} req.ops = []interface{}{} @@ -1060,7 +1092,7 @@ type UpsertRequest struct { // NewUpsertRequest returns a new empty UpsertRequest. func NewUpsertRequest(space interface{}) *UpsertRequest { req := new(UpsertRequest) - req.requestCode = UpsertRequestCode + req.requestCode = RequestCodeUpsert req.setSpace(space) req.tuple = []interface{}{} req.ops = []interface{}{} @@ -1116,7 +1148,7 @@ type CallRequest struct { // Tarantool >= 1.7. func NewCallRequest(function string) *CallRequest { req := new(CallRequest) - req.requestCode = CallRequestCode + req.requestCode = RequestCodeCall req.function = function return req } @@ -1153,7 +1185,7 @@ func (req *CallRequest) Context(ctx context.Context) *CallRequest { // Deprecated since Tarantool 1.7.2. func NewCall16Request(function string) *CallRequest { req := NewCallRequest(function) - req.requestCode = Call16RequestCode + req.requestCode = RequestCodeCall16 return req } @@ -1161,7 +1193,7 @@ func NewCall16Request(function string) *CallRequest { // Tarantool >= 1.7. func NewCall17Request(function string) *CallRequest { req := NewCallRequest(function) - req.requestCode = Call17RequestCode + req.requestCode = RequestCodeCall17 return req } @@ -1176,7 +1208,7 @@ type EvalRequest struct { // NewEvalRequest returns a new empty EvalRequest. func NewEvalRequest(expr string) *EvalRequest { req := new(EvalRequest) - req.requestCode = EvalRequestCode + req.requestCode = RequestCodeEval req.expr = expr req.args = []interface{}{} return req @@ -1216,7 +1248,7 @@ type ExecuteRequest struct { // NewExecuteRequest returns a new empty ExecuteRequest. func NewExecuteRequest(expr string) *ExecuteRequest { req := new(ExecuteRequest) - req.requestCode = ExecuteRequestCode + req.requestCode = RequestCodeExecute req.expr = expr req.args = []interface{}{} return req diff --git a/request_test.go b/request_test.go index b9a3a656d..197b87bc4 100644 --- a/request_test.go +++ b/request_test.go @@ -172,29 +172,29 @@ func TestRequestsInvalidIndex(t *testing.T) { func TestRequestsCodes(t *testing.T) { tests := []struct { req Request - code int32 + code RequestCode }{ - {req: NewSelectRequest(validSpace), code: SelectRequestCode}, - {req: NewUpdateRequest(validSpace), code: UpdateRequestCode}, - {req: NewUpsertRequest(validSpace), code: UpsertRequestCode}, - {req: NewInsertRequest(validSpace), code: InsertRequestCode}, - {req: NewReplaceRequest(validSpace), code: ReplaceRequestCode}, - {req: NewDeleteRequest(validSpace), code: DeleteRequestCode}, - {req: NewCallRequest(validExpr), code: CallRequestCode}, - {req: NewCallRequest(validExpr), code: Call17RequestCode}, - {req: NewCall16Request(validExpr), code: Call16RequestCode}, - {req: NewCall17Request(validExpr), code: Call17RequestCode}, - {req: NewEvalRequest(validExpr), code: EvalRequestCode}, - {req: NewExecuteRequest(validExpr), code: ExecuteRequestCode}, - {req: NewPingRequest(), code: PingRequestCode}, - {req: NewPrepareRequest(validExpr), code: PrepareRequestCode}, - {req: NewUnprepareRequest(validStmt), code: PrepareRequestCode}, - {req: NewExecutePreparedRequest(validStmt), code: ExecuteRequestCode}, - {req: NewBeginRequest(), code: BeginRequestCode}, - {req: NewCommitRequest(), code: CommitRequestCode}, - {req: NewRollbackRequest(), code: RollbackRequestCode}, - {req: NewIdRequest(validProtocolInfo), code: IdRequestCode}, - {req: NewBroadcastRequest(validKey), code: CallRequestCode}, + {req: NewSelectRequest(validSpace), code: RequestCodeSelect}, + {req: NewUpdateRequest(validSpace), code: RequestCodeUpdate}, + {req: NewUpsertRequest(validSpace), code: RequestCodeUpsert}, + {req: NewInsertRequest(validSpace), code: RequestCodeInsert}, + {req: NewReplaceRequest(validSpace), code: RequestCodeReplace}, + {req: NewDeleteRequest(validSpace), code: RequestCodeDelete}, + {req: NewCallRequest(validExpr), code: RequestCodeCall}, + {req: NewCallRequest(validExpr), code: RequestCodeCall17}, + {req: NewCall16Request(validExpr), code: RequestCodeCall16}, + {req: NewCall17Request(validExpr), code: RequestCodeCall17}, + {req: NewEvalRequest(validExpr), code: RequestCodeEval}, + {req: NewExecuteRequest(validExpr), code: RequestCodeExecute}, + {req: NewPingRequest(), code: RequestCodePing}, + {req: NewPrepareRequest(validExpr), code: RequestCodePrepare}, + {req: NewUnprepareRequest(validStmt), code: RequestCodePrepare}, + {req: NewExecutePreparedRequest(validStmt), code: RequestCodeExecute}, + {req: NewBeginRequest(), code: RequestCodeBegin}, + {req: NewCommitRequest(), code: RequestCodeCommit}, + {req: NewRollbackRequest(), code: RequestCodeRollback}, + {req: NewIdRequest(validProtocolInfo), code: RequestCodeId}, + {req: NewBroadcastRequest(validKey), code: RequestCodeCall}, } for _, test := range tests { diff --git a/response.go b/response.go index 962912675..1ec88d562 100644 --- a/response.go +++ b/response.go @@ -6,9 +6,19 @@ import ( "github.com/vmihailenco/msgpack/v5" ) +// Code is an enumeration type for a success response code. +type Code uint32 + +const ( + // CodeOk is a regular response. + CodeOk Code = 0 + // CodePush is a push message. + CodePush Code = 0x80 +) + type Response struct { RequestId uint32 - Code uint32 + Code Code // Error contains an error message. Error string // Data contains deserialized data for untyped requests. @@ -151,7 +161,7 @@ func (resp *Response) decodeHeader(d *msgpack.Decoder) (err error) { if rcode, err = d.DecodeUint64(); err != nil { return } - resp.Code = uint32(rcode) + resp.Code = Code(rcode) default: if err = d.Skip(); err != nil { return @@ -280,7 +290,7 @@ func (resp *Response) decodeBody() (err error) { resp.Data = []interface{}{serverProtocolInfo} } - if resp.Code != OkCode && resp.Code != PushCode { + if resp.Code != CodeOk && resp.Code != CodePush { resp.Code &^= errCodeBit err = Error{resp.Code, resp.Error, errorExtendedInfo} } @@ -341,7 +351,7 @@ func (resp *Response) decodeBodyTyped(res interface{}) (err error) { } } } - if resp.Code != OkCode && resp.Code != PushCode { + if resp.Code != CodeOk && resp.Code != CodePush { resp.Code &^= errCodeBit err = Error{resp.Code, resp.Error, errorExtendedInfo} } @@ -351,7 +361,7 @@ func (resp *Response) decodeBodyTyped(res interface{}) (err error) { // String implements Stringer interface. func (resp *Response) String() (str string) { - if resp.Code == OkCode { + if resp.Code == CodeOk { return fmt.Sprintf("<%d OK %v>", resp.RequestId, resp.Data) } return fmt.Sprintf("<%d ERR 0x%x %s>", resp.RequestId, resp.Code, resp.Error) diff --git a/settings/request.go b/settings/request.go index df6dcd0ac..811b797ec 100644 --- a/settings/request.go +++ b/settings/request.go @@ -86,7 +86,7 @@ func (req *SetRequest) Context(ctx context.Context) *SetRequest { } // Code returns IPROTO code for set session settings request. -func (req *SetRequest) Code() int32 { +func (req *SetRequest) Code() tarantool.RequestCode { return req.impl.Code() } @@ -126,7 +126,7 @@ func (req *GetRequest) Context(ctx context.Context) *GetRequest { } // Code returns IPROTO code for get session settings request. -func (req *GetRequest) Code() int32 { +func (req *GetRequest) Code() tarantool.RequestCode { return req.impl.Code() } diff --git a/settings/request_test.go b/settings/request_test.go index 7623a6cac..34d171d30 100644 --- a/settings/request_test.go +++ b/settings/request_test.go @@ -40,29 +40,29 @@ func TestRequestsAPI(t *testing.T) { tests := []struct { req tarantool.Request async bool - code int32 + code tarantool.RequestCode }{ - {req: NewErrorMarshalingEnabledSetRequest(false), async: false, code: tarantool.UpdateRequestCode}, - {req: NewErrorMarshalingEnabledGetRequest(), async: false, code: tarantool.SelectRequestCode}, - {req: NewSQLDefaultEngineSetRequest("memtx"), async: false, code: tarantool.UpdateRequestCode}, - {req: NewSQLDefaultEngineGetRequest(), async: false, code: tarantool.SelectRequestCode}, - {req: NewSQLDeferForeignKeysSetRequest(false), async: false, code: tarantool.UpdateRequestCode}, - {req: NewSQLDeferForeignKeysGetRequest(), async: false, code: tarantool.SelectRequestCode}, - {req: NewSQLFullColumnNamesSetRequest(false), async: false, code: tarantool.UpdateRequestCode}, - {req: NewSQLFullColumnNamesGetRequest(), async: false, code: tarantool.SelectRequestCode}, - {req: NewSQLFullMetadataSetRequest(false), async: false, code: tarantool.UpdateRequestCode}, - {req: NewSQLFullMetadataGetRequest(), async: false, code: tarantool.SelectRequestCode}, - {req: NewSQLParserDebugSetRequest(false), async: false, code: tarantool.UpdateRequestCode}, - {req: NewSQLParserDebugGetRequest(), async: false, code: tarantool.SelectRequestCode}, - {req: NewSQLRecursiveTriggersSetRequest(false), async: false, code: tarantool.UpdateRequestCode}, - {req: NewSQLRecursiveTriggersGetRequest(), async: false, code: tarantool.SelectRequestCode}, - {req: NewSQLReverseUnorderedSelectsSetRequest(false), async: false, code: tarantool.UpdateRequestCode}, - {req: NewSQLReverseUnorderedSelectsGetRequest(), async: false, code: tarantool.SelectRequestCode}, - {req: NewSQLSelectDebugSetRequest(false), async: false, code: tarantool.UpdateRequestCode}, - {req: NewSQLSelectDebugGetRequest(), async: false, code: tarantool.SelectRequestCode}, - {req: NewSQLVDBEDebugSetRequest(false), async: false, code: tarantool.UpdateRequestCode}, - {req: NewSQLVDBEDebugGetRequest(), async: false, code: tarantool.SelectRequestCode}, - {req: NewSessionSettingsGetRequest(), async: false, code: tarantool.SelectRequestCode}, + {req: NewErrorMarshalingEnabledSetRequest(false), async: false, code: tarantool.RequestCodeUpdate}, + {req: NewErrorMarshalingEnabledGetRequest(), async: false, code: tarantool.RequestCodeSelect}, + {req: NewSQLDefaultEngineSetRequest("memtx"), async: false, code: tarantool.RequestCodeUpdate}, + {req: NewSQLDefaultEngineGetRequest(), async: false, code: tarantool.RequestCodeSelect}, + {req: NewSQLDeferForeignKeysSetRequest(false), async: false, code: tarantool.RequestCodeUpdate}, + {req: NewSQLDeferForeignKeysGetRequest(), async: false, code: tarantool.RequestCodeSelect}, + {req: NewSQLFullColumnNamesSetRequest(false), async: false, code: tarantool.RequestCodeUpdate}, + {req: NewSQLFullColumnNamesGetRequest(), async: false, code: tarantool.RequestCodeSelect}, + {req: NewSQLFullMetadataSetRequest(false), async: false, code: tarantool.RequestCodeUpdate}, + {req: NewSQLFullMetadataGetRequest(), async: false, code: tarantool.RequestCodeSelect}, + {req: NewSQLParserDebugSetRequest(false), async: false, code: tarantool.RequestCodeUpdate}, + {req: NewSQLParserDebugGetRequest(), async: false, code: tarantool.RequestCodeSelect}, + {req: NewSQLRecursiveTriggersSetRequest(false), async: false, code: tarantool.RequestCodeUpdate}, + {req: NewSQLRecursiveTriggersGetRequest(), async: false, code: tarantool.RequestCodeSelect}, + {req: NewSQLReverseUnorderedSelectsSetRequest(false), async: false, code: tarantool.RequestCodeUpdate}, + {req: NewSQLReverseUnorderedSelectsGetRequest(), async: false, code: tarantool.RequestCodeSelect}, + {req: NewSQLSelectDebugSetRequest(false), async: false, code: tarantool.RequestCodeUpdate}, + {req: NewSQLSelectDebugGetRequest(), async: false, code: tarantool.RequestCodeSelect}, + {req: NewSQLVDBEDebugSetRequest(false), async: false, code: tarantool.RequestCodeUpdate}, + {req: NewSQLVDBEDebugGetRequest(), async: false, code: tarantool.RequestCodeSelect}, + {req: NewSessionSettingsGetRequest(), async: false, code: tarantool.RequestCodeSelect}, } for _, test := range tests { diff --git a/stream.go b/stream.go index acb5c0104..3d9d0c40c 100644 --- a/stream.go +++ b/stream.go @@ -92,7 +92,7 @@ type BeginRequest struct { // NewBeginRequest returns a new BeginRequest. func NewBeginRequest() *BeginRequest { req := new(BeginRequest) - req.requestCode = BeginRequestCode + req.requestCode = RequestCodeBegin req.txnIsolation = DefaultIsolationLevel return req } @@ -136,7 +136,7 @@ type CommitRequest struct { // NewCommitRequest returns a new CommitRequest. func NewCommitRequest() *CommitRequest { req := new(CommitRequest) - req.requestCode = CommitRequestCode + req.requestCode = RequestCodeCommit return req } @@ -166,7 +166,7 @@ type RollbackRequest struct { // NewRollbackRequest returns a new RollbackRequest. func NewRollbackRequest() *RollbackRequest { req := new(RollbackRequest) - req.requestCode = RollbackRequestCode + req.requestCode = RequestCodeRollback return req } diff --git a/tarantool_test.go b/tarantool_test.go index 09d6c3cc3..cdf5979e6 100644 --- a/tarantool_test.go +++ b/tarantool_test.go @@ -1277,7 +1277,7 @@ func TestClientSessionPush(t *testing.T) { t.Errorf("Response.Data is empty after CallAsync") break } - if resp.Code == PushCode { + if resp.Code == CodePush { pushCnt += 1 if val, err := test_helpers.ConvertUint64(resp.Data[0]); err != nil || val != pushCnt { t.Errorf("Unexpected push data = %v", resp.Data) @@ -1765,7 +1765,7 @@ func TestNewPrepared(t *testing.T) { if err != nil { t.Errorf("failed to execute prepared: %v", err) } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Errorf("failed to execute prepared: code %d", resp.Code) } if reflect.DeepEqual(resp.Data[0], []interface{}{1, "test"}) { @@ -1782,7 +1782,7 @@ func TestNewPrepared(t *testing.T) { if err != nil { t.Errorf("failed to unprepare prepared statement: %v", err) } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Errorf("failed to unprepare prepared statement: code %d", resp.Code) } @@ -1806,7 +1806,7 @@ func TestNewPrepared(t *testing.T) { if resp.Data == nil { t.Errorf("failed to prepare: Data is nil") } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Errorf("failed to unprepare prepared statement: code %d", resp.Code) } @@ -2443,7 +2443,7 @@ func TestClientRequestObjects(t *testing.T) { if len(resp.Data) != 0 { t.Fatalf("Response Body len != 0") } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Fatalf("Failed to Execute: %d", resp.Code) } if resp.SQLInfo.AffectedCount != 1 { @@ -2464,7 +2464,7 @@ func TestClientRequestObjects(t *testing.T) { if len(resp.Data) != 0 { t.Fatalf("Response Body len != 0") } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Fatalf("Failed to Execute: %d", resp.Code) } if resp.SQLInfo.AffectedCount != 1 { @@ -2676,7 +2676,7 @@ type waitCtxRequest struct { wg sync.WaitGroup } -func (req *waitCtxRequest) Code() int32 { +func (req *waitCtxRequest) Code() RequestCode { return NewPingRequest().Code() } @@ -2811,7 +2811,7 @@ func TestStream_Commit(t *testing.T) { if err != nil { t.Fatalf("Failed to Begin: %s", err.Error()) } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Fatalf("Failed to Begin: wrong code returned %d", resp.Code) } @@ -2822,7 +2822,7 @@ func TestStream_Commit(t *testing.T) { if err != nil { t.Fatalf("Failed to Insert: %s", err.Error()) } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Errorf("Failed to Insert: wrong code returned %d", resp.Code) } defer test_helpers.DeleteRecordByKey(t, conn, spaceNo, indexNo, []interface{}{uint(1001)}) @@ -2877,7 +2877,7 @@ func TestStream_Commit(t *testing.T) { if err != nil { t.Fatalf("Failed to Commit: %s", err.Error()) } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Fatalf("Failed to Commit: wrong code returned %d", resp.Code) } @@ -2926,7 +2926,7 @@ func TestStream_Rollback(t *testing.T) { if err != nil { t.Fatalf("Failed to Begin: %s", err.Error()) } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Fatalf("Failed to Begin: wrong code returned %d", resp.Code) } @@ -2937,7 +2937,7 @@ func TestStream_Rollback(t *testing.T) { if err != nil { t.Fatalf("Failed to Insert: %s", err.Error()) } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Errorf("Failed to Insert: wrong code returned %d", resp.Code) } defer test_helpers.DeleteRecordByKey(t, conn, spaceNo, indexNo, []interface{}{uint(1001)}) @@ -2992,7 +2992,7 @@ func TestStream_Rollback(t *testing.T) { if err != nil { t.Fatalf("Failed to Rollback: %s", err.Error()) } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Fatalf("Failed to Rollback: wrong code returned %d", resp.Code) } @@ -3047,7 +3047,7 @@ func TestStream_TxnIsolationLevel(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Begin") require.NotNilf(t, resp, "response is nil after Begin") - require.Equalf(t, OkCode, resp.Code, "wrong code returned") + require.Equalf(t, CodeOk, resp.Code, "wrong code returned") // Insert in stream req = NewInsertRequest(spaceName). @@ -3055,7 +3055,7 @@ func TestStream_TxnIsolationLevel(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Insert") require.NotNilf(t, resp, "response is nil after Insert") - require.Equalf(t, OkCode, resp.Code, "wrong code returned") + require.Equalf(t, CodeOk, resp.Code, "wrong code returned") // Select not related to the transaction // while transaction is not committed @@ -3097,7 +3097,7 @@ func TestStream_TxnIsolationLevel(t *testing.T) { resp, err = stream.Do(req).Get() require.Nilf(t, err, "failed to Rollback") require.NotNilf(t, resp, "response is nil after Rollback") - require.Equalf(t, OkCode, resp.Code, "wrong code returned") + require.Equalf(t, CodeOk, resp.Code, "wrong code returned") // Select outside of transaction resp, err = conn.Do(selectReq).Get() @@ -3675,7 +3675,7 @@ func TestBroadcastRequest(t *testing.T) { if err != nil { t.Fatalf("Got broadcast error: %s", err) } - if resp.Code != OkCode { + if resp.Code != CodeOk { t.Errorf("Got unexpected broadcast response code: %d", resp.Code) } if !reflect.DeepEqual(resp.Data, []interface{}{}) { diff --git a/test_helpers/request_mock.go b/test_helpers/request_mock.go index 0eaa11739..1d007203f 100644 --- a/test_helpers/request_mock.go +++ b/test_helpers/request_mock.go @@ -15,7 +15,7 @@ func NewStrangerRequest() *StrangerRequest { return &StrangerRequest{} } -func (sr *StrangerRequest) Code() int32 { +func (sr *StrangerRequest) Code() tarantool.RequestCode { return 0 } diff --git a/watch.go b/watch.go index c937533e8..1ad41be64 100644 --- a/watch.go +++ b/watch.go @@ -35,7 +35,7 @@ func (req *BroadcastRequest) Context(ctx context.Context) *BroadcastRequest { } // Code returns IPROTO code for the broadcast request. -func (req *BroadcastRequest) Code() int32 { +func (req *BroadcastRequest) Code() RequestCode { return req.call.Code() } @@ -66,7 +66,7 @@ type watchRequest struct { // newWatchRequest returns a new watchRequest. func newWatchRequest(key string) *watchRequest { req := new(watchRequest) - req.requestCode = WatchRequestCode + req.requestCode = RequestCodeWatch req.async = true req.key = key return req @@ -100,7 +100,7 @@ type unwatchRequest struct { // newUnwatchRequest returns a new unwatchRequest. func newUnwatchRequest(key string) *unwatchRequest { req := new(unwatchRequest) - req.requestCode = UnwatchRequestCode + req.requestCode = RequestCodeUnwatch req.async = true req.key = key return req