-
Notifications
You must be signed in to change notification settings - Fork 568
/
pgdump_reader.go
84 lines (77 loc) · 2.04 KB
/
pgdump_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
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
package pachsql
import (
"bufio"
"io"
"strings"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
)
// PGDumpReader parses a pgdump file into a header, rows, and a footer
type PGDumpReader struct {
Header []byte
Footer []byte
rd *bufio.Reader
}
// NewPGDumpReader creates a new PGDumpReader
func NewPGDumpReader(r *bufio.Reader) *PGDumpReader {
return &PGDumpReader{
rd: r,
}
}
// ReadRow parses the pgdump file and populates the header and the footer
// It returns EOF when done, and at that time both the Header and Footer will
// be populated. Both header and footer are required. If either are missing, an
// error is returned
func (r *PGDumpReader) ReadRow() ([]byte, error) {
if len(r.Header) == 0 {
err := r.readHeader()
if err != nil {
return nil, err
}
}
endLine := "\\.\n" // Trailing '\.' denotes the end of the row inserts
row, err := r.rd.ReadBytes('\n')
if err != nil && !errors.Is(err, io.EOF) {
return nil, errors.Wrapf(err, "error reading pgdump row")
}
// corner case: some pgdump files separate lines with \r\n (even on linux),
// so clean this case up so all handling below is unified
if len(row) >= 2 && row[len(row)-2] == '\r' {
row[len(row)-2] = '\n'
row = row[:len(row)-1]
}
if string(row) == endLine {
r.Footer = append(r.Footer, row...)
err = r.readFooter()
row = nil // The endline is part of the footer
}
if errors.Is(err, io.EOF) && len(r.Footer) == 0 {
return nil, errors.Errorf("invalid pgdump - missing footer")
}
return row, err
}
func (r *PGDumpReader) readHeader() error {
done := false
for !done {
b, err := r.rd.ReadBytes('\n')
if err != nil {
if errors.Is(err, io.EOF) {
return errors.Errorf("invalid header - missing row inserts")
}
return errors.EnsureStack(err)
}
if strings.HasPrefix(string(b), "COPY") {
done = true
}
r.Header = append(r.Header, b...)
}
return nil
}
func (r *PGDumpReader) readFooter() error {
for {
b, err := r.rd.ReadBytes('\n')
r.Footer = append(r.Footer, b...)
if err != nil {
return errors.EnsureStack(err)
}
}
}