risingwave_hummock_sdk/
key_cmp.rsuse std::cmp::{self, Ordering};
use super::key::split_key_epoch;
use crate::key::UserKey;
pub struct KeyComparator;
impl KeyComparator {
#[inline]
pub fn compare_encoded_full_key(lhs: &[u8], rhs: &[u8]) -> cmp::Ordering {
let (l_p, l_s) = split_key_epoch(lhs);
let (r_p, r_s) = split_key_epoch(rhs);
l_p.cmp(r_p).then_with(|| r_s.cmp(l_s))
}
#[inline]
pub fn encoded_full_key_less_than(lhs: &[u8], rhs: &[u8]) -> bool {
Self::compare_encoded_full_key(lhs, rhs) == cmp::Ordering::Less
}
pub fn compare_user_key_cross_format(
encoded: impl AsRef<[u8]>,
unencoded: &UserKey<impl AsRef<[u8]>>,
) -> Ordering {
UserKey::decode(encoded.as_ref()).cmp(&unencoded.as_ref())
}
#[inline(always)]
pub fn encoded_less_than_unencoded(
encoded: impl AsRef<[u8]>,
unencoded: &UserKey<impl AsRef<[u8]>>,
) -> bool {
Self::compare_user_key_cross_format(encoded, unencoded) == Ordering::Less
}
#[inline(always)]
pub fn encoded_less_equal_unencoded(
encoded: impl AsRef<[u8]>,
unencoded: &UserKey<impl AsRef<[u8]>>,
) -> bool {
Self::compare_user_key_cross_format(encoded, unencoded) != Ordering::Greater
}
#[inline(always)]
pub fn encoded_greater_than_unencoded(
encoded: impl AsRef<[u8]>,
unencoded: &UserKey<impl AsRef<[u8]>>,
) -> bool {
Self::compare_user_key_cross_format(encoded, unencoded) == Ordering::Greater
}
}
#[cfg(test)]
mod tests {
use std::cmp::Ordering;
use risingwave_common::catalog::TableId;
use risingwave_common::util::epoch::test_epoch;
use crate::key::{FullKey, UserKey};
use crate::KeyComparator;
#[test]
fn test_cmp_encoded_full_key() {
let epoch = test_epoch(1);
let epoch2 = test_epoch(2);
let key1 = FullKey::for_test(TableId::new(0), b"0".to_vec(), epoch);
let key2 = FullKey::for_test(TableId::new(1), b"0".to_vec(), epoch);
let key3 = FullKey::for_test(TableId::new(1), b"1".to_vec(), epoch2);
let key4 = FullKey::for_test(TableId::new(1), b"1".to_vec(), epoch);
assert_eq!(
KeyComparator::compare_encoded_full_key(&key1.encode(), &key1.encode()),
Ordering::Equal
);
assert_eq!(
KeyComparator::compare_encoded_full_key(&key1.encode(), &key2.encode()),
Ordering::Less
);
assert_eq!(
KeyComparator::compare_encoded_full_key(&key2.encode(), &key3.encode()),
Ordering::Less
);
assert_eq!(
KeyComparator::compare_encoded_full_key(&key3.encode(), &key4.encode()),
Ordering::Less
);
}
#[test]
fn test_cmp_user_key_cross_format() {
let key1 = UserKey::for_test(TableId::new(0), b"0".to_vec());
let key2 = UserKey::for_test(TableId::new(0), b"1".to_vec());
let key3 = UserKey::for_test(TableId::new(1), b"0".to_vec());
assert_eq!(
KeyComparator::compare_user_key_cross_format(key1.encode(), &key1),
Ordering::Equal
);
assert_eq!(
KeyComparator::compare_user_key_cross_format(key1.encode(), &key2),
Ordering::Less
);
assert_eq!(
KeyComparator::compare_user_key_cross_format(key2.encode(), &key3),
Ordering::Less
);
}
}