use std::cmp::Ordering;
use gxhash::gxhash64;
use super::txn_key_entry::TxnKeyEntry;
const KEY_HASH_SEED: i64 = 0;
pub struct TxnKeyEntryComparison;
impl TxnKeyEntryComparison {
#[inline]
pub fn key_hash(key: &[u8]) -> i64 {
gxhash64(key, KEY_HASH_SEED) as i64
}
pub fn compare(key1: &TxnKeyEntry, key2: &TxnKeyEntry) -> Ordering {
key1
.key_hash
.cmp(&key2.key_hash)
.then(key2.lock_type.cmp(&key1.lock_type))
}
}
#[cfg(test)]
mod tests {
use super::{super::txn_key_entry::LockType, *};
#[test]
fn compare_orders_by_hash_then_lock_strength() {
let a = TxnKeyEntry::new(1, LockType::Shared);
let b = TxnKeyEntry::new(2, LockType::Shared);
let x = TxnKeyEntry::new(3, LockType::Exclusive);
let s = TxnKeyEntry::new(3, LockType::Shared);
assert_eq!(TxnKeyEntryComparison::compare(&a, &b), Ordering::Less);
assert_eq!(TxnKeyEntryComparison::compare(&x, &s), Ordering::Less);
assert_eq!(TxnKeyEntryComparison::compare(&s, &x), Ordering::Greater);
}
#[test]
fn key_hash_is_signed_bitface_of_gxhash() {
let hash = TxnKeyEntryComparison::key_hash(b"some-key");
let _ = hash as u64; }
}