You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
3.7 KiB
161 lines
3.7 KiB
10 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
10 years ago
|
// +build amd64
|
||
10 years ago
|
|
||
|
package erasure
|
||
|
|
||
10 years ago
|
// #cgo CPPFLAGS: -Iisal/include
|
||
|
// #cgo LDFLAGS: isal/isa-l.a
|
||
10 years ago
|
// #include <stdlib.h>
|
||
|
// #include <erasure-code.h>
|
||
|
//
|
||
10 years ago
|
// #include "common.h"
|
||
10 years ago
|
import "C"
|
||
|
import (
|
||
|
"errors"
|
||
10 years ago
|
//"fmt"
|
||
10 years ago
|
"unsafe"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
VANDERMONDE = iota
|
||
|
CAUCHY = iota
|
||
|
)
|
||
|
|
||
|
const (
|
||
10 years ago
|
K = 10
|
||
|
M = 3
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// EncoderParams is a configuration set for building an encoder. It is created using ValidateParams.
|
||
10 years ago
|
type EncoderParams struct {
|
||
10 years ago
|
K,
|
||
|
M,
|
||
|
Technique int // cauchy or vandermonde matrix (RS)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Encoder is an object used to encode and decode data.
|
||
10 years ago
|
type Encoder struct {
|
||
|
p *EncoderParams
|
||
|
k,
|
||
10 years ago
|
m C.int
|
||
10 years ago
|
encode_matrix,
|
||
|
encode_tbls,
|
||
|
decode_matrix,
|
||
10 years ago
|
decode_tbls *C.uint8_t
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// ParseEncoderParams creates an EncoderParams object.
|
||
|
//
|
||
|
// k and n represent the matrix size, which corresponds to the protection level.
|
||
|
//
|
||
|
// technique is the matrix type. Valid inputs are CAUCHY (recommended) or VANDERMONDE.
|
||
10 years ago
|
func ParseEncoderParams(k, m, technique int) (*EncoderParams, error) {
|
||
10 years ago
|
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{
|
||
10 years ago
|
K: k,
|
||
|
M: m,
|
||
|
Technique: technique,
|
||
10 years ago
|
}, nil
|
||
|
}
|
||
|
|
||
10 years ago
|
// NewEncoder creates an encoder with a given set of parameters.
|
||
10 years ago
|
func NewEncoder(ep *EncoderParams) *Encoder {
|
||
10 years ago
|
var k = C.int(ep.K)
|
||
|
var m = C.int(ep.M)
|
||
10 years ago
|
|
||
10 years ago
|
var encode_matrix *C.uint8_t
|
||
|
var encode_tbls *C.uint8_t
|
||
10 years ago
|
|
||
10 years ago
|
C.minio_init_encoder(C.int(ep.Technique), k, m, &encode_matrix,
|
||
10 years ago
|
&encode_tbls)
|
||
10 years ago
|
|
||
10 years ago
|
return &Encoder{
|
||
|
p: ep,
|
||
|
k: k,
|
||
|
m: m,
|
||
|
encode_matrix: encode_matrix,
|
||
|
encode_tbls: encode_tbls,
|
||
|
decode_matrix: nil,
|
||
|
decode_tbls: nil,
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// 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.
|
||
10 years ago
|
func (e *Encoder) Encode(block []byte) ([][]byte, int) {
|
||
|
var block_len = len(block)
|
||
|
|
||
10 years ago
|
chunk_size := int(C.minio_calc_chunk_size(e.k, C.uint32_t(block_len)))
|
||
10 years ago
|
chunk_len := chunk_size * e.p.K
|
||
10 years ago
|
pad_len := chunk_len - block_len
|
||
10 years ago
|
|
||
10 years ago
|
if pad_len > 0 {
|
||
|
s := make([]byte, pad_len)
|
||
10 years ago
|
// Expand with new padded blocks to the byte array
|
||
|
block = append(block, s...)
|
||
|
}
|
||
|
|
||
10 years ago
|
coded_len := chunk_size * e.p.M
|
||
10 years ago
|
c := make([]byte, coded_len)
|
||
|
block = append(block, c...)
|
||
|
|
||
|
// Allocate chunks
|
||
10 years ago
|
chunks := make([][]byte, e.p.K+e.p.M)
|
||
|
pointers := make([]*byte, e.p.K+e.p.M)
|
||
10 years ago
|
|
||
|
var i int
|
||
10 years ago
|
// Add data blocks to chunks
|
||
10 years ago
|
for i = 0; i < e.p.K; i++ {
|
||
10 years ago
|
chunks[i] = block[i*chunk_size : (i+1)*chunk_size]
|
||
|
pointers[i] = &chunks[i][0]
|
||
|
}
|
||
|
|
||
10 years ago
|
for i = e.p.K; i < (e.p.K + e.p.M); i++ {
|
||
10 years ago
|
chunks[i] = make([]byte, chunk_size)
|
||
|
pointers[i] = &chunks[i][0]
|
||
|
}
|
||
|
|
||
10 years ago
|
data := (**C.uint8_t)(unsafe.Pointer(&pointers[:e.p.K][0]))
|
||
|
coding := (**C.uint8_t)(unsafe.Pointer(&pointers[e.p.K:][0]))
|
||
10 years ago
|
|
||
|
C.ec_encode_data(C.int(chunk_size), e.k, e.m, e.encode_tbls, data,
|
||
|
coding)
|
||
|
return chunks, block_len
|
||
|
}
|