-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinereader.go
93 lines (84 loc) · 1.82 KB
/
linereader.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
package giashard
import (
"bufio"
"compress/gzip"
"errors"
"io"
"log"
"os"
)
// support reading a gzip compressed file and sending lines to a channel
type LineReader struct {
f io.ReadCloser
z io.ReadCloser
fatal bool
}
// return an object that will read lines out of the gzip compressed file
func NewLineReader(filename string) (r *LineReader, err error) {
f, err := os.Open(filename)
if err != nil {
return
}
z, err := gzip.NewReader(f)
if err != nil {
return
}
r = &LineReader{f, z, true}
return
}
// should read errors be fatal (and abort the program with log.Fatalf)
func (r *LineReader)Fatal(flag bool) {
r.fatal = flag
}
// close the underlying files, of course
func (r *LineReader)Close() (err error) {
if e := r.z.Close(); e != nil {
err = e
}
if e := r.f.Close(); e != nil {
err = e
}
return
}
// send lines read from file to the channel
func (r *LineReader)Lines() (ch chan []byte) {
ch = make(chan []byte)
go func() {
buf := bufio.NewReader(r.z)
item := make([]byte, 0, 1024)
for {
line, pfx, err := buf.ReadLine()
// we got some bytes, accumulate
if len(line) > 0 {
item = append(item, line...)
}
// we're done
if err != nil {
if err == io.EOF {
if len(item) > 0 {
ch <- item
}
} else {
var perr *os.PathError
if errors.As(err, &perr) && perr.Err.Error() == "file already closed" {
// Ignore weird edge case we're we are closing ColReader
// so quickly we haven't had the time to encounter EOF
// in this LineReader yet.
} else if r.fatal {
log.Fatalf("error reading column: %v", err)
} else {
log.Printf("error reading column: %v", err)
}
}
close(ch)
return
}
// if we have a complete line, send it
if !pfx {
ch <- item
item = make([]byte, 0, 1024)
}
}
}()
return
}