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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package erasure
|
|
|
|
|
|
|
|
// #cgo CPPFLAGS: -Iisal/include
|
2014-11-22 14:14:10 -05:00
|
|
|
// #cgo LDFLAGS: isal/isa-l.a
|
2014-11-05 06:09:40 -05:00
|
|
|
// #include <stdlib.h>
|
|
|
|
// #include <erasure-code.h>
|
|
|
|
// #include <stdlib.h>
|
|
|
|
//
|
2014-11-29 17:58:40 -05:00
|
|
|
// #include "common.h"
|
2014-11-05 06:09:40 -05:00
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (e *Encoder) Decode(chunks [][]byte, length int) ([]byte, error) {
|
|
|
|
var decode_matrix *C.uchar
|
|
|
|
var decode_tbls *C.uchar
|
2014-11-29 17:58:40 -05:00
|
|
|
var source, target **C.uchar
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
k := int(e.p.k)
|
2014-11-27 22:43:54 -05:00
|
|
|
n := int(e.p.k + e.p.m)
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
if len(chunks) != n {
|
|
|
|
return nil, errors.New(fmt.Sprintf("chunks length must be %d", n))
|
|
|
|
}
|
|
|
|
|
2014-11-29 17:58:40 -05:00
|
|
|
chunk_size := int(C.minio_calc_chunk_size(e.k, C.uint32_t(length)))
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
src_err_list := make([]int, n+1)
|
|
|
|
var err_count int = 0
|
|
|
|
|
|
|
|
for i := range chunks {
|
|
|
|
// Check of chunks are really null
|
|
|
|
if chunks[i] == nil || len(chunks[i]) == 0 {
|
|
|
|
src_err_list[err_count] = i
|
|
|
|
err_count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
src_err_list[err_count] = -1
|
|
|
|
err_count++
|
|
|
|
|
|
|
|
// Too many missing chunks, cannot be more than parity `m`
|
|
|
|
if err_count-1 > e.p.m {
|
|
|
|
return nil, errors.New("too many erasures requested, can't decode")
|
|
|
|
}
|
|
|
|
|
2014-11-27 22:43:54 -05:00
|
|
|
src_err_list_ptr := int2cInt(src_err_list[:err_count])
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
for i := range chunks {
|
|
|
|
if chunks[i] == nil || len(chunks[i]) == 0 {
|
|
|
|
chunks[i] = make([]byte, chunk_size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 22:43:54 -05:00
|
|
|
C.minio_init_decoder(src_err_list_ptr, e.encode_matrix,
|
|
|
|
&decode_matrix, &decode_tbls,
|
|
|
|
e.k, e.k+e.m, C.int(err_count-1))
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2014-11-27 22:43:54 -05:00
|
|
|
defer C.free(unsafe.Pointer(decode_matrix))
|
2014-11-05 06:09:40 -05:00
|
|
|
defer C.free(unsafe.Pointer(decode_tbls))
|
|
|
|
|
|
|
|
pointers := make([]*byte, n)
|
|
|
|
for i := range chunks {
|
|
|
|
pointers[i] = &chunks[i][0]
|
|
|
|
}
|
2014-11-14 00:48:04 -05:00
|
|
|
|
2014-11-05 06:09:40 -05:00
|
|
|
data := (**C.uchar)(unsafe.Pointer(&pointers[:k][0]))
|
|
|
|
coding := (**C.uchar)(unsafe.Pointer(&pointers[k:][0]))
|
|
|
|
|
2014-11-29 17:58:40 -05:00
|
|
|
C.minio_get_source_target(src_err_list_ptr, C.int(err_count-1),
|
|
|
|
e.k, e.m, data, coding, &source, &target)
|
|
|
|
|
2014-11-27 22:43:54 -05:00
|
|
|
C.ec_encode_data(C.int(chunk_size), e.k, C.int(err_count-1), decode_tbls,
|
2014-11-29 17:58:40 -05:00
|
|
|
source, target)
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
recovered_output := make([]byte, 0, chunk_size*k)
|
|
|
|
for i := 0; i < k; i++ {
|
|
|
|
recovered_output = append(recovered_output, chunks[i]...)
|
|
|
|
}
|
2014-11-27 22:43:54 -05:00
|
|
|
|
|
|
|
e.decode_matrix = decode_matrix
|
|
|
|
e.decode_tbls = decode_tbls
|
|
|
|
|
2014-11-05 06:09:40 -05:00
|
|
|
return recovered_output[:length], nil
|
|
|
|
}
|
|
|
|
|
2014-11-29 16:39:30 -05:00
|
|
|
func Decode(block [][]byte, ep *EncoderParams, length int) ([]byte, error) {
|
2014-11-29 17:54:21 -05:00
|
|
|
encoder := NewEncoder(ep)
|
2014-11-29 16:39:30 -05:00
|
|
|
return encoder.Decode(block, length)
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|