Skip to content

Commit

Permalink
fixup!: fix clippy
Browse files Browse the repository at this point in the history
Signed-off-by: bsbds <[email protected]>
  • Loading branch information
bsbds committed May 9, 2024
1 parent 824a9fe commit 9869ce6
Showing 1 changed file with 33 additions and 55 deletions.
88 changes: 33 additions & 55 deletions crates/xline/src/storage/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Index {
/// Creates a transaction
pub(crate) fn state(&self) -> IndexState<'_> {
IndexState {
index_ref: &self,
index_ref: self,
state: Mutex::default(),
}
}
Expand Down Expand Up @@ -237,6 +237,7 @@ impl Index {
}
}

/// Maps a closure to an entry
fn map_entry<F, R>(mut op: F) -> impl FnMut(Entry<Vec<u8>, RwLock<Vec<KeyRevision>>>) -> R
where
F: FnMut((&[u8], &[KeyRevision])) -> R,
Expand All @@ -246,24 +247,15 @@ where
}
}

fn map_entry_mut<F, R>(mut op: F) -> impl FnMut(Entry<Vec<u8>, RwLock<Vec<KeyRevision>>>) -> R
where
F: FnMut((&[u8], &mut Vec<KeyRevision>)) -> R,
{
move |entry: Entry<Vec<u8>, RwLock<Vec<KeyRevision>>>| {
entry
.value()
.map_write(|mut revs| op((entry.key(), &mut revs)))
}
}

/// Maps a closure to an entry value
fn map_value<F, R>(mut op: F) -> impl FnMut(Entry<Vec<u8>, RwLock<Vec<KeyRevision>>>) -> R
where
F: FnMut(&[KeyRevision]) -> R,
{
move |entry: Entry<Vec<u8>, RwLock<Vec<KeyRevision>>>| entry.value().map_read(|revs| op(&revs))
}

