reifydb_value/value/partition/
mod.rs1use postcard::to_extend;
5use serde::{Deserialize, Serialize};
6
7use super::Value;
8use crate::util::hash::xxh3_128;
9
10#[repr(transparent)]
11#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
12#[serde(transparent)]
13pub struct Partition(pub u128);
14
15impl Partition {
16 pub fn of(values: &[Value]) -> Self {
17 let mut buf: Vec<u8> = Vec::new();
18 for value in values {
19 buf = to_extend(value, buf).expect("postcard serialization of a Value is total");
20 }
21 Partition(xxh3_128(&buf).0)
22 }
23}
24
25impl From<u128> for Partition {
26 fn from(value: u128) -> Self {
27 Partition(value)
28 }
29}
30
31#[cfg(test)]
32#[allow(clippy::disallowed_methods)]
33mod tests {
34 use super::*;
35
36 #[test]
37 fn test_deterministic() {
38 let a = Partition::of(&[Value::Utf8("us".to_string())]);
39 let b = Partition::of(&[Value::Utf8("us".to_string())]);
40 assert_eq!(a, b, "same values must hash to the same partition");
41 }
42
43 #[test]
44 fn test_distinct_values_distinct_partition() {
45 let us = Partition::of(&[Value::Utf8("us".to_string())]);
46 let eu = Partition::of(&[Value::Utf8("eu".to_string())]);
47 assert_ne!(us, eu, "different values should (overwhelmingly) hash differently");
48 }
49
50 #[test]
51 fn test_multi_column() {
52 let a = Partition::of(&[Value::Utf8("us".to_string()), Value::Uint8(1)]);
53 let b = Partition::of(&[Value::Utf8("us".to_string()), Value::Uint8(2)]);
54 assert_ne!(a, b);
55 }
56}