Skip to content

Commit 6e3c3fe

Browse files
committed
simplify
1 parent 5fd4a67 commit 6e3c3fe

File tree

3 files changed

+20
-59
lines changed

3 files changed

+20
-59
lines changed

server/handler.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -933,15 +933,10 @@ func updateMaxUsedConnectionsStatusVariable() {
933933

934934
func toSqlHelper(ctx *sql.Context, typ sql.Type, buf *sql.ByteBuffer, val interface{}) (sqltypes.Value, error) {
935935
if buf == nil {
936-
return typ.SQL(ctx, buf.Get(), val)
936+
return typ.SQL(ctx, nil, val)
937937
}
938-
spare := buf.Spare()
939938
ret, err := typ.SQL(ctx, buf.Get(), val)
940-
if ret.Len() > spare {
941-
buf.Double()
942-
} else {
943-
buf.Advance(ret.Len())
944-
}
939+
buf.Update(ret.Raw())
945940
return ret, err
946941
}
947942

server/handler_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package server
1616

1717
import (
18+
"bytes"
1819
"context"
1920
"fmt"
2021
"io"
@@ -799,27 +800,29 @@ func TestHandlerKillQuery(t *testing.T) {
799800
}()
800801

801802
time.Sleep(100 * time.Millisecond)
802-
var sleepQueryID string
803+
var sleepQueryID []byte
803804
err = handler.ComQuery(context.Background(), conn2, "SHOW PROCESSLIST", func(res *sqltypes.Result, more bool) error {
804805
// 1, , , test, Query, 0, ... , SELECT SLEEP(1000)
805806
// 2, , , test, Query, 0, running, SHOW PROCESSLIST
806807
require.Equal(2, len(res.Rows))
807808
hasSleepQuery := false
809+
fmt.Println(res.Rows[0][0].ToString(), res.Rows[0][7].ToString())
810+
fmt.Println(res.Rows[1][0].ToString(), res.Rows[1][7].ToString())
808811
for _, row := range res.Rows {
809-
if row[7].ToString() != sleepQuery {
812+
if !bytes.Equal(row[7].Raw(), []byte(sleepQuery)) {
810813
continue
811814
}
812815
hasSleepQuery = true
813-
sleepQueryID = row[0].ToString()
814-
require.Equal("Query", row[4].ToString())
816+
sleepQueryID = row[0].Raw()
817+
require.Equal([]byte("Query"), row[4].Raw())
815818
}
816819
require.True(hasSleepQuery)
817820
return nil
818821
})
819822
require.NoError(err)
820823

821824
time.Sleep(100 * time.Millisecond)
822-
err = handler.ComQuery(context.Background(), conn2, "KILL QUERY "+sleepQueryID, func(res *sqltypes.Result, more bool) error {
825+
err = handler.ComQuery(context.Background(), conn2, "KILL QUERY "+string(sleepQueryID), func(res *sqltypes.Result, more bool) error {
823826
return nil
824827
})
825828
require.NoError(err)
@@ -832,12 +835,12 @@ func TestHandlerKillQuery(t *testing.T) {
832835
require.Equal(2, len(res.Rows))
833836
hasSleepQueryID := false
834837
for _, row := range res.Rows {
835-
if row[0].ToString() != sleepQueryID {
838+
if !bytes.Equal(row[0].ToBytes(), sleepQueryID) {
836839
continue
837840
}
838841
hasSleepQueryID = true
839-
require.Equal("Sleep", row[4].ToString())
840-
require.Equal("", row[7].ToString())
842+
require.Equal([]byte("Sleep"), row[4].ToBytes())
843+
require.Equal([]byte{}, row[7].ToBytes())
841844
}
842845
require.True(hasSleepQueryID)
843846
return nil

sql/byte_buffer.go

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,32 @@ import (
44
"sync"
55
)
66

7-
var SingletonBuf = NewByteBuffer(16000)
8-
9-
var defaultByteBuffCap = 1000
7+
const defaultByteBuffCap = 1024
108

119
var ByteBufPool = sync.Pool{
1210
New: func() any {
13-
// The Pool's New function should generally only return pointer
14-
// types, since a pointer can be put into the return interface
15-
// value without an allocation:
1611
return NewByteBuffer(defaultByteBuffCap)
1712
},
1813
}
1914

2015
type ByteBuffer struct {
2116
buf []byte
22-
i int
2317
}
2418

2519
func NewByteBuffer(initCap int) *ByteBuffer {
26-
return &ByteBuffer{buf: make([]byte, initCap)}
27-
}
28-
29-
func (b *ByteBuffer) Bytes() []byte {
30-
return b.buf
20+
return &ByteBuffer{buf: make([]byte, 0, initCap)}
3121
}
3222

33-
func (b *ByteBuffer) GetFull(i int) []byte {
34-
start := b.i
35-
b.i = start + i
36-
if b.i > len(b.buf) {
37-
newBuf := make([]byte, len(b.buf)*2)
38-
copy(newBuf, b.buf[:])
39-
b.buf = newBuf
23+
func (b *ByteBuffer) Update(buf []byte) {
24+
if cap(buf) > cap(b.buf) {
25+
b.buf = buf
4026
}
41-
return b.buf[start:b.i]
42-
}
43-
44-
func (b *ByteBuffer) Double() {
45-
newBuf := make([]byte, len(b.buf)*2)
46-
copy(newBuf, b.buf[:])
47-
b.buf = newBuf
48-
}
49-
50-
func (b *ByteBuffer) Advance(i int) {
51-
b.i += i
52-
}
53-
54-
func (b *ByteBuffer) Spare() int {
55-
return len(b.buf) - b.i
5627
}
5728

5829
func (b *ByteBuffer) Get() []byte {
59-
//start := b.i
60-
//b.i = start + i
61-
//if b.i > len(b.buf) {
62-
// newBuf := make([]byte, len(b.buf)*2)
63-
// copy(newBuf, b.buf[:])
64-
// b.buf = newBuf
65-
//}
66-
//return b.buf[start:b.i][:0]
67-
return b.buf[b.i:b.i]
30+
return b.buf[len(b.buf):]
6831
}
6932

7033
func (b *ByteBuffer) Reset() {
71-
b.i = 0
34+
b.buf = b.buf[:0]
7235
}

0 commit comments

Comments
 (0)