2014-11-05 06:09:40 -05:00
|
|
|
/*
|
2015-03-19 17:35:50 -04:00
|
|
|
* Minimalist Object Storage, (C) 2014 Minio, Inc.
|
2014-11-05 06:09:40 -05:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
// Decode decodes erasure coded blocks of data into its original
|
|
|
|
// form. Erasure coded data contains K data blocks and M parity
|
|
|
|
// blocks. Decode can withstand data loss up to any M number of blocks.
|
2015-02-23 16:39:16 -05:00
|
|
|
//
|
2015-03-25 20:41:41 -04:00
|
|
|
// "encodedDataBlocks" is an array of K data blocks and M parity
|
|
|
|
// blocks. Data blocks are position and order dependent. Missing blocks
|
|
|
|
// are set to "nil". There must be at least "K" number of data|parity
|
|
|
|
// blocks.
|
|
|
|
// "dataLen" is the length of original source data
|
|
|
|
func (e *Encoder) Decode(encodedDataBlocks [][]byte, dataLen int) (decodedData []byte, err error) {
|
2015-03-24 21:44:21 -04:00
|
|
|
var decodeMatrix *C.uint8_t
|
|
|
|
var decodeTbls *C.uint8_t
|
|
|
|
var decodeIndex *C.uint32_t
|
2014-12-07 03:09:24 -05:00
|
|
|
var source, target **C.uint8_t
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2015-03-24 21:44:21 -04:00
|
|
|
k := int(e.params.K)
|
|
|
|
m := int(e.params.M)
|
2015-03-22 21:21:35 -04:00
|
|
|
n := k + m
|
2015-03-25 20:41:41 -04:00
|
|
|
// We need the data and parity blocks preserved in the same order. Missing blocks are set to nil.
|
|
|
|
if len(encodedDataBlocks) != n {
|
|
|
|
return nil, errors.New(fmt.Sprintf("Encoded data blocks slice must of length [%d]", n))
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
// Length of a single encoded block
|
|
|
|
encodedBlockLen := GetEncodedBlockLen(dataLen, uint8(k))
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
// Keep track of errors per block.
|
|
|
|
missingEncodedBlocks := make([]int, n+1)
|
|
|
|
var missingEncodedBlocksCount int = 0
|
|
|
|
|
|
|
|
// Check for the missing encoded blocks
|
|
|
|
for i := range encodedDataBlocks {
|
|
|
|
if encodedDataBlocks[i] == nil || len(encodedDataBlocks[i]) == 0 {
|
|
|
|
missingEncodedBlocks[missingEncodedBlocksCount] = i
|
|
|
|
missingEncodedBlocksCount++
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 20:41:41 -04:00
|
|
|
missingEncodedBlocks[missingEncodedBlocksCount] = -1
|
|
|
|
missingEncodedBlocksCount++
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
// Cannot reconstruct original data. Need at least M number of data or parity blocks.
|
|
|
|
if missingEncodedBlocksCount-1 > m {
|
|
|
|
return nil, fmt.Errorf("Cannot reconstruct original data. Need at least [%d] data or parity blocks", m)
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
// Convert from Go int slice to C int array
|
|
|
|
missingEncodedBlocksC := intSlice2CIntArray(missingEncodedBlocks[:missingEncodedBlocksCount])
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
// Allocate buffer for the missing blocks
|
|
|
|
for i := range encodedDataBlocks {
|
|
|
|
if encodedDataBlocks[i] == nil || len(encodedDataBlocks[i]) == 0 {
|
|
|
|
encodedDataBlocks[i] = make([]byte, encodedBlockLen)
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
// Initialzie decoder
|
|
|
|
C.minio_init_decoder(missingEncodedBlocksC, C.int(k), C.int(n), C.int(missingEncodedBlocksCount-1),
|
2015-03-24 21:44:21 -04:00
|
|
|
e.encodeMatrix, &decodeMatrix, &decodeTbls, &decodeIndex)
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
// Make a slice of pointers to encoded blocks. Necessary to bridge to the C world.
|
2014-11-05 06:09:40 -05:00
|
|
|
pointers := make([]*byte, n)
|
2015-03-25 20:41:41 -04:00
|
|
|
for i := range encodedDataBlocks {
|
|
|
|
pointers[i] = &encodedDataBlocks[i][0]
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
2014-11-14 00:48:04 -05:00
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
// Get pointers to source "data" and target "parity" blocks from the output byte array.
|
|
|
|
ret := C.minio_get_source_target(C.int(missingEncodedBlocksCount-1), C.int(k), C.int(m), missingEncodedBlocksC,
|
|
|
|
decodeIndex, (**C.uint8_t)(unsafe.Pointer(&pointers[0])), &source, &target)
|
2015-01-26 15:21:56 -05:00
|
|
|
if int(ret) == -1 {
|
2015-03-25 20:41:41 -04:00
|
|
|
return nil, errors.New("Unable to decode data")
|
2015-01-26 15:21:56 -05:00
|
|
|
}
|
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
// Decode data
|
|
|
|
C.ec_encode_data(C.int(encodedBlockLen), C.int(k), C.int(missingEncodedBlocksCount-1), decodeTbls,
|
2014-11-29 17:58:40 -05:00
|
|
|
source, target)
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
// Allocate buffer to output buffer
|
|
|
|
decodedData = make([]byte, 0, encodedBlockLen*int(k))
|
2015-03-22 20:17:53 -04:00
|
|
|
for i := 0; i < int(k); i++ {
|
2015-03-25 20:41:41 -04:00
|
|
|
decodedData = append(decodedData, encodedDataBlocks[i]...)
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
2014-11-27 22:43:54 -05:00
|
|
|
|
2015-03-12 05:00:36 -04:00
|
|
|
// TODO cache this if necessary
|
2015-03-24 21:44:21 -04:00
|
|
|
e.decodeMatrix = decodeMatrix
|
|
|
|
e.decodeTbls = decodeTbls
|
2014-11-27 22:43:54 -05:00
|
|
|
|
2015-03-25 20:41:41 -04:00
|
|
|
return decodedData[:dataLen], nil
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|