Skip to content

Commit

Permalink
fix fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 committed Feb 3, 2023
1 parent 86ffde6 commit 6e7ffb6
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 197 deletions.
32 changes: 18 additions & 14 deletions fuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,68 +226,72 @@ pub trait DbSimulator {
};
Self::reset_model_from_database(&db.db, &mut layers, &old_layers);
},
Action::IterPrev =>
Action::IterPrev => {
if let Some(iter) = &mut db.iter {
let mut old_key = if let Some(old_key) = db.iter_current_key.take() {
old_key
} else {
retry_operation(|| iter.seek_to_last());
IterPosition::End
};
let new_key_value =
iter.prev().unwrap_or_else(|e| {
let new_key_value = iter
.prev()
.unwrap_or_else(|e| {
log::debug!("Database error: {}, restarting iter.prev without I/O limitations.", e);

// We ignore the error and reset the iterator
parity_db::set_number_of_allowed_io_operations(usize::MAX);
iter.seek_to_last().unwrap();
old_key = IterPosition::End;
iter.prev().unwrap()
});
})
.map(|(k, v)| (k.value().clone(), v.value().clone()));
let expected = Self::valid_iter_value(old_key, &layers, Ordering::Greater);
log::info!(
"Prev lookup on iterator with old position {:?}, expecting one of {:?}",
old_key,
expected
);
assert!(expected.contains(&new_key_value), "Prev lookup on iterator with old position {:?}, expecting one of {:?}, found {:?}",
old_key,
expected, new_key_value);
assert!(expected.contains(&new_key_value), "{}", "Prev lookup on iterator with old position {old_key:?}, expecting one of {expected:?}, found {new_key_value:?}");
db.iter_current_key = Some(
new_key_value
.map_or(IterPosition::Start, |(k, _)| IterPosition::Value(k[0])),
);
},
Action::IterNext =>
}
},
Action::IterNext => {
if let Some(iter) = &mut db.iter {
let mut old_key = if let Some(old_key) = db.iter_current_key.take() {
old_key
} else {
retry_operation(|| iter.seek_to_first());
IterPosition::Start
};
let new_key_value =
iter.next().unwrap_or_else(|e| {
let new_key_value = iter
.next()
.unwrap_or_else(|e| {
log::debug!("Database error: {}, restarting iter.next without I/O limitations.", e);

// We ignore the error and reset the iterator
parity_db::set_number_of_allowed_io_operations(usize::MAX);
iter.seek_to_first().unwrap();
old_key = IterPosition::Start;
iter.next().unwrap()
});
})
.map(|(k, v)| (k.value().clone(), v.value().clone()));
let expected = Self::valid_iter_value(old_key, &layers, Ordering::Less);
log::info!(
"Next lookup on iterator with old position {:?}, expecting one of {:?}",
old_key,
expected
);
assert!(expected.contains(&new_key_value), "Next lookup on iterator with old position {:?}, expecting one of {:?}, found {:?}", old_key, expected, new_key_value);
assert!(expected.contains(&new_key_value), "{}", "Next lookup on iterator with old position {old_key:?}, expecting one of {expected:?}, found {new_key_value:?}");
db.iter_current_key = Some(
new_key_value
.map_or(IterPosition::End, |(k, _)| IterPosition::Value(k[0])),
);
},
}
},
}
retry_operation(|| Self::check_db_and_model_are_equals(&db.db, &layers)).unwrap();
}
Expand Down
5 changes: 2 additions & 3 deletions src/btree/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ impl BTree {
root.set_child(1, right);
root.set_separator(0, sep);
},
(_, true) => {
(_, true) =>
if let Some((node_index, node)) = root.need_remove_root(btree, log)? {
self.depth -= 1;
if let Some(index) = self.root_index.take() {
BTreeTable::write_plan_remove_node(btree, log, index)?;
}
self.root_index = node_index;
root = node;
}
},
},
_ => (),
}
*changes = &changes[1..];
Expand Down
83 changes: 36 additions & 47 deletions src/btree/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ impl<'a> BTreeIterator<'a> {
let result = match (next_commit_overlay, next_backend) {
(Some((commit_key, commit_value)), Some((backend_key, backend_value))) => {
match (direction, commit_key.value().cmp(&backend_key)) {
(IterDirection::Backward, std::cmp::Ordering::Greater)
| (IterDirection::Forward, std::cmp::Ordering::Less) => {
(IterDirection::Backward, std::cmp::Ordering::Greater) |
(IterDirection::Forward, std::cmp::Ordering::Less) => {
self.pending_backend = Some(PendingBackend {
next_item: Some((backend_key, backend_value)),
direction,
Expand All @@ -162,21 +162,19 @@ impl<'a> BTreeIterator<'a> {
Some((commit_key, value))
} else {
self.last_key = LastKey::At(commit_key);
continue;
continue
}
},
(IterDirection::Backward, std::cmp::Ordering::Less)
| (IterDirection::Forward, std::cmp::Ordering::Greater) => {
Some((backend_key.into(), backend_value.into()))
},
(_, std::cmp::Ordering::Equal) => {
(IterDirection::Backward, std::cmp::Ordering::Less) |
(IterDirection::Forward, std::cmp::Ordering::Greater) =>
Some((backend_key.into(), backend_value.into())),
(_, std::cmp::Ordering::Equal) =>
if let Some(value) = commit_value {
Some((backend_key.into(), value))
} else {
self.last_key = LastKey::At(commit_key);
continue;
}
},
continue
},
}
},
(Some((commit_key, Some(commit_value))), None) => {
Expand All @@ -186,11 +184,10 @@ impl<'a> BTreeIterator<'a> {
(Some((k, None)), None) => {
self.pending_backend = Some(PendingBackend { next_item: None, direction });
self.last_key = LastKey::At(k);
continue;
},
(None, Some((backend_key, backend_value))) => {
Some((backend_key.into(), backend_value.into()))
continue
},
(None, Some((backend_key, backend_value))) =>
Some((backend_key.into(), backend_value.into())),
(None, None) => {
self.pending_backend = Some(PendingBackend { next_item: None, direction });
None
Expand All @@ -201,14 +198,13 @@ impl<'a> BTreeIterator<'a> {
Some((key, _)) => {
self.last_key = LastKey::At(key.clone());
},
None => {
None =>
self.last_key = match direction {
IterDirection::Backward => LastKey::Start,
IterDirection::Forward => LastKey::End,
}
},
},
}
return Ok(result);
return Ok(result)
}
}

Expand Down Expand Up @@ -296,15 +292,13 @@ impl BTreeIterState {
IterDirection::Backward if *child == 0 => continue,
IterDirection::Forward
if *child == ORDER || node.separators[*child].separator.is_none() =>
{
continue
},
continue,
_ => LastIndex::Before(*child),
};
} else {
self.state.clear(); // should actually be unreachable
}
return false;
return false
}
}
true
Expand Down Expand Up @@ -333,58 +327,53 @@ impl BTreeIterState {

(IterDirection::Forward, LastIndex::At(sep))
if is_leaf && *sep + 1 == ORDER =>
{
if self.exit(direction) {
break;
break
} else {
continue;
}
},
(IterDirection::Forward, LastIndex::At(sep)) if is_leaf => {
LastIndex::At(*sep + 1)
},
continue
},
(IterDirection::Forward, LastIndex::At(sep)) if is_leaf =>
LastIndex::At(*sep + 1),
(IterDirection::Forward, LastIndex::At(sep)) => LastIndex::Descend(*sep + 1),
(IterDirection::Forward, LastIndex::Before(sep)) if *sep == ORDER => {
if self.exit(direction) {
break;
break
} else {
continue;
continue
}
},
(IterDirection::Forward, LastIndex::Before(sep)) => LastIndex::At(*sep),

(IterDirection::Backward, LastIndex::At(sep)) if is_leaf && *sep == 0 => {
if self.exit(direction) {
break;
break
} else {
continue;
continue
}
},
(IterDirection::Backward, LastIndex::At(sep)) if is_leaf => {
LastIndex::At(*sep - 1)
},
(IterDirection::Backward, LastIndex::At(sep)) if is_leaf =>
LastIndex::At(*sep - 1),
(IterDirection::Backward, LastIndex::At(sep)) => LastIndex::Descend(*sep),
(IterDirection::Backward, LastIndex::Before(sep)) if *sep == 0 => {
if self.exit(direction) {
break;
break
} else {
continue;
continue
}
},
(IterDirection::Backward, LastIndex::Before(sep)) => LastIndex::At(*sep - 1),
};
match next {
LastIndex::At(at) => {
LastIndex::At(at) =>
if let Some(address) = state.1.separator_address(at) {
state.0 = LastIndex::At(at);
let key = state.1.separator_key(at).unwrap();
let key_query = TableKeyQuery::Fetch(None);
let r = col.get_at_value_index(key_query, address, log)?;
return Ok(r.map(|r| (key, r.1)));
return Ok(r.map(|r| (key, r.1)))
} else if self.exit(direction) {
break;
}
},
break
},
LastIndex::Descend(child_ix) => {
if let Some(child) =
col.with_locked(|btree| state.1.fetch_child(child_ix, btree, log))?
Expand All @@ -395,13 +384,13 @@ impl BTreeIterState {
let is_child_leaf = btree.depth as usize == self.state.len();
self.state.push((node_start(&child, direction, is_child_leaf), child))
} else if self.exit(direction) {
break;
break
}
},
_ => unreachable!(),
}
} else {
break;
break
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/btree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ impl Entry {

fn read_separator(&mut self) -> Option<SeparatorInner> {
if self.encoded.offset() == self.encoded.inner_mut().len() {
return None;
return None
}
let value = self.encoded.read_u64();
let head = self.encoded.read_slice(1);
let head = head[0];
let size = if head == u8::MAX { self.encoded.read_u32() as usize } else { head as usize };
let key = self.encoded.read_slice(size).to_vec();
if value == 0 {
return None;
return None
}
let value = Address::from_u64(value);
Some(SeparatorInner { key, value })
Expand Down Expand Up @@ -188,7 +188,7 @@ impl BTreeTable {
pub fn get(key: &[u8], log: &impl LogQuery, values: TablesRef) -> Result<Option<Vec<u8>>> {
let btree_header = Self::btree_header(log, values)?;
if btree_header.root == NULL_ADDRESS {
return Ok(None);
return Ok(None)
}
let record_id = 0; // lifetime of Btree is the query, so no invalidate.
// keeping log locked when parsing tree.
Expand Down Expand Up @@ -240,7 +240,7 @@ impl BTreeTable {
},
_ => {
log::error!(target: "parity-db", "Unexpected log action");
return Err(Error::Corruption("Unexpected log action".to_string()));
return Err(Error::Corruption("Unexpected log action".to_string()))
},
}
Ok(())
Expand Down Expand Up @@ -289,20 +289,20 @@ impl BTreeTable {
if child.moved {
node.changed = true;
} else if child.entry_index.is_none() {
break;
break
}
}

for separator in node.separators.as_mut().iter_mut() {
if separator.modified {
node.changed = true;
} else if separator.separator.is_none() {
break;
break
}
}

if !node.changed {
return Ok(None);
return Ok(None)
}

let mut entry = Entry::empty();
Expand All @@ -316,13 +316,13 @@ impl BTreeTable {
}
i_children += 1;
if i_children == ORDER_CHILD {
break;
break
}
if let Some(sep) = &node.separators.as_mut()[i_separator].separator {
entry.write_separator(&sep.key, sep.value);
i_separator += 1
} else {
break;
break
}
}

Expand Down Expand Up @@ -412,7 +412,7 @@ pub mod commit_overlay {
return Err(Error::InvalidInput(format!(
"No Rc for column {}",
self.col
)));
)))
}
},
}
Expand Down
Loading

0 comments on commit 6e7ffb6

Please sign in to comment.