Skip to content

Commit

Permalink
feat: sort lock key in lexicographically order
Browse files Browse the repository at this point in the history
  • Loading branch information
WenyXu committed Jan 1, 2024
1 parent 8f02038 commit eb72343
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/common/procedure/src/procedure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,17 @@ impl<T: Procedure + ?Sized> Procedure for Box<T> {
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum StringKey {
Share(String),
Exclusive(String),
}

/// Keys to identify required locks.
///
/// [LockKey] respect the input order.
// Most procedures should only acquire 1 ~ 2 locks so we use smallvec to hold keys.
/// [LockKey] always sorts keys lexicographically so that they can be acquired
/// in the same order.
/// Most procedures should only acquire 1 ~ 2 locks so we use smallvec to hold keys.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LockKey(SmallVec<[StringKey; 2]>);

Expand Down Expand Up @@ -159,6 +160,7 @@ impl LockKey {
/// Returns a new [LockKey] with keys from specific `iter`.
pub fn new(iter: impl IntoIterator<Item = StringKey>) -> LockKey {
let mut vec: SmallVec<_> = iter.into_iter().collect();
vec.sort();
// Dedup keys to avoid acquiring the same key multiple times.
vec.dedup();
LockKey(vec)
Expand Down

0 comments on commit eb72343

Please sign in to comment.