[all] Apply cargo fmt

This commit is contained in:
Ming-Hung Tsai
2021-05-04 16:10:20 +08:00
parent 4b4584c830
commit 43e433149b
31 changed files with 178 additions and 160 deletions

View File

@@ -21,12 +21,7 @@ impl Unpack for ArrayBlockEntry {
let (i, n) = le_u64(i)?;
let block = n;
Ok((
i,
ArrayBlockEntry {
block,
}
))
Ok((i, ArrayBlockEntry { block }))
}
}

View File

@@ -33,7 +33,7 @@ impl Unpack for ArrayBlockHeader {
max_entries,
nr_entries,
value_size,
blocknr
blocknr,
},
))
}
@@ -54,17 +54,13 @@ fn convert_result<'a, V>(r: IResult<&'a [u8], V>) -> Result<(&'a [u8], V)> {
r.map_err(|_| anyhow!("parse error"))
}
pub fn unpack_array_block<V: Unpack>(
data: &[u8],
) -> Result<ArrayBlock<V>> {
pub fn unpack_array_block<V: Unpack>(data: &[u8]) -> Result<ArrayBlock<V>> {
// TODO: collect errors
let (i, header) = ArrayBlockHeader::unpack(data).map_err(|_e| anyhow!("Couldn't parse header"))?;
let (i, header) =
ArrayBlockHeader::unpack(data).map_err(|_e| anyhow!("Couldn't parse header"))?;
let (_i, values) = convert_result(count(V::unpack, header.nr_entries as usize)(i))?;
Ok(ArrayBlock {
header,
values,
})
Ok(ArrayBlock { header, values })
}
//------------------------------------------

View File

@@ -11,16 +11,12 @@ use crate::pdata::unpack::*;
pub struct ArrayWalker {
engine: Arc<dyn IoEngine + Send + Sync>,
ignore_non_fatal: bool
ignore_non_fatal: bool,
}
// FIXME: define another Result type for array visiting?
pub trait ArrayBlockVisitor<V: Unpack> {
fn visit(
&self,
index: u64,
v: V,
) -> anyhow::Result<()>;
fn visit(&self, index: u64, v: V) -> anyhow::Result<()>;
}
struct BlockValueVisitor<V> {
@@ -39,14 +35,12 @@ impl<V: Unpack + Copy> BlockValueVisitor<V> {
}
}
pub fn visit_array_block(
&self,
index: u64,
array_block: ArrayBlock<V>,
) {
pub fn visit_array_block(&self, index: u64, array_block: ArrayBlock<V>) {
let begin = index * u64::from(array_block.header.nr_entries);
for i in 0..array_block.header.nr_entries {
self.array_block_visitor.visit(begin + u64::from(i), array_block.values[i as usize]).unwrap();
self.array_block_visitor
.visit(begin + u64::from(i), array_block.values[i as usize])
.unwrap();
}
}
}
@@ -63,7 +57,10 @@ impl<V: Unpack + Copy> NodeVisitor<ArrayBlockEntry> for BlockValueVisitor<V> {
) -> Result<()> {
for n in 0..keys.len() {
let index = keys[n];
let b = self.engine.read(values[n].block).map_err(|_| io_err(path))?;
let b = self
.engine
.read(values[n].block)
.map_err(|_| io_err(path))?;
let array_block = unpack_array_block::<V>(b.get_data()).map_err(|_| io_err(path))?;
self.visit_array_block(index, array_block);
}

View File

@@ -24,9 +24,15 @@ pub trait RefCounter<Value> {
pub struct NoopRC {}
impl<Value> RefCounter<Value> for NoopRC {
fn get(&self, _v: &Value) -> Result<u32> {Ok(0)}
fn inc(&mut self, _v: &Value) -> Result<()> {Ok(())}
fn dec(&mut self, _v: &Value) -> Result<()> {Ok(())}
fn get(&self, _v: &Value) -> Result<u32> {
Ok(0)
}
fn inc(&mut self, _v: &Value) -> Result<()> {
Ok(())
}
fn dec(&mut self, _v: &Value) -> Result<()> {
Ok(())
}
}
/// Wraps a space map up to become a RefCounter.
@@ -150,11 +156,7 @@ fn write_node_<V: Unpack + Pack>(w: &mut WriteBatcher, mut node: Node<V>) -> Res
/// decide if it produces internal or leaf nodes.
pub trait NodeIO<V: Unpack + Pack> {
fn write(&self, w: &mut WriteBatcher, keys: Vec<u64>, values: Vec<V>) -> Result<WriteResult>;
fn read(
&self,
w: &mut WriteBatcher,
block: u64,
) -> Result<(Vec<u64>, Vec<V>)>;
fn read(&self, w: &mut WriteBatcher, block: u64) -> Result<(Vec<u64>, Vec<V>)>;
}
pub struct LeafIO {}
@@ -178,11 +180,7 @@ impl<V: Unpack + Pack> NodeIO<V> for LeafIO {
write_node_(w, node)
}
fn read(
&self,
w: &mut WriteBatcher,
block: u64,
) -> Result<(Vec<u64>, Vec<V>)> {
fn read(&self, w: &mut WriteBatcher, block: u64) -> Result<(Vec<u64>, Vec<V>)> {
let b = w.read(block)?;
let path = Vec::new();
match unpack_node::<V>(&path, b.get_data(), true, true)? {
@@ -215,11 +213,7 @@ impl NodeIO<u64> for InternalIO {
write_node_(w, node)
}
fn read(
&self,
w: &mut WriteBatcher,
block: u64,
) -> Result<(Vec<u64>, Vec<u64>)> {
fn read(&self, w: &mut WriteBatcher, block: u64) -> Result<(Vec<u64>, Vec<u64>)> {
let b = w.read(block)?;
let path = Vec::new();
match unpack_node::<u64>(&path, b.get_data(), true, true)? {
@@ -261,10 +255,7 @@ pub struct NodeSummary {
impl<'a, V: Pack + Unpack + Clone> NodeBuilder<V> {
/// Create a new NodeBuilder
pub fn new(
nio: Box<dyn NodeIO<V>>,
value_rc: Box<dyn RefCounter<V>>,
) -> Self {
pub fn new(nio: Box<dyn NodeIO<V>>, value_rc: Box<dyn RefCounter<V>>) -> Self {
NodeBuilder {
nio,
value_rc,
@@ -356,7 +347,7 @@ impl<'a, V: Pack + Unpack + Clone> NodeBuilder<V> {
if self.nodes.len() == 0 {
self.emit_empty_leaf(w)?
}
Ok(self.nodes)
}
@@ -460,14 +451,9 @@ pub struct Builder<V: Unpack + Pack> {
}
impl<V: Unpack + Pack + Clone> Builder<V> {
pub fn new(
value_rc: Box<dyn RefCounter<V>>,
) -> Builder<V> {
pub fn new(value_rc: Box<dyn RefCounter<V>>) -> Builder<V> {
Builder {
leaf_builder: NodeBuilder::new(
Box::new(LeafIO {}),
value_rc,
),
leaf_builder: NodeBuilder::new(Box::new(LeafIO {}), value_rc),
}
}
@@ -487,9 +473,7 @@ impl<V: Unpack + Pack + Clone> Builder<V> {
while nodes.len() > 1 {
let mut builder = NodeBuilder::new(
Box::new(InternalIO {}),
Box::new(SMRefCounter {
sm: w.sm.clone(),
}),
Box::new(SMRefCounter { sm: w.sm.clone() }),
);
for n in nodes {
@@ -505,4 +489,3 @@ impl<V: Unpack + Pack + Clone> Builder<V> {
}
//------------------------------------------

View File

@@ -3,10 +3,9 @@ pub mod array_block;
pub mod array_walker;
pub mod btree;
pub mod btree_builder;
pub mod btree_merge;
pub mod btree_leaf_walker;
pub mod btree_merge;
pub mod btree_walker;
pub mod space_map;
pub mod space_map_disk;
pub mod unpack;

View File

@@ -1,8 +1,8 @@
use anyhow::{anyhow, Result};
use byteorder::{LittleEndian, WriteBytesExt};
use nom::{number::complete::*, IResult};
use std::io::Cursor;
use std::collections::BTreeMap;
use std::io::Cursor;
use crate::checksum;
use crate::io_engine::*;
@@ -299,7 +299,7 @@ pub fn write_common(w: &mut WriteBatcher, sm: &dyn SpaceMap) -> Result<(Vec<Inde
pub fn write_disk_sm(w: &mut WriteBatcher, sm: &dyn SpaceMap) -> Result<SMRoot> {
let (index_entries, ref_count_root) = write_common(w, sm)?;
let mut index_builder: Builder<IndexEntry> = Builder::new(Box::new(NoopRC {}));
for (i, ie) in index_entries.iter().enumerate() {
index_builder.push_value(w, i as u64, *ie)?;
@@ -340,7 +340,7 @@ fn adjust_counts(w: &mut WriteBatcher, ie: &IndexEntry, allocs: &[u64]) -> Resul
if bitmap.entries[*a as usize] == Small(0) {
nr_free -= 1;
}
bitmap.entries[*a as usize] = Small(1);
}
@@ -350,7 +350,7 @@ fn adjust_counts(w: &mut WriteBatcher, ie: &IndexEntry, allocs: &[u64]) -> Resul
w.write(bitmap_block, checksum::BT::BITMAP)?;
// Return the adjusted index entry
Ok (IndexEntry {
Ok(IndexEntry {
blocknr: ie.blocknr,
nr_free,
none_free_before: first_free,
@@ -381,7 +381,7 @@ pub fn write_metadata_sm(w: &mut WriteBatcher, sm: &dyn SpaceMap) -> Result<SMRo
// Write out the metadata index
let metadata_index = MetadataIndex {
blocknr: bitmap_root.loc,
indexes
indexes,
};
let mut cur = Cursor::new(bitmap_root.get_data());
metadata_index.pack(&mut cur)?;

View File

@@ -1,6 +1,6 @@
use anyhow::{anyhow, Result};
use nom::{number::complete::*, IResult};
use byteorder::{LittleEndian, WriteBytesExt};
use nom::{number::complete::*, IResult};
//------------------------------------------