/
backup_helper_pipes.go
86 lines (71 loc) · 2.31 KB
/
backup_helper_pipes.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
package helper
import (
"bufio"
"compress/gzip"
"io"
"github.com/klauspost/compress/zstd"
)
type BackupPipeWriterCloser interface {
io.Writer
io.Closer
}
type CommonBackupPipeWriterCloser struct {
writeHandle io.WriteCloser
bufIoWriter *bufio.Writer
finalWriter io.Writer
}
func (cPipe CommonBackupPipeWriterCloser) Write(p []byte) (n int, err error) {
return cPipe.finalWriter.Write(p)
}
// Never returns error, suppressing them instead
func (cPipe CommonBackupPipeWriterCloser) Close() error {
_ = cPipe.bufIoWriter.Flush()
_ = cPipe.writeHandle.Close()
return nil
}
func NewCommonBackupPipeWriterCloser(writeHandle io.WriteCloser) (cPipe CommonBackupPipeWriterCloser) {
cPipe.writeHandle = writeHandle
cPipe.bufIoWriter = bufio.NewWriter(cPipe.writeHandle)
cPipe.finalWriter = cPipe.bufIoWriter
return
}
type GZipBackupPipeWriterCloser struct {
cPipe CommonBackupPipeWriterCloser
gzipWriter *gzip.Writer
}
func (gzPipe GZipBackupPipeWriterCloser) Write(p []byte) (n int, err error) {
return gzPipe.gzipWriter.Write(p)
}
// Returns errors from underlying common writer only
func (gzPipe GZipBackupPipeWriterCloser) Close() error {
_ = gzPipe.gzipWriter.Close()
return gzPipe.cPipe.Close()
}
func NewGZipBackupPipeWriterCloser(writeHandle io.WriteCloser, compressLevel int) (gzPipe GZipBackupPipeWriterCloser, err error) {
gzPipe.cPipe = NewCommonBackupPipeWriterCloser(writeHandle)
gzPipe.gzipWriter, err = gzip.NewWriterLevel(gzPipe.cPipe.bufIoWriter, compressLevel)
if err != nil {
gzPipe.cPipe.Close()
}
return
}
type ZSTDBackupPipeWriterCloser struct {
cPipe CommonBackupPipeWriterCloser
zstdEncoder *zstd.Encoder
}
func (zstdPipe ZSTDBackupPipeWriterCloser) Write(p []byte) (n int, err error) {
return zstdPipe.zstdEncoder.Write(p)
}
// Returns errors from underlying common writer only
func (zstdPipe ZSTDBackupPipeWriterCloser) Close() error {
_ = zstdPipe.zstdEncoder.Close()
return zstdPipe.cPipe.Close()
}
func NewZSTDBackupPipeWriterCloser(writeHandle io.WriteCloser, compressLevel int) (zstdPipe ZSTDBackupPipeWriterCloser, err error) {
zstdPipe.cPipe = NewCommonBackupPipeWriterCloser(writeHandle)
zstdPipe.zstdEncoder, err = zstd.NewWriter(zstdPipe.cPipe.bufIoWriter, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(compressLevel)))
if err != nil {
zstdPipe.cPipe.Close()
}
return
}