2015-02-23 14:57:56 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2015-02-24 20:36:21 +00:00
|
|
|
// Packer describes the methods to pack Entries to a storage destination
|
2015-02-23 14:57:56 +00:00
|
|
|
type Packer interface {
|
2015-02-24 20:36:21 +00:00
|
|
|
// AddEntry packs the Entry and returns its position
|
2015-02-23 14:57:56 +00:00
|
|
|
AddEntry(e Entry) (int, error)
|
|
|
|
}
|
|
|
|
|
2015-02-24 20:36:21 +00:00
|
|
|
// Unpacker describes the methods to read Entries from a source
|
2015-02-23 14:57:56 +00:00
|
|
|
type Unpacker interface {
|
2015-02-24 20:36:21 +00:00
|
|
|
// Next returns the next Entry being unpacked, or error, until io.EOF
|
2015-02-23 14:57:56 +00:00
|
|
|
Next() (*Entry, error)
|
|
|
|
}
|
|
|
|
|
2015-02-24 20:36:21 +00:00
|
|
|
/* TODO(vbatts) figure out a good model for this
|
2015-02-23 14:57:56 +00:00
|
|
|
type PackUnpacker interface {
|
|
|
|
Packer
|
|
|
|
Unpacker
|
|
|
|
}
|
2015-02-24 20:36:21 +00:00
|
|
|
*/
|
2015-02-23 14:57:56 +00:00
|
|
|
|
|
|
|
type jsonUnpacker struct {
|
|
|
|
r io.Reader
|
|
|
|
b *bufio.Reader
|
|
|
|
isEOF bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jup *jsonUnpacker) Next() (*Entry, error) {
|
|
|
|
var e Entry
|
|
|
|
if jup.isEOF {
|
|
|
|
// since ReadBytes() will return read bytes AND an EOF, we handle it this
|
|
|
|
// round-a-bout way so we can Unmarshal the tail with relevant errors, but
|
|
|
|
// still get an io.EOF when the stream is ended.
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
line, err := jup.b.ReadBytes('\n')
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return nil, err
|
|
|
|
} else if err == io.EOF {
|
|
|
|
jup.isEOF = true
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(line, &e)
|
|
|
|
if err != nil && jup.isEOF {
|
|
|
|
// if the remainder actually _wasn't_ a remaining json structure, then just EOF
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
return &e, err
|
|
|
|
}
|
|
|
|
|
2015-02-24 20:36:21 +00:00
|
|
|
// NewJsonUnpacker provides an Unpacker that reads Entries (SegmentType and
|
|
|
|
// FileType) as a json document.
|
|
|
|
//
|
|
|
|
// Each Entry read are expected to be delimited by new line.
|
2015-02-23 14:57:56 +00:00
|
|
|
func NewJsonUnpacker(r io.Reader) Unpacker {
|
|
|
|
return &jsonUnpacker{
|
|
|
|
r: r,
|
|
|
|
b: bufio.NewReader(r),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonPacker struct {
|
|
|
|
w io.Writer
|
|
|
|
e *json.Encoder
|
|
|
|
pos int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jp *jsonPacker) AddEntry(e Entry) (int, error) {
|
|
|
|
e.Position = jp.pos
|
|
|
|
err := jp.e.Encode(e)
|
|
|
|
if err == nil {
|
|
|
|
jp.pos++
|
|
|
|
}
|
|
|
|
return e.Position, err
|
|
|
|
}
|
|
|
|
|
2015-02-24 20:36:21 +00:00
|
|
|
// NewJsonPacker provides an Packer that writes each Entry (SegmentType and
|
|
|
|
// FileType) as a json document.
|
|
|
|
//
|
|
|
|
// The Entries are delimited by new line.
|
2015-02-23 14:57:56 +00:00
|
|
|
func NewJsonPacker(w io.Writer) Packer {
|
|
|
|
return &jsonPacker{
|
|
|
|
w: w,
|
|
|
|
e: json.NewEncoder(w),
|
|
|
|
}
|
|
|
|
}
|