From ca1a4b616c50dc298aebca4c11c00eca3550c590 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Sat, 7 Mar 2015 15:53:41 -0800 Subject: [PATCH] Adding read header for erasure --- pkg/storage/donut/erasure/erasure1/erasure.go | 22 ++++++++++++++++++- .../donut/erasure/erasure1/erasure_test.go | 22 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pkg/storage/donut/erasure/erasure1/erasure.go b/pkg/storage/donut/erasure/erasure1/erasure.go index 5cfe885b2..a40da6844 100644 --- a/pkg/storage/donut/erasure/erasure1/erasure.go +++ b/pkg/storage/donut/erasure/erasure1/erasure.go @@ -22,6 +22,11 @@ import ( "encoding/gob" "errors" "io" + "strconv" +) + +const ( + Version = uint32(1) ) // 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) // write version - binary.Write(target, binary.LittleEndian, uint32(1)) + binary.Write(target, binary.LittleEndian, uint32(Version)) // write encoded header 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 } + +// 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 +} diff --git a/pkg/storage/donut/erasure/erasure1/erasure_test.go b/pkg/storage/donut/erasure/erasure1/erasure_test.go index 8111aeab8..37a1a3af4 100644 --- a/pkg/storage/donut/erasure/erasure1/erasure_test.go +++ b/pkg/storage/donut/erasure/erasure1/erasure_test.go @@ -66,3 +66,25 @@ func (s *MySuite) TestSingleWrite(c *C) { c.Assert(actualData.Bytes(), DeepEquals, []byte(testData)) 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)) +}