2015-03-01 03:27:52 -05:00
|
|
|
package data_v1
|
|
|
|
|
2015-03-01 14:32:30 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/gob"
|
|
|
|
"io"
|
|
|
|
)
|
2015-03-01 03:27:52 -05:00
|
|
|
|
|
|
|
type DataHeader struct {
|
|
|
|
Key string
|
|
|
|
Part int
|
2015-03-01 04:05:12 -05:00
|
|
|
Metadata map[string]string
|
2015-03-01 03:27:52 -05:00
|
|
|
EncoderParams EncoderParams
|
|
|
|
}
|
|
|
|
|
2015-03-01 04:05:12 -05:00
|
|
|
type EncoderTechnique int
|
|
|
|
|
|
|
|
const (
|
|
|
|
VANDERMONDE EncoderTechnique = iota
|
|
|
|
CAUCHY
|
|
|
|
)
|
|
|
|
|
2015-03-01 03:27:52 -05:00
|
|
|
type EncoderParams struct {
|
|
|
|
Length int
|
2015-03-01 04:28:11 -05:00
|
|
|
K uint8
|
|
|
|
M uint8
|
2015-03-01 04:05:12 -05:00
|
|
|
Technique EncoderTechnique
|
2015-03-01 03:27:52 -05:00
|
|
|
}
|
|
|
|
|
2015-03-01 14:32:30 -05:00
|
|
|
func Write(target io.Writer, header DataHeader, data io.Reader) error {
|
|
|
|
var headerBuffer bytes.Buffer
|
|
|
|
// encode header
|
|
|
|
encoder := gob.NewEncoder(&headerBuffer)
|
|
|
|
encoder.Encode(header)
|
|
|
|
// write length of header
|
|
|
|
if err := binary.Write(target, binary.LittleEndian, headerBuffer.Len()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// write encoded header
|
|
|
|
if _, err := io.Copy(target, &headerBuffer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// write data
|
|
|
|
if _, err := io.Copy(target, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-03-01 03:27:52 -05:00
|
|
|
}
|