Skip to content

Commit 15605d5

Browse files
committed
api: add separate types for constants
Closes #158
1 parent 2c0d8ad commit 15605d5

11 files changed

+90
-52
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1515
- Support CRUD API (#108)
1616
- An ability to replace a base network connection to a Tarantool
1717
instance (#265)
18+
- Enumeration types for RLimitAction/iterators (#158)
1819

1920
### Changed
2021

connection.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ type connShard struct {
231231
enc *msgpack.Encoder
232232
}
233233

234+
// RLimitActions is an enumeration type for an action to do when a rate limit
235+
// is reached.
236+
type RLimitAction int
237+
238+
const (
239+
// RLimitDrop immediately aborts the request.
240+
RLimitDrop RLimitAction = iota
241+
// RLimitWait waits during timeout period for some request to be answered.
242+
// If no request answered during timeout period, this request is aborted.
243+
// If no timeout period is set, it will wait forever.
244+
RLimitWait
245+
)
246+
234247
// Opts is a way to configure Connection
235248
type Opts struct {
236249
// Auth is an authentication method.
@@ -269,14 +282,9 @@ type Opts struct {
269282
// It is disabled by default.
270283
// See RLimitAction for possible actions when RateLimit.reached.
271284
RateLimit uint
272-
// RLimitAction tells what to do when RateLimit reached:
273-
// RLimitDrop - immediately abort request,
274-
// RLimitWait - wait during timeout period for some request to be answered.
275-
// If no request answered during timeout period, this request
276-
// is aborted.
277-
// If no timeout period is set, it will wait forever.
285+
// RLimitAction tells what to do when RateLimit is reached.
278286
// It is required if RateLimit is specified.
279-
RLimitAction uint
287+
RLimitAction RLimitAction
280288
// Concurrency is amount of separate mutexes for request
281289
// queues and buffers inside of connection.
282290
// It is rounded up to nearest power of 2.

connector.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Connector interface {
88
Ping() (resp *Response, err error)
99
ConfiguredTimeout() time.Duration
1010

11-
Select(space, index interface{}, offset, limit, iterator uint32, key interface{}) (resp *Response, err error)
11+
Select(space, index interface{}, offset, limit uint32, iterator Iter, key interface{}) (resp *Response, err error)
1212
Insert(space interface{}, tuple interface{}) (resp *Response, err error)
1313
Replace(space interface{}, tuple interface{}) (resp *Response, err error)
1414
Delete(space, index interface{}, key interface{}) (resp *Response, err error)
@@ -21,7 +21,7 @@ type Connector interface {
2121
Execute(expr string, args interface{}) (resp *Response, err error)
2222

2323
GetTyped(space, index interface{}, key interface{}, result interface{}) (err error)
24-
SelectTyped(space, index interface{}, offset, limit, iterator uint32, key interface{}, result interface{}) (err error)
24+
SelectTyped(space, index interface{}, offset, limit uint32, iterator Iter, key interface{}, result interface{}) (err error)
2525
InsertTyped(space interface{}, tuple interface{}, result interface{}) (err error)
2626
ReplaceTyped(space interface{}, tuple interface{}, result interface{}) (err error)
2727
DeleteTyped(space, index interface{}, key interface{}, result interface{}) (err error)
@@ -32,7 +32,7 @@ type Connector interface {
3232
EvalTyped(expr string, args interface{}, result interface{}) (err error)
3333
ExecuteTyped(expr string, args interface{}, result interface{}) (SQLInfo, []ColumnMetaData, error)
3434

35-
SelectAsync(space, index interface{}, offset, limit, iterator uint32, key interface{}) *Future
35+
SelectAsync(space, index interface{}, offset, limit uint32, iterator Iter, key interface{}) *Future
3636
InsertAsync(space interface{}, tuple interface{}) *Future
3737
ReplaceAsync(space interface{}, tuple interface{}) *Future
3838
DeleteAsync(space, index interface{}, key interface{}) *Future

const.go

-14
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,6 @@ const (
99
)
1010

1111
const (
12-
IterEq = uint32(0) // key == x ASC order
13-
IterReq = uint32(1) // key == x DESC order
14-
IterAll = uint32(2) // all tuples
15-
IterLt = uint32(3) // key < x
16-
IterLe = uint32(4) // key <= x
17-
IterGe = uint32(5) // key >= x
18-
IterGt = uint32(6) // key > x
19-
IterBitsAllSet = uint32(7) // all bits from x are set in key
20-
IterBitsAnySet = uint32(8) // at least one x's bit is set
21-
IterBitsAllNotSet = uint32(9) // all bits are not set
22-
23-
RLimitDrop = 1
24-
RLimitWait = 2
25-
2612
OkCode = uint32(iproto.IPROTO_OK)
2713
PushCode = uint32(iproto.IPROTO_CHUNK)
2814
)

export_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func RefImplPingBody(enc *msgpack.Encoder) error {
2424

2525
// RefImplSelectBody is reference implementation for filling of a select
2626
// request's body.
27-
func RefImplSelectBody(enc *msgpack.Encoder, space, index, offset, limit, iterator uint32,
27+
func RefImplSelectBody(enc *msgpack.Encoder, space, index, offset, limit uint32, iterator Iter,
2828
key, after interface{}, fetchPos bool) error {
2929
return fillSelect(enc, space, index, offset, limit, iterator, key, after, fetchPos)
3030
}

iterator.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package tarantool
2+
3+
import (
4+
"github.com/tarantool/go-iproto"
5+
)
6+
7+
// Iter is an enumeration type of a select iterator.
8+
type Iter uint32
9+
10+
const (
11+
// Key == x ASC order.
12+
IterEq Iter = Iter(iproto.ITER_EQ)
13+
// Key == x DESC order.
14+
IterReq Iter = Iter(iproto.ITER_REQ)
15+
// All tuples.
16+
IterAll Iter = Iter(iproto.ITER_ALL)
17+
// Key < x.
18+
IterLt Iter = Iter(iproto.ITER_LT)
19+
// Key <= x.
20+
IterLe Iter = Iter(iproto.ITER_LE)
21+
// Key >= x.
22+
IterGe Iter = Iter(iproto.ITER_GE)
23+
// Key > x.
24+
IterGt Iter = Iter(iproto.ITER_GT)
25+
// All bits from x are set in key.
26+
IterBitsAllSet Iter = Iter(iproto.ITER_BITS_ALL_SET)
27+
// All bits are not set.
28+
IterBitsAnySet Iter = Iter(iproto.ITER_BITS_ANY_SET)
29+
// All bits are not set.
30+
IterBitsAllNotSet Iter = Iter(iproto.ITER_BITS_ALL_NOT_SET)
31+
// Key overlaps x.
32+
IterOverlaps Iter = Iter(iproto.ITER_OVERLAPS)
33+
// Tuples in distance ascending order from specified point.
34+
IterNeighbor Iter = Iter(iproto.ITER_NEIGHBOR)
35+
)

pool/connection_pool.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ func (connPool *ConnectionPool) Ping(userMode Mode) (*tarantool.Response, error)
281281
}
282282

283283
// Select performs select to box space.
284-
func (connPool *ConnectionPool) Select(space, index interface{}, offset, limit, iterator uint32, key interface{}, userMode ...Mode) (resp *tarantool.Response, err error) {
284+
func (connPool *ConnectionPool) Select(space, index interface{},
285+
offset, limit uint32,
286+
iterator tarantool.Iter, key interface{}, userMode ...Mode) (resp *tarantool.Response, err error) {
285287
conn, err := connPool.getConnByMode(ANY, userMode)
286288
if err != nil {
287289
return nil, err
@@ -413,7 +415,9 @@ func (connPool *ConnectionPool) GetTyped(space, index interface{}, key interface
413415
}
414416

415417
// SelectTyped performs select to box space and fills typed result.
416-
func (connPool *ConnectionPool) SelectTyped(space, index interface{}, offset, limit, iterator uint32, key interface{}, result interface{}, userMode ...Mode) (err error) {
418+
func (connPool *ConnectionPool) SelectTyped(space, index interface{},
419+
offset, limit uint32,
420+
iterator tarantool.Iter, key interface{}, result interface{}, userMode ...Mode) (err error) {
417421
conn, err := connPool.getConnByMode(ANY, userMode)
418422
if err != nil {
419423
return err
@@ -521,7 +525,9 @@ func (connPool *ConnectionPool) ExecuteTyped(expr string, args interface{}, resu
521525
}
522526

523527
// SelectAsync sends select request to Tarantool and returns Future.
524-
func (connPool *ConnectionPool) SelectAsync(space, index interface{}, offset, limit, iterator uint32, key interface{}, userMode ...Mode) *tarantool.Future {
528+
func (connPool *ConnectionPool) SelectAsync(space, index interface{},
529+
offset, limit uint32,
530+
iterator tarantool.Iter, key interface{}, userMode ...Mode) *tarantool.Future {
525531
conn, err := connPool.getConnByMode(ANY, userMode)
526532
if err != nil {
527533
return newErrorFuture(err)

pool/connector.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (c *ConnectorAdapter) ConfiguredTimeout() time.Duration {
6363

6464
// Select performs select to box space.
6565
func (c *ConnectorAdapter) Select(space, index interface{},
66-
offset, limit, iterator uint32,
66+
offset, limit uint32, iterator tarantool.Iter,
6767
key interface{}) (*tarantool.Response, error) {
6868
return c.pool.Select(space, index, offset, limit, iterator, key, c.mode)
6969
}
@@ -143,7 +143,7 @@ func (c *ConnectorAdapter) GetTyped(space, index interface{},
143143

144144
// SelectTyped performs select to box space and fills typed result.
145145
func (c *ConnectorAdapter) SelectTyped(space, index interface{},
146-
offset, limit, iterator uint32,
146+
offset, limit uint32, iterator tarantool.Iter,
147147
key interface{}, result interface{}) error {
148148
return c.pool.SelectTyped(space, index, offset, limit, iterator, key, result, c.mode)
149149
}
@@ -210,7 +210,7 @@ func (c *ConnectorAdapter) ExecuteTyped(expr string, args interface{},
210210

211211
// SelectAsync sends select request to Tarantool and returns Future.
212212
func (c *ConnectorAdapter) SelectAsync(space, index interface{},
213-
offset, limit, iterator uint32, key interface{}) *tarantool.Future {
213+
offset, limit uint32, iterator tarantool.Iter, key interface{}) *tarantool.Future {
214214
return c.pool.SelectAsync(space, index, offset, limit, iterator, key, c.mode)
215215
}
216216

pool/connector_test.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,14 @@ func TestConnectorConfiguredTimeoutWithError(t *testing.T) {
125125

126126
type baseRequestMock struct {
127127
Pooler
128-
called int
129-
functionName string
130-
offset, limit, iterator uint32
131-
space, index interface{}
132-
args, tuple, key, ops interface{}
133-
result interface{}
134-
mode Mode
128+
called int
129+
functionName string
130+
offset, limit uint32
131+
iterator tarantool.Iter
132+
space, index interface{}
133+
args, tuple, key, ops interface{}
134+
result interface{}
135+
mode Mode
135136
}
136137

137138
var reqResp *tarantool.Response = &tarantool.Response{}
@@ -141,7 +142,7 @@ var reqFuture *tarantool.Future = &tarantool.Future{}
141142
var reqFunctionName string = "any_name"
142143
var reqOffset uint32 = 1
143144
var reqLimit uint32 = 2
144-
var reqIterator uint32 = 3
145+
var reqIterator tarantool.Iter = tarantool.IterLt
145146
var reqSpace interface{} = []interface{}{1}
146147
var reqIndex interface{} = []interface{}{2}
147148
var reqArgs interface{} = []interface{}{3}
@@ -188,7 +189,7 @@ type selectMock struct {
188189
}
189190

190191
func (m *selectMock) Select(space, index interface{},
191-
offset, limit, iterator uint32, key interface{},
192+
offset, limit uint32, iterator tarantool.Iter, key interface{},
192193
mode ...Mode) (*tarantool.Response, error) {
193194
m.called++
194195
m.space = space
@@ -224,7 +225,7 @@ type selectTypedMock struct {
224225
}
225226

226227
func (m *selectTypedMock) SelectTyped(space, index interface{},
227-
offset, limit, iterator uint32, key interface{},
228+
offset, limit uint32, iterator tarantool.Iter, key interface{},
228229
result interface{}, mode ...Mode) error {
229230
m.called++
230231
m.space = space
@@ -262,7 +263,7 @@ type selectAsyncMock struct {
262263
}
263264

264265
func (m *selectAsyncMock) SelectAsync(space, index interface{},
265-
offset, limit, iterator uint32, key interface{},
266+
offset, limit uint32, iterator tarantool.Iter, key interface{},
266267
mode ...Mode) *tarantool.Future {
267268
m.called++
268269
m.space = space

pool/pooler.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Pooler interface {
1313
Ping(mode Mode) (*tarantool.Response, error)
1414
ConfiguredTimeout(mode Mode) (time.Duration, error)
1515

16-
Select(space, index interface{}, offset, limit, iterator uint32,
16+
Select(space, index interface{}, offset, limit uint32, iterator tarantool.Iter,
1717
key interface{}, mode ...Mode) (*tarantool.Response, error)
1818
Insert(space interface{}, tuple interface{},
1919
mode ...Mode) (*tarantool.Response, error)
@@ -38,7 +38,7 @@ type Pooler interface {
3838

3939
GetTyped(space, index interface{}, key interface{}, result interface{},
4040
mode ...Mode) error
41-
SelectTyped(space, index interface{}, offset, limit, iterator uint32,
41+
SelectTyped(space, index interface{}, offset, limit uint32, iterator tarantool.Iter,
4242
key interface{}, result interface{}, mode ...Mode) error
4343
InsertTyped(space interface{}, tuple interface{}, result interface{},
4444
mode ...Mode) error
@@ -59,7 +59,7 @@ type Pooler interface {
5959
ExecuteTyped(expr string, args interface{}, result interface{},
6060
mode Mode) (tarantool.SQLInfo, []tarantool.ColumnMetaData, error)
6161

62-
SelectAsync(space, index interface{}, offset, limit, iterator uint32,
62+
SelectAsync(space, index interface{}, offset, limit uint32, iterator tarantool.Iter,
6363
key interface{}, mode ...Mode) *tarantool.Future
6464
InsertAsync(space interface{}, tuple interface{},
6565
mode ...Mode) *tarantool.Future

request.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func fillSearch(enc *msgpack.Encoder, spaceNo, indexNo uint32, key interface{})
3131
return enc.Encode(key)
3232
}
3333

34-
func fillIterator(enc *msgpack.Encoder, offset, limit, iterator uint32) error {
34+
func fillIterator(enc *msgpack.Encoder, offset, limit uint32, iterator Iter) error {
3535
if err := enc.EncodeUint(uint64(iproto.IPROTO_ITERATOR)); err != nil {
3636
return err
3737
}
@@ -66,7 +66,7 @@ func fillInsert(enc *msgpack.Encoder, spaceNo uint32, tuple interface{}) error {
6666
return enc.Encode(tuple)
6767
}
6868

69-
func fillSelect(enc *msgpack.Encoder, spaceNo, indexNo, offset, limit, iterator uint32,
69+
func fillSelect(enc *msgpack.Encoder, spaceNo, indexNo, offset, limit uint32, iterator Iter,
7070
key, after interface{}, fetchPos bool) error {
7171
mapLen := 6
7272
if fetchPos {
@@ -174,7 +174,7 @@ func (conn *Connection) Ping() (resp *Response, err error) {
174174
// Select performs select to box space.
175175
//
176176
// It is equal to conn.SelectAsync(...).Get().
177-
func (conn *Connection) Select(space, index interface{}, offset, limit, iterator uint32, key interface{}) (resp *Response, err error) {
177+
func (conn *Connection) Select(space, index interface{}, offset, limit uint32, iterator Iter, key interface{}) (resp *Response, err error) {
178178
return conn.SelectAsync(space, index, offset, limit, iterator, key).Get()
179179
}
180180

@@ -294,7 +294,7 @@ func (conn *Connection) GetTyped(space, index interface{}, key interface{}, resu
294294
// SelectTyped performs select to box space and fills typed result.
295295
//
296296
// It is equal to conn.SelectAsync(space, index, offset, limit, iterator, key).GetTyped(&result)
297-
func (conn *Connection) SelectTyped(space, index interface{}, offset, limit, iterator uint32, key interface{}, result interface{}) (err error) {
297+
func (conn *Connection) SelectTyped(space, index interface{}, offset, limit uint32, iterator Iter, key interface{}, result interface{}) (err error) {
298298
return conn.SelectAsync(space, index, offset, limit, iterator, key).GetTyped(result)
299299
}
300300

@@ -373,7 +373,7 @@ func (conn *Connection) ExecuteTyped(expr string, args interface{}, result inter
373373
}
374374

375375
// SelectAsync sends select request to Tarantool and returns Future.
376-
func (conn *Connection) SelectAsync(space, index interface{}, offset, limit, iterator uint32, key interface{}) *Future {
376+
func (conn *Connection) SelectAsync(space, index interface{}, offset, limit uint32, iterator Iter, key interface{}) *Future {
377377
req := NewSelectRequest(space).
378378
Index(index).
379379
Offset(offset).
@@ -746,7 +746,8 @@ func (req *PingRequest) Context(ctx context.Context) *PingRequest {
746746
type SelectRequest struct {
747747
spaceIndexRequest
748748
isIteratorSet, fetchPos bool
749-
offset, limit, iterator uint32
749+
offset, limit uint32
750+
iterator Iter
750751
key, after interface{}
751752
}
752753

@@ -787,7 +788,7 @@ func (req *SelectRequest) Limit(limit uint32) *SelectRequest {
787788

788789
// Iterator set the iterator for the select request.
789790
// Note: default value is IterAll if key is not set or IterEq otherwise.
790-
func (req *SelectRequest) Iterator(iterator uint32) *SelectRequest {
791+
func (req *SelectRequest) Iterator(iterator Iter) *SelectRequest {
791792
req.iterator = iterator
792793
req.isIteratorSet = true
793794
return req

0 commit comments

Comments
 (0)