mod aggregate;
mod display;
mod expand;
mod group;
mod tree;
use std::{
cmp::Ordering,
panic,
sync::{
Arc, OnceLock,
atomic::{AtomicBool, AtomicUsize, Ordering as AtomicOrdering},
},
thread,
};
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Serialize};
pub use self::{
aggregate::{Aggregate, AggregateFn},
display::{
AggregateRow, DisplayRow, ExpandDepth, Expansion, GroupId, GroupInfo, GroupPath, GroupTable,
},
group::{
AggregateOrder, EveryGroupFooter, GroupBy, GroupFooter, GroupKey, GroupOrder,
GroupOrderContext, KeyOrder, NoGroupFooter,
},
tree::{
Branch, TreeFilter, TreeFilterContext, TreeFilterRule, TreeSpec, TreeTotals,
TreeTotalsContext, TreeTotalsRule,
},
};
use self::{
display::{AggregateValues, Lines, SummaryStamps},
group::GroupSpec,
};
use crate::{
model::{CellValue, ColumnSet, GridColumns, GridRow},
quick_filter::{QuickFilter, QuickFilterPass},
store::{RowId, StoreSnapshot},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SortDirection {
Ascending,
Descending,
}
type Filter<R> = Arc<dyn Fn(&R) -> bool + Send + Sync>;
pub struct ViewSpec<R: GridRow> {
sort: Vec<(R::Column, SortDirection)>,
filter: Option<(ColumnSet, Filter<R>)>,
quick_filter: Option<QuickFilter>,
structure: Structure<R>,
group_order: Option<Arc<dyn GroupOrder<R>>>,
group_footer: Option<Arc<dyn GroupFooter>>,
aggregates: Option<Arc<[(R::Column, Aggregate<R>)]>>,
expansion: Expansion<R::Key>,
}
pub(crate) enum Structure<R: GridRow> {
Flat,
Groups(GroupSpec<R>),
Tree(TreeSpec<R>),
}
impl<R: GridRow> Clone for Structure<R> {
fn clone(&self) -> Self {
match self {
Self::Flat => Self::Flat,
Self::Groups(groups) => Self::Groups(groups.clone()),
Self::Tree(tree) => Self::Tree(tree.clone()),
}
}
}
impl<R: GridRow> Clone for ViewSpec<R> {
fn clone(&self) -> Self {
Self {
sort: self.sort.clone(),
filter: self.filter.clone(),
quick_filter: self.quick_filter.clone(),
structure: self.structure.clone(),
group_order: self.group_order.clone(),
group_footer: self.group_footer.clone(),
aggregates: self.aggregates.clone(),
expansion: self.expansion.clone(),
}
}
}
impl<R: GridRow> Default for ViewSpec<R> {
fn default() -> Self {
Self {
sort: Vec::new(),
filter: None,
quick_filter: None,
structure: Structure::Flat,
group_order: None,
group_footer: None,
aggregates: None,
expansion: Expansion::default(),
}
}
}
impl<R: GridRow> ViewSpec<R> {
pub fn new() -> Self {
Self::default()
}
pub fn with_sort(mut self, sort: impl IntoIterator<Item = (R::Column, SortDirection)>) -> Self {
self.sort = sort.into_iter().collect();
self
}
pub fn with_filter(
mut self,
depends_on: ColumnSet,
filter: impl Fn(&R) -> bool + Send + Sync + 'static,
) -> Self {
self.filter = Some((depends_on, Arc::new(filter)));
self
}
pub fn with_quick_filter(mut self, filter: Option<QuickFilter>) -> Self {
self.quick_filter = filter;
self
}
pub fn sort(&self) -> &[(R::Column, SortDirection)] {
&self.sort
}
pub fn quick_filter(&self) -> Option<&QuickFilter> {
self.quick_filter.as_ref()
}
pub fn dependencies(&self) -> ColumnSet {
let mut columns = ColumnSet::EMPTY;
for (column, _) in &self.sort {
columns.insert(column.ix());
}
if let Some((depends_on, _)) = &self.filter {
columns.union_with(*depends_on);
}
if let Some(quick_filter) = &self.quick_filter {
columns.union_with(quick_filter.columns());
}
if let Structure::Groups(groups) = &self.structure {
for level in groups.levels() {
match (level.as_column(), level.as_key()) {
(Some(column), _) => columns.insert(column.ix()),
(None, Some(key)) => columns.union_with(key.depends_on()),
(None, None) => {}
}
}
let order_columns = self.group_order().depends_on();
columns.union_with(order_columns);
for (column, aggregate) in self.aggregates() {
if order_columns.contains(column.ix()) {
columns.union_with(aggregate.depends_on(*column));
}
}
}
columns
}
pub(crate) fn structure(&self) -> &Structure<R> {
&self.structure
}
}
#[derive(Debug, Default)]
pub struct Projection {
store_version: u64,
rows: Vec<RowId>,
row_to_order: Vec<u32>,
shared_order: FxHashMap<RowId, u32>,
row_count: usize,
lines: Lines,
totals: AggregateValues,
summary_stamps: Option<Box<SummaryStamps>>,
}
impl Projection {
fn new<R: GridRow>(snapshot: &StoreSnapshot<R>, rows: Vec<RowId>) -> Self {
let (row_to_order, shared_order) =
places(rows.iter().copied().enumerate(), snapshot.capacity());
Self {
store_version: snapshot.version(),
row_count: rows.len(),
rows,
row_to_order,
shared_order,
lines: Lines::Flat,
totals: AggregateValues::default(),
summary_stamps: None,
}
}
pub(crate) fn duplicate(&self) -> Self {
Self {
store_version: self.store_version,
rows: self.rows.clone(),
row_to_order: self.row_to_order.clone(),
shared_order: self.shared_order.clone(),
row_count: self.row_count,
lines: self.lines.clone(),
totals: self.totals.clone(),
summary_stamps: self.summary_stamps.clone(),
}
}
pub fn store_version(&self) -> u64 {
self.store_version
}
pub fn rows(&self) -> &[RowId] {
&self.rows
}
pub fn contains(&self, id: RowId) -> bool {
self.order_ix(id).is_some()
}
}
pub trait ProjectionEngine<R: GridRow>: Send + Sync + 'static {
fn rebuild(&self, snapshot: &StoreSnapshot<R>, spec: &ViewSpec<R>) -> Projection;
fn compact(&self, previous: &Projection, snapshot: &StoreSnapshot<R>) -> Projection {
if previous.is_tree() {
tree::compact(previous, snapshot)
} else {
display::compact(previous, snapshot)
}
}
fn aggregate(
&self,
previous: &Projection,
snapshot: &StoreSnapshot<R>,
spec: &ViewSpec<R>,
) -> Projection {
if previous.is_tree() {
tree::refresh(previous, snapshot, spec)
} else {
aggregate::refresh(previous, snapshot, spec)
}
}
fn expand(
&self,
previous: &Projection,
snapshot: &StoreSnapshot<R>,
spec: &ViewSpec<R>,
) -> Projection {
if previous.is_tree() {
tree::relayout(previous, snapshot, spec)
} else {
expand::relayout(previous, snapshot, spec)
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Rebuild {
max_threads: Option<usize>,
}
impl Rebuild {
pub fn with_max_threads(mut self, threads: usize) -> Self {
self.max_threads = Some(threads.max(1));
self
}
pub fn max_threads(&self) -> usize {
if cfg!(target_family = "wasm") {
1
} else {
self.max_threads.unwrap_or_else(default_thread_budget)
}
}
fn rebuild_flat<R: GridRow>(
&self,
snapshot: &StoreSnapshot<R>,
spec: &ViewSpec<R>,
) -> Projection {
let parallelism = Parallelism::for_view(snapshot.len(), spec, self.max_threads());
let (rows, fading) = project(snapshot, spec, parallelism);
let live = rows.len() - fading;
let projection = Projection::new(snapshot, rows).with_row_count(live);
let aggregates = aggregate::ViewAggregates::new(spec);
if aggregates.is_empty() {
return projection;
}
let (totals, _) = aggregate::flat_totals(
&aggregates,
projection
.rows
.iter()
.filter_map(|id| snapshot.row(*id).map(|row| &**row)),
);
projection.with_totals(AggregateValues::new(aggregates.columns(), totals))
}
}
fn directed(ordering: Ordering, direction: SortDirection) -> Ordering {
match direction {
SortDirection::Ascending => ordering,
SortDirection::Descending => ordering.reverse(),
}
}
impl<R: GridRow> ProjectionEngine<R> for Rebuild {
fn rebuild(&self, snapshot: &StoreSnapshot<R>, spec: &ViewSpec<R>) -> Projection {
match spec.structure() {
Structure::Flat => self.rebuild_flat(snapshot, spec),
Structure::Groups(groups) => group::rebuild(self, snapshot, spec, groups),
Structure::Tree(tree) => tree::rebuild(self, snapshot, spec, tree),
}
}
}
const MIN_ROWS_PER_THREAD: usize = 8 * 1024;
const CHUNK_ROWS: usize = 1024;
const RESERVED_CORES: usize = 2;
const MAX_THREADS: usize = 8;
#[derive(Clone, Copy, Debug)]
struct Parallelism {
threads: usize,
chunk_rows: usize,
}
impl Parallelism {
fn for_view<R: GridRow>(rows: usize, spec: &ViewSpec<R>, budget: usize) -> Self {
let has_work =
!spec.sort.is_empty() || spec.filter.is_some() || spec.quick_filter.is_some();
if has_work {
Self::for_rows(rows, budget)
} else {
Self {
threads: 1,
chunk_rows: CHUNK_ROWS,
}
}
}
fn for_rows(rows: usize, budget: usize) -> Self {
Self {
threads: budget.min(rows / MIN_ROWS_PER_THREAD).max(1),
chunk_rows: CHUNK_ROWS,
}
}
}
fn default_thread_budget() -> usize {
static BUDGET: OnceLock<usize> = OnceLock::new();
*BUDGET.get_or_init(|| {
thread::available_parallelism().map_or(1, |cores| {
cores
.get()
.saturating_sub(RESERVED_CORES)
.clamp(1, MAX_THREADS)
})
})
}
fn project<R: GridRow>(
snapshot: &StoreSnapshot<R>,
spec: &ViewSpec<R>,
parallelism: Parallelism,
) -> (Vec<RowId>, usize) {
match spec.sort.split_first() {
None => sorted_rows(
snapshot,
spec,
parallelism,
|id, _, seq| (seq, id),
|a, b| a.0.cmp(&b.0),
|&(_, id)| id,
),
Some((&(first, first_direction), rest)) => sorted_rows(
snapshot,
spec,
parallelism,
|id, row, seq| (row.value(first), seq, id, row),
move |a, b| compare_sorted(a, b, first_direction, rest),
|&(_, _, id, _)| id,
),
}
}
type SortEntry<'a, R> = (CellValue<'a>, u64, RowId, &'a Arc<R>);
#[inline(always)]
fn compare_sorted<R: GridRow>(
a: &SortEntry<'_, R>,
b: &SortEntry<'_, R>,
first_direction: SortDirection,
rest: &[(R::Column, SortDirection)],
) -> Ordering {
directed(a.0.cmp_total(&b.0), first_direction)
.then_with(|| {
rest.iter()
.map(|&(column, direction)| {
directed(a.3.value(column).cmp_total(&b.3.value(column)), direction)
})
.find(|ordering| ordering.is_ne())
.unwrap_or(Ordering::Equal)
})
.then_with(|| a.1.cmp(&b.1))
}
fn sorted_rows<'a, R: GridRow, E: Copy + Send + Sync, T>(
snapshot: &'a StoreSnapshot<R>,
spec: &ViewSpec<R>,
parallelism: Parallelism,
entry: impl Fn(RowId, &'a Arc<R>, u64) -> E + Sync,
compare: impl Fn(&E, &E) -> Ordering + Sync,
row_id: impl Fn(&E) -> T,
) -> (Vec<T>, usize) {
let mut fading = fading_entries(snapshot, spec, &entry);
let fading_count = fading.len();
if parallelism.threads <= 1 {
let mut visible = VisibleRows::new(spec);
let mut entries: Vec<E> = snapshot
.iter()
.filter(|(_, row)| visible.contains(row))
.map(|(id, row)| entry(id, row, snapshot.seq(id)))
.collect();
entries.append(&mut fading);
entries.sort_unstable_by(|a, b| compare(a, b));
let rows = entries.into_iter().map(|entry| row_id(&entry)).collect();
return (rows, fading_count);
}
let rows: Vec<(RowId, &Arc<R>)> = snapshot.iter().collect();
let next_chunk = AtomicUsize::new(0);
let fading_taken = AtomicBool::new(fading.is_empty());
let sorted_run = || {
let mut visible = VisibleRows::new(spec);
let mut entries = Vec::new();
loop {
let start = next_chunk.fetch_add(parallelism.chunk_rows, AtomicOrdering::Relaxed);
if start >= rows.len() {
break;
}
let chunk = &rows[start..rows.len().min(start + parallelism.chunk_rows)];
entries.extend(
chunk
.iter()
.filter(|(_, row)| visible.contains(row))
.map(|&(id, row)| entry(id, row, snapshot.seq(id))),
);
}
if !fading_taken.swap(true, AtomicOrdering::Relaxed) {
entries.extend_from_slice(&fading);
}
entries.sort_unstable_by(|a, b| compare(a, b));
entries
};
let mut runs = run_on_threads(&vec![&sorted_run; parallelism.threads]);
runs.retain(|run| !run.is_empty());
while runs.len() > 2 {
let merges: Vec<_> = runs
.chunks(2)
.map(|pair| {
let compare = &compare;
move || match pair {
[first, second] => {
let mut merged = Vec::with_capacity(first.len() + second.len());
merge(first, second, compare, |entry| *entry, &mut merged);
merged
}
_ => pair.concat(),
}
})
.collect();
runs = run_on_threads(&merges);
}
let mut rows = Vec::with_capacity(runs.iter().map(Vec::len).sum());
match runs.as_slice() {
[first, second] => merge(first, second, &compare, row_id, &mut rows),
runs => rows.extend(runs.iter().flatten().map(row_id)),
}
(rows, fading_count)
}
fn fading_entries<'a, R: GridRow, E>(
snapshot: &'a StoreSnapshot<R>,
spec: &ViewSpec<R>,
entry: &impl Fn(RowId, &'a Arc<R>, u64) -> E,
) -> Vec<E> {
let mut fading = snapshot.fading_rows().peekable();
if fading.peek().is_none() {
return Vec::new();
}
let mut visible = VisibleRows::new(spec);
fading
.filter(|(_, row, ..)| visible.contains(row))
.map(|(id, row, _, seq)| entry(id, row, seq))
.collect()
}
fn places(
rows: impl Iterator<Item = (usize, RowId)>,
capacity: usize,
) -> (Vec<u32>, FxHashMap<RowId, u32>) {
let mut by_slot = vec![u32::MAX; capacity];
let mut shared = FxHashMap::default();
for (place, id) in rows {
match by_slot.get_mut(id.ix()) {
Some(slot) if *slot == u32::MAX => *slot = place as u32,
_ => {
shared.insert(id, place as u32);
}
}
}
(by_slot, shared)
}
#[inline]
fn shared_place(shared: &FxHashMap<RowId, u32>, id: RowId) -> Option<usize> {
if shared.is_empty() {
None
} else {
shared.get(&id).map(|&place| place as usize)
}
}
#[derive(Default)]
struct FadingIds {
slots: Vec<u64>,
ids: FxHashSet<RowId>,
}
impl FadingIds {
fn new<R: GridRow>(snapshot: &StoreSnapshot<R>) -> Self {
let mut fading = Self::default();
for (id, ..) in snapshot.fading_rows() {
if fading.slots.is_empty() {
fading.slots = vec![0; snapshot.capacity().div_ceil(64)];
}
if let Some(word) = fading.slots.get_mut(id.ix() / 64) {
*word |= 1 << (id.ix() % 64);
}
fading.ids.insert(id);
}
fading
}
#[inline]
fn contains(&self, id: RowId) -> bool {
self.slots
.get(id.ix() / 64)
.is_some_and(|word| word & (1 << (id.ix() % 64)) != 0)
&& self.ids.contains(&id)
}
fn is_empty(&self) -> bool {
self.ids.is_empty()
}
}
fn merge<E, T>(
first: &[E],
second: &[E],
compare: impl Fn(&E, &E) -> Ordering,
map: impl Fn(&E) -> T,
out: &mut Vec<T>,
) {
let (mut first_ix, mut second_ix) = (0, 0);
while let (Some(a), Some(b)) = (first.get(first_ix), second.get(second_ix)) {
if compare(b, a).is_lt() {
out.push(map(b));
second_ix += 1;
} else {
out.push(map(a));
first_ix += 1;
}
}
out.extend(first[first_ix..].iter().map(&map));
out.extend(second[second_ix..].iter().map(&map));
}
fn run_on_threads<T: Send>(jobs: &[impl FnOnce() -> T + Copy + Send]) -> Vec<T> {
let Some((&first, rest)) = jobs.split_first() else {
return Vec::new();
};
thread::scope(|scope| {
let started: Vec<_> = rest
.iter()
.map(|&job| {
thread::Builder::new()
.name("live-grid-rebuild".into())
.spawn_scoped(scope, job)
.map_err(|_| job)
})
.collect();
let mut results = Vec::with_capacity(jobs.len());
results.push(first());
for job in started {
results.push(match job {
Ok(thread) => thread
.join()
.unwrap_or_else(|panic| panic::resume_unwind(panic)),
Err(job) => job(),
});
}
results
})
}
struct VisibleRows<'a, R: GridRow> {
filter: Option<&'a Filter<R>>,
quick_filter: Option<QuickFilterPass<'a, R>>,
}
impl<'a, R: GridRow> VisibleRows<'a, R> {
fn new(spec: &'a ViewSpec<R>) -> Self {
Self {
filter: spec.filter.as_ref().map(|(_, filter)| filter),
quick_filter: spec.quick_filter.as_ref().map(QuickFilterPass::new),
}
}
fn contains(&mut self, row: &'a R) -> bool {
self.filter.is_none_or(|filter| filter(row))
&& self
.quick_filter
.as_mut()
.is_none_or(|quick_filter| quick_filter.matches(row))
}
}
#[cfg(test)]
mod tests {
use std::time::{Duration, Instant};
use super::*;
use crate::{
highlight::HighlightPolicy,
model::{ColumnId, ColumnIx, OwnedValue},
quick_filter::ContainsAllTerms,
store::{Ingest, Op},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum Column {
Id,
Name,
Price,
}
impl GridColumns for Column {
fn all() -> &'static [Self] {
&[Self::Id, Self::Name, Self::Price]
}
fn id(self) -> &'static str {
match self {
Self::Id => "id",
Self::Name => "name",
Self::Price => "price",
}
}
fn ix(self) -> ColumnIx {
ColumnIx::new(self as u16)
}
}
struct Row;
impl GridRow for Row {
type Key = u32;
type Column = Column;
fn key(&self) -> &u32 {
&0
}
fn value(&self, _column: Column) -> CellValue<'_> {
CellValue::Empty
}
}
#[test]
fn test_quick_filter_dependencies() {
let mut name = ColumnSet::EMPTY;
name.insert(Column::Name.ix());
let quick_filter = QuickFilter::new("sber", name, &ContainsAllTerms);
let spec = ViewSpec::<Row>::new()
.with_sort([(Column::Price, SortDirection::Ascending)])
.with_quick_filter(quick_filter);
assert_eq!(spec.quick_filter().map(QuickFilter::text), Some("sber"));
let dependencies = spec.dependencies();
assert!(dependencies.contains(Column::Name.ix()));
assert!(dependencies.contains(Column::Price.ix()));
assert!(!dependencies.contains(Column::Id.ix()));
let off = spec.with_quick_filter(None);
assert!(off.quick_filter().is_none());
assert!(!off.dependencies().contains(Column::Name.ix()));
assert!(off.dependencies().contains(Column::Price.ix()));
}
#[test]
fn test_sort_direction_serde() {
for (direction, json) in [
(SortDirection::Ascending, r#""ascending""#),
(SortDirection::Descending, r#""descending""#),
] {
assert_eq!(serde_json::to_string(&direction).unwrap(), json);
assert_eq!(
serde_json::from_str::<SortDirection>(json).unwrap(),
direction
);
}
assert!(serde_json::from_str::<SortDirection>(r#""Ascending""#).is_err());
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum TradeColumn {
Id,
Group,
Price,
Name,
Flag,
Mixed,
}
impl GridColumns for TradeColumn {
fn all() -> &'static [Self] {
&[
Self::Id,
Self::Group,
Self::Price,
Self::Name,
Self::Flag,
Self::Mixed,
]
}
fn id(self) -> &'static str {
match self {
Self::Id => "id",
Self::Group => "group",
Self::Price => "price",
Self::Name => "name",
Self::Flag => "flag",
Self::Mixed => "mixed",
}
}
fn ix(self) -> ColumnIx {
ColumnIx::new(self as u16)
}
}
struct Trade {
id: u32,
group: i64,
price: f64,
name: String,
flag: Option<bool>,
mixed: u8,
}
impl Trade {
fn random(rng: &mut fastrand::Rng, id: u32) -> Self {
const NAME_CHARS: &[char] = &['a', 'B', 'я', 'Ж', '1'];
Self {
id,
group: rng.i64(0..4),
price: [0.5, 1.0, 1.5, f64::NAN, -0.0][rng.usize(..5)],
name: (0..rng.usize(0..=2))
.map(|_| NAME_CHARS[rng.usize(..NAME_CHARS.len())])
.collect(),
flag: [None, Some(false), Some(true)][rng.usize(..3)],
mixed: rng.u8(0..8),
}
}
}
impl GridRow for Trade {
type Key = u32;
type Column = TradeColumn;
fn key(&self) -> &u32 {
&self.id
}
fn value(&self, column: TradeColumn) -> CellValue<'_> {
match column {
TradeColumn::Id => CellValue::Int(self.id.into()),
TradeColumn::Group => CellValue::Int(self.group),
TradeColumn::Price => CellValue::Float(self.price),
TradeColumn::Name => CellValue::Text(&self.name),
TradeColumn::Flag => self.flag.map_or(CellValue::Empty, CellValue::Bool),
TradeColumn::Mixed => match self.mixed {
0 => CellValue::Empty,
1 => CellValue::Bool(true),
2 | 3 => CellValue::Int(i64::from(self.mixed)),
4 | 5 => CellValue::Float(f64::from(self.mixed) / 2.0),
_ => CellValue::Text(if self.mixed == 6 { "x" } else { "Y" }),
},
}
}
}
fn random_spec(rng: &mut fastrand::Rng) -> ViewSpec<Trade> {
let columns = TradeColumn::all();
let direction = |rng: &mut fastrand::Rng| {
[SortDirection::Ascending, SortDirection::Descending][rng.usize(..2)]
};
let sort: Vec<_> = (0..rng.usize(0..=3))
.map(|_| (columns[rng.usize(..columns.len())], direction(rng)))
.collect();
let mut quick_columns = ColumnSet::EMPTY;
for column in columns {
if rng.bool() {
quick_columns.insert(column.ix());
}
}
let text = ["", "a", "я 1", "b ж", ".5", "1 a"][rng.usize(..6)];
let spec = ViewSpec::new()
.with_sort(sort)
.with_quick_filter(QuickFilter::new(text, quick_columns, &ContainsAllTerms));
if rng.bool() {
spec.with_filter(ColumnSet::first(columns.len()), |trade: &Trade| {
trade.group != 1
})
} else {
spec
}
}
fn policy(fade: bool) -> HighlightPolicy {
if fade {
HighlightPolicy::default().with_removal_fade(Duration::from_secs(3_600))
} else {
HighlightPolicy::default()
}
}
#[test]
fn test_parallel_rebuild_matches_sequential() {
let epoch = Instant::now();
let mut fading = 0;
for seed in 0..48 {
let mut rng = fastrand::Rng::with_seed(seed);
let mut ingest = Ingest::<Trade>::new(policy(seed % 2 == 1), epoch);
let count = [0, 1, 2, 7, 100, 1_000, 3_000][rng.usize(..7)];
ingest.apply(
(0..count).map(|id| Op::UpsertSilent(Arc::new(Trade::random(&mut rng, id)))),
epoch,
);
ingest.apply(
(0..count / 3).map(|_| Op::Remove(rng.u32(..count.max(1)))),
epoch,
);
ingest.apply(
(count..count + count / 4)
.map(|id| Op::UpsertSilent(Arc::new(Trade::random(&mut rng, id)))),
epoch,
);
let snapshot = ingest.snapshot();
for _ in 0..8 {
let spec = random_spec(&mut rng);
let sequential = project(
&snapshot,
&spec,
Parallelism {
threads: 1,
chunk_rows: CHUNK_ROWS,
},
);
fading += sequential.1;
let parallelism = Parallelism {
threads: rng.usize(2..=7),
chunk_rows: rng.usize(1..=97),
};
assert_eq!(
project(&snapshot, &spec, parallelism),
sequential,
"seed {seed}, {count} rows, {parallelism:?}, sort {:?}, quick filter {:?}",
spec.sort(),
spec.quick_filter().map(QuickFilter::text),
);
}
}
assert!(fading > 1_000, "{fading} fading rows");
}
#[test]
fn test_parallelism_for_view() {
let sorted =
ViewSpec::<Trade>::new().with_sort([(TradeColumn::Name, SortDirection::Ascending)]);
let budget = Rebuild::default().max_threads();
assert_eq!(
Parallelism::for_view(MIN_ROWS_PER_THREAD - 1, &sorted, budget).threads,
1
);
assert_eq!(
Parallelism::for_view(1 << 20, &ViewSpec::<Trade>::new(), budget).threads,
1
);
let threads = Parallelism::for_view(1 << 20, &sorted, budget).threads;
assert_eq!(threads, default_thread_budget());
assert!((1..=MAX_THREADS).contains(&threads));
if let Ok(cores) = thread::available_parallelism() {
assert!(threads == 1 || threads + RESERVED_CORES <= cores.get());
}
for (rows, threads) in [
(MIN_ROWS_PER_THREAD * 3 - 1, 2),
(MIN_ROWS_PER_THREAD * 3, 3),
(MIN_ROWS_PER_THREAD * 40, 12),
] {
assert_eq!(
Parallelism::for_view(rows, &sorted, 12).threads,
threads,
"{rows} rows"
);
}
}
#[test]
fn test_rebuild_max_threads() {
assert_eq!(Rebuild::default().max_threads(), default_thread_budget());
assert_eq!(Rebuild::default().with_max_threads(1).max_threads(), 1);
assert_eq!(
Rebuild::default().with_max_threads(0).max_threads(),
1,
"at least one"
);
assert_eq!(
Rebuild::default()
.with_max_threads(MAX_THREADS * 2)
.max_threads(),
MAX_THREADS * 2
);
}
#[test]
fn test_parallel_rebuild_resumes_panic() {
let epoch = Instant::now();
let mut rng = fastrand::Rng::with_seed(1);
let mut ingest = Ingest::<Trade>::new(HighlightPolicy::default(), epoch);
ingest.apply(
(0..2_000).map(|id| Op::UpsertSilent(Arc::new(Trade::random(&mut rng, id)))),
epoch,
);
let snapshot = ingest.snapshot();
let spec = ViewSpec::new().with_filter(ColumnSet::first(1), |trade: &Trade| {
assert_ne!(trade.id, 1_500, "filter failed");
true
});
let parallelism = Parallelism {
threads: 4,
chunk_rows: 16,
};
let panic = panic::catch_unwind(panic::AssertUnwindSafe(|| {
project(&snapshot, &spec, parallelism)
}))
.expect_err("the filter panics");
let message = panic
.downcast_ref::<String>()
.map(String::as_str)
.unwrap_or_default();
assert!(message.contains("filter failed"), "{message}");
}
fn random_snapshot(rng: &mut fastrand::Rng, count: u32) -> Arc<StoreSnapshot<Trade>> {
random_snapshot_with(rng, count, false)
}
fn random_snapshot_with(
rng: &mut fastrand::Rng,
count: u32,
fade: bool,
) -> Arc<StoreSnapshot<Trade>> {
let epoch = Instant::now();
let mut ingest = Ingest::<Trade>::new(policy(fade), epoch);
ingest.apply(
(0..count).map(|id| Op::UpsertSilent(Arc::new(Trade::random(rng, id)))),
epoch,
);
ingest.apply(
(0..count / 3).map(|_| Op::Remove(rng.u32(..count.max(1)))),
epoch,
);
ingest.apply(
(count..count + count / 4).map(|id| Op::UpsertSilent(Arc::new(Trade::random(rng, id)))),
epoch,
);
ingest.snapshot()
}
#[test]
fn test_flat_projection_readers() {
let engine = Rebuild::default().with_max_threads(1);
for seed in 0..24 {
let mut rng = fastrand::Rng::with_seed(seed);
let count = [0, 1, 7, 100, 300][rng.usize(..5)];
let snapshot = random_snapshot(&mut rng, count);
let spec = random_spec(&mut rng);
let projection = engine.rebuild(&snapshot, &spec);
let context = format!("seed {seed}, {count} rows");
assert_eq!(projection.len(), projection.rows().len(), "{context}");
assert_eq!(projection.row_count(), projection.len(), "{context}");
assert_eq!(projection.is_empty(), projection.rows().is_empty());
for (ix, &id) in projection.rows().iter().enumerate() {
assert_eq!(projection.row(ix), Some(id), "{context}");
assert_eq!(
projection.line(ix),
Some(DisplayRow::Data { row: id, depth: 0 }),
"{context}"
);
assert_eq!(projection.display_ix(id), Some(ix), "{context}");
assert_eq!(projection.order_ix(id), Some(ix), "{context}");
assert!(projection.contains(id), "{context}");
}
assert_eq!(projection.line(projection.len()), None);
assert_eq!(projection.row(projection.len()), None);
for (id, _) in snapshot.iter() {
let shown = projection.rows().contains(&id);
assert_eq!(projection.contains(id), shown, "{context}");
assert_eq!(projection.order_ix(id).is_some(), shown, "{context}");
assert_eq!(projection.branch(id), None, "{context}");
}
assert!(!projection.is_grouped() && !projection.is_tree());
assert!(projection.groups().is_empty());
assert_eq!(projection.groups().iter().count(), 0);
assert_eq!(projection.group_line(GroupId::new(0)), None);
assert!(projection.totals().is_empty());
assert_eq!(projection.totals().columns(), ColumnSet::EMPTY);
assert_eq!(projection.totals().get(TradeColumn::Price.ix()), None);
assert_eq!(projection.totals().row_count(), projection.row_count());
let expanded: Vec<DisplayRow> = projection.expanded_lines().collect();
let lines: Vec<DisplayRow> = (0..projection.len())
.filter_map(|ix| projection.line(ix))
.collect();
assert_eq!(expanded, lines, "{context}");
assert_eq!(
projection.shown_rows().collect::<Vec<_>>(),
projection.rows(),
"{context}"
);
let aggregated = engine.aggregate(&projection, &snapshot, &spec);
assert_eq!(aggregated.rows(), projection.rows(), "{context}");
assert_eq!(aggregated.store_version(), snapshot.version());
let expanded = engine.expand(&projection, &snapshot, &spec);
assert_eq!(expanded.rows(), projection.rows(), "{context}");
assert_eq!(expanded.len(), projection.len(), "{context}");
assert_eq!(expanded.store_version(), projection.store_version());
let compacted = engine.compact(&projection, &snapshot);
assert_eq!(compacted.rows(), projection.rows(), "{context}");
}
}
#[test]
fn test_grouped_rows_follow_the_level_sort() {
let engine = Rebuild::default().with_max_threads(1);
for seed in 0..12 {
let mut rng = fastrand::Rng::with_seed(seed);
let snapshot = random_snapshot(&mut rng, 200);
let spec = random_spec(&mut rng);
let direction = [SortDirection::Ascending, SortDirection::Descending][rng.usize(..2)];
let level =
[TradeColumn::Group, TradeColumn::Mixed, TradeColumn::Price][rng.usize(..3)];
let grouped = engine.rebuild(
&snapshot,
&spec
.clone()
.with_group_by([GroupBy::column(level).with_direction(direction)]),
);
let sort: Vec<_> = std::iter::once((level, direction))
.chain(spec.sort().iter().copied())
.collect();
let flat = engine.rebuild(&snapshot, &spec.clone().with_sort(sort));
assert_eq!(grouped.rows(), flat.rows(), "seed {seed}");
assert!(grouped.is_grouped() && !grouped.is_tree());
assert_eq!(grouped.row_count(), flat.row_count());
}
}
struct OrderedSum;
impl AggregateFn<Trade> for OrderedSum {
fn aggregate(&self, _: TradeColumn, rows: &mut dyn Iterator<Item = &Trade>) -> OwnedValue {
OwnedValue::Float(rows.map(|trade| trade.price * 0.1).sum())
}
}
fn random_grouped_spec(rng: &mut fastrand::Rng) -> ViewSpec<Trade> {
let direction = |rng: &mut fastrand::Rng| {
[SortDirection::Ascending, SortDirection::Descending][rng.usize(..2)]
};
let letter = || {
GroupKey::new("letter", ColumnSet::first(4), |trade: &Trade| {
CellValue::Text(trade.name.get(..1).unwrap_or_default())
})
};
let levels: Vec<GroupBy<Trade>> = (0..rng.usize(1..=3))
.map(|_| {
let level = match rng.u8(0..5) {
0 => GroupBy::column(TradeColumn::Group),
1 => GroupBy::column(TradeColumn::Flag),
2 => GroupBy::column(TradeColumn::Mixed),
3 => GroupBy::column(TradeColumn::Price),
_ => GroupBy::key(letter()),
};
level.with_direction(direction(rng))
})
.collect();
let spec = random_spec(rng).with_group_by(levels).with_aggregates([
(TradeColumn::Price, Aggregate::Sum),
(TradeColumn::Group, Aggregate::Avg),
(TradeColumn::Name, Aggregate::Min),
(TradeColumn::Mixed, Aggregate::Max),
(TradeColumn::Flag, Aggregate::Count),
(TradeColumn::Id, Aggregate::Custom(Arc::new(OrderedSum))),
]);
let spec = if rng.bool() {
spec.with_group_order(AggregateOrder::new(TradeColumn::Price, direction(rng)))
} else {
spec
};
if rng.bool() {
spec.with_group_footer(EveryGroupFooter)
} else {
spec
}
}
fn assert_same_projection(a: &Projection, b: &Projection, context: &str) {
let same = |a: &OwnedValue, b: &OwnedValue| {
std::mem::discriminant(a) == std::mem::discriminant(b) && a == b
};
let same_row = |a: AggregateRow<'_>, b: AggregateRow<'_>| {
a.columns() == b.columns()
&& a.row_count() == b.row_count()
&& TradeColumn::all().iter().all(|column| {
match (a.get(column.ix()), b.get(column.ix())) {
(Some(a), Some(b)) => same(a, b),
(a, b) => a.is_none() && b.is_none(),
}
})
};
assert_eq!(a.rows(), b.rows(), "{context}");
assert_eq!(a.len(), b.len(), "{context}");
for ix in 0..a.len() {
assert_eq!(a.line(ix), b.line(ix), "{context}, line {ix}");
}
assert_eq!(a.row_count(), b.row_count(), "{context}");
assert!(same_row(a.totals(), b.totals()), "{context}: totals");
assert_eq!(a.groups().len(), b.groups().len(), "{context}");
for (a, b) in a.groups().iter().zip(b.groups().iter()) {
assert_eq!(a.path(), b.path(), "{context}");
assert_eq!(a.parent(), b.parent(), "{context}");
assert_eq!(a.rows(), b.rows(), "{context}");
assert_eq!(a.row_count(), b.row_count(), "{context}");
assert_eq!(
(a.is_expanded(), a.has_footer()),
(b.is_expanded(), b.has_footer()),
"{context}"
);
assert!(same_row(a.aggregates(), b.aggregates()), "{context}: {a:?}");
}
}
#[test]
fn test_grouped_rebuild_is_thread_independent() {
let mut fading = 0;
for seed in 0..48 {
let mut rng = fastrand::Rng::with_seed(seed);
let count = [0, 1, 2, 7, 100, 1_000, 3_000][rng.usize(..7)];
let snapshot = random_snapshot_with(&mut rng, count, seed % 2 == 1);
for _ in 0..4 {
let spec = random_grouped_spec(&mut rng);
let Structure::Groups(grouping) = spec.structure() else {
unreachable!("a grouped view");
};
let sequential = group::rebuild_with(
&snapshot,
&spec,
grouping,
Parallelism {
threads: 1,
chunk_rows: CHUNK_ROWS,
},
);
let parallelism = Parallelism {
threads: rng.usize(2..=7),
chunk_rows: rng.usize(1..=97),
};
let parallel = group::rebuild_with(&snapshot, &spec, grouping, parallelism);
assert_same_projection(
¶llel,
&sequential,
&format!("seed {seed}, {count} rows, {parallelism:?}"),
);
fading += sequential.rows().len() - sequential.row_count();
for (order_ix, &id) in parallel.rows().iter().enumerate() {
assert_eq!(parallel.order_ix(id), Some(order_ix), "seed {seed}");
}
}
}
assert!(fading > 1_000, "{fading} fading rows");
}
#[test]
fn test_group_key_panic_resumes() {
let mut rng = fastrand::Rng::with_seed(2);
let snapshot = random_snapshot(&mut rng, 3_000);
let spec = ViewSpec::new().with_group_by([GroupBy::key(GroupKey::new(
"checked",
ColumnSet::first(1),
|trade: &Trade| {
assert_ne!(trade.id, 1_500, "group key failed");
CellValue::Int(trade.group)
},
))]);
let Structure::Groups(grouping) = spec.structure() else {
unreachable!("a grouped view");
};
let parallelism = Parallelism {
threads: 4,
chunk_rows: 16,
};
let panic = panic::catch_unwind(panic::AssertUnwindSafe(|| {
group::rebuild_with(&snapshot, &spec, grouping, parallelism)
}))
.expect_err("the group key panics");
let message = panic
.downcast_ref::<String>()
.map(String::as_str)
.unwrap_or_default();
assert!(message.contains("group key failed"), "{message}");
}
#[test]
fn test_dependencies_with_groups() {
let mut expected = ColumnSet::EMPTY;
expected.insert(TradeColumn::Price.ix());
let sorted =
ViewSpec::<Trade>::new().with_sort([(TradeColumn::Price, SortDirection::Ascending)]);
assert_eq!(sorted.dependencies(), expected);
let mut key_columns = ColumnSet::EMPTY;
key_columns.insert(TradeColumn::Name.ix());
let grouped = sorted
.clone()
.with_group_by([
GroupBy::column(TradeColumn::Group),
GroupBy::key(GroupKey::new("letter", key_columns, |trade: &Trade| {
CellValue::Text(trade.name.get(..1).unwrap_or_default())
})),
])
.with_aggregates([(TradeColumn::Mixed, Aggregate::Sum)])
.with_group_order(AggregateOrder::new(
TradeColumn::Flag,
SortDirection::Ascending,
));
for column in [TradeColumn::Group, TradeColumn::Name, TradeColumn::Flag] {
expected.insert(column.ix());
}
assert_eq!(grouped.dependencies(), expected);
assert!(!grouped.dependencies().contains(TradeColumn::Mixed.ix()));
assert!(
grouped
.aggregate_columns()
.contains(TradeColumn::Mixed.ix())
);
let flat = grouped.with_group_by([]);
let mut price = ColumnSet::EMPTY;
price.insert(TradeColumn::Price.ix());
assert_eq!(flat.dependencies(), price);
}
struct Weighted;
impl AggregateFn<Trade> for Weighted {
fn aggregate(&self, _: TradeColumn, _: &mut dyn Iterator<Item = &Trade>) -> OwnedValue {
OwnedValue::Empty
}
fn depends_on(&self, column: TradeColumn) -> ColumnSet {
let mut columns = ColumnSet::EMPTY;
columns.insert(column.ix());
columns.insert(TradeColumn::Group.ix());
columns
}
fn name(&self) -> &str {
"Weighted"
}
}
#[test]
fn test_view_spec_structure() {
let by_group = GroupBy::column(TradeColumn::Group);
let by_letter = GroupBy::key(
GroupKey::new("letter", ColumnSet::first(4), |trade: &Trade| {
CellValue::Text(trade.name.get(..1).unwrap_or_default())
})
.with_title("First letter"),
)
.with_direction(SortDirection::Descending);
let spec = ViewSpec::<Trade>::new()
.with_aggregates([
(TradeColumn::Price, Aggregate::Sum),
(TradeColumn::Id, Aggregate::Count),
(TradeColumn::Price, Aggregate::Max),
])
.with_group_by([by_group.clone(), by_letter, by_group.clone()]);
let levels: Vec<ColumnId> = spec.group_by().iter().map(GroupBy::id).collect();
assert_eq!(levels, [ColumnId::from("group"), ColumnId::from("letter")]);
assert_eq!(spec.group_by()[0].as_column(), Some(TradeColumn::Group));
assert_eq!(spec.group_by()[1].as_column(), None);
let key = spec.group_by()[1].as_key().expect("a key level");
assert_eq!((key.id(), key.title()), ("letter", "First letter"));
assert_eq!(spec.group_by()[1].direction(), SortDirection::Descending);
assert!(spec.tree().is_none());
let aggregates: Vec<(TradeColumn, String)> = spec
.aggregates()
.iter()
.map(|(column, aggregate)| (*column, format!("{aggregate:?}")))
.collect();
assert_eq!(
aggregates,
[
(TradeColumn::Price, "Max".to_owned()),
(TradeColumn::Id, "Count".to_owned())
]
);
let tree = spec
.clone()
.with_tree(TreeSpec::new().with_column(TradeColumn::Name));
assert!(tree.group_by().is_empty());
assert_eq!(
tree.tree().and_then(TreeSpec::column),
Some(TradeColumn::Name)
);
assert!(matches!(
tree.tree().unwrap().filter(),
TreeFilter::KeepAncestors
));
assert!(matches!(tree.tree().unwrap().totals(), TreeTotals::Leaves));
assert_eq!(tree.aggregates().len(), 2);
let regrouped = tree.with_group_by([by_group]);
assert!(regrouped.tree().is_none());
assert_eq!(regrouped.group_by().len(), 1);
let flat = regrouped.with_group_by([]);
assert!(flat.group_by().is_empty() && flat.tree().is_none());
assert!(matches!(flat.structure(), Structure::Flat));
assert_eq!(flat.aggregates().len(), 2);
let mut expected = ColumnSet::EMPTY;
expected.insert(TradeColumn::Price.ix());
expected.insert(TradeColumn::Id.ix());
assert_eq!(flat.aggregate_columns(), expected);
let custom =
flat.with_aggregates([(TradeColumn::Price, Aggregate::Custom(Arc::new(Weighted)))]);
let mut expected = ColumnSet::EMPTY;
expected.insert(TradeColumn::Price.ix());
expected.insert(TradeColumn::Group.ix());
assert_eq!(custom.aggregate_columns(), expected);
assert_eq!(
format!("{:?}", custom.aggregates()[0].1),
r#"Custom("Weighted")"#
);
assert!(ViewSpec::<Trade>::new().aggregates().is_empty());
assert!(ViewSpec::<Trade>::new().aggregate_columns().is_empty());
let a = GroupPath::new([(ColumnId::from("group"), OwnedValue::Int(1))]);
let b = GroupPath::new([(ColumnId::from("group"), OwnedValue::Int(2))]);
let values = [OwnedValue::Float(2.0)];
let low = [OwnedValue::Float(1.0)];
let mut price = ColumnSet::EMPTY;
price.insert(TradeColumn::Price.ix());
let first = GroupOrderContext::new(&a, 3, AggregateRow::new(price, &values, 3));
let second = GroupOrderContext::new(&b, 1, AggregateRow::new(price, &low, 1));
let spec = ViewSpec::<Trade>::new();
assert_eq!(spec.group_order().compare(&first, &second), Ordering::Equal);
assert!(spec.group_order().depends_on().is_empty());
let by_sum = spec.with_group_order(AggregateOrder::new(
TradeColumn::Price,
SortDirection::Descending,
));
assert_eq!(
by_sum.group_order().compare(&first, &second),
Ordering::Less
);
assert_eq!(by_sum.group_order().depends_on(), price);
assert_eq!(first.value(), &OwnedValue::Int(1));
assert_eq!((first.depth(), first.row_count()), (0, 3));
assert_eq!(first.path(), &a);
assert!(by_sum.expansion() == &Expansion::default());
}
}