forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsv_reader.go
47 lines (42 loc) · 945 Bytes
/
tsv_reader.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 clickhouse
import (
"bufio"
"io"
"strings"
)
type tsvReader struct {
r *bufio.Reader
rawBuffer []byte
}
func newReader(r io.Reader) *tsvReader {
return &tsvReader{
r: bufio.NewReader(r),
}
}
func (r *tsvReader) readLine() ([]byte, error) {
line, err := r.r.ReadSlice('\n')
if err == bufio.ErrBufferFull {
r.rawBuffer = append(r.rawBuffer[:0], line...)
for err == bufio.ErrBufferFull {
line, err = r.r.ReadSlice('\n')
r.rawBuffer = append(r.rawBuffer, line...)
}
line = r.rawBuffer
}
// drop trailing \n
if n := len(line); n > 0 && line[n-1] == '\n' {
line = line[:n-1]
}
// drop trailing \r
if n := len(line); n > 0 && line[n-1] == '\r' {
line = line[:n-1]
}
return line, err
}
func (r *tsvReader) Read() (record []string, err error) {
line, errRead := r.readLine()
if errRead != nil && errRead != io.EOF {
return nil, errRead
}
return strings.Split(string(line), "\t"), errRead
}