-
Notifications
You must be signed in to change notification settings - Fork 6
/
buffer.go
173 lines (141 loc) · 3.35 KB
/
buffer.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package cellar
import (
"bufio"
"crypto/cipher"
"io"
"log"
"os"
"path"
"github.com/pierrec/lz4"
"github.com/pkg/errors"
)
type Buffer struct {
fileName string
maxBytes int64
startPos int64
records int64
pos int64
writer *bufio.Writer
stream *os.File
}
func openBuffer(d *BufferDto, folder string) (*Buffer, error) {
if len(d.FileName) == 0 {
return nil, errors.New("empty filename")
}
fullPath := path.Join(folder, d.FileName)
f, err := os.OpenFile(fullPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return nil, errors.Wrap(err, "Open file")
}
f.Truncate(int64(d.MaxBytes))
if _, err := f.Seek(int64(d.Pos), io.SeekStart); err != nil {
return nil, errors.Wrap(err, "Seek")
}
b := &Buffer{
fileName: d.FileName,
startPos: d.StartPos,
maxBytes: d.MaxBytes,
pos: d.Pos,
records: d.Records,
stream: f,
writer: bufio.NewWriter(f),
}
return b, nil
}
func (b *Buffer) getState() *BufferDto {
return &BufferDto{
FileName: b.fileName,
MaxBytes: b.maxBytes,
StartPos: b.startPos,
Pos: b.pos,
Records: b.records,
}
}
func (b *Buffer) fits(bytes int64) bool {
return (b.pos + bytes) <= b.maxBytes
}
func (b *Buffer) writeBytes(bs []byte) error {
if _, err := b.writer.Write(bs); err != nil {
return errors.Wrap(err, "Write")
}
b.pos += int64(len(bs))
return nil
}
func (b *Buffer) endRecord() {
b.records++
}
func (b *Buffer) flush() error {
if err := b.writer.Flush(); err != nil {
return errors.Wrap(err, "Flush")
}
return nil
}
func (b *Buffer) close() error {
if b.stream == nil {
return nil
}
var err error
if err = b.stream.Close(); err != nil {
return errors.Wrap(err, "stream.Close")
}
b.stream = nil
return nil
}
func (b *Buffer) compress(key []byte) (dto *ChunkDto, err error) {
loc := b.stream.Name() + ".lz4"
if err = b.writer.Flush(); err != nil {
log.Panicf("Failed to flush buffer: %s", err)
}
if err = b.stream.Sync(); err != nil {
log.Panicf("Failed to Fsync buffer: %s", err)
}
if _, err = b.stream.Seek(0, io.SeekStart); err != nil {
log.Panicf("Failed to seek to 0 in buffer: %s", err)
}
// create chunk file
var chunkFile *os.File
if chunkFile, err = os.Create(loc); err != nil {
return nil, errors.Wrap(err, "os.Create")
}
defer func() {
if err := chunkFile.Sync(); err != nil {
panic("Failed to sync")
}
if err := chunkFile.Close(); err != nil {
panic("Failed to close")
}
}()
// buffer writes to file
buffer := bufio.NewWriter(chunkFile)
defer buffer.Flush()
// encrypt before buffering
var encryptor *cipher.StreamWriter
if encryptor, err = chainEncryptor(key, buffer); err != nil {
log.Panicf("Failed to chain encryptor for %s: %s", loc, err)
}
defer encryptor.Close()
// compress before encrypting
var zw *lz4.Writer
if zw, err = chainCompressor(encryptor); err != nil {
log.Panicf("Failed to chain compressor: %s", err)
}
// copy chunk to the chain
if _, err = io.CopyN(zw, b.stream, b.pos); err != nil {
return nil, errors.Wrap(err, "CopyN")
}
zw.Close()
chunkFile.Sync()
b.close()
var size int64
if size, err = chunkFile.Seek(0, io.SeekEnd); err != nil {
return nil, errors.Wrap(err, "Seek")
}
dto = &ChunkDto{
FileName: b.fileName + ".lz4",
Records: b.records,
UncompressedByteSize: b.pos,
StartPos: b.startPos,
CompressedDiskSize: size,
}
return dto, nil
}