2015-04-05 04:53:41 -04:00
|
|
|
/*
|
2015-07-24 20:51:40 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-04-05 04:53:41 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package donut
|
|
|
|
|
|
|
|
import (
|
2015-04-07 18:55:44 -04:00
|
|
|
"strconv"
|
2015-04-05 04:53:41 -04:00
|
|
|
|
2015-05-11 19:23:10 -04:00
|
|
|
encoding "github.com/minio/minio/pkg/erasure"
|
|
|
|
"github.com/minio/minio/pkg/iodine"
|
2015-04-05 04:53:41 -04:00
|
|
|
)
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// encoder internal struct
|
2015-04-05 04:53:41 -04:00
|
|
|
type encoder struct {
|
|
|
|
encoder *encoding.Erasure
|
|
|
|
k, m uint8
|
|
|
|
technique encoding.Technique
|
|
|
|
}
|
|
|
|
|
|
|
|
// getErasureTechnique - convert technique string into Technique type
|
|
|
|
func getErasureTechnique(technique string) (encoding.Technique, error) {
|
|
|
|
switch true {
|
|
|
|
case technique == "Cauchy":
|
|
|
|
return encoding.Cauchy, nil
|
|
|
|
case technique == "Vandermonde":
|
|
|
|
return encoding.Cauchy, nil
|
|
|
|
default:
|
2015-06-23 14:44:32 -04:00
|
|
|
return encoding.None, iodine.New(InvalidErasureTechnique{Technique: technique}, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 00:00:28 -04:00
|
|
|
// newEncoder - instantiate a new encoder
|
|
|
|
func newEncoder(k, m uint8, technique string) (encoder, error) {
|
2015-04-07 18:55:44 -04:00
|
|
|
errParams := map[string]string{
|
|
|
|
"k": strconv.FormatUint(uint64(k), 10),
|
|
|
|
"m": strconv.FormatUint(uint64(m), 10),
|
|
|
|
"technique": technique,
|
|
|
|
}
|
2015-04-05 04:53:41 -04:00
|
|
|
e := encoder{}
|
|
|
|
t, err := getErasureTechnique(technique)
|
|
|
|
if err != nil {
|
2015-06-24 22:43:38 -04:00
|
|
|
return encoder{}, iodine.New(err, errParams)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
params, err := encoding.ValidateParams(k, m, t)
|
|
|
|
if err != nil {
|
2015-06-24 22:43:38 -04:00
|
|
|
return encoder{}, iodine.New(err, errParams)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
e.encoder = encoding.NewErasure(params)
|
|
|
|
e.k = k
|
|
|
|
e.m = m
|
|
|
|
e.technique = t
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// TODO - think again if this is needed
|
|
|
|
// GetEncodedBlockLen - wrapper around erasure function with the same name
|
2015-04-05 04:53:41 -04:00
|
|
|
func (e encoder) GetEncodedBlockLen(dataLength int) (int, error) {
|
2015-04-07 18:55:44 -04:00
|
|
|
if dataLength <= 0 {
|
2015-06-23 14:44:32 -04:00
|
|
|
return 0, iodine.New(InvalidArgument{}, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
return encoding.GetEncodedBlockLen(dataLength, e.k), nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// Encode - erasure code input bytes
|
2015-04-05 04:53:41 -04:00
|
|
|
func (e encoder) Encode(data []byte) (encodedData [][]byte, err error) {
|
|
|
|
if data == nil {
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, iodine.New(InvalidArgument{}, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
encodedData, err = e.encoder.Encode(data)
|
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
return encodedData, nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// Decode - erasure decode input encoded bytes
|
2015-04-05 04:53:41 -04:00
|
|
|
func (e encoder) Decode(encodedData [][]byte, dataLength int) (data []byte, err error) {
|
|
|
|
decodedData, err := e.encoder.Decode(encodedData, dataLength)
|
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
return decodedData, nil
|
|
|
|
}
|