-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.go
More file actions
362 lines (293 loc) · 6.93 KB
/
Copy pathbuffer.go
File metadata and controls
362 lines (293 loc) · 6.93 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package rdb
import (
"fmt"
"io"
"os"
)
// a partial view of buffer, starting from a particular position.
type bufferView interface {
buffer
Close() error
}
type buffer interface {
Get(n int) ([]byte, error)
Pos() int
SupportsView() bool
View(pos int) (bufferView, error)
DoNotCalcCrc()
Crc() uint64
}
type memoryBackedBuffer struct {
buf []byte
len int
pos int
}
func newMemoryBackedBuffer(buf []byte) *memoryBackedBuffer {
return &memoryBackedBuffer{
buf: buf,
len: len(buf),
}
}
func (b *memoryBackedBuffer) Get(n int) ([]byte, error) {
if b.len < b.pos+n {
if b.len == b.pos {
return nil, io.EOF
}
return nil, io.ErrUnexpectedEOF
}
value := b.buf[b.pos : b.pos+n]
b.pos += n
return value, nil
}
func (b *memoryBackedBuffer) Pos() int {
return b.pos
}
func (b *memoryBackedBuffer) DoNotCalcCrc() {
// nop
}
func (b *memoryBackedBuffer) Crc() uint64 {
return 0
}
type memoryBackedBufferView struct {
buf *memoryBackedBuffer
}
func (b *memoryBackedBuffer) SupportsView() bool {
return true
}
func (b *memoryBackedBuffer) View(pos int) (bufferView, error) {
return &memoryBackedBufferView{
buf: &memoryBackedBuffer{
buf: b.buf,
len: b.len,
pos: pos,
},
}, nil
}
func (v *memoryBackedBufferView) Get(n int) ([]byte, error) {
return v.buf.Get(n)
}
func (v *memoryBackedBufferView) Pos() int {
return v.buf.Pos()
}
func (v *memoryBackedBufferView) DoNotCalcCrc() {
// nop
}
func (v *memoryBackedBufferView) Crc() uint64 {
return 0
}
func (v *memoryBackedBufferView) SupportsView() bool {
return false
}
func (v *memoryBackedBufferView) View(_ int) (bufferView, error) {
return nil, nil
}
func (v *memoryBackedBufferView) Close() error {
return nil
}
type crcCalculator struct {
crc uint64
checkedLen int
fileLen int
doNotUpdate bool
}
func (c *crcCalculator) Update(b []byte) {
if c.doNotUpdate {
return
}
limit := c.fileLen - crcLen
if c.checkedLen+len(b) > limit {
// we don't want to update crc with more than the limit we know.
// there might be more bytes after limit(i.e. crc footer),
// but updating the crc with them would result in a wrong crc.
b = b[:limit-c.checkedLen]
}
c.crc = getCRC(c.crc, b)
c.checkedLen += len(b)
}
func (c *crcCalculator) Crc() uint64 {
return c.crc
}
type fileBackedBuffer struct {
file *os.File
fileLen int
filePos int
bufCap int
buf []byte
len int
pos int
crcCalc *crcCalculator
}
func newFileBackedBuffer(file *os.File, fileLen int, bufCap int) *fileBackedBuffer {
return &fileBackedBuffer{
file: file,
fileLen: fileLen,
bufCap: bufCap,
buf: make([]byte, 0),
crcCalc: &crcCalculator{
crc: 0,
checkedLen: 0,
fileLen: fileLen,
doNotUpdate: false,
},
}
}
func (b *fileBackedBuffer) Get(n int) ([]byte, error) {
if b.fileLen < b.filePos+n {
// we use the file pos as the source of truth
if b.fileLen == b.filePos {
return nil, io.EOF
}
return nil, io.ErrUnexpectedEOF
}
if b.len < b.pos+n {
// there are enough bytes in the file, but not in the buffer.
// we need to read some more bytes into buffer. after this call
// it is guaranteed that b.pos + n <= b.len
err := b.read(n)
if err != nil {
return nil, err
}
}
value := b.buf[b.pos : b.pos+n]
b.pos += n
b.filePos += n
return value, nil
}
func (b *fileBackedBuffer) Pos() int {
return b.filePos
}
type fileBackedBufferView struct {
file *os.File
buf *fileBackedBuffer
}
func (b *fileBackedBuffer) SupportsView() bool {
return true
}
func (b *fileBackedBuffer) View(pos int) (bufferView, error) {
// reopen the same file, and seek to the current position
file, err := os.Open(b.file.Name())
if err != nil {
return nil, err
}
shouldSeek := int64(pos)
seek, err := file.Seek(shouldSeek, 0) // from the start
if err != nil {
_ = file.Close()
return nil, err
}
if seek != shouldSeek {
_ = file.Close()
return nil, fmt.Errorf("expected to seek %d, but it was %d", shouldSeek, seek)
}
buf := newFileBackedBuffer(file, b.fileLen, b.bufCap)
buf.filePos = pos
return &fileBackedBufferView{
file: file,
buf: buf,
}, nil
}
func (v *fileBackedBufferView) Get(n int) ([]byte, error) {
return v.buf.Get(n)
}
func (v *fileBackedBufferView) Pos() int {
return v.buf.Pos()
}
func (v *fileBackedBufferView) DoNotCalcCrc() {
// nop
}
func (v *fileBackedBufferView) Crc() uint64 {
return 0
}
func (v *fileBackedBufferView) SupportsView() bool {
return false
}
func (v *fileBackedBufferView) View(_ int) (bufferView, error) {
return nil, nil
}
func (v *fileBackedBufferView) Close() error {
return v.file.Close()
}
func (b *fileBackedBuffer) DoNotCalcCrc() {
b.crcCalc.doNotUpdate = true
}
// caller must guarantee that there are at least n bytes in the file starting
// at file pos, and there are not enough not-yet-read bytes in the buffer.
func (b *fileBackedBuffer) read(n int) error {
remaining := b.len - b.pos // not-yet-read bytes in the buffer
// the reason we are using a new buffer each time is that
// the downstream users of the buffer might use methods like
// unsafe.ToString() which is backed by this buffer. Using a
// single buffer and reading into it can be accomplished easily,
// but that would require us to copy bytes instead of unsafe.ToString()
// because otherwise we would break the invariant that the string
// is immutable. So, we would either have to copy downstream or
// here.
dst := make([]byte, maxInt(b.bufCap, n)) // n might be >> bufCap
copied := copy(dst, b.buf[b.pos:]) // copy remaining bytes into new buffer
if copied != remaining {
return fmt.Errorf("expected to copy %d bytes, but it was %d", remaining, copied)
}
b.buf = dst
b.len = len(dst)
b.pos = 0
readLen := minInt(b.len, b.fileLen-b.filePos) - remaining
read, err := b.file.Read(b.buf[remaining : remaining+readLen])
if err != nil {
return err
}
if read != readLen {
return fmt.Errorf("expected to read %d bytes, but it was %d", readLen, read)
}
if b.crcCalc != nil {
b.crcCalc.Update(b.buf[remaining : remaining+readLen])
}
return nil
}
func (b *fileBackedBuffer) Crc() uint64 {
return b.crcCalc.Crc()
}
func newForwardOnlyBuffer(r io.Reader) buffer {
return &forwardOnlyBuffer{
reader: r,
calcCRC: true,
crc: 0,
}
}
type forwardOnlyBuffer struct {
reader io.Reader
calcCRC bool
crc uint64
}
func (f *forwardOnlyBuffer) Get(n int) ([]byte, error) {
b := make([]byte, n)
remaining := n
for remaining > 0 {
read, err := f.reader.Read(b[n-remaining:])
if err != nil {
return nil, err
}
if read == 0 {
return nil, io.ErrUnexpectedEOF
}
remaining -= read
}
if f.calcCRC {
f.crc = getCRC(f.crc, b)
}
return b, nil
}
func (f *forwardOnlyBuffer) Pos() int {
return 0
}
func (f *forwardOnlyBuffer) DoNotCalcCrc() {
f.calcCRC = false
}
func (f *forwardOnlyBuffer) Crc() uint64 {
return f.crc
}
func (f *forwardOnlyBuffer) SupportsView() bool {
return false
}
func (f *forwardOnlyBuffer) View(_ int) (bufferView, error) {
return nil, nil
}