Skip to main content

reifydb_value/value/partition/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use 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
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub enum ScanPartitions {
27	All,
28	One(Partition),
29	Many(Vec<Partition>),
30	Nothing,
31}
32
33impl From<u128> for Partition {
34	fn from(value: u128) -> Self {
35		Partition(value)
36	}
37}
38
39#[cfg(test)]
40#[allow(clippy::disallowed_methods)]
41mod tests {
42	use super::*;
43
44	#[test]
45	fn test_deterministic() {
46		let a = Partition::of(&[Value::Utf8("us".to_string())]);
47		let b = Partition::of(&[Value::Utf8("us".to_string())]);
48		assert_eq!(a, b, "same values must hash to the same partition");
49	}
50
51	#[test]
52	fn test_distinct_values_distinct_partition() {
53		let us = Partition::of(&[Value::Utf8("us".to_string())]);
54		let eu = Partition::of(&[Value::Utf8("eu".to_string())]);
55		assert_ne!(us, eu, "different values should (overwhelmingly) hash differently");
56	}
57
58	#[test]
59	fn test_multi_column() {
60		let a = Partition::of(&[Value::Utf8("us".to_string()), Value::Uint8(1)]);
61		let b = Partition::of(&[Value::Utf8("us".to_string()), Value::Uint8(2)]);
62		assert_ne!(a, b);
63	}
64}