Merge pull request #15 from harshavardhana/go_isal_binding
Add erasure coding and decoding using Intel Storage Acceleration librarymaster
commit
d88158b75a
@ -1,2 +1,3 @@ |
||||
**/*.swp |
||||
/cover/ |
||||
*~ |
||||
|
@ -0,0 +1,16 @@ |
||||
all: build test |
||||
.PHONY: all |
||||
|
||||
test: cauchy vandermonde |
||||
|
||||
cauchy: |
||||
@go test -test.run="TestCauchy*"
|
||||
|
||||
vandermonde: |
||||
@go test -test.run="TestVanderMonde*"
|
||||
|
||||
isal/isal-l.so: |
||||
@$(MAKE) --quiet -C isal
|
||||
|
||||
build: isal/isal-l.so |
||||
@go build
|
@ -0,0 +1,63 @@ |
||||
/* |
||||
* 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 |
||||
|
||||
import ( |
||||
"bytes" |
||||
"testing" |
||||
) |
||||
|
||||
func TestCachyEncode(t *testing.T) { |
||||
ep, _ := ValidateParams(10, 5, 8, CAUCHY) |
||||
p := NewEncoder(ep) |
||||
|
||||
data := make([]byte, 1000) |
||||
chunks, length := p.Encode(data) |
||||
|
||||
t.Logf("chunks length: %d;\nlength: %d\n", len(chunks), length) |
||||
if length != len(data) { |
||||
t.Fatal() |
||||
} |
||||
} |
||||
|
||||
func TestCauchyDecode(t *testing.T) { |
||||
ep, _ := ValidateParams(10, 5, 8, CAUCHY) |
||||
|
||||
data := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.") |
||||
|
||||
p := NewEncoder(ep) |
||||
chunks, length := p.Encode(data) |
||||
t.Logf("chunks length: %d;\nlength: %d\n", len(chunks), length) |
||||
if length != len(data) { |
||||
t.Fatal() |
||||
} |
||||
|
||||
chunks[0] = nil |
||||
chunks[3] = nil |
||||
chunks[5] = nil |
||||
chunks[9] = nil |
||||
chunks[13] = nil |
||||
|
||||
recovered_data, err := p.Decode(chunks, length) |
||||
if err != nil { |
||||
t.Fatalf("Error: %s", err) |
||||
} |
||||
|
||||
if i := bytes.Compare(recovered_data, data); i < 0 { |
||||
t.Fatalf("Error: recovered data is less than original data") |
||||
} |
||||
} |
@ -0,0 +1,59 @@ |
||||
/*
|
||||
* 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. |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
static void cpuid(int cpuinfo[4] ,int infotype) { |
||||
__asm__ __volatile__ ( |
||||
"cpuid": |
||||
"=a" (cpuinfo[0]), |
||||
"=b" (cpuinfo[1]), |
||||
"=c" (cpuinfo[2]), |
||||
"=d" (cpuinfo[3]) : |
||||
"a" (infotype), "c" (0) |
||||
); |
||||
} |
||||
|
||||
/*
|
||||
SSE41 : return true |
||||
no SSE41 : return false |
||||
*/ |
||||
int has_sse41 (void) { |
||||
int info[4]; |
||||
cpuid(info, 0x00000001); |
||||
return ((info[2] & ((int)1 << 19)) != 0); |
||||
} |
||||
|
||||
/*
|
||||
AVX : return true |
||||
no AVX : return false |
||||
*/ |
||||
int has_avx (void) { |
||||
int info[4]; |
||||
cpuid(info, 0x00000001); |
||||
return ((info[2] & ((int)1 << 28)) != 0); |
||||
} |
||||
|
||||
/*
|
||||
AVX2 : return true |
||||
no AVX2 : return false |
||||
*/ |
||||
int has_avx2 (void) { |
||||
int info[4]; |
||||
cpuid(info, 0x00000007); |
||||
return ((info[1] & ((int)1 << 5)) != 0); |
||||
} |
@ -0,0 +1,24 @@ |
||||
/*
|
||||
* 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. |
||||
*/ |
||||
|
||||
#ifndef __CPU_H__ |
||||
#define __CPU_H__ |
||||
|
||||
int has_sse41 (void); |
||||
int has_avx (void); |
||||
int has_avx2 (void); |
||||
|
||||
#endif /* __CPU_H__ */ |
@ -0,0 +1,58 @@ |
||||
/* |
||||
* 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 |
||||
|
||||
import "C" |
||||
import ( |
||||
"fmt" |
||||
"unsafe" |
||||
) |
||||
|
||||
// Integer to Int conversion
|
||||
func Int2cInt(src_err_list []int) *C.int { |
||||
SrcErrInt := int(unsafe.Sizeof(src_err_list[0])) |
||||
switch SrcErrInt { |
||||
case SizeInt: |
||||
return (*C.int)(unsafe.Pointer(&src_err_list[0])) |
||||
case SizeInt8: |
||||
Int8Array := make([]int8, len(src_err_list)) |
||||
for i, v := range src_err_list { |
||||
Int8Array[i] = int8(v) |
||||
} |
||||
return (*C.int)(unsafe.Pointer(&Int8Array[0])) |
||||
case SizeInt16: |
||||
Int16Array := make([]int16, len(src_err_list)) |
||||
for i, v := range src_err_list { |
||||
Int16Array[i] = int16(v) |
||||
} |
||||
return (*C.int)(unsafe.Pointer(&Int16Array[0])) |
||||
case SizeInt32: |
||||
Int32Array := make([]int32, len(src_err_list)) |
||||
for i, v := range src_err_list { |
||||
Int32Array[i] = int32(v) |
||||
} |
||||
return (*C.int)(unsafe.Pointer(&Int32Array[0])) |
||||
case SizeInt64: |
||||
Int64Array := make([]int64, len(src_err_list)) |
||||
for i, v := range src_err_list { |
||||
Int64Array[i] = int64(v) |
||||
} |
||||
return (*C.int)(unsafe.Pointer(&Int64Array[0])) |
||||
default: |
||||
panic(fmt.Sprintf("Unsupported: %d", SizeInt)) |
||||
} |
||||
} |
@ -0,0 +1,112 @@ |
||||
/* |
||||
* 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
|
||||
// amd64
|
||||
|
||||
package erasure |
||||
|
||||
// #cgo CPPFLAGS: -Iisal/include
|
||||
// #cgo LDFLAGS: isal/isa-l.so
|
||||
// #include <stdlib.h>
|
||||
// #include <erasure-code.h>
|
||||
// #include <stdlib.h>
|
||||
//
|
||||
// #include "matrix_decode.h"
|
||||
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 |
||||
|
||||
var matrix_size C.size_t |
||||
var decode_tbls_size C.size_t |
||||
|
||||
k := int(e.p.k) |
||||
n := int(e.p.n) |
||||
|
||||
if len(chunks) != n { |
||||
return nil, errors.New(fmt.Sprintf("chunks length must be %d", n)) |
||||
} |
||||
|
||||
var chunk_size int = e.CalcChunkSize(length) |
||||
|
||||
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") |
||||
} |
||||
|
||||
src_err_list_ptr := Int2cInt(src_err_list[:err_count]) |
||||
|
||||
for i := range chunks { |
||||
if chunks[i] == nil || len(chunks[i]) == 0 { |
||||
chunks[i] = make([]byte, chunk_size) |
||||
} |
||||
} |
||||
|
||||
matrix_size = C.size_t(k * n) |
||||
decode_matrix = (*C.uchar)(unsafe.Pointer(C.malloc(matrix_size))) |
||||
defer C.free(unsafe.Pointer(decode_matrix)) |
||||
|
||||
decode_tbls_size = C.size_t(k * n * 32) |
||||
decode_tbls = (*C.uchar)(unsafe.Pointer(C.malloc(decode_tbls_size))) |
||||
defer C.free(unsafe.Pointer(decode_tbls)) |
||||
|
||||
C.gf_gen_decode_matrix(src_err_list_ptr, e.encode_matrix, |
||||
decode_matrix, e.k, e.k+e.m, C.int(err_count-1), matrix_size) |
||||
|
||||
C.ec_init_tables(e.k, C.int(err_count-1), decode_matrix, decode_tbls) |
||||
e.decode_matrix = decode_matrix |
||||
e.decode_tbls = decode_tbls |
||||
|
||||
pointers := make([]*byte, n) |
||||
for i := range chunks { |
||||
pointers[i] = &chunks[i][0] |
||||
} |
||||
data := (**C.uchar)(unsafe.Pointer(&pointers[:k][0])) |
||||
coding := (**C.uchar)(unsafe.Pointer(&pointers[k:][0])) |
||||
|
||||
C.ec_encode_data(C.int(matrix_size), e.k, C.int(err_count-1), e.decode_tbls, |
||||
data, coding) |
||||
|
||||
recovered_output := make([]byte, 0, chunk_size*k) |
||||
for i := 0; i < k; i++ { |
||||
recovered_output = append(recovered_output, chunks[i]...) |
||||
} |
||||
return recovered_output[:length], nil |
||||
} |
||||
|
||||
func Decode(chunks [][]byte, ep EncoderParams, length int) (block []byte, err error) { |
||||
return GetEncoder(ep).Decode(chunks, length) |
||||
} |
@ -0,0 +1,205 @@ |
||||
/* |
||||
* 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
|
||||
// amd64
|
||||
|
||||
package erasure |
||||
|
||||
// #cgo CPPFLAGS: -Iisal/include
|
||||
// #cgo LDFLAGS: isal/isa-l.so
|
||||
// #include <stdlib.h>
|
||||
// #include <erasure-code.h>
|
||||
// #include <stdlib.h>
|
||||
//
|
||||
// #include "cpufeatures.h"
|
||||
import "C" |
||||
import ( |
||||
"errors" |
||||
"unsafe" |
||||
) |
||||
|
||||
const ( |
||||
VANDERMONDE = iota |
||||
CAUCHY = iota |
||||
) |
||||
|
||||
const ( |
||||
DEFAULT_K = 10 |
||||
DEFAULT_M = 3 |
||||
) |
||||
|
||||
type EncoderParams struct { |
||||
k, |
||||
m, |
||||
w, |
||||
n, |
||||
technique int // cauchy or vandermonde matrix (RS)
|
||||
} |
||||
|
||||
type Encoder struct { |
||||
p *EncoderParams |
||||
k, |
||||
m, |
||||
w C.int |
||||
encode_matrix, |
||||
encode_tbls, |
||||
decode_matrix, |
||||
decode_tbls *C.uchar |
||||
} |
||||
|
||||
// Parameter validation
|
||||
func ValidateParams(k, m, w, technique int) (*EncoderParams, error) { |
||||
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") |
||||
} |
||||
|
||||
if 1<<uint(w) < k+m { |
||||
return nil, errors.New("Wordsize should be bigger than Galois field GF(2^8) - 1") |
||||
} |
||||
|
||||
if w < 0 { |
||||
return nil, errors.New("Wordsize cannot be negative") |
||||
} |
||||
|
||||
switch technique { |
||||
case VANDERMONDE: |
||||
break |
||||
case CAUCHY: |
||||
break |
||||
default: |
||||
return nil, errors.New("Technique can be either vandermonde or cauchy") |
||||
} |
||||
|
||||
return &EncoderParams{ |
||||
k: k, |
||||
m: m, |
||||
w: w, |
||||
n: k + m, |
||||
technique: technique, |
||||
}, nil |
||||
} |
||||
|
||||
func NewEncoder(ep *EncoderParams) *Encoder { |
||||
var k = C.int(ep.k) |
||||
var m = C.int(ep.m) |
||||
var w = C.int(ep.w) |
||||
var n = C.int(ep.n) |
||||
|
||||
var encode_matrix *C.uchar |
||||
var encode_tbls *C.uchar |
||||
|
||||
var matrix_size C.size_t |
||||
var encode_tbls_size C.size_t |
||||
|
||||
matrix_size = C.size_t(k * n) |
||||
encode_matrix = (*C.uchar)(unsafe.Pointer(C.malloc(matrix_size))) |
||||
defer C.free(unsafe.Pointer(encode_matrix)) |
||||
|
||||
encode_tbls_size = C.size_t(k * n * 32) |
||||
encode_tbls = (*C.uchar)(unsafe.Pointer(C.malloc(encode_tbls_size))) |
||||
defer C.free(unsafe.Pointer(encode_tbls)) |
||||
|
||||
if ep.technique == VANDERMONDE { |
||||
// Commonly used method for choosing coefficients in erasure encoding
|
||||
// but does not guarantee invertable for every sub matrix. For large
|
||||
// k it is possible to find cases where the decode matrix chosen from
|
||||
// sources and parity not in erasure are not invertable. Users may
|
||||
// want to adjust for k > 5.
|
||||
// -- Intel
|
||||
C.gf_gen_rs_matrix(encode_matrix, n, k) |
||||
} else if ep.technique == CAUCHY { |
||||
C.gf_gen_cauchy1_matrix(encode_matrix, n, k) |
||||
} |
||||
|
||||
C.ec_init_tables(k, m, encode_matrix, encode_tbls) |
||||
|
||||
return &Encoder{ |
||||
p: ep, |
||||
k: k, |
||||
m: m, |
||||
w: w, |
||||
encode_matrix: encode_matrix, |
||||
encode_tbls: encode_tbls, |
||||
decode_matrix: nil, |
||||
decode_tbls: nil, |
||||
} |
||||
} |
||||
|
||||
func (e *Encoder) CalcChunkSize(block_len int) int { |
||||
var alignment int = e.p.k * e.p.w |
||||
var padding = block_len % alignment |
||||
var padded_len int |
||||
|
||||
if padding > 0 { |
||||
padded_len = block_len + (alignment - padding) |
||||
} else { |
||||
padded_len = block_len |
||||
} |
||||
return padded_len / e.p.k |
||||
} |
||||
|
||||
func (e *Encoder) Encode(block []byte) ([][]byte, int) { |
||||
var block_len = len(block) |
||||
|
||||
chunk_size := e.CalcChunkSize(block_len) |
||||
padded_len := chunk_size * e.p.k |
||||
|
||||
if (padded_len - block_len) > 0 { |
||||
s := make([]byte, (padded_len - block_len)) |
||||
// Expand with new padded blocks to the byte array
|
||||
block = append(block, s...) |
||||
} |
||||
|
||||
coded_len := chunk_size * e.p.m |
||||
c := make([]byte, coded_len) |
||||
block = append(block, c...) |
||||
|
||||
// Allocate chunks
|
||||
chunks := make([][]byte, e.p.n) |
||||
pointers := make([]*byte, e.p.n) |
||||
|
||||
var i int |
||||
// Add data and code blocks to chunks
|
||||
for i = 0; i < e.p.n; i++ { |
||||
chunks[i] = block[i*chunk_size : (i+1)*chunk_size] |
||||
pointers[i] = &chunks[i][0] |
||||
} |
||||
|
||||
data := (**C.uchar)(unsafe.Pointer(&pointers[:e.p.k][0])) |
||||
coding := (**C.uchar)(unsafe.Pointer(&pointers[e.p.k:][0])) |
||||
|
||||
C.ec_encode_data(C.int(chunk_size), e.k, e.m, e.encode_tbls, data, |
||||
coding) |
||||
|
||||
return chunks, block_len |
||||
} |
||||
|
||||
func GetEncoder(ep EncoderParams) *Encoder { |
||||
return DefaultCache.GetC(ep) |
||||
} |
||||
|
||||
func Encode(data []byte, ep EncoderParams) (chunks [][]byte, length int) { |
||||
return GetEncoder(ep).Encode(data) |
||||
} |
@ -0,0 +1,64 @@ |
||||
/* |
||||
* 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 |
||||
|
||||
import ( |
||||
"github.com/golang/groupcache/lru" |
||||
"sync" |
||||
) |
||||
|
||||
// thread-safe LRU cache from GroupCache
|
||||
type Cache struct { |
||||
mutex sync.RWMutex |
||||
cache *lru.Cache |
||||
} |
||||
|
||||
var DefaultCache *Cache = GetCache(0) |
||||
|
||||
// Allocate ``Cache`` LRU
|
||||
func GetCache(capacity int) *Cache { |
||||
return &Cache{ |
||||
cache: lru.New(capacity), |
||||
} |
||||
} |
||||
|
||||
// ``GetC()`` -- Grab encoder from LRU
|
||||
func (c *Cache) GetC(ep EncoderParams) *Encoder { |
||||
if encoder, ret := c._Get(ep); ret { |
||||
return encoder |
||||
} |
||||
encoder := NewEncoder(&ep) |
||||
c._Put(ep, encoder) |
||||
return encoder |
||||
} |
||||
|
||||
// ``_Get()`` -- Get key from existing LRU
|
||||
func (c *Cache) _Get(ep EncoderParams) (*Encoder, bool) { |
||||
c.mutex.RLock() |
||||
defer c.mutex.RUnlock() |
||||
if encoder, ret := c.cache.Get(ep); ret { |
||||
return encoder.(*Encoder), ret |
||||
} |
||||
return nil, false |
||||
} |
||||
|
||||
// ``_Put()`` -- Add key to existing LRU
|
||||
func (c *Cache) _Put(ep EncoderParams, encoder *Encoder) { |
||||
c.mutex.Lock() |
||||
defer c.mutex.Unlock() |
||||
c.cache.Add(ep, encoder) |
||||
} |
@ -0,0 +1,98 @@ |
||||
/*
|
||||
* 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. |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include <erasure-code.h> |
||||
#include "matrix_decode.h" |
||||
|
||||
static int src_in_err (int r, int *src_err_list) |
||||
{ |
||||
|
||||
int i; |
||||
for (i = 0; src_err_list[i] != -1; i++) { |
||||
if (src_err_list[i] == r) { |
||||
return 1; |
||||
} |
||||
} |
||||
// false
|
||||
return 0; |
||||
} |
||||
|
||||
/*
|
||||
Generate decode matrix during the decoding phase |
||||
*/ |
||||
|
||||
int gf_gen_decode_matrix (int *src_err_list, |
||||
unsigned char *encode_matrix, |
||||
unsigned char *decode_matrix, |
||||
int k, int n, int errs, |
||||
size_t matrix_size) |
||||
{ |
||||
int i, j, r, s, l, z; |
||||
unsigned char *input_matrix = NULL; |
||||
unsigned char *inverse_matrix = NULL; |
||||
|
||||
input_matrix = malloc(k * n); |
||||
if (!input_matrix) { |
||||
return -1; |
||||
} |
||||
|
||||
inverse_matrix = malloc(matrix_size); |
||||
if (!inverse_matrix) { |
||||
return -1; |
||||
} |
||||
|
||||
for (i = 0, r = 0; i < k; i++, r++) { |
||||
while (src_in_err(r, src_err_list)) |
||||
r++; |
||||
for (j = 0; j < k; j++) { |
||||
input_matrix[k * i + j] = encode_matrix[k * r + j]; |
||||
} |
||||
} |
||||
|
||||
// Not all Vandermonde matrix can be inverted
|
||||
if (gf_invert_matrix(input_matrix, inverse_matrix, k) < 0) { |
||||
return -1; |
||||
} |
||||
|
||||
for (l = 0; l < errs; l++) { |
||||
if (src_err_list[l] < k) { |
||||
// decoding matrix elements for data chunks
|
||||
for (j = 0; j < k; j++) { |
||||
decode_matrix[k * l + j] = |
||||
inverse_matrix[k * |
||||
src_err_list[l] + j]; |
||||
} |
||||
} else { |
||||
int s = 0; |
||||
// decoding matrix element for coding chunks
|
||||
for (i = 0; i < k; i++) { |
||||
s = 0; |
||||
for (j = 0; j < k; j++) { |
||||
s ^= gf_mul(inverse_matrix[j * k + i], |
||||
encode_matrix[k * |
||||
src_err_list[l] + j]); |
||||
} |
||||
decode_matrix[k * l + i] = s; |
||||
} |
||||
} |
||||
} |
||||
free(input_matrix); |
||||
free(inverse_matrix); |
||||
return 0; |
||||
} |
@ -0,0 +1,24 @@ |
||||
/*
|
||||
* 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. |
||||
*/ |
||||
|
||||
#ifndef __MATRIX_DECODE_H__ |
||||
#define __MATRIX_DECODE_H__ |
||||
|
||||
int gf_gen_decode_matrix (int *src_err_list, |
||||
unsigned char *encoding_matrix, |
||||
unsigned char *decode_matrix, int k, int n, |
||||
int errs, size_t matrix_size); |
||||
#endif /* __MATRIX_DECODE_H__ */ |
@ -0,0 +1,48 @@ |
||||
/* |
||||
* 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 |
||||
|
||||
//
|
||||
// int SizeInt()
|
||||
// {
|
||||
// return sizeof(int);
|
||||
// }
|
||||
import "C" |
||||
import "unsafe" |
||||
|
||||
var ( |
||||
// See http://golang.org/ref/spec#Numeric_types
|
||||
|
||||
// SizeUint8 is the byte size of a uint8.
|
||||
SizeUint8 = int(unsafe.Sizeof(uint8(0))) |
||||
// SizeUint16 is the byte size of a uint16.
|
||||
SizeUint16 = int(unsafe.Sizeof(uint16(0))) |
||||
// SizeUint32 is the byte size of a uint32.
|
||||
SizeUint32 = int(unsafe.Sizeof(uint32(0))) |
||||
// SizeUint64 is the byte size of a uint64.
|
||||
SizeUint64 = int(unsafe.Sizeof(uint64(0))) |
||||
|
||||
SizeInt = int(C.SizeInt()) |
||||
// SizeInt8 is the byte size of a int8.
|
||||
SizeInt8 = int(unsafe.Sizeof(int8(0))) |
||||
// SizeInt16 is the byte size of a int16.
|
||||
SizeInt16 = int(unsafe.Sizeof(int16(0))) |
||||
// SizeInt32 is the byte size of a int32.
|
||||
SizeInt32 = int(unsafe.Sizeof(int32(0))) |
||||
// SizeInt64 is the byte size of a int64.
|
||||
SizeInt64 = int(unsafe.Sizeof(int64(0))) |
||||
) |
@ -0,0 +1,63 @@ |
||||
/* |
||||
* 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 |
||||
|
||||
import ( |
||||
"bytes" |
||||
"testing" |
||||
) |
||||
|
||||
func TestVanderMondeEncode(t *testing.T) { |
||||
ep, _ := ValidateParams(10, 5, 8, VANDERMONDE) |
||||
p := NewEncoder(ep) |
||||
|
||||
data := make([]byte, 1000) |
||||
chunks, length := p.Encode(data) |
||||
|
||||
t.Logf("chunks length: %d;\nlength: %d\n", len(chunks), length) |
||||
if length != len(data) { |
||||
t.Fatal() |
||||
} |
||||
} |
||||
|
||||
func TestVanderMondeDecode(t *testing.T) { |
||||
ep, _ := ValidateParams(10, 5, 8, VANDERMONDE) |
||||
p := NewEncoder(ep) |
||||
|
||||
data := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.") |
||||
|
||||
chunks, length := p.Encode(data) |
||||
t.Logf("chunks length: %d;\nlength: %d\n", len(chunks), length) |
||||
if length != len(data) { |
||||
t.Fatal() |
||||
} |
||||
|
||||
chunks[0] = nil |
||||
chunks[3] = nil |
||||
chunks[5] = nil |
||||
chunks[9] = nil |
||||
chunks[13] = nil |
||||
|
||||
recovered_data, err := p.Decode(chunks, length) |
||||
if err != nil { |
||||
t.Fatalf("Error: %s", err) |
||||
} |
||||
|
||||
if i := bytes.Compare(recovered_data, data); i < 0 { |
||||
t.Fatalf("Error: recovered data is less than original data") |
||||
} |
||||
} |
Loading…
Reference in new issue