mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-11 07:36:53 +00:00
add Erasure
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
use reed_solomon_erasure::{galois_8::ReedSolomon, Error};
|
||||
|
||||
struct Erasure {
|
||||
data_shards: usize,
|
||||
parity_shards: usize,
|
||||
encoder: ReedSolomon,
|
||||
}
|
||||
|
||||
impl Erasure {
|
||||
pub fn new(data_shards: usize, parity_shards: usize) -> Self {
|
||||
Erasure {
|
||||
data_shards,
|
||||
parity_shards,
|
||||
encoder: ReedSolomon::new(data_shards, parity_shards).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_data(&self, data: &[u8]) -> Result<Vec<Vec<u8>>, Error> {
|
||||
let (shard_size, total_size) = self.need_size(data.len());
|
||||
|
||||
let mut data_buffer = vec![0u8; total_size];
|
||||
{
|
||||
let (left, _) = data_buffer.split_at_mut(data.len());
|
||||
left.copy_from_slice(data);
|
||||
}
|
||||
|
||||
{
|
||||
let data_slices: Vec<&mut [u8]> = data_buffer.chunks_mut(shard_size).collect();
|
||||
|
||||
self.encoder.encode(data_slices)?;
|
||||
}
|
||||
|
||||
// Ok(data_buffer)
|
||||
|
||||
let mut shards = Vec::with_capacity(self.encoder.total_shard_count());
|
||||
|
||||
let slices: Vec<&[u8]> = data_buffer.chunks(shard_size).collect();
|
||||
|
||||
for &d in slices.iter() {
|
||||
shards.push(d.to_vec());
|
||||
}
|
||||
|
||||
Ok(shards)
|
||||
}
|
||||
|
||||
// 所需要的总长度
|
||||
fn need_size(&self, data_size: usize) -> (usize, usize) {
|
||||
let shard_size = self.shard_size(data_size);
|
||||
(shard_size, shard_size * (self.encoder.total_shard_count()))
|
||||
}
|
||||
|
||||
fn shard_size(&self, data_size: usize) -> usize {
|
||||
(data_size + self.encoder.data_shard_count() - 1) / self.encoder.data_shard_count()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_erasure() {
|
||||
let data_shards = 3;
|
||||
let parity_shards = 2;
|
||||
let data: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
|
||||
let ec = Erasure::new(data_shards, parity_shards);
|
||||
let shards = ec.encode_data(data).unwrap();
|
||||
println!("shards:{:?}", shards);
|
||||
|
||||
// let (data_buffer, parity_buffer) = buf.split_at(shard_size * 3);
|
||||
|
||||
// println!(
|
||||
// "data_buffer: {:?},parity_buffer: {:?}",
|
||||
// data_buffer, parity_buffer
|
||||
// );
|
||||
|
||||
// let data_slices: Vec<&[u8]> = data.chunks(shard_size).collect();
|
||||
// let mut parity_slices: Vec<&mut [u8]> = parity_buffer.chunks_mut(shard_size).collect();
|
||||
|
||||
// println!(
|
||||
// "data_slices: {:?},parity_slices: {:?}",
|
||||
// data_slices, parity_slices
|
||||
// );
|
||||
|
||||
// ec.encoder
|
||||
// .encode_sep(&data_slices, &mut parity_slices)
|
||||
// .unwrap();
|
||||
// ec.encoder.encode(all_shards);
|
||||
// println!("shards:{:?}", shards);
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
mod endpoint;
|
||||
mod erasure;
|
||||
mod store;
|
||||
|
||||
Reference in New Issue
Block a user