-
Notifications
You must be signed in to change notification settings - Fork 0
/
filebuf.go
496 lines (404 loc) · 7.98 KB
/
filebuf.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package filebuf
import (
"bufio"
"errors"
"fmt"
"io"
"os"
)
const (
opNone = iota
opRead
opWrite
)
var pagesize = os.Getpagesize() * 2
type ProcessorReadHandler func(r io.Reader) (io.ReadCloser, error)
type ProcessorWriteHandler func(w io.Writer) (io.Writer, error)
type Reader struct {
*bufio.Reader
rdr io.Reader
// gz io.ReadCloser
processor io.ReadCloser
}
func min(v1, v2 int) int {
if v1 < v2 {
return v1
}
return v2
}
func max(v1, v2 int) int {
if v1 > v2 {
return v1
}
return v2
}
func min64(v1, v2 int64) int64 {
if v1 < v2 {
return v1
}
return v2
}
func max64(v1, v2 int64) int64 {
if v1 > v2 {
return v1
}
return v2
}
func (r *Reader) Read(p []byte) (n int, err error) {
return r.rdr.Read(p)
}
// Close the associated files.
func (r *Reader) Close() error {
if r.processor != nil {
r.processor.Close()
}
if c, ok := r.rdr.(io.ReadCloser); ok {
c.Close()
}
return nil
}
// Writer is returned by Wopen
type Writer struct {
*bufio.Writer
wtr *os.File
// gz *gzip.Writer
processor io.Writer
}
func BufferReader(r io.Reader, processor ProcessorReadHandler) (*Reader, error) {
b := bufio.NewReaderSize(r, pagesize)
if rdr, err := processor(b); err != nil {
return nil, err
} else {
b = bufio.NewReaderSize(rdr, pagesize)
return &Reader{b, r, rdr}, nil
}
/*
if is, err := IsGzip(b); err != nil && err != io.EOF {
log.Fatal(err)
} else if is {
rdr, err = gzip.NewReader(b)
if err != nil {
log.Fatal(err)
}
b = bufio.NewReaderSize(rdr, pageSize)
}
return &Reader{b, r, rdr}
*/
}
func DefaultBufferSize() int {
return pagesize
}
func New(bufferSize int, useMemory bool, removeAfterClose bool) Buf {
return Buf{
useMemory: useMemory,
bufferSize: bufferSize,
removeAfterClose: removeAfterClose,
lastOp: opNone,
}
}
type Buf struct {
buf []byte
f *os.File
useMemory bool
filename string
r *bufio.Reader
w *bufio.Writer
writeMode bool
bufferSize int
//dataInMemoryLen int
index int
flushAfter int64
writtenCount int64
removeAfterClose bool
lastOp int
totalWritten int64
}
var ErrTooLarge = errors.New("filebuf: Out of free memory")
// makeSlice allocates a slice of size n. If the allocation fails, it panics
// with ErrTooLarge.
func makeSlice(n int) []byte {
// If the make fails, give a known error.
defer func() {
if recover() != nil {
panic(ErrTooLarge)
}
}()
return make([]byte, n)
}
func (b *Buf) Offset() int64 {
return int64(b.index)
}
func (b *Buf) openFile() (err error) {
if b.useMemory {
return errors.New("filebuf: The file cannot be opened in RAM mode")
}
if b.f == nil {
b.f, err = os.CreateTemp("", "filebuf")
}
return
}
func (b *Buf) Flush() (err error) {
if b.lastOp != opWrite {
return nil
}
if b.w != nil {
err = b.w.Flush()
}
if err != nil {
return err
}
b.writtenCount = 0
return nil
}
func (b *Buf) SwitchToRead() (err error) {
if b.lastOp == opRead {
return
}
if !b.useMemory {
if err = b.Flush(); err != nil {
return err
}
if b.w != nil {
b.w.Reset(nil)
}
b.lastOp = opNone
b.w = nil
if b.f == nil {
if err = b.openFile(); err != nil {
return err
}
}
b.r = bufio.NewReaderSize(b.f, b.bufferSize)
}
b.lastOp = opRead
return nil
}
func (b *Buf) SwitchToWrite() (err error) {
if b.lastOp == opWrite {
return
}
b.lastOp = opNone
if b.useMemory {
b.lastOp = opWrite
return
}
if b.r != nil {
b.r.Reset(nil)
}
b.r = nil
b.lastOp = opWrite
if b.f == nil {
if err = b.openFile(); err != nil {
return err
}
}
if b.w == nil {
b.w = bufio.NewWriterSize(b.f, b.bufferSize)
}
return nil
}
func (b *Buf) Truncate(n int64) (err error) {
if b.useMemory {
switch {
case n < 0 || n > int64(b.len()):
return errors.New("bytes.Buffer: truncation out of range")
case n == 0:
// Reuse buffer space.
b.index = 0
}
b.buf = b.buf[0 : b.index+int(n)]
return nil
}
if err = b.f.Truncate(n); err != nil {
return
}
if b.lastOp == opRead {
b.r.Reset(b.f)
} else {
b.w.Reset(b.w)
}
return nil
}
// Len for io.Reader compatibility
func (b *Buf) Len() int {
s, _ := b.Size()
return int(s)
}
func (b *Buf) LenInt64() int64 {
s, _ := b.Size()
return s
}
func (b *Buf) Size() (int64, error) {
if b.useMemory {
return int64(b.len()), nil
} else {
if b.f == nil {
return 0, nil
} else {
if s, err := b.f.Stat(); err != nil {
return 0, err
} else {
return s.Size(), nil
}
}
}
}
func (b *Buf) Seek(offset int64, whence int) (int64, error) {
if b.useMemory {
var off int
switch whence {
case io.SeekStart:
off = int(offset)
break
case io.SeekCurrent:
off = b.index + int(offset)
break
case io.SeekEnd:
off = b.len() + int(offset)
break
default:
return 0, fmt.Errorf("filebuf: whence = %d not supported", whence)
}
if off < 0 || off > b.len() {
return 0, errors.New("filebuf: seek after offset")
}
b.index = int(offset)
return int64(off), nil
}
if b.lastOp == opNone {
return 0, errors.New("filebuf: no specific mode")
}
off, err := b.f.Seek(offset, whence)
if err != nil {
return off, err
}
if b.lastOp == opWrite {
if err = b.Flush(); err != nil {
return 0, err
}
b.w.Reset(b.f)
} else {
b.r.Reset(b.f)
}
return off, nil
}
func (b *Buf) Read(data []byte) (n int, err error) {
// fmt.Println("Read")
if b.lastOp != opRead {
return 0, errors.New("filebuf: we are not in read mode")
}
b.lastOp = opRead
if b.useMemory {
if b.index >= len(b.buf) {
if len(data) == 0 {
return
}
return 0, io.EOF
}
n = copy(data, b.buf[b.index:])
b.index += n
return
}
if b.f == nil {
return 0, nil
}
return b.r.Read(data)
}
func (b *Buf) WriteString(data string) (int, error) {
return b.Write([]byte(data))
}
// only in memory mode
func (b *Buf) len() int {
return len(b.buf) - b.index
}
func (b *Buf) grow(n int) int {
difference := n - cap(b.buf)
if difference >= 0 {
// if we are at the meeting point
if difference == 0 {
//n+=512
}
}
m := b.len()
afterWrite := b.index + n
// If buffer is empty, reset to recover space.
if m == 0 && b.index != 0 {
// b.Truncate(0)
}
currentCap := cap(b.buf)
// 05102023:
// There was > currentCap but I think it still makes the buffo too small
// so I changed it to >= or we still have something wrong with crossing the free space
if currentCap <= afterWrite {
missingSpace := b.index + n - currentCap
growCapacity := 0
if currentCap < 1024 && missingSpace < 1024 {
growCapacity = 1024
} else {
growCapacity = max(missingSpace+(n/4), b.index/4)
}
newSlice := make([]byte, currentCap+growCapacity)
copy(newSlice, b.buf[0:])
b.buf = newSlice
}
b.buf = b.buf[0:afterWrite]
return b.index + m
}
func (b *Buf) Write(data []byte) (int, error) {
if b.lastOp != opWrite {
return 0, errors.New("filebuf: we are not in write mode")
}
b.lastOp = opWrite
if b.useMemory {
b.grow(len(data))
wl := copy(b.buf[b.index:], data)
b.index += wl
b.totalWritten += int64(wl)
//if int64(cap(b.buf)) < b.totalWritten {
// log.Printf("We saved more than the capacity of the buffer %d - although we wanted to have a minimum capacity of %d\n", cap(b.buf), b.bufferSize)
//}
return wl, nil
}
count, err := b.w.Write(data)
b.writtenCount += int64(count)
b.totalWritten += int64(count)
if err == nil {
if b.flushAfter > 0 && b.writtenCount >= b.flushAfter {
return count, b.Flush()
}
}
return count, err
}
func (b *Buf) Close() (err error) {
if b.useMemory {
b.buf = b.buf[0:0]
b.buf = nil
b.index = 0
b.lastOp = opNone
return nil
}
if b.writeMode {
err = b.Flush()
}
if b.r != nil {
b.r.Reset(nil)
b.r = nil
}
if b.w != nil {
b.w.Reset(nil)
b.w = nil
}
if b.f != nil {
if err2 := b.f.Close(); err2 != nil {
err = fmt.Errorf("err: [filebuf]: %v, flush state: %v", err2, err)
}
if b.removeAfterClose {
if b.f != nil {
fn := b.f.Name()
os.Remove(fn)
}
}
b.f = nil
}
return
}