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 {
20 let mut buf: Vec<u8> = Vec::new();
21 for value in values {
22 buf = to_extend(value, buf).expect("postcard serialization of a Value is total");
23 }
24 Partition(xxh3_128(&buf).0)
25 }
26}
27
28impl From<u128> for Partition {
29 fn from(value: u128) -> Self {
30 Partition(value)
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37
38 #[test]
39 fn test_deterministic() {
40 let a = Partition::of(&[Value::Utf8("us".to_string())]);
41 let b = Partition::of(&[Value::Utf8("us".to_string())]);
42 assert_eq!(a, b, "same values must hash to the same partition");
43 }
44
45 #[test]
46 fn test_distinct_values_distinct_partition() {
47 let us = Partition::of(&[Value::Utf8("us".to_string())]);
48 let eu = Partition::of(&[Value::Utf8("eu".to_string())]);
49 assert_ne!(us, eu, "different values should (overwhelmingly) hash differently");
50 }
51
52 #[test]
53 fn test_multi_column() {
54 let a = Partition::of(&[Value::Utf8("us".to_string()), Value::Uint8(1)]);
55 let b = Partition::of(&[Value::Utf8("us".to_string()), Value::Uint8(2)]);
56 assert_ne!(a, b);
57 }
58}