make triedbmut lookup shorter

This commit is contained in:
debris 2017-08-29 12:31:40 +02:00
parent e390e6b0af
commit 100d1c7bf6

View File

@ -370,62 +370,47 @@ impl<'a> TrieDBMut<'a> {
fn lookup<'x, 'key>(&'x self, mut partial: NibbleSlice<'key>, handle: &NodeHandle) -> super::Result<Option<DBValue>> fn lookup<'x, 'key>(&'x self, mut partial: NibbleSlice<'key>, handle: &NodeHandle) -> super::Result<Option<DBValue>>
where 'x: 'key where 'x: 'key
{ {
enum Step<'s> {
Return(Option<DBValue>),
Lookup(usize, &'s NodeHandle),
}
let mut handle = handle; let mut handle = handle;
loop { loop {
let step = { let (mid, child) = match *handle {
match *handle { NodeHandle::Hash(ref hash) => return Lookup {
NodeHandle::Hash(ref hash) => { db: &*self.db,
let lookup = Lookup { query: DBValue::from_slice,
db: &*self.db, hash: hash.clone(),
query: DBValue::from_slice, }.look_up(partial),
hash: hash.clone(), NodeHandle::InMemory(ref handle) => match self.storage[handle] {
}.look_up(partial)?; Node::Empty => return Ok(None),
Step::Return(lookup) Node::Leaf(ref key, ref value) => {
}, if NibbleSlice::from_encoded(key).0 == partial {
NodeHandle::InMemory(ref handle) => match self.storage[handle] { return Ok(Some(DBValue::from_slice(value)));
Node::Empty => Step::Return(None), } else {
Node::Leaf(ref key, ref value) => { return Ok(None);
if NibbleSlice::from_encoded(key).0 == partial {
Step::Return(Some(DBValue::from_slice(value)))
} else {
Step::Return(None)
}
} }
Node::Extension(ref slice, ref child) => { }
let slice = NibbleSlice::from_encoded(slice).0; Node::Extension(ref slice, ref child) => {
if partial.starts_with(&slice) { let slice = NibbleSlice::from_encoded(slice).0;
Step::Lookup(slice.len(), child) if partial.starts_with(&slice) {
} else { (slice.len(), child)
Step::Return(None) } else {
} return Ok(None);
} }
Node::Branch(ref children, ref value) => { }
if partial.is_empty() { Node::Branch(ref children, ref value) => {
Step::Return(value.as_ref().map(|v| DBValue::from_slice(v))) if partial.is_empty() {
} else { return Ok(value.as_ref().map(|v| DBValue::from_slice(v)));
let idx = partial.at(0); } else {
match children[idx as usize].as_ref() { let idx = partial.at(0);
Some(child) => Step::Lookup(1, child), match children[idx as usize].as_ref() {
None => Step::Return(None), Some(child) => (1, child),
} None => return Ok(None),
} }
} }
} }
} }
}; };
match step { partial = partial.mid(mid);
Step::Return(r) => return Ok(r), handle = child;
Step::Lookup(mid, child) => {
partial = partial.mid(mid);
handle = child;
}
}
} }
} }