Skip to content

Commit

Permalink
api: remove IPROTO constants
Browse files Browse the repository at this point in the history
IPROTO constants have been moved to a separate package go-iproto [1].

1. https://github.com/tarantool/go-iproto

Part of #158
  • Loading branch information
oleg-jukovec committed Jun 7, 2023
1 parent 306b13e commit 9c11fa7
Show file tree
Hide file tree
Showing 22 changed files with 271 additions and 411 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.

### Added

- Type() method to the Request interface (#158)

### Changed

- connection_pool renamed to pool (#239)
Expand All @@ -23,6 +25,8 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
- pool/RoundRobinStrategy (#158)
- DeadlineIO (#158)
- UUID_extId (#158)
- IPROTO constants (#158)
- Code() method from the Request interface (#158)

### Fixed

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ faster than other packages according to public benchmarks.
* [pool package](#pool-package)
* [msgpack.v5](#msgpackv5)
* [Call = Call17](#call--call17)
* [IPROTO constants](#iproto-constants)
* [Request interface](#request-interface)
* [Contributing](#contributing)
* [Alternative connectors](#alternative-connectors)

Expand Down Expand Up @@ -190,6 +192,14 @@ Call requests uses `IPROTO_CALL` instead of `IPROTO_CALL_16`.
So now `Call` = `Call17` and `NewCallRequest` = `NewCall17Request`. A result
of the requests is an array instead of array of arrays.

#### IPROTO constants

IPROTO constants have been moved to a separate package [go-iproto](https://github.com/tarantool/go-iproto).

#### Request interface

* The method `Code() uint32` replaced by the `Type() iproto.Type`.

## Contributing

See [the contributing guide](CONTRIBUTING.md) for detailed instructions on how
Expand Down
21 changes: 11 additions & 10 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync/atomic"
"time"

"github.com/tarantool/go-iproto"
"github.com/vmihailenco/msgpack/v5"
)

Expand Down Expand Up @@ -167,7 +168,7 @@ type Connection struct {
opts Opts
state uint32
dec *msgpack.Decoder
lenbuf [PacketLengthBytes]byte
lenbuf [packetLengthBytes]byte

lastStreamId uint64

Expand Down Expand Up @@ -410,8 +411,8 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
ter, ok := err.(Error)
if conn.opts.Reconnect <= 0 {
return nil, err
} else if ok && (ter.Code == ErrNoSuchUser ||
ter.Code == ErrPasswordMismatch) {
} else if ok && (ter.Code == iproto.ER_NO_SUCH_USER ||
ter.Code == iproto.ER_CREDS_MISMATCH) {
// Reported auth errors immediately.
return nil, err
} else {
Expand Down Expand Up @@ -595,7 +596,7 @@ func pack(h *smallWBuf, enc *msgpack.Encoder, reqid uint32,
hMapLen := byte(0x82) // 2 element map.
if streamId != ignoreStreamId {
hMapLen = byte(0x83) // 3 element map.
streamBytes[0] = KeyStreamId
streamBytes[0] = byte(iproto.IPROTO_STREAM_ID)
if streamId > math.MaxUint32 {
streamBytesLen = streamBytesLenUint64
streamBytes[1] = uint64Code
Expand All @@ -610,8 +611,8 @@ func pack(h *smallWBuf, enc *msgpack.Encoder, reqid uint32,
hBytes := append([]byte{
uint32Code, 0, 0, 0, 0, // Length.
hMapLen,
KeyCode, byte(req.Code()), // Request code.
KeySync, uint32Code,
byte(iproto.IPROTO_REQUEST_TYPE), byte(req.Type()), // Request type.
byte(iproto.IPROTO_SYNC), uint32Code,
byte(reqid >> 24), byte(reqid >> 16),
byte(reqid >> 8), byte(reqid),
}, streamBytes[:streamBytesLen]...)
Expand Down Expand Up @@ -813,13 +814,13 @@ func readWatchEvent(reader io.Reader) (connWatchEvent, error) {
return event, err
}

switch cd {
case KeyEvent:
switch iproto.Key(cd) {
case iproto.IPROTO_EVENT_KEY:
if event.key, err = d.DecodeString(); err != nil {
return event, err
}
keyExist = true
case KeyEventData:
case iproto.IPROTO_EVENT_DATA:
if event.value, err = d.DecodeInterface(); err != nil {
return event, err
}
Expand Down Expand Up @@ -857,7 +858,7 @@ func (conn *Connection) reader(r io.Reader, c Conn) {
}

var fut *Future = nil
if resp.Code == EventCode {
if iproto.Type(resp.Code) == iproto.IPROTO_EVENT {
if event, err := readWatchEvent(&resp.buf); err == nil {
events <- event
} else {
Expand Down
103 changes: 19 additions & 84 deletions const.go
Original file line number Diff line number Diff line change
@@ -1,95 +1,30 @@
package tarantool

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

KeyCode = 0x00
KeySync = 0x01
KeyStreamId = 0x0a
KeySpaceNo = 0x10
KeyIndexNo = 0x11
KeyLimit = 0x12
KeyOffset = 0x13
KeyIterator = 0x14
KeyFetchPos = 0x1f
KeyKey = 0x20
KeyTuple = 0x21
KeyFunctionName = 0x22
KeyUserName = 0x23
KeyExpression = 0x27
KeyAfterPos = 0x2e
KeyAfterTuple = 0x2f
KeyDefTuple = 0x28
KeyData = 0x30
KeyError24 = 0x31 /* Error in pre-2.4 format. */
KeyMetaData = 0x32
KeyBindCount = 0x34
KeyPos = 0x35
KeySQLText = 0x40
KeySQLBind = 0x41
KeySQLInfo = 0x42
KeyStmtID = 0x43
KeyError = 0x52 /* Extended error in >= 2.4 format. */
KeyVersion = 0x54
KeyFeatures = 0x55
KeyTimeout = 0x56
KeyEvent = 0x57
KeyEventData = 0x58
KeyTxnIsolation = 0x59
KeyAuthType = 0x5b

KeyFieldName = 0x00
KeyFieldType = 0x01
KeyFieldColl = 0x02
KeyFieldIsNullable = 0x03
KeyIsAutoincrement = 0x04
KeyFieldSpan = 0x05
KeySQLInfoRowCount = 0x00
KeySQLInfoAutoincrementIds = 0x01
import (
"github.com/tarantool/go-iproto"
)

// https://github.com/fl00r/go-tarantool-1.6/issues/2
const (
packetLengthBytes = 5
)

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
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(0)
EventCode = uint32(0x4c)
PushCode = uint32(0x80)
ErrorCodeBit = 0x8000
PacketLengthBytes = 5
ErSpaceExistsCode = 0xa
IteratorCode = 0x14
OkCode = uint32(iproto.IPROTO_OK)
PushCode = uint32(iproto.IPROTO_CHUNK)
)
8 changes: 5 additions & 3 deletions crud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ package crud
import (
"context"

"github.com/tarantool/go-iproto"

"github.com/tarantool/go-tarantool/v2"
)

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

// Code returns IPROTO code for CRUD request.
func (req baseRequest) Code() int32 {
return req.impl.Code()
// Type returns IPROTO type for CRUD request.
func (req baseRequest) Type() iproto.Type {
return req.impl.Type()
}

// Ctx returns a context of CRUD request.
Expand Down
57 changes: 29 additions & 28 deletions crud/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"testing"

"github.com/tarantool/go-iproto"
"github.com/vmihailenco/msgpack/v5"

"github.com/tarantool/go-tarantool/v2"
Expand All @@ -22,7 +23,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 CrudRequestType = iproto.IPROTO_CALL

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

func TestRequestsCodes(t *testing.T) {
tests := []struct {
req tarantool.Request
code int32
req tarantool.Request
rtype iproto.Type
}{
{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), rtype: CrudRequestType},
{req: crud.MakeInsertObjectRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeInsertManyRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeInsertObjectManyRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeGetRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeUpdateRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeDeleteRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeReplaceRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeReplaceObjectRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeReplaceManyRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeReplaceObjectManyRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeUpsertRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeUpsertObjectRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeUpsertManyRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeUpsertObjectManyRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeMinRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeMaxRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeSelectRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeTruncateRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeLenRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeCountRequest(validSpace), rtype: CrudRequestType},
{req: crud.MakeStorageInfoRequest(), rtype: CrudRequestType},
{req: crud.MakeStatsRequest(), rtype: CrudRequestType},
}

for _, test := range tests {
if code := test.req.Code(); code != test.code {
t.Errorf("An invalid request code 0x%x, expected 0x%x", code, test.code)
if rtype := test.req.Type(); rtype != test.rtype {
t.Errorf("An invalid request type 0x%x, expected 0x%x", rtype, test.rtype)
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions crud/tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"

"github.com/stretchr/testify/require"
"github.com/tarantool/go-iproto"

"github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/crud"
"github.com/tarantool/go-tarantool/v2/test_helpers"
Expand Down Expand Up @@ -399,9 +401,9 @@ func getCrudError(req tarantool.Request, crudError interface{}) (interface{}, er
var err []interface{}
var ok bool

code := req.Code()
rtype := req.Type()
if crudError != nil {
if code == tarantool.Call17RequestCode {
if rtype == iproto.IPROTO_CALL {
return crudError, nil
}

Expand Down
5 changes: 3 additions & 2 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/tarantool/go-iproto"
"github.com/vmihailenco/msgpack/v5"
)

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

resp, err := readResponse(r)
if err != nil {
if resp.Code == ErrUnknownRequestType {
if iproto.Error(resp.Code) == iproto.ER_UNKNOWN_REQUEST_TYPE {
// IPROTO_ID requests are not supported by server.
return info, nil
}
Expand Down Expand Up @@ -368,7 +369,7 @@ func writeRequest(w writeFlusher, req Request) error {

// readResponse reads a response from the reader.
func readResponse(r io.Reader) (Response, error) {
var lenbuf [PacketLengthBytes]byte
var lenbuf [packetLengthBytes]byte

respBytes, err := read(r, lenbuf[:])
if err != nil {
Expand Down
Loading

0 comments on commit 9c11fa7

Please sign in to comment.