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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
VANDERMONDE = iota
|
2014-12-30 01:07:05 -05:00
|
|
|
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
|
|
|
)
|
|
|
|
|
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 {
|
2014-12-11 05:11:48 -05:00
|
|
|
K,
|
|
|
|
M,
|
|
|
|
Technique int // 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
|
2014-11-29 16:18:52 -05:00
|
|
|
// technique is the matrix type. Valid inputs are CAUCHY (recommended) or VANDERMONDE.
|
2015-02-23 16:39:16 -05:00
|
|
|
//
|
2014-11-27 22:43:54 -05:00
|
|
|
func ParseEncoderParams(k, m, technique int) (*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 {
|
|
|
|
case VANDERMONDE:
|
|
|
|
break
|
|
|
|
case CAUCHY:
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2014-11-29 17:58:40 -05:00
|
|
|
chunk_size := int(C.minio_calc_chunk_size(e.k, C.uint32_t(block_len)))
|
2014-12-11 05:11:48 -05:00
|
|
|
chunk_len := chunk_size * e.p.K
|
2014-11-27 22:43:54 -05:00
|
|
|
pad_len := 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...)
|
|
|
|
}
|
|
|
|
|
2014-12-11 05:11:48 -05:00
|
|
|
coded_len := chunk_size * 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
|
2014-12-11 05:11:48 -05:00
|
|
|
for i = 0; i < 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]
|
|
|
|
}
|
|
|
|
|
2014-12-11 05:11:48 -05:00
|
|
|
for i = e.p.K; i < (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
|
|
|
|
}
|