Skip to content

Commit

Permalink
api: add separate types for constants
Browse files Browse the repository at this point in the history
Closes #158
  • Loading branch information
oleg-jukovec committed Jun 7, 2023
1 parent 9c11fa7 commit 176f1ce
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
### Added

- Type() method to the Request interface (#158)
- Enumeration types for RLimitAction/iterators (#158)

### Changed

Expand Down
22 changes: 15 additions & 7 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,19 @@ type connShard struct {
enc *msgpack.Encoder
}

// RLimitActions is an enumeration type for an action to do when a rate limit
// is reached.
type RLimitAction int

const (
// RLimitDrop immediately aborts the 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 @@ -274,14 +287,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
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
16 changes: 0 additions & 16 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ const (
)

const (
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
IterOverlaps = uint32(10) // key overlaps x
IterNeighbor = uint32(11) // tuples in distance ascending order from specified point

RLimitDrop = 1
RLimitWait = 2

OkCode = uint32(iproto.IPROTO_OK)
PushCode = uint32(iproto.IPROTO_CHUNK)
)
2 changes: 1 addition & 1 deletion export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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,
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
35 changes: 35 additions & 0 deletions iterator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tarantool

import (
"github.com/tarantool/go-iproto"
)

// Iter is an enumeration type of a select iterator.
type Iter uint32

const (
// Key == x ASC order.
IterEq Iter = Iter(iproto.ITER_EQ)
// Key == x DESC order.
IterReq Iter = Iter(iproto.ITER_REQ)
// All tuples.
IterAll Iter = Iter(iproto.ITER_ALL)
// Key < x.
IterLt Iter = Iter(iproto.ITER_LT)
// Key <= x.
IterLe Iter = Iter(iproto.ITER_LE)
// Key >= x.
IterGe Iter = Iter(iproto.ITER_GE)
// Key > x.
IterGt Iter = Iter(iproto.ITER_GT)
// All bits from x are set in key.
IterBitsAllSet Iter = Iter(iproto.ITER_BITS_ALL_SET)
// All bits are not set.
IterBitsAnySet Iter = Iter(iproto.ITER_BITS_ANY_SET)
// All bits are not set.
IterBitsAllNotSet Iter = Iter(iproto.ITER_BITS_ALL_NOT_SET)
// Key overlaps x.
IterOverlaps Iter = Iter(iproto.ITER_OVERLAPS)
// Tuples in distance ascending order from specified point.
IterNeighbor Iter = Iter(iproto.ITER_NEIGHBOR)
)
12 changes: 9 additions & 3 deletions pool/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ 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
Expand Down Expand Up @@ -503,7 +505,9 @@ 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
Expand Down Expand Up @@ -609,7 +613,9 @@ 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)
Expand Down
6 changes: 3 additions & 3 deletions pool/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -141,7 +141,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)
}
Expand Down Expand Up @@ -206,7 +206,7 @@ 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)
}

Expand Down
23 changes: 12 additions & 11 deletions pool/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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.IterLt
var reqSpace interface{} = []interface{}{1}
var reqIndex interface{} = []interface{}{2}
var reqArgs interface{} = []interface{}{3}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pool/pooler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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)
Expand All @@ -38,7 +38,7 @@ 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
Expand All @@ -59,7 +59,7 @@ 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
Expand Down
15 changes: 8 additions & 7 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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(uint64(iproto.IPROTO_ITERATOR)); err != nil {
return err
}
Expand Down Expand Up @@ -66,7 +66,7 @@ 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 {
Expand Down Expand Up @@ -174,7 +174,7 @@ 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()
}

Expand Down Expand Up @@ -292,7 +292,7 @@ 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)
}

Expand Down Expand Up @@ -369,7 +369,7 @@ 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).
Expand Down Expand Up @@ -740,7 +740,8 @@ 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{}
}

Expand Down Expand Up @@ -781,7 +782,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
Expand Down

0 comments on commit 176f1ce

Please sign in to comment.