Skip to content

Commit

Permalink
code health: extract a msgpack code from the code and tests
Browse files Browse the repository at this point in the history
The patch replaces the msgpack code by internal wrappers. The msgpack
usage have been extracted to msgpack.go file for the code and to
msgpack_helper_test.go for tests. It is the same logic for submodules.

Part of #124
  • Loading branch information
oleg-jukovec committed May 27, 2022
1 parent eb72cb3 commit 6038440
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 106 deletions.
16 changes: 6 additions & 10 deletions client_tools.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package tarantool

import (
"gopkg.in/vmihailenco/msgpack.v2"
)

// IntKey is utility type for passing integer key to Select*, Update* and Delete*.
// It serializes to array with single integer element.
type IntKey struct {
I int
}

func (k IntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
func (k IntKey) encodeMsgpackImpl(enc *Encoder) error {
enc.EncodeArrayLen(1)
enc.EncodeInt(k.I)
return nil
Expand All @@ -22,7 +18,7 @@ type UintKey struct {
I uint
}

func (k UintKey) EncodeMsgpack(enc *msgpack.Encoder) error {
func (k UintKey) encodeMsgpackImpl(enc *Encoder) error {
enc.EncodeArrayLen(1)
enc.EncodeUint(k.I)
return nil
Expand All @@ -34,7 +30,7 @@ type StringKey struct {
S string
}

func (k StringKey) EncodeMsgpack(enc *msgpack.Encoder) error {
func (k StringKey) encodeMsgpackImpl(enc *Encoder) error {
enc.EncodeArrayLen(1)
enc.EncodeString(k.S)
return nil
Expand All @@ -46,7 +42,7 @@ type IntIntKey struct {
I1, I2 int
}

func (k IntIntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
func (k IntIntKey) encodeMsgpackImpl(enc *Encoder) error {
enc.EncodeArrayLen(2)
enc.EncodeInt(k.I1)
enc.EncodeInt(k.I2)
Expand All @@ -60,7 +56,7 @@ type Op struct {
Arg interface{}
}

func (o Op) EncodeMsgpack(enc *msgpack.Encoder) error {
func (o Op) encodeMsgpackImpl(enc *Encoder) error {
enc.EncodeArrayLen(3)
enc.EncodeString(o.Op)
enc.EncodeInt(o.Field)
Expand All @@ -75,7 +71,7 @@ type OpSplice struct {
Replace string
}

func (o OpSplice) EncodeMsgpack(enc *msgpack.Encoder) error {
func (o OpSplice) encodeMsgpackImpl(enc *Encoder) error {
enc.EncodeArrayLen(5)
enc.EncodeString(o.Op)
enc.EncodeInt(o.Field)
Expand Down
14 changes: 6 additions & 8 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"sync"
"sync/atomic"
"time"

"gopkg.in/vmihailenco/msgpack.v2"
)

const requestsMap = 128
Expand Down Expand Up @@ -132,7 +130,7 @@ type Connection struct {
rlimit chan struct{}
opts Opts
state uint32
dec *msgpack.Decoder
dec *Decoder
lenbuf [PacketLengthBytes]byte
}

Expand All @@ -146,7 +144,7 @@ type connShard struct {
}
bufmut sync.Mutex
buf smallWBuf
enc *msgpack.Encoder
enc *Encoder
_pad [16]uint64 //nolint: unused,structcheck
}

Expand Down Expand Up @@ -235,7 +233,7 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
Greeting: &Greeting{},
control: make(chan struct{}),
opts: opts,
dec: msgpack.NewDecoder(&smallBuf{}),
dec: newDecoder(&smallBuf{}),
}
maxprocs := uint32(runtime.GOMAXPROCS(-1))
if conn.opts.Concurrency == 0 || conn.opts.Concurrency > maxprocs*128 {
Expand Down Expand Up @@ -434,7 +432,7 @@ func (conn *Connection) writeAuthRequest(w *bufio.Writer, scramble []byte) (err
requestCode: AuthRequest,
}
var packet smallWBuf
err = request.pack(&packet, msgpack.NewEncoder(&packet), func(enc *msgpack.Encoder) error {
err = request.pack(&packet, newEncoder(&packet), func(enc *Encoder) error {
return enc.Encode(map[uint32]interface{}{
KeyUserName: conn.opts.User,
KeyTuple: []interface{}{string("chap-sha1"), string(scramble)},
Expand Down Expand Up @@ -708,7 +706,7 @@ func (conn *Connection) newFuture(requestCode int32) (fut *Future) {
return
}

func (conn *Connection) putFuture(fut *Future, body func(*msgpack.Encoder) error) {
func (conn *Connection) putFuture(fut *Future, body func(*Encoder) error) {
shardn := fut.requestId & (conn.opts.Concurrency - 1)
shard := &conn.shard[shardn]
shard.bufmut.Lock()
Expand All @@ -721,7 +719,7 @@ func (conn *Connection) putFuture(fut *Future, body func(*msgpack.Encoder) error
firstWritten := shard.buf.Len() == 0
if shard.buf.Cap() == 0 {
shard.buf.b = make([]byte, 0, 128)
shard.enc = msgpack.NewEncoder(&shard.buf)
shard.enc = newEncoder(&shard.buf)
}
blen := shard.buf.Len()
if err := fut.pack(&shard.buf, shard.enc, body); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions example_custom_unpacking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tarantool_test
import (
"fmt"
"github.com/tarantool/go-tarantool"
"gopkg.in/vmihailenco/msgpack.v2"
"log"
"time"
)
Expand All @@ -23,7 +22,7 @@ type Tuple3 struct {
Members []Member
}

func (c *Tuple2) EncodeMsgpack(e *msgpack.Encoder) error {
func (c *Tuple2) encodeMsgpackImpl(e *tarantool.Encoder) error {
if err := e.EncodeArrayLen(3); err != nil {
return err
}
Expand All @@ -37,7 +36,7 @@ func (c *Tuple2) EncodeMsgpack(e *msgpack.Encoder) error {
return nil
}

func (c *Tuple2) DecodeMsgpack(d *msgpack.Decoder) error {
func (c *Tuple2) decodeMsgpackImpl(d *tarantool.Decoder) error {
var err error
var l int
if l, err = d.DecodeArrayLen(); err != nil {
Expand Down
98 changes: 98 additions & 0 deletions msgpack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package tarantool

import (
"io"

"gopkg.in/vmihailenco/msgpack.v2"
msgpcode "gopkg.in/vmihailenco/msgpack.v2/codes"
)

type Encoder struct {
*msgpack.Encoder
}

type Decoder struct {
*msgpack.Decoder
}

func newEncoder(w io.Writer) *Encoder {
enc := msgpack.NewEncoder(w)
return &Encoder{Encoder: enc}
}

func newDecoder(r io.Reader) *Decoder {
dec := msgpack.NewDecoder(r)
return &Decoder{Decoder: dec}
}

func msgpackIsUint(code byte) bool {
return code == msgpcode.Uint8 || code == msgpcode.Uint16 ||
code == msgpcode.Uint32 || code == msgpcode.Uint64 ||
msgpcode.IsFixedNum(code)
}

func msgpackIsMap(code byte) bool {
return code == msgpcode.Map16 || code == msgpcode.Map32 || msgpcode.IsFixedMap(code)
}

func msgpackIsArray(code byte) bool {
return code == msgpcode.Array16 || code == msgpcode.Array32 ||
msgpcode.IsFixedArray(code)
}

func msgpackIsString(code byte) bool {
return msgpcode.IsFixedString(code) || code == msgpcode.Str8 ||
code == msgpcode.Str16 || code == msgpcode.Str32
}

func (s *single) DecodeMsgpack(d *msgpack.Decoder) error {
return s.decodeMsgpackImpl(&Decoder{d})
}

func (space *Space) DecodeMsgpack(d *msgpack.Decoder) error {
return space.decodeMsgpackImpl(&Decoder{d})
}

func (field *Field) DecodeMsgpack(d *msgpack.Decoder) error {
return field.decodeMsgpackImpl(&Decoder{d})
}

func (index *Index) DecodeMsgpack(d *msgpack.Decoder) error {
return index.decodeMsgpackImpl(&Decoder{d})
}

func (indexField *IndexField) DecodeMsgpack(d *msgpack.Decoder) error {
return indexField.decodeMsgpackImpl(&Decoder{d})
}

func (meta *ColumnMetaData) DecodeMsgpack(d *msgpack.Decoder) error {
return meta.decodeMsgpackImpl(&Decoder{d})
}

func (info *SQLInfo) DecodeMsgpack(d *msgpack.Decoder) error {
return info.decodeMsgpackImpl(&Decoder{d})
}

func (k IntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
return k.encodeMsgpackImpl(&Encoder{enc})
}

func (k UintKey) EncodeMsgpack(enc *msgpack.Encoder) error {
return k.encodeMsgpackImpl(&Encoder{enc})
}

func (k StringKey) EncodeMsgpack(enc *msgpack.Encoder) error {
return k.encodeMsgpackImpl(&Encoder{enc})
}

func (k IntIntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
return k.encodeMsgpackImpl(&Encoder{enc})
}

func (o Op) EncodeMsgpack(enc *msgpack.Encoder) error {
return o.encodeMsgpackImpl(&Encoder{enc})
}

func (o OpSplice) EncodeMsgpack(enc *msgpack.Encoder) error {
return o.encodeMsgpackImpl(&Encoder{enc})
}
22 changes: 22 additions & 0 deletions msgpack_helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tarantool_test

import (
. "github.com/tarantool/go-tarantool"
"gopkg.in/vmihailenco/msgpack.v2"
)

func (m *Member) EncodeMsgpack(e *msgpack.Encoder) error {
return m.encodeMsgpackImpl(&Encoder{e})
}

func (m *Member) DecodeMsgpack(d *msgpack.Decoder) error {
return m.decodeMsgpackImpl(&Decoder{d})
}

func (c *Tuple2) EncodeMsgpack(e *msgpack.Encoder) error {
return c.encodeMsgpackImpl(&Encoder{e})
}

func (c *Tuple2) DecodeMsgpack(d *msgpack.Decoder) error {
return c.decodeMsgpackImpl(&Decoder{d})
}
5 changes: 2 additions & 3 deletions queue/example_msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ import (

"github.com/tarantool/go-tarantool"
"github.com/tarantool/go-tarantool/queue"
"gopkg.in/vmihailenco/msgpack.v2"
"log"
)

type dummyData struct {
Dummy bool
}

func (c *dummyData) DecodeMsgpack(d *msgpack.Decoder) error {
func (c *dummyData) decodeMsgpackImpl(d *tarantool.Decoder) error {
var err error
if c.Dummy, err = d.DecodeBool(); err != nil {
return err
}
return nil
}

func (c *dummyData) EncodeMsgpack(e *msgpack.Encoder) error {
func (c *dummyData) encodeMsgpackImpl(e *tarantool.Encoder) error {
return e.EncodeBool(c.Dummy)
}

Expand Down
14 changes: 14 additions & 0 deletions queue/msgpack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package queue

import (
"github.com/tarantool/go-tarantool"
"gopkg.in/vmihailenco/msgpack.v2"
)

func (qd *queueData) DecodeMsgpack(d *msgpack.Decoder) error {
return qd.decodeMsgpackImpl(&tarantool.Decoder{Decoder: d})
}

func (t *Task) DecodeMsgpack(d *msgpack.Decoder) error {
return t.decodeMsgpackImpl(&tarantool.Decoder{Decoder: d})
}
22 changes: 22 additions & 0 deletions queue/msgpack_helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package queue_test

import (
. "github.com/tarantool/go-tarantool"
"gopkg.in/vmihailenco/msgpack.v2"
)

func (c *customData) DecodeMsgpack(d *msgpack.Decoder) error {
return c.decodeMsgpackImpl(&Decoder{Decoder: d})
}

func (c *customData) EncodeMsgpack(e *msgpack.Encoder) error {
return c.encodeMsgpackImpl(&Encoder{Encoder: e})
}

func (c *dummyData) DecodeMsgpack(d *msgpack.Decoder) error {
return c.decodeMsgpackImpl(&Decoder{Decoder: d})
}

func (c *dummyData) EncodeMsgpack(e *msgpack.Encoder) error {
return c.encodeMsgpackImpl(&Encoder{Encoder: e})
}
3 changes: 1 addition & 2 deletions queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/tarantool/go-tarantool"
msgpack "gopkg.in/vmihailenco/msgpack.v2"
)

// Queue is a handle to Tarantool queue's tube.
Expand Down Expand Up @@ -339,7 +338,7 @@ type queueData struct {
result interface{}
}

func (qd *queueData) DecodeMsgpack(d *msgpack.Decoder) error {
func (qd *queueData) decodeMsgpackImpl(d *tarantool.Decoder) error {
var err error
var l int
if l, err = d.DecodeArrayLen(); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
. "github.com/tarantool/go-tarantool"
"github.com/tarantool/go-tarantool/queue"
"github.com/tarantool/go-tarantool/test_helpers"
"gopkg.in/vmihailenco/msgpack.v2"
)

var server = "127.0.0.1:3013"
Expand Down Expand Up @@ -215,7 +214,7 @@ type customData struct {
customField string
}

func (c *customData) DecodeMsgpack(d *msgpack.Decoder) error {
func (c *customData) decodeMsgpackImpl(d *Decoder) error {
var err error
var l int
if l, err = d.DecodeArrayLen(); err != nil {
Expand All @@ -230,7 +229,7 @@ func (c *customData) DecodeMsgpack(d *msgpack.Decoder) error {
return nil
}

func (c *customData) EncodeMsgpack(e *msgpack.Encoder) error {
func (c *customData) encodeMsgpackImpl(e *Encoder) error {
if err := e.EncodeArrayLen(1); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions queue/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package queue
import (
"fmt"

msgpack "gopkg.in/vmihailenco/msgpack.v2"
"github.com/tarantool/go-tarantool"
)

// Task represents a task from Tarantool queue's tube.
Expand All @@ -14,7 +14,7 @@ type Task struct {
q *queue
}

func (t *Task) DecodeMsgpack(d *msgpack.Decoder) error {
func (t *Task) decodeMsgpackImpl(d *tarantool.Decoder) error {
var err error
var l int
if l, err = d.DecodeArrayLen(); err != nil {
Expand Down
Loading

0 comments on commit 6038440

Please sign in to comment.