2019-05-15 16:02:00 +08:00
|
|
|
package erasure_coding
|
|
|
|
|
2019-05-16 01:02:44 +08:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/klauspost/reedsolomon"
|
|
|
|
)
|
|
|
|
|
2019-05-15 16:02:00 +08:00
|
|
|
const (
|
|
|
|
DataShardsCount = 10
|
|
|
|
ParityShardsCount = 4
|
|
|
|
ErasureCodingLargeBlockSize = 1024 * 1024 * 1024 // 1GB
|
|
|
|
ErasureCodingSmallBlockSize = 1024 * 1024 // 1MB
|
|
|
|
)
|
2019-05-16 01:02:44 +08:00
|
|
|
|
|
|
|
func encodeData(file *os.File, enc reedsolomon.Encoder, startOffset, blockSize int64, buffers [][]byte) error {
|
|
|
|
|
|
|
|
// read data into buffers
|
|
|
|
for i := 0; i < DataShardsCount; i++ {
|
|
|
|
n, err := file.ReadAt(buffers[i], startOffset+blockSize*int64(i))
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if n < len(buffers[i]) {
|
|
|
|
for t := len(buffers[i]) - 1; t >= n; t-- {
|
|
|
|
buffers[i][t] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := enc.Encode(buffers)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|