make triedbmut lookup shorter
This commit is contained in:
parent
e390e6b0af
commit
100d1c7bf6
@ -370,64 +370,49 @@ 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) => {
|
|
||||||
let lookup = Lookup {
|
|
||||||
db: &*self.db,
|
db: &*self.db,
|
||||||
query: DBValue::from_slice,
|
query: DBValue::from_slice,
|
||||||
hash: hash.clone(),
|
hash: hash.clone(),
|
||||||
}.look_up(partial)?;
|
}.look_up(partial),
|
||||||
Step::Return(lookup)
|
|
||||||
},
|
|
||||||
NodeHandle::InMemory(ref handle) => match self.storage[handle] {
|
NodeHandle::InMemory(ref handle) => match self.storage[handle] {
|
||||||
Node::Empty => Step::Return(None),
|
Node::Empty => return Ok(None),
|
||||||
Node::Leaf(ref key, ref value) => {
|
Node::Leaf(ref key, ref value) => {
|
||||||
if NibbleSlice::from_encoded(key).0 == partial {
|
if NibbleSlice::from_encoded(key).0 == partial {
|
||||||
Step::Return(Some(DBValue::from_slice(value)))
|
return Ok(Some(DBValue::from_slice(value)));
|
||||||
} else {
|
} else {
|
||||||
Step::Return(None)
|
return Ok(None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Node::Extension(ref slice, ref child) => {
|
Node::Extension(ref slice, ref child) => {
|
||||||
let slice = NibbleSlice::from_encoded(slice).0;
|
let slice = NibbleSlice::from_encoded(slice).0;
|
||||||
if partial.starts_with(&slice) {
|
if partial.starts_with(&slice) {
|
||||||
Step::Lookup(slice.len(), child)
|
(slice.len(), child)
|
||||||
} else {
|
} else {
|
||||||
Step::Return(None)
|
return Ok(None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Node::Branch(ref children, ref value) => {
|
Node::Branch(ref children, ref value) => {
|
||||||
if partial.is_empty() {
|
if partial.is_empty() {
|
||||||
Step::Return(value.as_ref().map(|v| DBValue::from_slice(v)))
|
return Ok(value.as_ref().map(|v| DBValue::from_slice(v)));
|
||||||
} else {
|
} else {
|
||||||
let idx = partial.at(0);
|
let idx = partial.at(0);
|
||||||
match children[idx as usize].as_ref() {
|
match children[idx as usize].as_ref() {
|
||||||
Some(child) => Step::Lookup(1, child),
|
Some(child) => (1, child),
|
||||||
None => Step::Return(None),
|
None => return Ok(None),
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match step {
|
|
||||||
Step::Return(r) => return Ok(r),
|
|
||||||
Step::Lookup(mid, child) => {
|
|
||||||
partial = partial.mid(mid);
|
partial = partial.mid(mid);
|
||||||
handle = child;
|
handle = child;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// insert a key, value pair into the trie, creating new nodes if necessary.
|
/// insert a key, value pair into the trie, creating new nodes if necessary.
|
||||||
fn insert_at(&mut self, handle: NodeHandle, partial: NibbleSlice, value: DBValue, old_val: &mut Option<DBValue>)
|
fn insert_at(&mut self, handle: NodeHandle, partial: NibbleSlice, value: DBValue, old_val: &mut Option<DBValue>)
|
||||||
|
Loading…
Reference in New Issue
Block a user