storj/cmd/statreceiver/file.go

79 lines
1.4 KiB
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// 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 {
2019-01-01 09:41:27 +00:00
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) {
2019-01-01 09:41:27 +00:00
f.mu.Lock()
defer f.mu.Unlock()
if f.decoder == nil {
2019-01-01 09:41:27 +00:00
file, err := os.Open(f.path)
if err != nil {
return nil, time.Time{}, err
}
2019-01-01 09:41:27 +00:00
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 {
2019-01-01 09:41:27 +00:00
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 {
2019-01-01 09:41:27 +00:00
f.mu.Lock()
defer f.mu.Unlock()
if f.encoder == nil {
2019-01-01 09:41:27 +00:00
file, err := os.Create(f.path)
if err != nil {
return err
}
2019-01-01 09:41:27 +00:00
f.file = file
f.encoder = gob.NewEncoder(bufio.NewWriter(file))
}
2019-01-01 09:41:27 +00:00
return f.encoder.Encode(Packet{Data: data, TS: ts})
}