Skip to content

Commit

Permalink
api: add separate types for constants
Browse files Browse the repository at this point in the history
Part of #158
  • Loading branch information
oleg-jukovec committed Mar 24, 2023
1 parent 7d8f33c commit 56a13bc
Show file tree
Hide file tree
Showing 29 changed files with 271 additions and 234 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Call>RequestCode renamed to RequestCode<Call> (#158)

### Removed

Expand Down
28 changes: 18 additions & 10 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
45 changes: 1 addition & 44 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
)
2 changes: 1 addition & 1 deletion crud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
50 changes: 25 additions & 25 deletions crud/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crud/tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion future.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
Expand Down
20 changes: 10 additions & 10 deletions future_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.")
}
Expand All @@ -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.")
}
Expand All @@ -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)
Expand Down Expand Up @@ -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.")
}
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestFutureGetIteratorResponse(t *testing.T) {
responses := []*Response{
{},
{},
{Code: OkCode},
{Code: CodeOk},
}
fut := NewFuture()
for i, resp := range responses {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
19 changes: 19 additions & 0 deletions iterator.go
Original file line number Diff line number Diff line change
@@ -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
)
Loading

0 comments on commit 56a13bc

Please sign in to comment.