-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxencoder.go
201 lines (169 loc) · 4.07 KB
/
txencoder.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package bucket
import (
"encoding/binary"
"io"
"math/bits"
"sync"
)
// startTx starts encoding a transaction.
func startTx(w io.Writer) txEncoder {
buf := txBufPool.Get().(*txbuf)
return txEncoder{
w: w,
buf: buf,
}
}
type txEncoder struct {
// w is the destination to write the transaction.
w io.Writer
// n is the number of bytes written to w.
n int64
// buffered is the number of bytes currently stored in the buffer.
buffered uint
// buf is a buffer used to hold bytes before writing them.
buf *txbuf
}
// writePair encodes a raw pair update.
// An empty value is equivalent to a deletion.
func (e *txEncoder) writePair(key, value string) error {
switch {
case key == "":
// An empty key is not representable.
return errEmptyKey
case len(key) >= 1<<30:
// The key is too large to encode.
return errKeyTooLarge
case len(value) >= 1<<30:
// The value is too large to encode.
return errValueTooLarge
}
// Encode the lengths.
err := e.writeLengths(uint32(len(key)), uint32(len(value)))
if err != nil {
return err
}
// Encode the key.
err = e.writeString(key)
if err != nil {
return err
}
// Encode the value.
err = e.writeString(value)
if err != nil {
return err
}
return nil
}
// writeString encodes raw string data.
func (e *txEncoder) writeString(str string) error {
var notStringWriter bool
for {
if len(str) > len(e.buf) && e.buffered == 0 && !notStringWriter {
if sw, ok := e.w.(io.StringWriter); ok {
// Write the string directly.
n, err := sw.WriteString(str)
e.n += int64(n)
if err != nil {
return err
}
str = str[n:]
continue
} else {
// Set this flag to avoid repeated useless type assertions.
notStringWriter = true
}
}
// Copy (part) of the string into the buffer.
n := copy(e.buf[e.buffered:], str)
e.buffered += uint(n)
str = str[n:]
if len(str) == 0 {
// The string has been copied completely into the buffer.
break
}
// Flush the buffer to make space for more string data.
err := e.flush()
if err != nil {
return err
}
}
return nil
}
// writeLengths encodes lengths for a key-value pair.
func (e *txEncoder) writeLengths(keyLen, valueLen uint32) error {
// Carve out a chunk of buffer to store the lengths.
if e.buffered > uint(len(e.buf))-8 {
err := e.flush()
if err != nil {
return err
}
}
lenBuf := e.buf[e.buffered:][:8]
// Encode the key.
keyLenWidth := (bits.Len32(keyLen) + 1) / 8
binary.LittleEndian.PutUint32(lenBuf[:], (keyLen<<2)|uint32(keyLenWidth))
// Encode the value.
valueLenWidth := (bits.Len32(valueLen) + 1) / 8
binary.LittleEndian.PutUint32(lenBuf[keyLenWidth+1:][:4], (valueLen<<2)|uint32(valueLenWidth))
// Update the buffer state.
e.buffered += uint(keyLenWidth) + uint(valueLenWidth) + 2
return nil
}
// finish terminates the transaction by encoding a transaction commit marker and flushing the data.
// The txEncoder must not be used after this call returns.
func (e *txEncoder) finish() (int64, error) {
err := e.doFinish()
n := e.abort()
return n, err
}
func (e *txEncoder) doFinish() error {
if e.n == 0 && e.buffered == 0 {
// This is a no-op transaction.
// There is nothing to do.
return nil
}
// Encode a deletion marker.
if int(e.buffered) >= len(e.buf) {
// Flush the buffer to make room for the deletion marker.
err := e.flush()
if err != nil {
return err
}
}
e.buf[e.buffered] = 0
e.buffered++
// Flush the buffer.
return e.flush()
}
// abort the transaction.
// The txEncoder must not be used after this call returns.
func (e *txEncoder) abort() int64 {
e.w = nil
txBufPool.Put(e.buf)
return e.n
}
func (e *txEncoder) flush() error {
if e.buffered == 0 {
// There is nothing to flush.
return nil
}
// Write all buffered data.
n, err := e.w.Write(e.buf[:e.buffered])
e.n += int64(n)
if err != nil {
e.w = nil
return err
}
if n != int(e.buffered) {
panic("short write without error")
}
// There is no remaining buffered data.
e.buffered = 0
return nil
}
type txbuf = [1 << 16]byte
var txBufPool = sync.Pool{
New: func() interface{} {
return &txbuf{}
},
}