forked from lloyd/wnram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
47 lines (44 loc) · 1.22 KB
/
read.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
package wnram
import (
"bufio"
"io"
"os"
)
// InPlaceReadLine scans a file and invoke the provided callback for
// every line read. Because scanning a file for newline delimiters is
// an incredibly cheap operation, the overhead of multi-thread
// communication can be slower than processing on a single thread when
// the per line computational cost is low.
func inPlaceReadLine(s io.Reader, cb func([]byte, int64, int64) error) error {
const bufSize = 8396800 // 8 meg
reader := bufio.NewReaderSize(s, bufSize)
count := int64(1)
var offset int64
var err error
var line []byte
for line, err = reader.ReadSlice('\n'); err == nil; line, err = reader.ReadSlice('\n') {
if err = cb(line[:len(line)-1], count, offset); err != nil {
return err
}
offset += int64(len(line))
count++
}
// If we reached end of file and the line contents are empty, don't return an additional line.
if err == io.EOF {
err = nil
if len(line) > 0 {
return cb(line, count, offset)
}
} else {
return cb(line, count, offset)
}
return nil
}
func inPlaceReadLineFromPath(filePath string, cb func([]byte, int64, int64) error) error {
f, err := os.Open(filePath)
if err != nil {
return err
}
defer f.Close()
return inPlaceReadLine(f, cb)
}