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)]
32mod tests {
33 use super::*;
34
35 #[test]
36 fn test_deterministic() {
37 let a = Partition::of(&[Value::Utf8("us".to_string())]);
38 let b = Partition::of(&[Value::Utf8("us".to_string())]);
39 assert_eq!(a, b, "same values must hash to the same partition");
40 }
41
42 #[test]
43 fn test_distinct_values_distinct_partition() {
44 let us = Partition::of(&[Value::Utf8("us".to_string())]);
45 let eu = Partition::of(&[Value::Utf8("eu".to_string())]);
46 assert_ne!(us, eu, "different values should (overwhelmingly) hash differently");
47 }
48
49 #[test]
50 fn test_multi_column() {
51 let a = Partition::of(&[Value::Utf8("us".to_string()), Value::Uint8(1)]);
52 let b = Partition::of(&[Value::Utf8("us".to_string()), Value::Uint8(2)]);
53 assert_ne!(a, b);
54 }
55}