From 36a9db06697ec12f018620b32e4c3b383fca7a23 Mon Sep 17 00:00:00 2001 From: Oleg Jukovec Date: Thu, 20 Apr 2023 17:08:26 +0300 Subject: [PATCH] api: add separate types for constants Closes #158 --- CHANGELOG.md | 1 + connection.go | 22 +++++++++++++++------- connector.go | 6 +++--- const.go | 14 -------------- export_test.go | 2 +- iterator.go | 17 +++++++++++++++++ pool/connection_pool.go | 12 +++++++++--- pool/connector.go | 6 +++--- pool/connector_test.go | 23 ++++++++++++----------- pool/pooler.go | 6 +++--- request.go | 15 ++++++++------- 11 files changed, 72 insertions(+), 52 deletions(-) create mode 100644 iterator.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ad04ec94..537a732b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ 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 RLimitAction/iterators (#158) ### Changed diff --git a/connection.go b/connection.go index f641b2617..b925369a7 100644 --- a/connection.go +++ b/connection.go @@ -231,6 +231,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. @@ -269,14 +282,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. 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 5163b53c3..62650b5be 100644 --- a/const.go +++ b/const.go @@ -9,20 +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 - - RLimitDrop = 1 - RLimitWait = 2 - OkCode = uint32(iproto.IPROTO_OK) PushCode = uint32(iproto.IPROTO_CHUNK) ) diff --git a/export_test.go b/export_test.go index 879d345ac..688d09059 100644 --- a/export_test.go +++ b/export_test.go @@ -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) } diff --git a/iterator.go b/iterator.go new file mode 100644 index 000000000..7aa5d1afb --- /dev/null +++ b/iterator.go @@ -0,0 +1,17 @@ +package tarantool + +// Iter is an enumeration type of a select iterator. +type Iter uint32 + +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 5e27af3c3..287f0ce7e 100644 --- a/pool/connection_pool.go +++ b/pool/connection_pool.go @@ -281,7 +281,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 @@ -413,7 +415,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 @@ -521,7 +525,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) diff --git a/pool/connector.go b/pool/connector.go index 2cf094858..a6f66d56c 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,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) } diff --git a/pool/connector_test.go b/pool/connector_test.go index d4d197607..8a954fa6e 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.IterLt 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..a7cae8f95 100644 --- a/pool/pooler.go +++ b/pool/pooler.go @@ -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) @@ -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 @@ -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 diff --git a/request.go b/request.go index 686bb6845..b05a8ae50 100644 --- a/request.go +++ b/request.go @@ -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 } @@ -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 { @@ -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() } @@ -294,7 +294,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) } @@ -373,7 +373,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). @@ -746,7 +746,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{} } @@ -787,7 +788,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