2
0

Refactor pgio and types to append buffers

This commit is contained in:
Jack Christensen
2017-05-02 20:38:26 -05:00
parent 579b6cd612
commit d4fe3edf84
4 changed files with 180 additions and 166 deletions
+21 -85
View File
@@ -2,103 +2,39 @@ package pgio
import ( import (
"encoding/binary" "encoding/binary"
"io"
) )
type Uint16Reader interface { func NextByte(buf []byte) ([]byte, byte) {
ReadUint16() (n uint16, err error) b := buf[0]
return buf[1:], b
} }
type Uint32Reader interface { func NextUint16(buf []byte) ([]byte, uint16) {
ReadUint32() (n uint32, err error) n := binary.BigEndian.Uint16(buf)
return buf[2:], n
} }
type Uint64Reader interface { func NextUint32(buf []byte) ([]byte, uint32) {
ReadUint64() (n uint64, err error) n := binary.BigEndian.Uint32(buf)
return buf[4:], n
} }
// ReadByte reads a byte from r. func NextUint64(buf []byte) ([]byte, uint64) {
func ReadByte(r io.Reader) (byte, error) { n := binary.BigEndian.Uint64(buf)
if r, ok := r.(io.ByteReader); ok { return buf[8:], n
return r.ReadByte()
}
buf := make([]byte, 1)
_, err := r.Read(buf)
return buf[0], err
} }
// ReadUint16 reads an uint16 from r in PostgreSQL wire format (network byte order). This func NextInt16(buf []byte) ([]byte, int16) {
// may be more efficient than directly using Read if r provides a ReadUint16 buf, n := NextUint16(buf)
// method. return buf, int16(n)
func ReadUint16(r io.Reader) (uint16, error) {
if r, ok := r.(Uint16Reader); ok {
return r.ReadUint16()
}
buf := make([]byte, 2)
_, err := io.ReadFull(r, buf)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint16(buf), nil
} }
// ReadInt16 reads an int16 r in PostgreSQL wire format (network byte order). This func NextInt32(buf []byte) ([]byte, int32) {
// may be more efficient than directly using Read if r provides a ReadUint16 buf, n := NextUint32(buf)
// method. return buf, int32(n)
func ReadInt16(r io.Reader) (int16, error) {
n, err := ReadUint16(r)
return int16(n), err
} }
// ReadUint32 reads an uint32 r in PostgreSQL wire format (network byte order). This func NextInt64(buf []byte) ([]byte, int64) {
// may be more efficient than directly using Read if r provides a ReadUint32 buf, n := NextUint64(buf)
// method. return buf, int64(n)
func ReadUint32(r io.Reader) (uint32, error) {
if r, ok := r.(Uint32Reader); ok {
return r.ReadUint32()
}
buf := make([]byte, 4)
_, err := io.ReadFull(r, buf)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint32(buf), nil
}
// ReadInt32 reads an int32 r in PostgreSQL wire format (network byte order). This
// may be more efficient than directly using Read if r provides a ReadUint32
// method.
func ReadInt32(r io.Reader) (int32, error) {
n, err := ReadUint32(r)
return int32(n), err
}
// ReadUint64 reads an uint64 r in PostgreSQL wire format (network byte order). This
// may be more efficient than directly using Read if r provides a ReadUint64
// method.
func ReadUint64(r io.Reader) (uint64, error) {
if r, ok := r.(Uint64Reader); ok {
return r.ReadUint64()
}
buf := make([]byte, 8)
_, err := io.ReadFull(r, buf)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint64(buf), nil
}
// ReadInt64 reads an int64 r in PostgreSQL wire format (network byte order). This
// may be more efficient than directly using Read if r provides a ReadUint64
// method.
func ReadInt64(r io.Reader) (int64, error) {
n, err := ReadUint64(r)
return int64(n), err
} }
+57
View File
@@ -0,0 +1,57 @@
package pgio
import (
"testing"
)
func TestNextByte(t *testing.T) {
buf := []byte{42, 1}
var b byte
buf, b = NextByte(buf)
if b != 42 {
t.Errorf("NextByte(buf) => %v, want %v", b, 42)
}
buf, b = NextByte(buf)
if b != 1 {
t.Errorf("NextByte(buf) => %v, want %v", b, 1)
}
}
func TestNextUint16(t *testing.T) {
buf := []byte{0, 42, 0, 1}
var n uint16
buf, n = NextUint16(buf)
if n != 42 {
t.Errorf("NextUint16(buf) => %v, want %v", n, 42)
}
buf, n = NextUint16(buf)
if n != 1 {
t.Errorf("NextUint16(buf) => %v, want %v", n, 1)
}
}
func TestNextUint32(t *testing.T) {
buf := []byte{0, 0, 0, 42, 0, 0, 0, 1}
var n uint32
buf, n = NextUint32(buf)
if n != 42 {
t.Errorf("NextUint32(buf) => %v, want %v", n, 42)
}
buf, n = NextUint32(buf)
if n != 1 {
t.Errorf("NextUint32(buf) => %v, want %v", n, 1)
}
}
func TestNextUint64(t *testing.T) {
buf := []byte{0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 1}
var n uint64
buf, n = NextUint64(buf)
if n != 42 {
t.Errorf("NextUint64(buf) => %v, want %v", n, 42)
}
buf, n = NextUint64(buf)
if n != 1 {
t.Errorf("NextUint64(buf) => %v, want %v", n, 1)
}
}
+24 -81
View File
@@ -1,97 +1,40 @@
package pgio package pgio
import ( import "encoding/binary"
"encoding/binary"
"io"
)
type Uint16Writer interface { func AppendUint16(buf []byte, n uint16) []byte {
WriteUint16(uint16) (n int, err error) wp := len(buf)
buf = append(buf, 0, 0)
binary.BigEndian.PutUint16(buf[wp:], n)
return buf
} }
type Uint32Writer interface { func AppendUint32(buf []byte, n uint32) []byte {
WriteUint32(uint32) (n int, err error) wp := len(buf)
buf = append(buf, 0, 0, 0, 0)
binary.BigEndian.PutUint32(buf[wp:], n)
return buf
} }
type Uint64Writer interface { func AppendUint64(buf []byte, n uint64) []byte {
WriteUint64(uint64) (n int, err error) wp := len(buf)
buf = append(buf, 0, 0, 0, 0, 0, 0, 0, 0)
binary.BigEndian.PutUint64(buf[wp:], n)
return buf
} }
// WriteByte writes b to w. func AppendInt16(buf []byte, n int16) []byte {
func WriteByte(w io.Writer, b byte) error { return AppendUint16(buf, uint16(n))
if w, ok := w.(io.ByteWriter); ok {
return w.WriteByte(b)
}
_, err := w.Write([]byte{b})
return err
} }
// WriteUint16 writes n to w in PostgreSQL wire format (network byte order). This func AppendInt32(buf []byte, n int32) []byte {
// may be more efficient than directly using Write if w provides a WriteUint16 return AppendUint32(buf, uint32(n))
// method.
func WriteUint16(w io.Writer, n uint16) (int, error) {
if w, ok := w.(Uint16Writer); ok {
return w.WriteUint16(n)
}
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, n)
return w.Write(b)
} }
// WriteInt16 writes n to w in PostgreSQL wire format (network byte order). This func AppendInt64(buf []byte, n int64) []byte {
// may be more efficient than directly using Write if w provides a WriteUint16 return AppendUint64(buf, uint64(n))
// method.
func WriteInt16(w io.Writer, n int16) (int, error) {
return WriteUint16(w, uint16(n))
} }
// WriteUint32 writes n to w in PostgreSQL wire format (network byte order). This func SetInt32(buf []byte, n int32) {
// may be more efficient than directly using Write if w provides a WriteUint32 binary.BigEndian.PutUint32(buf, uint32(n))
// method.
func WriteUint32(w io.Writer, n uint32) (int, error) {
if w, ok := w.(Uint32Writer); ok {
return w.WriteUint32(n)
}
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, n)
return w.Write(b)
}
// WriteInt32 writes n to w in PostgreSQL wire format (network byte order). This
// may be more efficient than directly using Write if w provides a WriteUint32
// method.
func WriteInt32(w io.Writer, n int32) (int, error) {
return WriteUint32(w, uint32(n))
}
// WriteUint64 writes n to w in PostgreSQL wire format (network byte order). This
// may be more efficient than directly using Write if w provides a WriteUint64
// method.
func WriteUint64(w io.Writer, n uint64) (int, error) {
if w, ok := w.(Uint64Writer); ok {
return w.WriteUint64(n)
}
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, n)
return w.Write(b)
}
// WriteInt64 writes n to w in PostgreSQL wire format (network byte order). This
// may be more efficient than directly using Write if w provides a WriteUint64
// method.
func WriteInt64(w io.Writer, n int64) (int, error) {
return WriteUint64(w, uint64(n))
}
// WriteCString writes s to w followed by a null byte.
func WriteCString(w io.Writer, s string) (int, error) {
n, err := io.WriteString(w, s)
if err != nil {
return n, err
}
err = WriteByte(w, 0)
if err != nil {
return n, err
}
return n + 1, nil
} }
+78
View File
@@ -0,0 +1,78 @@
package pgio
import (
"reflect"
"testing"
)
func TestAppendUint16NilBuf(t *testing.T) {
buf := AppendUint16(nil, 1)
if !reflect.DeepEqual(buf, []byte{0, 1}) {
t.Errorf("AppendUint16(nil, 1) => %v, want %v", buf, []byte{0, 1})
}
}
func TestAppendUint16EmptyBuf(t *testing.T) {
buf := []byte{}
buf = AppendUint16(buf, 1)
if !reflect.DeepEqual(buf, []byte{0, 1}) {
t.Errorf("AppendUint16(nil, 1) => %v, want %v", buf, []byte{0, 1})
}
}
func TestAppendUint16BufWithCapacityDoesNotAllocate(t *testing.T) {
buf := make([]byte, 0, 4)
AppendUint16(buf, 1)
buf = buf[0:2]
if !reflect.DeepEqual(buf, []byte{0, 1}) {
t.Errorf("AppendUint16(nil, 1) => %v, want %v", buf, []byte{0, 1})
}
}
func TestAppendUint32NilBuf(t *testing.T) {
buf := AppendUint32(nil, 1)
if !reflect.DeepEqual(buf, []byte{0, 0, 0, 1}) {
t.Errorf("AppendUint32(nil, 1) => %v, want %v", buf, []byte{0, 0, 0, 1})
}
}
func TestAppendUint32EmptyBuf(t *testing.T) {
buf := []byte{}
buf = AppendUint32(buf, 1)
if !reflect.DeepEqual(buf, []byte{0, 0, 0, 1}) {
t.Errorf("AppendUint32(nil, 1) => %v, want %v", buf, []byte{0, 0, 0, 1})
}
}
func TestAppendUint32BufWithCapacityDoesNotAllocate(t *testing.T) {
buf := make([]byte, 0, 4)
AppendUint32(buf, 1)
buf = buf[0:4]
if !reflect.DeepEqual(buf, []byte{0, 0, 0, 1}) {
t.Errorf("AppendUint32(nil, 1) => %v, want %v", buf, []byte{0, 0, 0, 1})
}
}
func TestAppendUint64NilBuf(t *testing.T) {
buf := AppendUint64(nil, 1)
if !reflect.DeepEqual(buf, []byte{0, 0, 0, 0, 0, 0, 0, 1}) {
t.Errorf("AppendUint64(nil, 1) => %v, want %v", buf, []byte{0, 0, 0, 0, 0, 0, 0, 1})
}
}
func TestAppendUint64EmptyBuf(t *testing.T) {
buf := []byte{}
buf = AppendUint64(buf, 1)
if !reflect.DeepEqual(buf, []byte{0, 0, 0, 0, 0, 0, 0, 1}) {
t.Errorf("AppendUint64(nil, 1) => %v, want %v", buf, []byte{0, 0, 0, 0, 0, 0, 0, 1})
}
}
func TestAppendUint64BufWithCapacityDoesNotAllocate(t *testing.T) {
buf := make([]byte, 0, 8)
AppendUint64(buf, 1)
buf = buf[0:8]
if !reflect.DeepEqual(buf, []byte{0, 0, 0, 0, 0, 0, 0, 1}) {
t.Errorf("AppendUint64(nil, 1) => %v, want %v", buf, []byte{0, 0, 0, 0, 0, 0, 0, 1})
}
}