Skip to content

Commit

Permalink
Merge pull request #6 from TRON-US/chunker-meta
Browse files Browse the repository at this point in the history
Add MetaData() interface to splitters
  • Loading branch information
taiyangc authored Oct 29, 2019
2 parents 798e431 + 926e609 commit 434384d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
7 changes: 6 additions & 1 deletion rabin.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ func (r *Rabin) Reader() io.Reader {
return r.reader
}

// Size returns the chunk size of this Splitter.
// ChunkSize returns the chunk size of this Splitter.
func (r *Rabin) ChunkSize() uint64 {
return r.size
}

// MetaData returns metadata object from this chunker (none).
func (r *Rabin) MetaData() interface{} {
return nil
}
13 changes: 13 additions & 0 deletions reed_solomon.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type reedSolomonSplitter struct {
numData uint64
numParity uint64
size uint64
fileSize uint64
err error
}

Expand Down Expand Up @@ -109,6 +110,7 @@ func NewReedSolomonSplitter(r io.Reader, numData, numParity, size uint64) (
numData: numData,
numParity: numParity,
size: size,
fileSize: uint64(fileSize),
}

return rsSpl, nil
Expand Down Expand Up @@ -154,6 +156,17 @@ func (rss *reedSolomonSplitter) Splitters() []Splitter {
return rss.spls
}

type RsMetaMap struct {
NumData uint64
NumParity uint64
FileSize uint64
}

// MetaData returns metadata object of this reed solomon scheme.
func (rss *reedSolomonSplitter) MetaData() interface{} {
return &RsMetaMap{NumData: rss.numData, NumParity: rss.numParity, FileSize: rss.fileSize}
}

// setError saves the first error so it can be returned to caller or other functions.
func (rss *reedSolomonSplitter) setError(err error) {
if rss.err != nil {
Expand Down
11 changes: 11 additions & 0 deletions reed_solomon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ func getReedSolomonShards(t *testing.T, dataShards, parityShards, size uint64) (
t.Fatal("unable to create reed solomon splitter", err)
}

md, ok := spl.MetaData().(*RsMetaMap)
if !ok {
t.Fatal("unable to get metadata map type from reed solomon splitter")
}
if md.NumData != dataShards ||
md.NumParity != parityShards ||
md.FileSize != uint64(max) {
t.Fatalf("reed solomon splitter metadata [%d %d %d] do not match set parameters [%d %d %d]",
md.NumData, md.NumParity, md.FileSize, dataShards, parityShards, max)
}

return shards, spl
}

Expand Down
25 changes: 18 additions & 7 deletions splitting.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Splitter interface {
Reader() io.Reader
NextBytes() ([]byte, error)
ChunkSize() uint64
MetaData() interface{}
}

// A MultiSplitter encapsulates multiple splitters useful for concurrent
Expand Down Expand Up @@ -121,11 +122,23 @@ func (ss *sizeSplitterv2) Reader() io.Reader {
return ss.r
}

// Size returns the chunk size of this Splitter.
// ChunkSize returns the chunk size of this Splitter.
func (ss *sizeSplitterv2) ChunkSize() uint64 {
return uint64(ss.size)
}

// MetaData returns metadata object from this chunker (none).
func (ss *sizeSplitterv2) MetaData() interface{} {
return nil
}

func NewMetaSplitter(r io.Reader, size uint64) Splitter {
return &MetaSplitter{
r: r,
size: size,
}
}

// NextBytes produces a new chunk.
func (ms *MetaSplitter) NextBytes() ([]byte, error) {
if ms.err != nil {
Expand Down Expand Up @@ -160,9 +173,7 @@ func (ms *MetaSplitter) ChunkSize() uint64 {
return uint64(ms.size)
}

func NewMetaSplitter(r io.Reader, size uint64) Splitter {
return &MetaSplitter{
r: r,
size: size,
}
}
// MetaData returns metadata object from this chunker (none).
func (ms *MetaSplitter) MetaData() interface{} {
return nil
}

0 comments on commit 434384d

Please sign in to comment.