1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
package proto
import (
"bytes"
"encoding/binary"
"io"
"math"
)
// Buffer implements ClickHouse binary protocol encoding.
type Buffer struct {
Buf []byte
}
// Reader returns new *Reader from *Buffer.
func (b *Buffer) Reader() *Reader {
return NewReader(bytes.NewReader(b.Buf))
}
// Ensure Buf length.
func (b *Buffer) Ensure(n int) {
b.Buf = append(b.Buf[:0], make([]byte, n)...)
}
// Encoder implements encoding to Buffer.
type Encoder interface {
Encode(b *Buffer)
}
// AwareEncoder implements encoding to Buffer that depends on version.
type AwareEncoder interface {
EncodeAware(b *Buffer, version int)
}
// EncodeAware value that implements AwareEncoder.
func (b *Buffer) EncodeAware(e AwareEncoder, version int) {
e.EncodeAware(b, version)
}
// Encode value that implements Encoder.
func (b *Buffer) Encode(e Encoder) {
e.Encode(b)
}
// Reset buffer to zero length.
func (b *Buffer) Reset() {
b.Buf = b.Buf[:0]
}
// Read implements io.Reader.
func (b *Buffer) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
if len(b.Buf) == 0 {
return 0, io.EOF
}
n = copy(p, b.Buf)
b.Buf = b.Buf[n:]
return n, nil
}
// PutRaw writes v as raw bytes to buffer.
func (b *Buffer) PutRaw(v []byte) {
b.Buf = append(b.Buf, v...)
}
// PutUVarInt encodes x as uvarint.
func (b *Buffer) PutUVarInt(x uint64) {
buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutUvarint(buf, x)
b.Buf = append(b.Buf, buf[:n]...)
}
// PutInt encodes integer as uvarint.
func (b *Buffer) PutInt(x int) {
b.PutUVarInt(uint64(x))
}
// PutByte encodes byte as uint8.
func (b *Buffer) PutByte(x byte) {
b.PutUInt8(x)
}
// PutLen encodes length to buffer as uvarint.
func (b *Buffer) PutLen(x int) {
b.PutUVarInt(uint64(x))
}
// PutString encodes sting value to buffer.
func (b *Buffer) PutString(s string) {
b.PutLen(len(s))
b.Buf = append(b.Buf, s...)
}
func (b *Buffer) PutUInt8(x uint8) {
b.Buf = append(b.Buf, x)
}
func (b *Buffer) PutUInt16(x uint16) {
buf := make([]byte, 16/8)
binary.LittleEndian.PutUint16(buf, x)
b.Buf = append(b.Buf, buf...)
}
func (b *Buffer) PutUInt32(x uint32) {
buf := make([]byte, 32/8)
binary.LittleEndian.PutUint32(buf, x)
b.Buf = append(b.Buf, buf...)
}
func (b *Buffer) PutUInt64(x uint64) {
buf := make([]byte, 64/8)
binary.LittleEndian.PutUint64(buf, x)
b.Buf = append(b.Buf, buf...)
}
func (b *Buffer) PutUInt128(x UInt128) {
buf := make([]byte, 128/8)
binPutUInt128(buf, x)
b.Buf = append(b.Buf, buf...)
}
func (b *Buffer) PutInt8(v int8) {
b.PutUInt8(uint8(v))
}
func (b *Buffer) PutInt16(v int16) {
b.PutUInt16(uint16(v))
}
func (b *Buffer) PutInt32(x int32) {
b.PutUInt32(uint32(x))
}
func (b *Buffer) PutInt64(x int64) {
b.PutUInt64(uint64(x))
}
func (b *Buffer) PutInt128(x Int128) {
b.PutUInt128(UInt128(x))
}
func (b *Buffer) PutBool(v bool) {
if v {
b.PutUInt8(boolTrue)
} else {
b.PutUInt8(boolFalse)
}
}
func (b *Buffer) PutFloat64(v float64) {
b.PutUInt64(math.Float64bits(v))
}
func (b *Buffer) PutFloat32(v float32) {
b.PutUInt32(math.Float32bits(v))
}
|