use std::cell::Cell;
use relate_core::Value;
use super::*;
struct Build {
tail_growths: usize,
capacity: usize,
buckets: usize,
}
fn build(n: i64, value_of: impl Fn(i64) -> i64, distinct: Option<u64>) -> Build {
let mut st = PropPairIndex::default();
let last = Cell::new(0usize);
let growths = Cell::new(0usize);
let feed = |id: i64, st: &mut PropPairIndex| {
let gk = GroupKey::for_property_index(&Value::Int(value_of(id))).unwrap();
st.postings.post(&gk, id);
let cap = st.postings.capacities().0;
if id > PRESIZE_PREFIX as i64 && cap != last.get() {
growths.set(growths.get() + 1);
}
last.set(cap);
};
feed_build((0..n).collect(), &mut st, &feed, distinct);
Build {
tail_growths: growths.get(),
capacity: st.postings.capacities().0,
buckets: st.postings.len(),
}
}
#[test]
fn a_published_distinct_count_stops_an_unrepresentative_prefix_from_growing_the_table() {
let n = 60_000;
let value_of = |id: i64| if id < PRESIZE_PREFIX as i64 { 7 } else { id };
let distinct = (n - PRESIZE_PREFIX as i64 + 1) as u64;
let sampled = build(n, value_of, None);
let stated = build(n, value_of, Some(distinct));
assert_eq!(sampled.buckets, stated.buckets, "the same pair either way");
assert!(
sampled.tail_growths >= 5,
"the sample path doubled through the tail: {}",
sampled.tail_growths
);
assert_eq!(stated.tail_growths, 0, "reserved once, never grown");
assert!(stated.capacity >= distinct as usize);
}
#[test]
fn a_representative_prefix_builds_the_same_with_or_without_statistics() {
let n = 40_000;
let sampled = build(n, |id| id, None);
let stated = build(n, |id| id, Some(n as u64));
assert_eq!(sampled.buckets, stated.buckets);
assert_eq!(sampled.tail_growths, 0);
assert_eq!(stated.tail_growths, 0);
assert_eq!(sampled.capacity, stated.capacity);
}