2014-11-05 06:09:40 -05:00
|
|
|
/*
|
|
|
|
* Mini Object Storage, (C) 2014 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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 erasure
|
|
|
|
|
2015-01-26 15:21:56 -05:00
|
|
|
// #cgo CFLAGS: -O0
|
2014-11-05 06:09:40 -05:00
|
|
|
// #include <stdlib.h>
|
2015-01-27 15:36:44 -05:00
|
|
|
// #include "ec-code.h"
|
|
|
|
// #include "ec-common.h"
|
2014-11-05 06:09:40 -05:00
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2015-03-01 04:25:25 -05:00
|
|
|
type Technique int
|
|
|
|
|
2014-11-05 06:09:40 -05:00
|
|
|
const (
|
2015-03-02 21:03:01 -05:00
|
|
|
Vandermonde Technique = iota
|
|
|
|
Cauchy
|
2014-11-05 06:09:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-11-27 22:43:54 -05:00
|
|
|
K = 10
|
|
|
|
M = 3
|
2014-11-05 06:09:40 -05:00
|
|
|
)
|
|
|
|
|
2015-03-12 05:00:36 -04:00
|
|
|
const (
|
|
|
|
SimdAlign = 32
|
|
|
|
)
|
|
|
|
|
2014-11-29 16:18:52 -05:00
|
|
|
// EncoderParams is a configuration set for building an encoder. It is created using ValidateParams.
|
2014-11-05 06:09:40 -05:00
|
|
|
type EncoderParams struct {
|
2015-03-01 04:25:25 -05:00
|
|
|
K uint8
|
|
|
|
M uint8
|
|
|
|
Technique Technique // cauchy or vandermonde matrix (RS)
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
|
|
|
|
2014-11-29 16:18:52 -05:00
|
|
|
// Encoder is an object used to encode and decode data.
|
2014-11-05 06:09:40 -05:00
|
|
|
type Encoder struct {
|
|
|
|
p *EncoderParams
|
|
|
|
k,
|
2014-11-27 22:43:54 -05:00
|
|
|
m C.int
|
2014-11-05 06:09:40 -05:00
|
|
|
encode_matrix,
|
|
|
|
encode_tbls,
|
|
|
|
decode_matrix,
|
2014-12-07 03:09:24 -05:00
|
|
|
decode_tbls *C.uint8_t
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
|
|
|
|
2014-11-29 16:18:52 -05:00
|
|
|
// ParseEncoderParams creates an EncoderParams object.
|
|
|
|
//
|
2015-02-23 16:39:16 -05:00
|
|
|
// k and m represent the matrix size, which corresponds to the protection level
|
2015-03-02 21:03:01 -05:00
|
|
|
// technique is the matrix type. Valid inputs are Cauchy (recommended) or Vandermonde.
|
2015-02-23 16:39:16 -05:00
|
|
|
//
|
2015-03-01 04:25:25 -05:00
|
|
|
func ParseEncoderParams(k, m uint8, technique Technique) (*EncoderParams, error) {
|
2014-11-05 06:09:40 -05:00
|
|
|
if k < 1 {
|
|
|
|
return nil, errors.New("k cannot be zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
if m < 1 {
|
|
|
|
return nil, errors.New("m cannot be zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
if k+m > 255 {
|
|
|
|
return nil, errors.New("(k + m) cannot be bigger than Galois field GF(2^8) - 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch technique {
|
2015-03-02 21:03:01 -05:00
|
|
|
case Vandermonde:
|
2014-11-05 06:09:40 -05:00
|
|
|
break
|
2015-03-02 21:03:01 -05:00
|
|
|
case Cauchy:
|
2014-11-05 06:09:40 -05:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Technique can be either vandermonde or cauchy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &EncoderParams{
|
2014-12-11 05:11:48 -05:00
|
|
|
K: k,
|
|
|
|
M: m,
|
|
|
|
Technique: technique,
|
2014-11-05 06:09:40 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-02-23 16:39:16 -05:00
|
|
|
// NewEncoder creates an encoder object with a given set of parameters.
|
2014-11-29 17:54:21 -05:00
|
|
|
func NewEncoder(ep *EncoderParams) *Encoder {
|
2014-12-11 05:11:48 -05:00
|
|
|
var k = C.int(ep.K)
|
|
|
|
var m = C.int(ep.M)
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2014-12-07 03:09:24 -05:00
|
|
|
var encode_matrix *C.uint8_t
|
|
|
|
var encode_tbls *C.uint8_t
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2014-12-11 05:11:48 -05:00
|
|
|
C.minio_init_encoder(C.int(ep.Technique), k, m, &encode_matrix,
|
2014-11-27 22:43:54 -05:00
|
|
|
&encode_tbls)
|
2014-12-07 03:09:24 -05:00
|
|
|
|
2014-11-05 06:09:40 -05:00
|
|
|
return &Encoder{
|
|
|
|
p: ep,
|
|
|
|
k: k,
|
|
|
|
m: m,
|
|
|
|
encode_matrix: encode_matrix,
|
|
|
|
encode_tbls: encode_tbls,
|
|
|
|
decode_matrix: nil,
|
|
|
|
decode_tbls: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 05:00:36 -04:00
|
|
|
func getChunkSize(k, split_len int) int {
|
|
|
|
var alignment, remainder, padded_len int
|
|
|
|
|
|
|
|
alignment = k * SimdAlign
|
|
|
|
remainder = split_len % alignment
|
|
|
|
|
|
|
|
padded_len = split_len
|
|
|
|
if remainder != 0 {
|
|
|
|
padded_len = split_len + (alignment - remainder)
|
|
|
|
}
|
|
|
|
return padded_len / k
|
|
|
|
}
|
|
|
|
|
2014-11-29 16:18:52 -05:00
|
|
|
// Encode encodes a block of data. The input is the original data. The output
|
|
|
|
// is a 2 tuple containing (k + m) chunks of erasure encoded data and the
|
|
|
|
// length of the original object.
|
2014-11-05 06:09:40 -05:00
|
|
|
func (e *Encoder) Encode(block []byte) ([][]byte, int) {
|
|
|
|
var block_len = len(block)
|
|
|
|
|
2015-03-12 05:00:36 -04:00
|
|
|
chunk_size := getChunkSize(int(e.k), block_len)
|
|
|
|
chunk_len := chunk_size * int(e.k)
|
|
|
|
pad_len := int(chunk_len) - block_len
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2014-11-27 22:43:54 -05:00
|
|
|
if pad_len > 0 {
|
|
|
|
s := make([]byte, pad_len)
|
2014-11-05 06:09:40 -05:00
|
|
|
// Expand with new padded blocks to the byte array
|
|
|
|
block = append(block, s...)
|
|
|
|
}
|
|
|
|
|
2015-03-01 04:25:25 -05:00
|
|
|
coded_len := chunk_size * int(e.p.M)
|
2014-11-05 06:09:40 -05:00
|
|
|
c := make([]byte, coded_len)
|
|
|
|
block = append(block, c...)
|
|
|
|
|
|
|
|
// Allocate chunks
|
2014-12-11 05:11:48 -05:00
|
|
|
chunks := make([][]byte, e.p.K+e.p.M)
|
|
|
|
pointers := make([]*byte, e.p.K+e.p.M)
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
var i int
|
2014-11-14 00:48:04 -05:00
|
|
|
// Add data blocks to chunks
|
2015-03-01 04:25:25 -05:00
|
|
|
for i = 0; i < int(e.p.K); i++ {
|
2014-11-05 06:09:40 -05:00
|
|
|
chunks[i] = block[i*chunk_size : (i+1)*chunk_size]
|
|
|
|
pointers[i] = &chunks[i][0]
|
|
|
|
}
|
|
|
|
|
2015-03-01 04:25:25 -05:00
|
|
|
for i = int(e.p.K); i < int(e.p.K+e.p.M); i++ {
|
2014-11-14 00:48:04 -05:00
|
|
|
chunks[i] = make([]byte, chunk_size)
|
|
|
|
pointers[i] = &chunks[i][0]
|
|
|
|
}
|
|
|
|
|
2014-12-11 05:11:48 -05:00
|
|
|
data := (**C.uint8_t)(unsafe.Pointer(&pointers[:e.p.K][0]))
|
|
|
|
coding := (**C.uint8_t)(unsafe.Pointer(&pointers[e.p.K:][0]))
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
C.ec_encode_data(C.int(chunk_size), e.k, e.m, e.encode_tbls, data,
|
|
|
|
coding)
|
|
|
|
return chunks, block_len
|
|
|
|
}
|