From 08c755251564c8170c9e7e58d7960202cc9ecd10 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Tue, 20 Nov 2018 21:28:43 +0100 Subject: [PATCH] Move buffer package --- binlog/event_format_description.go | 4 ++-- binlog/event_header.go | 4 ++-- binlog/event_query.go | 4 ++-- binlog/event_rotate.go | 4 ++-- binlog/event_rows.go | 10 +++++----- binlog/event_table_map.go | 4 ++-- {tools => buffer}/buffer.go | 8 ++++---- mysql/slave/slave_conn.go | 6 +++--- 8 files changed, 22 insertions(+), 22 deletions(-) rename {tools => buffer}/buffer.go (96%) diff --git a/binlog/event_format_description.go b/binlog/event_format_description.go index 0a48ea2..84c9147 100644 --- a/binlog/event_format_description.go +++ b/binlog/event_format_description.go @@ -5,7 +5,7 @@ import ( "strconv" "strings" - "github.com/localhots/bocadillo/tools" + "github.com/localhots/bocadillo/buffer" ) // FormatDescription is a description of binary log format. @@ -52,7 +52,7 @@ const ( // Decode decodes given buffer into a format description event. // Spec: https://dev.mysql.com/doc/internals/en/format-description-event.html func (e *FormatDescriptionEvent) Decode(data []byte) error { - buf := tools.NewBuffer(data) + buf := buffer.New(data) e.Version = buf.ReadUint16() e.ServerVersion = trimStringEOF(buf.ReadStringVarLen(50)) e.CreateTimestamp = buf.ReadUint32() diff --git a/binlog/event_header.go b/binlog/event_header.go index 05f6436..a8817ef 100644 --- a/binlog/event_header.go +++ b/binlog/event_header.go @@ -3,7 +3,7 @@ package binlog import ( "errors" - "github.com/localhots/bocadillo/tools" + "github.com/localhots/bocadillo/buffer" ) var ( @@ -30,7 +30,7 @@ func (h *EventHeader) Decode(connBuff []byte, fd FormatDescription) error { return ErrInvalidHeader } - buf := tools.NewBuffer(connBuff) + buf := buffer.New(connBuff) h.Timestamp = buf.ReadUint32() h.Type = EventType(buf.ReadUint8()) h.ServerID = buf.ReadUint32() diff --git a/binlog/event_query.go b/binlog/event_query.go index 9dc2c6a..916b160 100644 --- a/binlog/event_query.go +++ b/binlog/event_query.go @@ -1,7 +1,7 @@ package binlog import ( - "github.com/localhots/bocadillo/tools" + "github.com/localhots/bocadillo/buffer" ) // QueryEvent contains query details. @@ -17,7 +17,7 @@ type QueryEvent struct { // Decode given buffer into a qeury event. // Spec: https://dev.mysql.com/doc/internals/en/query-event.html func (e *QueryEvent) Decode(connBuff []byte) { - buf := tools.NewBuffer(connBuff) + buf := buffer.New(connBuff) e.SlaveProxyID = buf.ReadUint32() e.ExecutionTime = buf.ReadUint32() diff --git a/binlog/event_rotate.go b/binlog/event_rotate.go index 19879b2..06a25ba 100644 --- a/binlog/event_rotate.go +++ b/binlog/event_rotate.go @@ -1,6 +1,6 @@ package binlog -import "github.com/localhots/bocadillo/tools" +import "github.com/localhots/bocadillo/buffer" // Position is a pair of log file name and a binary offset in it that is used to // represent the beginning of the event description. @@ -18,7 +18,7 @@ type RotateEvent struct { // Decode decodes given buffer into a rotate event. // Spec: https://dev.mysql.com/doc/internals/en/rotate-event.html func (e *RotateEvent) Decode(connBuff []byte, fd FormatDescription) error { - buf := tools.NewBuffer(connBuff) + buf := buffer.New(connBuff) if fd.Version > 1 { e.NextFile.Offset = buf.ReadUint64() } else { diff --git a/binlog/event_rows.go b/binlog/event_rows.go index e255061..d69f9c3 100644 --- a/binlog/event_rows.go +++ b/binlog/event_rows.go @@ -6,8 +6,8 @@ import ( "fmt" "runtime/debug" + "github.com/localhots/bocadillo/buffer" "github.com/localhots/bocadillo/mysql" - "github.com/localhots/bocadillo/tools" ) // RowsEvent contains a Rows Event. @@ -58,7 +58,7 @@ func (e *RowsEvent) Decode(connBuff []byte, fd FormatDescription, td TableDescri } }() - buf := tools.NewBuffer(connBuff) + buf := buffer.New(connBuff) idSize := fd.TableIDSize(e.Type) if idSize == 6 { e.TableID = buf.ReadUint48() @@ -103,7 +103,7 @@ func (e *RowsEvent) Decode(connBuff []byte, fd FormatDescription, td TableDescri return nil } -func (e *RowsEvent) decodeRows(buf *tools.Buffer, td TableDescription, bm []byte) ([]interface{}, error) { +func (e *RowsEvent) decodeRows(buf *buffer.Buffer, td TableDescription, bm []byte) ([]interface{}, error) { count := 0 for i := 0; i < int(e.ColumnCount); i++ { if isBitSet(bm, i) { @@ -132,7 +132,7 @@ func (e *RowsEvent) decodeRows(buf *tools.Buffer, td TableDescription, bm []byte return row, nil } -func (e *RowsEvent) decodeValue(buf *tools.Buffer, ct mysql.ColumnType, meta uint16) interface{} { +func (e *RowsEvent) decodeValue(buf *buffer.Buffer, ct mysql.ColumnType, meta uint16) interface{} { var length int if ct == mysql.ColumnTypeString { if meta > 0xFF { @@ -251,7 +251,7 @@ func (e *RowsEvent) decodeValue(buf *tools.Buffer, ct mysql.ColumnType, meta uin } } -func readString(buf *tools.Buffer, length int) string { +func readString(buf *buffer.Buffer, length int) string { // Length is encoded in 1 byte if length < 256 { return string(buf.ReadStringVarEnc(1)) diff --git a/binlog/event_table_map.go b/binlog/event_table_map.go index d2baa36..749f1d8 100644 --- a/binlog/event_table_map.go +++ b/binlog/event_table_map.go @@ -1,8 +1,8 @@ package binlog import ( + "github.com/localhots/bocadillo/buffer" "github.com/localhots/bocadillo/mysql" - "github.com/localhots/bocadillo/tools" ) // TableDescription contains table details required to process rows events. @@ -26,7 +26,7 @@ type TableMapEvent struct { // Decode decodes given buffer into a table map event. // Spec: https://dev.mysql.com/doc/internals/en/table-map-event.html func (e *TableMapEvent) Decode(connBuff []byte, fd FormatDescription) error { - buf := tools.NewBuffer(connBuff) + buf := buffer.New(connBuff) idSize := fd.TableIDSize(EventTypeTableMap) if idSize == 6 { e.TableID = buf.ReadUint48() diff --git a/tools/buffer.go b/buffer/buffer.go similarity index 96% rename from tools/buffer.go rename to buffer/buffer.go index 5798220..dd1676b 100644 --- a/tools/buffer.go +++ b/buffer/buffer.go @@ -1,4 +1,4 @@ -package tools +package buffer import ( "encoding/binary" @@ -13,9 +13,9 @@ type Buffer struct { pos int } -// NewBuffer creates a new buffer from a given slice of bytes and sets the -// cursor to the beginning. -func NewBuffer(data []byte) *Buffer { +// New creates a new buffer from a given slice of bytes and sets the cursor to +// the beginning. +func New(data []byte) *Buffer { return &Buffer{data: data} } diff --git a/mysql/slave/slave_conn.go b/mysql/slave/slave_conn.go index 28e5a79..fbbaac0 100644 --- a/mysql/slave/slave_conn.go +++ b/mysql/slave/slave_conn.go @@ -6,8 +6,8 @@ import ( "io" "os" + "github.com/localhots/bocadillo/buffer" "github.com/localhots/bocadillo/mysql/driver" - "github.com/localhots/bocadillo/tools" ) // Conn is a slave connection used to issue a binlog dump command. @@ -83,7 +83,7 @@ func (c *Conn) ReadPacket(ctx context.Context) ([]byte, error) { func (c *Conn) RegisterSlave() error { c.conn.ResetSequence() - buf := tools.NewCommandBuffer(1 + 4 + 1 + len(c.conf.Hostname) + 1 + 1 + 2 + 4 + 4) + buf := buffer.NewCommandBuffer(1 + 4 + 1 + len(c.conf.Hostname) + 1 + 1 + 2 + 4 + 4) buf.WriteByte(comRegisterSlave) buf.WriteUint32(c.conf.ServerID) buf.WriteStringLenEnc(c.conf.Hostname) @@ -105,7 +105,7 @@ func (c *Conn) RegisterSlave() error { func (c *Conn) StartBinlogDump() error { c.conn.ResetSequence() - buf := tools.NewCommandBuffer(1 + 4 + 2 + 4 + len(c.conf.File)) + buf := buffer.NewCommandBuffer(1 + 4 + 2 + 4 + len(c.conf.File)) buf.WriteByte(comBinlogDump) buf.WriteUint32(uint32(c.conf.Offset)) buf.Skip(2) // Flags