Skip to content

Commit

Permalink
Allow reconstructSome to reconstruct parity shards
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Jan 5, 2024
1 parent 674f18b commit 794c1c6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion leopard.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (r *leopardFF16) Split(data []byte) ([][]byte, error) {
}

func (r *leopardFF16) ReconstructSome(shards [][]byte, required []bool) error {
return r.ReconstructData(shards)
return r.Reconstruct(shards)
}

func (r *leopardFF16) Reconstruct(shards [][]byte) error {
Expand Down
2 changes: 1 addition & 1 deletion leopard8.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func (r *leopardFF8) Split(data []byte) ([][]byte, error) {
}

func (r *leopardFF8) ReconstructSome(shards [][]byte, required []bool) error {
return r.ReconstructData(shards)
return r.Reconstruct(shards)
}

func (r *leopardFF8) Reconstruct(shards [][]byte) error {
Expand Down
18 changes: 9 additions & 9 deletions reedsolomon.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type Encoder interface {
// Given a list of shards, some of which contain data, fills in the
// data shards that don't have data.
//
// The length of the array must be equal to Shards.
// The length of the array must be equal to TotalShards.
// You indicate that a shard is missing by setting it to nil or zero-length.
// If a shard is zero-length but has sufficient capacity, that memory will
// be used, otherwise a new []byte will be allocated.
Expand All @@ -77,11 +77,11 @@ type Encoder interface {
// calling the Verify function is likely to fail.
ReconstructData(shards [][]byte) error

// ReconstructSome will recreate only requested data shards, if possible.
// ReconstructSome will recreate only requested shards, if possible.
//
// Given a list of shards, some of which contain data, fills in the
// data shards indicated by true values in the "required" parameter.
// The length of "required" array must be equal to DataShards.
// shards indicated by true values in the "required" parameter.
// The length of "required" array must be equal to Shards.
//
// The length of "shards" array must be equal to Shards.
// You indicate that a shard is missing by setting it to nil or zero-length.
Expand Down Expand Up @@ -1402,11 +1402,11 @@ func (r *reedSolomon) ReconstructData(shards [][]byte) error {
return r.reconstruct(shards, true, nil)
}

// ReconstructSome will recreate only requested data shards, if possible.
// ReconstructSome will recreate only requested shards, if possible.
//
// Given a list of shards, some of which contain data, fills in the
// data shards indicated by true values in the "required" parameter.
// The length of "required" array must be equal to dataShards.
// Given a list of shards, fills in the shards
// indicated by true values in the "required" parameter.
// The length of "required" array must be equal to TotalShards.
//
// The length of "shards" array must be equal to shards.
// You indicate that a shard is missing by setting it to nil or zero-length.
Expand All @@ -1419,7 +1419,7 @@ func (r *reedSolomon) ReconstructData(shards [][]byte) error {
// As the reconstructed shard set may contain missing parity shards,
// calling the Verify function is likely to fail.
func (r *reedSolomon) ReconstructSome(shards [][]byte, required []bool) error {
return r.reconstruct(shards, true, required)
return r.reconstruct(shards, false, required)
}

// reconstruct will recreate the missing data totalShards, and unless
Expand Down
22 changes: 20 additions & 2 deletions reedsolomon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,16 +826,34 @@ func testReconstructData(t *testing.T, o ...Option) {
t.Fatal(err)
}

// Reconstruct 3 shards with 3 data and 5 parity shards
// Reconstruct parity shards from data
shardsCopy := make([][]byte, 13)
for i := 0; i < 8; i++ {
shardsCopy[i] = shards[i]
}

shardsRequired := make([]bool, 13)
shardsRequired[10] = true

err = r.ReconstructSome(shardsCopy, shardsRequired)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(shardsCopy[10], shards[10]) {
t.Fatal("ReconstructSome did not reconstruct required shards correctly")
}

// Reconstruct 3 shards with 3 data and 5 parity shards
shardsCopy = make([][]byte, 13)
copy(shardsCopy, shards)
shardsCopy[2] = nil
shardsCopy[3] = nil
shardsCopy[4] = nil
shardsCopy[5] = nil
shardsCopy[6] = nil

shardsRequired := make([]bool, 8)
shardsRequired = make([]bool, 8)
shardsRequired[3] = true
shardsRequired[4] = true
err = r.ReconstructSome(shardsCopy, shardsRequired)
Expand Down

0 comments on commit 794c1c6

Please sign in to comment.