Adding read header for erasure

This commit is contained in:
Frederick F. Kautz IV 2015-03-07 15:53:41 -08:00
parent 00381c887a
commit ca1a4b616c
2 changed files with 43 additions and 1 deletions

View File

@ -22,6 +22,11 @@ import (
"encoding/gob" "encoding/gob"
"errors" "errors"
"io" "io"
"strconv"
)
const (
Version = uint32(1)
) )
// DataHeader represents the structure serialized to gob. // DataHeader represents the structure serialized to gob.
@ -84,7 +89,7 @@ func Write(target io.Writer, key string, part uint8, length uint32, k, m uint8,
encoder.Encode(header) encoder.Encode(header)
// write version // write version
binary.Write(target, binary.LittleEndian, uint32(1)) binary.Write(target, binary.LittleEndian, uint32(Version))
// write encoded header // write encoded header
if _, err := io.Copy(target, &headerBuffer); err != nil { if _, err := io.Copy(target, &headerBuffer); err != nil {
@ -96,3 +101,18 @@ func Write(target io.Writer, key string, part uint8, length uint32, k, m uint8,
} }
return nil return nil
} }
// Read an erasure block
func ReadHeader(reader io.Reader) (dataHeader DataHeader, err error) {
versionArray := make([]byte, 4)
if err := binary.Read(reader, binary.LittleEndian, versionArray); err != nil {
return dataHeader, err
}
version := binary.LittleEndian.Uint32(versionArray)
if version > Version || version == 0 {
return dataHeader, errors.New("Unknown version: " + strconv.FormatUint(uint64(version), 10))
}
decoder := gob.NewDecoder(reader)
err = decoder.Decode(&dataHeader)
return dataHeader, err
}

View File

@ -66,3 +66,25 @@ func (s *MySuite) TestSingleWrite(c *C) {
c.Assert(actualData.Bytes(), DeepEquals, []byte(testData)) c.Assert(actualData.Bytes(), DeepEquals, []byte(testData))
c.Assert(err, IsNil) c.Assert(err, IsNil)
} }
func (s *MySuite) TestReadWrite(c *C) {
var testBuffer bytes.Buffer
testData := "Hello, World"
testHeader := DataHeader{
Key: "testobj",
ChunkIndex: 1,
OriginalLength: uint32(len(testData)),
EncoderK: 8,
EncoderM: 8,
EncoderTechnique: Cauchy,
}
err := Write(&testBuffer, testHeader.Key, testHeader.ChunkIndex, testHeader.OriginalLength, testHeader.EncoderK, testHeader.EncoderM, testHeader.EncoderTechnique, bytes.NewBufferString(testData))
c.Assert(err, IsNil)
header, err := ReadHeader(&testBuffer)
c.Assert(err, IsNil)
c.Assert(header, DeepEquals, testHeader)
c.Assert(testBuffer.Len(), Equals, len(testData))
c.Assert(testBuffer.Bytes(), DeepEquals, []byte(testData))
}