fixed TrieDb::seek accordingly to review suggestions

This commit is contained in:
debris 2017-08-28 15:08:16 +02:00
parent 7246c9c74b
commit 8019d222a7
2 changed files with 16 additions and 23 deletions

View File

@ -182,8 +182,11 @@ impl HashDB for MemoryDB {
fn keys(&self) -> HashMap<H256, i32> { fn keys(&self) -> HashMap<H256, i32> {
self.data.iter() self.data.iter()
.filter(|&(_, v)| v.1 != 0) .filter_map(|(k, v)| if v.1 != 0 {
.map(|(k, v)| (*k, v.1)) Some((*k, v.1))
} else {
None
})
.collect() .collect()
} }

View File

@ -217,14 +217,9 @@ impl<'a> TrieDBIterator<'a> {
Ok(r) Ok(r)
} }
fn seek_descend<'key>(&mut self, mut node_data: DBValue, mut key: NibbleSlice<'key>) -> super::Result<()> { fn seek<'key>(&mut self, mut node_data: DBValue, mut key: NibbleSlice<'key>) -> super::Result<()> {
enum Step {
Descend(DBValue, usize),
Return,
}
loop { loop {
let step = { let (data, mid) = {
let node = Node::decoded(&node_data); let node = Node::decoded(&node_data);
match node { match node {
Node::Leaf(slice, _) => { Node::Leaf(slice, _) => {
@ -241,7 +236,7 @@ impl<'a> TrieDBIterator<'a> {
} }
self.key_nibbles.extend(slice.iter()); self.key_nibbles.extend(slice.iter());
Step::Return return Ok(())
}, },
Node::Extension(ref slice, ref item) => { Node::Extension(ref slice, ref item) => {
if key.starts_with(slice) { if key.starts_with(slice) {
@ -251,10 +246,10 @@ impl<'a> TrieDBIterator<'a> {
}); });
self.key_nibbles.extend(slice.iter()); self.key_nibbles.extend(slice.iter());
let data = self.db.get_raw_or_lookup(&*item)?; let data = self.db.get_raw_or_lookup(&*item)?;
Step::Descend(data, slice.len()) (data, slice.len())
} else { } else {
self.descend(&node_data)?; self.descend(&node_data)?;
Step::Return return Ok(())
} }
}, },
Node::Branch(ref nodes, _) => match key.is_empty() { Node::Branch(ref nodes, _) => match key.is_empty() {
@ -263,7 +258,7 @@ impl<'a> TrieDBIterator<'a> {
status: Status::At, status: Status::At,
node: node.clone().into(), node: node.clone().into(),
}); });
Step::Return return Ok(())
}, },
false => { false => {
let i = key.at(0); let i = key.at(0);
@ -273,20 +268,15 @@ impl<'a> TrieDBIterator<'a> {
}); });
self.key_nibbles.push(i); self.key_nibbles.push(i);
let child = self.db.get_raw_or_lookup(&*nodes[i as usize])?; let child = self.db.get_raw_or_lookup(&*nodes[i as usize])?;
Step::Descend(child, 1) (child, 1)
} }
}, },
_ => Step::Return _ => return Ok(()),
} }
}; };
match step { node_data = data;
Step::Return => return Ok(()), key = key.mid(mid);
Step::Descend(data, mid) => {
node_data = data;
key = key.mid(mid);
}
}
} }
} }
@ -331,7 +321,7 @@ impl<'a> TrieIterator for TrieDBIterator<'a> {
self.trail.clear(); self.trail.clear();
self.key_nibbles.clear(); self.key_nibbles.clear();
let root_rlp = self.db.root_data()?; let root_rlp = self.db.root_data()?;
self.seek_descend(root_rlp, NibbleSlice::new(key)) self.seek(root_rlp, NibbleSlice::new(key))
} }
} }