-
Notifications
You must be signed in to change notification settings - Fork 402
/
file.go
78 lines (65 loc) · 1.44 KB
/
file.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"bufio"
"encoding/gob"
"io"
"os"
"sync"
"time"
)
// FileSource reads packets from a file
type FileSource struct {
path string
mu sync.Mutex
decoder *gob.Decoder
}
// NewFileSource creates a FileSource
func NewFileSource(path string) *FileSource {
return &FileSource{path: path}
}
// Next implements the Source interface
func (f *FileSource) Next() ([]byte, time.Time, error) {
f.mu.Lock()
defer f.mu.Unlock()
if f.decoder == nil {
file, err := os.Open(f.path)
if err != nil {
return nil, time.Time{}, err
}
f.decoder = gob.NewDecoder(bufio.NewReader(file))
}
var p Packet
err := f.decoder.Decode(&p)
if err != nil {
return nil, time.Time{}, err
}
return p.Data, p.TS, nil
}
// FileDest sends packets to a file for later processing. FileDest preserves
// the timestamps.
type FileDest struct {
path string
mu sync.Mutex
file io.Closer
encoder *gob.Encoder
}
// NewFileDest creates a FileDest
func NewFileDest(path string) *FileDest {
return &FileDest{path: path}
}
// Packet implements PacketDest
func (f *FileDest) Packet(data []byte, ts time.Time) error {
f.mu.Lock()
defer f.mu.Unlock()
if f.encoder == nil {
file, err := os.Create(f.path)
if err != nil {
return err
}
f.file = file
f.encoder = gob.NewEncoder(bufio.NewWriter(file))
}
return f.encoder.Encode(Packet{Data: data, TS: ts})
}