pub const VSHARD_COUNT: u32 = 1024;
pub fn vshard_from_collection(array_name: &str) -> u32 {
let hash = array_name
.as_bytes()
.iter()
.fold(0u32, |h, &b| h.wrapping_mul(31).wrapping_add(b as u32));
hash % VSHARD_COUNT
}
fn vshard_from_key(key: &[u8]) -> u32 {
let mut h: u64 = 0;
for &b in key {
h = h.wrapping_mul(0x100000001B3).wrapping_add(b as u64);
}
(h % VSHARD_COUNT as u64) as u32
}
pub fn tile_id_of_coord(coord: &[u64], tile_extents: &[u64]) -> Option<u64> {
if coord.is_empty() || tile_extents.is_empty() {
return None;
}
if coord.len() != tile_extents.len() {
return None;
}
if tile_extents.contains(&0) {
return None;
}
let tile_indices: Vec<u64> = coord
.iter()
.zip(tile_extents.iter())
.map(|(&c, &e)| c / e)
.collect();
let n = tile_indices.len();
let mut tile_id: u64 = 0;
let mut stride: u64 = 1;
for i in (0..n).rev() {
tile_id = tile_id.wrapping_add(tile_indices[i].wrapping_mul(stride));
stride = stride.wrapping_mul(tile_indices[i].wrapping_add(1).max(1));
}
Some(tile_id)
}
pub fn vshard_for_array_coord(array_name: &str, coord: &[u64], tile_extents: &[u64]) -> u32 {
match tile_id_of_coord(coord, tile_extents) {
Some(tile_id) => vshard_for_array_tile(array_name, tile_id),
None => vshard_from_collection(array_name),
}
}
pub fn vshard_for_array_tile(array_name: &str, tile_id: u64) -> u32 {
let mut buf = Vec::with_capacity(array_name.len() + 8);
buf.extend_from_slice(array_name.as_bytes());
buf.extend_from_slice(&tile_id.to_le_bytes());
vshard_from_key(&buf)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tile_id_empty_coord_is_none() {
assert!(tile_id_of_coord(&[], &[]).is_none());
}
#[test]
fn tile_id_zero_extent_is_none() {
assert!(tile_id_of_coord(&[5], &[0]).is_none());
}
#[test]
fn tile_id_mismatched_lengths_is_none() {
assert!(tile_id_of_coord(&[1, 2], &[4]).is_none());
}
#[test]
fn tile_id_single_dim_same_tile() {
let t = tile_id_of_coord(&[9], &[10]).unwrap();
assert_eq!(t, tile_id_of_coord(&[0], &[10]).unwrap());
assert_eq!(t, 0);
}
#[test]
fn tile_id_single_dim_different_tiles() {
let t0 = tile_id_of_coord(&[0], &[10]).unwrap();
let t1 = tile_id_of_coord(&[10], &[10]).unwrap();
assert_ne!(
t0, t1,
"coords in different tiles must yield different tile_ids"
);
}
#[test]
fn tile_id_two_dim_row_major() {
let t00 = tile_id_of_coord(&[0, 0], &[4, 4]).unwrap();
let t03 = tile_id_of_coord(&[0, 3], &[4, 4]).unwrap();
assert_eq!(t00, t03, "same tile");
let t04 = tile_id_of_coord(&[0, 4], &[4, 4]).unwrap();
assert_ne!(t00, t04, "different tile column");
let t40 = tile_id_of_coord(&[4, 0], &[4, 4]).unwrap();
assert_ne!(t00, t40, "different tile row");
let t44 = tile_id_of_coord(&[4, 4], &[4, 4]).unwrap();
assert_ne!(t40, t44);
assert_ne!(t04, t44);
}
#[test]
fn collection_fallback_is_deterministic() {
let a = vshard_from_collection("prices");
let b = vshard_from_collection("prices");
assert_eq!(a, b);
assert!(a < VSHARD_COUNT);
}
#[test]
fn same_tile_gives_same_vshard() {
let s0 = vshard_for_array_coord("prices", &[0], &[10]);
let s9 = vshard_for_array_coord("prices", &[9], &[10]);
assert_eq!(s0, s9, "same tile → same shard");
}
#[test]
fn different_tiles_likely_different_vshards() {
let shards: Vec<u32> = (0u64..256)
.map(|i| vshard_for_array_coord("matrix", &[i], &[1]))
.collect();
let unique: std::collections::HashSet<_> = shards.iter().copied().collect();
assert!(
unique.len() >= 200,
"expected high shard diversity for 256 distinct tiles, got {}",
unique.len()
);
}
#[test]
fn different_arrays_same_tile_different_shards() {
let mut differs = 0usize;
for t in 0u64..64 {
let sa = vshard_for_array_tile("alpha", t);
let sb = vshard_for_array_tile("beta", t);
if sa != sb {
differs += 1;
}
}
assert!(
differs >= 32,
"expected most tiles to yield different shards for different arrays, got {differs}/64"
);
}
#[test]
fn fallback_on_empty_coord() {
let s = vshard_for_array_coord("prices", &[], &[]);
let expected = vshard_from_collection("prices");
assert_eq!(s, expected, "empty coord must fall back to from_collection");
}
#[test]
fn fallback_on_zero_extent() {
let s = vshard_for_array_coord("prices", &[5], &[0]);
let expected = vshard_from_collection("prices");
assert_eq!(s, expected, "zero extent must fall back to from_collection");
}
#[test]
fn fallback_on_mismatched_dims() {
let s = vshard_for_array_coord("prices", &[1, 2], &[4]);
let expected = vshard_from_collection("prices");
assert_eq!(
s, expected,
"mismatched dims must fall back to from_collection"
);
}
#[test]
fn tile_fn_matches_coord_fn_for_same_tile() {
let coord = &[7u64, 3];
let extents = &[4u64, 4];
let tile_id = tile_id_of_coord(coord, extents).unwrap();
let from_coord = vshard_for_array_coord("mat", coord, extents);
let from_tile = vshard_for_array_tile("mat", tile_id);
assert_eq!(
from_coord, from_tile,
"vshard_for_array_coord and vshard_for_array_tile must agree"
);
}
#[test]
fn all_vshards_in_range() {
for t in 0u64..1024 {
let s = vshard_for_array_tile("arr", t);
assert!(s < VSHARD_COUNT, "vshard {s} out of range for tile {t}");
}
}
}