-
Notifications
You must be signed in to change notification settings - Fork 20
/
readahead.go
57 lines (49 loc) · 1.33 KB
/
readahead.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
package readahead
import (
"bufio"
"fmt"
"io"
"os"
)
const (
KiB = 1024
MiB = 1024 * KiB
)
const DefaultChunkSize = 4 * KiB
type CachingReader struct {
file io.ReadCloser
buffer *bufio.Reader
chunkSize int
}
// NewCachingReader returns a reader that reads from the given file, but caches
// the last chunkSize bytes in memory. This is useful for reading CAR files
// because the CAR format is optimized for sequential reads, but the CAR reader
// needs to first read the object size before reading the object data.
func NewCachingReader(filePath string, chunkSize int) (*CachingReader, error) {
if chunkSize <= 0 {
chunkSize = DefaultChunkSize
}
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
return &CachingReader{file: file, buffer: bufio.NewReaderSize(file, chunkSize), chunkSize: chunkSize}, nil
}
func NewCachingReaderFromReader(file io.ReadCloser, chunkSize int) (*CachingReader, error) {
if chunkSize <= 0 {
chunkSize = DefaultChunkSize
}
return &CachingReader{file: file, buffer: bufio.NewReaderSize(file, chunkSize), chunkSize: chunkSize}, nil
}
func (cr *CachingReader) Read(p []byte) (int, error) {
if cr.file == nil {
return 0, fmt.Errorf("file not open")
}
if len(p) == 0 {
return 0, nil
}
return cr.buffer.Read(p)
}
func (cr *CachingReader) Close() error {
return cr.file.Close()
}