mirror of
https://github.com/minio/minio.git
synced 2024-12-25 06:35:56 -05:00
Adding read header for erasure
This commit is contained in:
parent
00381c887a
commit
ca1a4b616c
@ -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
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user