-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
42 lines (32 loc) · 820 Bytes
/
writer.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
package shipper
import (
"os"
"sync"
)
type Writer struct {
DestinationPath string
BatchSize int
DestinationFile *os.File
}
func (writer *Writer) open() error {
destinationFile, err := os.OpenFile(writer.DestinationPath, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
writer.DestinationFile = destinationFile
return nil
}
func (writer *Writer) write(wg *sync.WaitGroup, packet Packet) {
go writer.chunkWrite(wg, packet)
}
func (writer *Writer) chunkWrite(wg *sync.WaitGroup, packet Packet) error {
defer wg.Done()
offset := int64(packet.Index * writer.BatchSize)
if _, writeErr := writer.DestinationFile.WriteAt([]byte(packet.Value), offset); writeErr != nil {
return writeErr
}
return nil
}
func (writer *Writer) close() error {
return writer.DestinationFile.Close()
}