https://blog.openacid.com/storage/ec-2/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "erasure_code.h" // use <isa-l.h> instead when linking against installed
#define MMAX 255
#define KMAX 255
typedef unsigned char u8;
// 计算指定的校验块
void ec_encode_data_spec(int len, int srcs, int dest_i,
unsigned char *v,unsigned char **src,
unsigned char *dest)
{
int i, j;
unsigned char s;
for (i = 0; i < len; i++) {
s = 0;
for (j = 0; j < srcs; j++) {
s ^= gf_mul(src[j][i], v[j * 32 + dest_i * srcs * 32 + 1]);
dest[i] = s;
}
}
}
// 计算增量校验块
void ec_encode_data_delta_spec(int len, int k, int vec_i,
int dest_i, unsigned char *v,
unsigned char *data,
unsigned char *dest)
{
int i;
for (i = 0; i < len; i++) {
dest[i] = gf_mul(data[i], v[vec_i * 32 + dest_i * k * 32 + 1]);
}
}
// 计算增量校验块并更新
void ec_encode_data_update_spec(int len, int k, int vec_i,
int dest_i, unsigned char *v,
unsigned char *data,
unsigned char *dest)
{
int i;
unsigned char s;
for (i = 0; i < len; i++) {
s = dest[i];
s ^= gf_mul(data[i], v[vec_i * 32 + dest_i * k * 32 + 1]);
dest[i] = s;
}
}
int main(int argc, char *argv[])
{
u8 *frag_ptrs[MMAX];
u8 *encode_matrix;
u8 *g_tbls;
int k = 4,p =2;
int m = k + p;
int len =5;
encode_matrix = malloc(m * k);
g_tbls = malloc(k * p * 32);
for (int i = 0; i < m; i++) {
if (NULL == (frag_ptrs[i] = malloc(len))) {
printf("alloc error: Fail\n");
return -1;
}
}
for (int i = 0; i < k; i++)
for (int j = 0; j < len; j++)
frag_ptrs[i][j] = i+1;
printf(" encode (m,k,p)=(%d,%d,%d) len=%d\n", m, k, p, len);
gf_gen_rs_matrix(encode_matrix, m, k);
for (int i=0;i<m;i++) {
for (int j=0;j<k;j++) {
printf("%d ",encode_matrix[i*k+j]);
}
printf("\n");
}
printf("-------------------\n");
// Initialize g_tbls from encode matrix
ec_init_tables(k, p, &encode_matrix[k * k], g_tbls);
printf("ec encode\n");
printf("------------------\n");
// Generate EC parity blocks from sources
ec_encode_data(len, k, p, g_tbls, frag_ptrs, &frag_ptrs[k]);
for (int i = 0; i < m; i++) {
for (int j = 0; j < len; j++)
printf("%d ",frag_ptrs[i][j]);
printf("\n");
}
u8* old_delta_ec = malloc(p*len);
for (int i=0;i<p;i++) {
ec_encode_data_delta_spec(len,k,0,i,g_tbls,frag_ptrs[0],&old_delta_ec[i*len]);
}
printf("change data--------------\n");
for (int i=0;i<len;i++) {
frag_ptrs[0][i] *= 10;
}
for (int i=0;i<k;i++) {
for (int j=0;j<len;j++) {
printf("%d ",frag_ptrs[i][j]);
}
printf("\n");
}
u8* new_delta_ec = malloc(p*len);
for (int i=0;i<p;i++) {
ec_encode_data_delta_spec(len,k,0,i,g_tbls,frag_ptrs[0],&new_delta_ec[i*len]);
}
printf("encode delta-------------------\n");
for (int j=0;j<p;j++) {
for (int i=0;i<len;i++) {
printf("%d ",frag_ptrs[k+j][i]^old_delta_ec[j*len+i]^new_delta_ec[j*len+i]);
}
printf("\n");
}
printf("encode update--------------\n");
for (int i=0;i<p;i++) {
ec_encode_data_update_spec(len,k,0,i,g_tbls,frag_ptrs[0],frag_ptrs[k+i]);
}
for (int i = 0; i < p; i++) {
for (int j = 0; j < len; j++)
printf("%d ",frag_ptrs[k+i][j] ^ old_delta_ec[i*len+j]);
printf("\n");
}
printf("re encode-----------\n");
ec_encode_data(len, k, p, g_tbls, frag_ptrs, &frag_ptrs[k]);
for (int i = 0; i < m; i++) {
for (int j = 0; j < len; j++)
printf("%d ",frag_ptrs[i][j]);
printf("\n");
}
}
网友评论