[thin_check (rust)] NodeVisitor only needs to see leaf nodes

This commit is contained in:
Joe Thornber 2020-08-20 10:55:38 +01:00
parent 936e06e132
commit a1c206b774
2 changed files with 48 additions and 65 deletions

View File

@ -147,7 +147,7 @@ pub fn unpack_node<V: Unpack>(
//------------------------------------------ //------------------------------------------
pub trait NodeVisitor<V: Unpack> { pub trait NodeVisitor<V: Unpack> {
fn visit(&mut self, node: &Node<V>) -> Result<()>; fn visit(&mut self, header: &NodeHeader, keys: &[u64], values: &[V]) -> Result<()>;
} }
#[derive(Clone)] #[derive(Clone)]
@ -219,21 +219,26 @@ impl BTreeWalker {
NV: NodeVisitor<V>, NV: NodeVisitor<V>,
V: Unpack, V: Unpack,
{ {
use Node::*;
let bt = checksum::metadata_block_type(b.get_data()); let bt = checksum::metadata_block_type(b.get_data());
if bt != checksum::BT::NODE { if bt != checksum::BT::NODE {
return Err(anyhow!("checksum failed for node {}, {:?}", b.loc, bt)); return Err(anyhow!("checksum failed for node {}, {:?}", b.loc, bt));
} }
let node = unpack_node::<V>(&b.get_data(), self.ignore_non_fatal, is_root)?; let node = unpack_node::<V>(&b.get_data(), self.ignore_non_fatal, is_root)?;
visitor.visit(&node)?;
if let Node::Internal { match node {
header: _h, Internal {
keys: _k, header: _h,
values, keys: _k,
} = node values,
{ } => {
self.walk_nodes(visitor, &values)?; self.walk_nodes(visitor, &values)?;
}
Leaf { header, keys, values } => {
visitor.visit(&header, &keys, &values)?;
}
} }
Ok(()) Ok(())
@ -281,18 +286,11 @@ impl<V> ValueCollector<V> {
} }
impl<V: Unpack + Clone> NodeVisitor<V> for ValueCollector<V> { impl<V: Unpack + Clone> NodeVisitor<V> for ValueCollector<V> {
fn visit(&mut self, node: &Node<V>) -> Result<()> { fn visit(&mut self, _h: &NodeHeader, keys: &[u64], values: &[V]) -> Result<()> {
if let Node::Leaf { for n in 0..keys.len() {
header: _h, let k = keys[n];
keys, let v = values[n].clone();
values, self.values.insert(k, v);
} = node
{
for n in 0..keys.len() {
let k = keys[n];
let v = values[n].clone();
self.values.insert(k, v);
}
} }
Ok(()) Ok(())

View File

@ -9,7 +9,7 @@ use threadpool::ThreadPool;
use crate::checksum; use crate::checksum;
use crate::io_engine::{AsyncIoEngine, Block, IoEngine, SyncIoEngine}; use crate::io_engine::{AsyncIoEngine, Block, IoEngine, SyncIoEngine};
use crate::pdata::btree::{btree_to_map, btree_to_map_with_sm, BTreeWalker, Node, NodeVisitor}; use crate::pdata::btree::{btree_to_map, btree_to_map_with_sm, BTreeWalker, NodeHeader, NodeVisitor};
use crate::pdata::space_map::*; use crate::pdata::space_map::*;
use crate::pdata::unpack::*; use crate::pdata::unpack::*;
use crate::report::*; use crate::report::*;
@ -50,38 +50,30 @@ struct BottomLevelVisitor {
//------------------------------------------ //------------------------------------------
impl NodeVisitor<BlockTime> for BottomLevelVisitor { impl NodeVisitor<BlockTime> for BottomLevelVisitor {
fn visit(&mut self, node: &Node<BlockTime>) -> Result<()> { fn visit(&mut self, _h: &NodeHeader, _k: &[u64], values: &[BlockTime]) -> Result<()> {
// FIXME: do other checks // FIXME: do other checks
if let Node::Leaf { if values.len() == 0 {
header: _h, return Ok(());
keys: _k,
values,
} = node
{
if values.len() == 0 {
return Ok(());
}
let mut data_sm = self.data_sm.lock().unwrap();
let mut start = values[0].block;
let mut len = 1;
for n in 1..values.len() {
let block = values[n].block;
if block == start + len {
len += 1;
} else {
data_sm.inc(start, len)?;
start = block;
len = 1;
}
}
data_sm.inc(start, len)?;
} }
let mut data_sm = self.data_sm.lock().unwrap();
let mut start = values[0].block;
let mut len = 1;
for n in 1..values.len() {
let block = values[n].block;
if block == start + len {
len += 1;
} else {
data_sm.inc(start, len)?;
start = block;
len = 1;
}
}
data_sm.inc(start, len)?;
Ok(()) Ok(())
} }
} }
@ -132,21 +124,14 @@ impl<'a> OverflowChecker<'a> {
} }
impl<'a> NodeVisitor<u32> for OverflowChecker<'a> { impl<'a> NodeVisitor<u32> for OverflowChecker<'a> {
fn visit(&mut self, node: &Node<u32>) -> Result<()> { fn visit(&mut self, _h: &NodeHeader, keys: &[u64], values: &[u32]) -> Result<()> {
if let Node::Leaf { for n in 0..keys.len() {
header: _h, let k = keys[n];
keys, let v = values[n];
values, let expected = self.data_sm.get(k)?;
} = node if expected != v {
{ return Err(anyhow!("Bad reference count for data block {}. Expected {}, but space map contains {}.",
for n in 0..keys.len() { k, expected, v));
let k = keys[n];
let v = values[n];
let expected = self.data_sm.get(k)?;
if expected != v {
return Err(anyhow!("Bad reference count for data block {}. Expected {}, but space map contains {}.",
k, expected, v));
}
} }
} }