Skip to content

Commit 0abc12b

Browse files
committed
api: remove IPROTO constants
IPROTO constants have been moved to a separate package go-iproto [1]. 1. https://github.com/tarantool/go-iproto Part of #158
1 parent ccc67b6 commit 0abc12b

22 files changed

+253
-400
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
2929
- pool/RoundRobinStrategy (#158)
3030
- DeadlineIO (#158)
3131
- UUID_extId (#158)
32+
- IPROTO constants (#158)
3233

3334
### Fixed
3435

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ faster than other packages according to public benchmarks.
3636
* [pool package](#pool-package)
3737
* [msgpack.v5](#msgpackv5)
3838
* [Call = Call17](#call--call17)
39+
* [IPROTO constants](#iproto-constants)
3940
* [Contributing](#contributing)
4041
* [Alternative connectors](#alternative-connectors)
4142

@@ -195,6 +196,10 @@ Call requests uses `IPROTO_CALL` instead of `IPROTO_CALL_16`.
195196
So now `Call` = `Call17` and `NewCallRequest` = `NewCall17Request`. A result
196197
of the requests is an array instead of array of arrays.
197198

199+
#### IPROTO constants
200+
201+
IPROTO constants have been moved to a separate package [go-iproto](https://github.com/tarantool/go-iproto).
202+
198203
## Contributing
199204

200205
See [the contributing guide](CONTRIBUTING.md) for detailed instructions on how

connection.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"sync/atomic"
1616
"time"
1717

18+
"github.com/tarantool/go-iproto"
1819
"github.com/vmihailenco/msgpack/v5"
1920
)
2021

@@ -162,7 +163,7 @@ type Connection struct {
162163
opts Opts
163164
state uint32
164165
dec *msgpack.Decoder
165-
lenbuf [PacketLengthBytes]byte
166+
lenbuf [packetLengthBytes]byte
166167

167168
lastStreamId uint64
168169

@@ -405,8 +406,8 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
405406
ter, ok := err.(Error)
406407
if conn.opts.Reconnect <= 0 {
407408
return nil, err
408-
} else if ok && (ter.Code == ErrNoSuchUser ||
409-
ter.Code == ErrPasswordMismatch) {
409+
} else if ok && (ter.Code == iproto.ER_NO_SUCH_USER ||
410+
ter.Code == iproto.ER_CREDS_MISMATCH) {
410411
// Reported auth errors immediately.
411412
return nil, err
412413
} else {
@@ -583,7 +584,7 @@ func pack(h *smallWBuf, enc *msgpack.Encoder, reqid uint32,
583584
hMapLen := byte(0x82) // 2 element map.
584585
if streamId != ignoreStreamId {
585586
hMapLen = byte(0x83) // 3 element map.
586-
streamBytes[0] = KeyStreamId
587+
streamBytes[0] = byte(iproto.IPROTO_STREAM_ID)
587588
if streamId > math.MaxUint32 {
588589
streamBytesLen = streamBytesLenUint64
589590
streamBytes[1] = uint64Code
@@ -598,8 +599,8 @@ func pack(h *smallWBuf, enc *msgpack.Encoder, reqid uint32,
598599
hBytes := append([]byte{
599600
uint32Code, 0, 0, 0, 0, // Length.
600601
hMapLen,
601-
KeyCode, byte(req.Code()), // Request code.
602-
KeySync, uint32Code,
602+
byte(iproto.IPROTO_REQUEST_TYPE), byte(req.Type()), // Request type.
603+
byte(iproto.IPROTO_SYNC), uint32Code,
603604
byte(reqid >> 24), byte(reqid >> 16),
604605
byte(reqid >> 8), byte(reqid),
605606
}, streamBytes[:streamBytesLen]...)
@@ -801,13 +802,13 @@ func readWatchEvent(reader io.Reader) (connWatchEvent, error) {
801802
return event, err
802803
}
803804

804-
switch cd {
805-
case KeyEvent:
805+
switch iproto.Key(cd) {
806+
case iproto.IPROTO_EVENT_KEY:
806807
if event.key, err = d.DecodeString(); err != nil {
807808
return event, err
808809
}
809810
keyExist = true
810-
case KeyEventData:
811+
case iproto.IPROTO_EVENT_DATA:
811812
if event.value, err = d.DecodeInterface(); err != nil {
812813
return event, err
813814
}
@@ -845,7 +846,7 @@ func (conn *Connection) reader(r io.Reader, c Conn) {
845846
}
846847

847848
var fut *Future = nil
848-
if resp.Code == EventCode {
849+
if iproto.Type(resp.Code) == iproto.IPROTO_EVENT {
849850
if event, err := readWatchEvent(&resp.buf); err == nil {
850851
events <- event
851852
} else {

const.go

+5-74
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,10 @@
11
package tarantool
22

33
const (
4-
SelectRequestCode = 1
5-
InsertRequestCode = 2
6-
ReplaceRequestCode = 3
7-
UpdateRequestCode = 4
8-
DeleteRequestCode = 5
9-
Call16RequestCode = 6 /* call in 1.6 format */
10-
AuthRequestCode = 7
11-
EvalRequestCode = 8
12-
UpsertRequestCode = 9
13-
Call17RequestCode = 10 /* call in >= 1.7 format */
14-
ExecuteRequestCode = 11
15-
PrepareRequestCode = 13
16-
BeginRequestCode = 14
17-
CommitRequestCode = 15
18-
RollbackRequestCode = 16
19-
PingRequestCode = 64
20-
SubscribeRequestCode = 66
21-
IdRequestCode = 73
22-
WatchRequestCode = 74
23-
UnwatchRequestCode = 75
24-
CallRequestCode = Call17RequestCode
25-
26-
KeyCode = 0x00
27-
KeySync = 0x01
28-
KeyStreamId = 0x0a
29-
KeySpaceNo = 0x10
30-
KeyIndexNo = 0x11
31-
KeyLimit = 0x12
32-
KeyOffset = 0x13
33-
KeyIterator = 0x14
34-
KeyFetchPos = 0x1f
35-
KeyKey = 0x20
36-
KeyTuple = 0x21
37-
KeyFunctionName = 0x22
38-
KeyUserName = 0x23
39-
KeyExpression = 0x27
40-
KeyAfterPos = 0x2e
41-
KeyAfterTuple = 0x2f
42-
KeyDefTuple = 0x28
43-
KeyData = 0x30
44-
KeyError24 = 0x31 /* Error in pre-2.4 format. */
45-
KeyMetaData = 0x32
46-
KeyBindCount = 0x34
47-
KeyPos = 0x35
48-
KeySQLText = 0x40
49-
KeySQLBind = 0x41
50-
KeySQLInfo = 0x42
51-
KeyStmtID = 0x43
52-
KeyError = 0x52 /* Extended error in >= 2.4 format. */
53-
KeyVersion = 0x54
54-
KeyFeatures = 0x55
55-
KeyTimeout = 0x56
56-
KeyEvent = 0x57
57-
KeyEventData = 0x58
58-
KeyTxnIsolation = 0x59
59-
KeyAuthType = 0x5b
60-
61-
KeyFieldName = 0x00
62-
KeyFieldType = 0x01
63-
KeyFieldColl = 0x02
64-
KeyFieldIsNullable = 0x03
65-
KeyIsAutoincrement = 0x04
66-
KeyFieldSpan = 0x05
67-
KeySQLInfoRowCount = 0x00
68-
KeySQLInfoAutoincrementIds = 0x01
69-
70-
// https://github.com/fl00r/go-tarantool-1.6/issues/2
4+
packetLengthBytes = 5
5+
)
716

7+
const (
728
IterEq = uint32(0) // key == x ASC order
739
IterReq = uint32(1) // key == x DESC order
7410
IterAll = uint32(2) // all tuples
@@ -83,11 +19,6 @@ const (
8319
RLimitDrop = 1
8420
RLimitWait = 2
8521

86-
OkCode = uint32(0)
87-
EventCode = uint32(0x4c)
88-
PushCode = uint32(0x80)
89-
ErrorCodeBit = 0x8000
90-
PacketLengthBytes = 5
91-
ErSpaceExistsCode = 0xa
92-
IteratorCode = 0x14
22+
OkCode = uint32(iproto.IPROTO_OK)
23+
PushCode = uint32(iproto.IPROTO_CHUNK)
9324
)

crud/common.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ package crud
5656
import (
5757
"context"
5858

59+
"github.com/tarantool/go-iproto"
60+
5961
"github.com/tarantool/go-tarantool/v2"
6062
)
6163

@@ -67,9 +69,9 @@ func newCall(method string) *tarantool.CallRequest {
6769
return tarantool.NewCall17Request(method)
6870
}
6971

70-
// Code returns IPROTO code for CRUD request.
71-
func (req baseRequest) Code() int32 {
72-
return req.impl.Code()
72+
// Type returns IPROTO type for CRUD request.
73+
func (req baseRequest) Type() iproto.Type {
74+
return req.impl.Type()
7375
}
7476

7577
// Ctx returns a context of CRUD request.

crud/request_test.go

+29-28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"testing"
99

10+
"github.com/tarantool/go-iproto"
1011
"github.com/vmihailenco/msgpack/v5"
1112

1213
"github.com/tarantool/go-tarantool/v2"
@@ -22,7 +23,7 @@ const validSpace = "test" // Any valid value != default.
2223
const defaultSpace = 0 // And valid too.
2324
const defaultIndex = 0 // And valid too.
2425

25-
const CrudRequestCode = tarantool.Call17RequestCode
26+
const CrudRequestType = iproto.IPROTO_CALL
2627

2728
var reqObject = crud.MapObject{
2829
"id": uint(24),
@@ -166,37 +167,37 @@ func BenchmarkSelectRequest(b *testing.B) {
166167

167168
func TestRequestsCodes(t *testing.T) {
168169
tests := []struct {
169-
req tarantool.Request
170-
code int32
170+
req tarantool.Request
171+
rtype iproto.Type
171172
}{
172-
{req: crud.MakeInsertRequest(validSpace), code: CrudRequestCode},
173-
{req: crud.MakeInsertObjectRequest(validSpace), code: CrudRequestCode},
174-
{req: crud.MakeInsertManyRequest(validSpace), code: CrudRequestCode},
175-
{req: crud.MakeInsertObjectManyRequest(validSpace), code: CrudRequestCode},
176-
{req: crud.MakeGetRequest(validSpace), code: CrudRequestCode},
177-
{req: crud.MakeUpdateRequest(validSpace), code: CrudRequestCode},
178-
{req: crud.MakeDeleteRequest(validSpace), code: CrudRequestCode},
179-
{req: crud.MakeReplaceRequest(validSpace), code: CrudRequestCode},
180-
{req: crud.MakeReplaceObjectRequest(validSpace), code: CrudRequestCode},
181-
{req: crud.MakeReplaceManyRequest(validSpace), code: CrudRequestCode},
182-
{req: crud.MakeReplaceObjectManyRequest(validSpace), code: CrudRequestCode},
183-
{req: crud.MakeUpsertRequest(validSpace), code: CrudRequestCode},
184-
{req: crud.MakeUpsertObjectRequest(validSpace), code: CrudRequestCode},
185-
{req: crud.MakeUpsertManyRequest(validSpace), code: CrudRequestCode},
186-
{req: crud.MakeUpsertObjectManyRequest(validSpace), code: CrudRequestCode},
187-
{req: crud.MakeMinRequest(validSpace), code: CrudRequestCode},
188-
{req: crud.MakeMaxRequest(validSpace), code: CrudRequestCode},
189-
{req: crud.MakeSelectRequest(validSpace), code: CrudRequestCode},
190-
{req: crud.MakeTruncateRequest(validSpace), code: CrudRequestCode},
191-
{req: crud.MakeLenRequest(validSpace), code: CrudRequestCode},
192-
{req: crud.MakeCountRequest(validSpace), code: CrudRequestCode},
193-
{req: crud.MakeStorageInfoRequest(), code: CrudRequestCode},
194-
{req: crud.MakeStatsRequest(), code: CrudRequestCode},
173+
{req: crud.MakeInsertRequest(validSpace), rtype: CrudRequestType},
174+
{req: crud.MakeInsertObjectRequest(validSpace), rtype: CrudRequestType},
175+
{req: crud.MakeInsertManyRequest(validSpace), rtype: CrudRequestType},
176+
{req: crud.MakeInsertObjectManyRequest(validSpace), rtype: CrudRequestType},
177+
{req: crud.MakeGetRequest(validSpace), rtype: CrudRequestType},
178+
{req: crud.MakeUpdateRequest(validSpace), rtype: CrudRequestType},
179+
{req: crud.MakeDeleteRequest(validSpace), rtype: CrudRequestType},
180+
{req: crud.MakeReplaceRequest(validSpace), rtype: CrudRequestType},
181+
{req: crud.MakeReplaceObjectRequest(validSpace), rtype: CrudRequestType},
182+
{req: crud.MakeReplaceManyRequest(validSpace), rtype: CrudRequestType},
183+
{req: crud.MakeReplaceObjectManyRequest(validSpace), rtype: CrudRequestType},
184+
{req: crud.MakeUpsertRequest(validSpace), rtype: CrudRequestType},
185+
{req: crud.MakeUpsertObjectRequest(validSpace), rtype: CrudRequestType},
186+
{req: crud.MakeUpsertManyRequest(validSpace), rtype: CrudRequestType},
187+
{req: crud.MakeUpsertObjectManyRequest(validSpace), rtype: CrudRequestType},
188+
{req: crud.MakeMinRequest(validSpace), rtype: CrudRequestType},
189+
{req: crud.MakeMaxRequest(validSpace), rtype: CrudRequestType},
190+
{req: crud.MakeSelectRequest(validSpace), rtype: CrudRequestType},
191+
{req: crud.MakeTruncateRequest(validSpace), rtype: CrudRequestType},
192+
{req: crud.MakeLenRequest(validSpace), rtype: CrudRequestType},
193+
{req: crud.MakeCountRequest(validSpace), rtype: CrudRequestType},
194+
{req: crud.MakeStorageInfoRequest(), rtype: CrudRequestType},
195+
{req: crud.MakeStatsRequest(), rtype: CrudRequestType},
195196
}
196197

197198
for _, test := range tests {
198-
if code := test.req.Code(); code != test.code {
199-
t.Errorf("An invalid request code 0x%x, expected 0x%x", code, test.code)
199+
if rtype := test.req.Type(); rtype != test.rtype {
200+
t.Errorf("An invalid request type 0x%x, expected 0x%x", rtype, test.rtype)
200201
}
201202
}
202203
}

crud/tarantool_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"time"
99

1010
"github.com/stretchr/testify/require"
11+
"github.com/tarantool/go-iproto"
12+
1113
"github.com/tarantool/go-tarantool/v2"
1214
"github.com/tarantool/go-tarantool/v2/crud"
1315
"github.com/tarantool/go-tarantool/v2/test_helpers"
@@ -372,9 +374,9 @@ func getCrudError(req tarantool.Request, crudError interface{}) (interface{}, er
372374
var err []interface{}
373375
var ok bool
374376

375-
code := req.Code()
377+
rtype := req.Type()
376378
if crudError != nil {
377-
if code == tarantool.Call17RequestCode {
379+
if rtype == iproto.IPROTO_CALL {
378380
return crudError, nil
379381
}
380382

dial.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/tarantool/go-iproto"
1314
"github.com/vmihailenco/msgpack/v5"
1415
)
1516

@@ -261,7 +262,7 @@ func identify(w writeFlusher, r io.Reader) (ProtocolInfo, error) {
261262

262263
resp, err := readResponse(r)
263264
if err != nil {
264-
if resp.Code == ErrUnknownRequestType {
265+
if iproto.Error(resp.Code) == iproto.ER_UNKNOWN_REQUEST_TYPE {
265266
// IPROTO_ID requests are not supported by server.
266267
return info, nil
267268
}
@@ -368,7 +369,7 @@ func writeRequest(w writeFlusher, req Request) error {
368369

369370
// readResponse reads a response from the reader.
370371
func readResponse(r io.Reader) (Response, error) {
371-
var lenbuf [PacketLengthBytes]byte
372+
var lenbuf [packetLengthBytes]byte
372373

373374
respBytes, err := read(r, lenbuf[:])
374375
if err != nil {

0 commit comments

Comments
 (0)