/// Mutablely maps a closure to an entry value
fn map_value_mut<F, R>(mut op: F) -> impl FnMut(Entry<Vec<u8>, RwLock<Vec<KeyRevision>>>) -> R
where
F: FnMut(&mut Vec<KeyRevision>) -> R,
Expand All @@ -279,24 +271,18 @@ impl IndexOperate for Index {
RangeType::OneKey => self
.inner
.get(key)
.and_then(map_value(|revs| {
Index::get_revision(revs.as_ref(), revision)
}))
.and_then(map_value(|revs| Index::get_revision(revs, revision)))
.map(|rev| vec![rev])
.unwrap_or_default(),
RangeType::AllKeys => self
.inner
.iter()
.filter_map(map_value(|revs| {
Index::get_revision(revs.as_ref(), revision)
}))
.filter_map(map_value(|revs| Index::get_revision(revs, revision)))
.collect(),
RangeType::Range => self
.inner
.range(KeyRange::new(key, range_end))
.filter_map(map_value(|revs| {
Index::get_revision(revs.as_ref(), revision)
}))
.filter_map(map_value(|revs| Index::get_revision(revs, revision)))
.collect(),
}
}
Expand All @@ -307,13 +293,16 @@ impl IndexOperate for Index {
revision: i64,
sub_revision: i64,
) -> (KeyRevision, Option<KeyRevision>) {
self.inner
.get(&key)
.map(map_value_mut(|revisions| {
let last = revisions
self.inner.get(&key).map_or_else(
|| {
let new_rev = KeyRevision::new(revision, 1, revision, sub_revision);
let _ignore = self.inner.insert(key, RwLock::new(vec![new_rev]));
(new_rev, None)
},
map_value_mut(|revisions| {
let last = *revisions
.last()
.unwrap_or_else(|| unreachable!("empty revision list"))
.clone();
.unwrap_or_else(|| unreachable!("empty revision list"));
let new_rev = if last.is_deleted() {
KeyRevision::new(revision, 1, revision, sub_revision)
} else {
Expand All @@ -326,12 +315,8 @@ impl IndexOperate for Index {
};
revisions.push(new_rev);
(new_rev, Some(last))
}))
.unwrap_or_else(|| {
let new_rev = KeyRevision::new(revision, 1, revision, sub_revision);
let _ignore = self.inner.insert(key, RwLock::new(vec![new_rev]));
(new_rev, None)
})
}),
)
}

fn prev_rev(&self, key: &[u8]) -> Option<KeyRevision> {
Expand All @@ -342,14 +327,14 @@ impl IndexOperate for Index {

fn insert(&self, key_revisions: Vec<(Vec<u8>, KeyRevision)>) {
for (key, revision) in key_revisions {
self.inner
.get(&key)
.map(map_value_mut(|revs| {
revs.push(revision);
}))
.unwrap_or_else(|| {
self.inner.get(&key).map_or_else(
|| {
let _ignore = self.inner.insert(key, RwLock::new(vec![revision]));
});
},
map_value_mut(|revs| {
revs.push(revision);
}),
);
}
}

Expand Down Expand Up @@ -436,10 +421,10 @@ impl IndexState<'_> {
let state = self.state.lock();
let mut result = index
.get(key)
.map(map_value(|revs| revs.to_vec()))
.map(map_value(<[KeyRevision]>::to_vec))
.unwrap_or_default();
if let Some(revs) = state.get(key) {
result.extend_from_slice(&revs);
result.extend_from_slice(revs);
}
result
}
Expand Down Expand Up @@ -486,7 +471,7 @@ impl IndexState<'_> {
let mut state = self.state.lock();
let revs = self.one_key_revisions(key);
let last_available_rev = Index::get_revision(&revs, 0)?;
let entry = state.entry(key.to_vec()).or_insert(vec![]);
let entry = state.entry(key.to_vec()).or_default();
let del_rev = KeyRevision::new_deletion(revision, sub_revision);
entry.push(del_rev);
let pair = (last_available_rev, del_rev.as_revision());
Expand Down Expand Up @@ -557,10 +542,9 @@ impl IndexOperate for IndexState<'_> {
let mut state = self.state.lock();

let next_rev = |revisions: &[KeyRevision]| {
let last = revisions
let last = *revisions
.last()
.unwrap_or_else(|| unreachable!("empty revision list"))
.clone();
.unwrap_or_else(|| unreachable!("empty revision list"));
let new_rev = if last.is_deleted() {
KeyRevision::new(revision, 1, revision, sub_revision)
} else {
Expand All @@ -580,7 +564,7 @@ impl IndexOperate for IndexState<'_> {
let _ignore = e.insert(vec![new_rev]);
(new_rev, None)
}
(None, btree_map::Entry::Occupied(mut e)) => {
(None | Some(_), btree_map::Entry::Occupied(mut e)) => {
let (new_rev, last) = next_rev(e.get_mut());
e.get_mut().push(new_rev);
(new_rev, last)
Expand All @@ -590,11 +574,6 @@ impl IndexOperate for IndexState<'_> {
let _ignore = se.insert(vec![new_rev]);
(new_rev, last)
}
(Some(_), btree_map::Entry::Occupied(mut e)) => {
let (new_rev, last) = next_rev(e.get_mut());
e.get_mut().push(new_rev);
(new_rev, last)
}
}
}

Expand All @@ -604,9 +583,8 @@ impl IndexOperate for IndexState<'_> {

match (index.get(key), state.get(key)) {
(None, None) => None,
(None, Some(revs)) => revs.last().copied(),
(None | Some(_), Some(revs)) => revs.last().copied(),
(Some(e), None) => map_value(|revs| revs.last().copied())(e),
(Some(_), Some(revs)) => revs.last().copied(),
}
}

Expand Down Expand Up @@ -648,7 +626,7 @@ mod test {
use super::*;
#[allow(clippy::expect_used)]
fn match_values(index: &Index, key: impl AsRef<[u8]>, expected_values: &[KeyRevision]) {
let revs = index
index
.inner
.get(key.as_ref())
.map(map_value(|revs| assert_eq!(revs, expected_values)))
Expand Down

0 comments on commit 9869ce6

Please sign in to comment.