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.
103 lines
2.5 KiB
103 lines
2.5 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 CFLAGS: -O0
|
||
10 years ago
|
// #include <stdlib.h>
|
||
10 years ago
|
// #include "ec-code.h"
|
||
|
// #include "ec-common.h"
|
||
10 years ago
|
import "C"
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"unsafe"
|
||
|
)
|
||
|
|
||
|
func (e *Encoder) Decode(chunks [][]byte, length int) ([]byte, error) {
|
||
10 years ago
|
var decode_matrix *C.uint8_t
|
||
|
var decode_tbls *C.uint8_t
|
||
|
var decode_index *C.uint32_t
|
||
|
var source, target **C.uint8_t
|
||
10 years ago
|
|
||
10 years ago
|
k := int(e.k)
|
||
|
n := int(e.k + e.m)
|
||
10 years ago
|
|
||
|
if len(chunks) != n {
|
||
|
return nil, errors.New(fmt.Sprintf("chunks length must be %d", n))
|
||
|
}
|
||
|
|
||
10 years ago
|
chunk_size := int(C.minio_calc_chunk_size(e.k, C.uint32_t(length)))
|
||
10 years ago
|
|
||
10 years ago
|
error_index := make([]int, n+1)
|
||
10 years ago
|
var err_count int = 0
|
||
|
|
||
|
for i := range chunks {
|
||
|
// Check of chunks are really null
|
||
|
if chunks[i] == nil || len(chunks[i]) == 0 {
|
||
10 years ago
|
error_index[err_count] = i
|
||
10 years ago
|
err_count++
|
||
|
}
|
||
|
}
|
||
10 years ago
|
error_index[err_count] = -1
|
||
10 years ago
|
err_count++
|
||
|
|
||
|
// Too many missing chunks, cannot be more than parity `m`
|
||
10 years ago
|
if err_count-1 > (n - k) {
|
||
10 years ago
|
return nil, errors.New("too many erasures requested, can't decode")
|
||
|
}
|
||
|
|
||
10 years ago
|
error_index_ptr := int2cInt(error_index[:err_count])
|
||
10 years ago
|
|
||
|
for i := range chunks {
|
||
|
if chunks[i] == nil || len(chunks[i]) == 0 {
|
||
|
chunks[i] = make([]byte, chunk_size)
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
C.minio_init_decoder(error_index_ptr, e.k, e.k+e.m, C.int(err_count-1),
|
||
|
e.encode_matrix, &decode_matrix, &decode_tbls, &decode_index)
|
||
10 years ago
|
|
||
|
pointers := make([]*byte, n)
|
||
|
for i := range chunks {
|
||
|
pointers[i] = &chunks[i][0]
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
data := (**C.uint8_t)(unsafe.Pointer(&pointers[0]))
|
||
10 years ago
|
|
||
10 years ago
|
ret := C.minio_get_source_target(C.int(err_count-1), e.k, e.m, error_index_ptr,
|
||
10 years ago
|
decode_index, data, &source, &target)
|
||
10 years ago
|
|
||
10 years ago
|
if int(ret) == -1 {
|
||
|
return nil, errors.New("Decoding source target failed")
|
||
|
}
|
||
|
|
||
10 years ago
|
C.ec_encode_data(C.int(chunk_size), e.k, C.int(err_count-1), decode_tbls,
|
||
10 years ago
|
source, target)
|
||
10 years ago
|
|
||
|
recovered_output := make([]byte, 0, chunk_size*k)
|
||
|
for i := 0; i < k; i++ {
|
||
|
recovered_output = append(recovered_output, chunks[i]...)
|
||
|
}
|
||
10 years ago
|
|
||
|
e.decode_matrix = decode_matrix
|
||
|
e.decode_tbls = decode_tbls
|
||
|
|
||
10 years ago
|
return recovered_output[:length], nil
|
||
|
}
|