use std::{
fmt,
hash::Hash,
iter::{self, FusedIterator},
ops::Range,
slice,
sync::{Arc, OnceLock},
};
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use super::{Projection, ViewSpec, expand, places, shared_place, tree::TreeData};
use crate::{
highlight::{ChangeDirection, HighlightPolicy, RowChange, Stamp},
model::{ColumnId, ColumnIx, ColumnSet, GridRow, OwnedValue},
store::{RowId, StoreSnapshot},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct GroupId(u32);
impl GroupId {
pub(crate) fn new(ix: usize) -> Self {
Self(ix as u32)
}
pub fn ix(self) -> usize {
self.0 as usize
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DisplayRow {
Data {
row: RowId,
depth: u16,
},
GroupCaption {
group: GroupId,
depth: u16,
},
GroupFooter {
group: GroupId,
depth: u16,
},
}
impl DisplayRow {
pub fn depth(self) -> u16 {
match self {
Self::Data { depth, .. }
| Self::GroupCaption { depth, .. }
| Self::GroupFooter { depth, .. } => depth,
}
}
pub fn row(self) -> Option<RowId> {
match self {
Self::Data { row, .. } => Some(row),
Self::GroupCaption { .. } | Self::GroupFooter { .. } => None,
}
}
pub fn group(self) -> Option<GroupId> {
match self {
Self::GroupCaption { group, .. } | Self::GroupFooter { group, .. } => Some(group),
Self::Data { .. } => None,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct GroupPath(Arc<[(ColumnId, OwnedValue)]>);
impl GroupPath {
pub fn new(levels: impl IntoIterator<Item = (ColumnId, OwnedValue)>) -> Self {
Self(levels.into_iter().collect())
}
pub fn depth(&self) -> u16 {
self.0.len().saturating_sub(1) as u16
}
pub fn is_root(&self) -> bool {
self.0.is_empty()
}
pub fn levels(&self) -> &[(ColumnId, OwnedValue)] {
&self.0
}
pub fn level_id(&self, depth: u16) -> Option<&ColumnId> {
self.0.get(usize::from(depth)).map(|(id, _)| id)
}
pub fn value(&self, depth: u16) -> Option<&OwnedValue> {
self.0.get(usize::from(depth)).map(|(_, value)| value)
}
pub fn parent(&self) -> Option<GroupPath> {
let (_, parent) = self.0.split_last()?;
Some(Self(parent.into()))
}
pub fn starts_with(&self, other: &GroupPath) -> bool {
self.0.starts_with(&other.0)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ExpandDepth {
#[default]
All,
Levels(u16),
Captions,
}
impl ExpandDepth {
fn expands_group(self, depth: u16, levels: usize) -> bool {
match self {
Self::All => true,
Self::Levels(n) => depth < n,
Self::Captions => usize::from(depth) + 1 < levels,
}
}
fn expands_row(self, depth: u16) -> bool {
match self {
Self::All | Self::Captions => true,
Self::Levels(n) => depth < n,
}
}
}
impl Serialize for ExpandDepth {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
Self::All => serializer.serialize_str("all"),
Self::Levels(levels) => serializer.serialize_u16(*levels),
Self::Captions => serializer.serialize_str("captions"),
}
}
}
impl<'de> Deserialize<'de> for ExpandDepth {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct DepthVisitor;
impl de::Visitor<'_> for DepthVisitor {
type Value = ExpandDepth;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(r#""all", "captions" or a number of levels"#)
}
fn visit_str<E: de::Error>(self, value: &str) -> Result<ExpandDepth, E> {
match value {
"all" => Ok(ExpandDepth::All),
"captions" => Ok(ExpandDepth::Captions),
_ => Err(E::invalid_value(de::Unexpected::Str(value), &self)),
}
}
fn visit_u64<E: de::Error>(self, value: u64) -> Result<ExpandDepth, E> {
u16::try_from(value)
.map(ExpandDepth::Levels)
.map_err(|_| E::invalid_value(de::Unexpected::Unsigned(value), &self))
}
fn visit_i64<E: de::Error>(self, value: i64) -> Result<ExpandDepth, E> {
u64::try_from(value)
.map_err(|_| E::invalid_value(de::Unexpected::Signed(value), &self))
.and_then(|value| self.visit_u64(value))
}
}
deserializer.deserialize_any(DepthVisitor)
}
}
pub struct Expansion<K> {
depth: ExpandDepth,
groups: Option<Arc<FxHashSet<GroupPath>>>,
rows: Option<Arc<FxHashSet<K>>>,
}
impl<K> Clone for Expansion<K> {
fn clone(&self) -> Self {
Self {
depth: self.depth,
groups: self.groups.clone(),
rows: self.rows.clone(),
}
}
}
impl<K> Default for Expansion<K> {
fn default() -> Self {
Self {
depth: ExpandDepth::default(),
groups: None,
rows: None,
}
}
}
impl<K: fmt::Debug> fmt::Debug for Expansion<K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Expansion")
.field("depth", &self.depth)
.field("groups", &self.groups.as_deref())
.field("rows", &self.rows.as_deref())
.finish()
}
}
impl<K: Eq + Hash> PartialEq for Expansion<K> {
fn eq(&self, other: &Self) -> bool {
fn same<T: Eq + Hash>(a: Option<&FxHashSet<T>>, b: Option<&FxHashSet<T>>) -> bool {
match (a, b) {
(Some(a), Some(b)) => a == b,
(Some(set), None) | (None, Some(set)) => set.is_empty(),
(None, None) => true,
}
}
self.depth == other.depth
&& same(self.groups.as_deref(), other.groups.as_deref())
&& same(self.rows.as_deref(), other.rows.as_deref())
}
}
impl<K: Eq + Hash> Eq for Expansion<K> {}
macro_rules! pub_for_tests {
($(#[$meta:meta])* fn $($rest:tt)*) => {
$(#[$meta])*
#[cfg(any(test, feature = "test-support"))]
pub fn $($rest)*
$(#[$meta])*
#[cfg(not(any(test, feature = "test-support")))]
pub(crate) fn $($rest)*
};
}
impl<K: Clone + Eq + Hash> Expansion<K> {
pub_for_tests! {
fn new(depth: ExpandDepth) -> Self {
Self {
depth,
..Self::default()
}
}
}
pub_for_tests! {
fn with_group(mut self, path: GroupPath, expanded: bool, levels: usize) -> Self {
let toggled = expanded != self.depth.expands_group(path.depth(), levels);
toggle(&mut self.groups, path, toggled);
self
}
}
pub_for_tests! {
fn with_row(mut self, key: K, expanded: bool, depth: u16) -> Self {
let toggled = expanded != self.depth.expands_row(depth);
toggle(&mut self.rows, key, toggled);
self
}
}
pub(crate) fn toggled_groups(&self) -> impl Iterator<Item = &GroupPath> {
self.groups.iter().flat_map(|groups| groups.iter())
}
#[cfg_attr(
not(test),
allow(dead_code, reason = "a tree's expansion is not saved in layouts (9.4)")
)]
pub(crate) fn toggled_rows(&self) -> impl Iterator<Item = &K> {
self.rows.iter().flat_map(|rows| rows.iter())
}
}
fn toggle<T: Clone + Eq + Hash>(set: &mut Option<Arc<FxHashSet<T>>>, item: T, toggled: bool) {
if toggled {
Arc::make_mut(set.get_or_insert_with(Default::default)).insert(item);
} else if let Some(items) = set {
if items.contains(&item) {
Arc::make_mut(items).remove(&item);
}
}
}
impl<K: Eq + Hash> Expansion<K> {
pub fn depth(&self) -> ExpandDepth {
self.depth
}
pub fn is_group_expanded(&self, path: &GroupPath, levels: usize) -> bool {
let toggled = self
.groups
.as_ref()
.is_some_and(|groups| groups.contains(path));
self.depth.expands_group(path.depth(), levels) != toggled
}
pub fn is_row_expanded(&self, key: &K, depth: u16) -> bool {
let toggled = self.rows.as_ref().is_some_and(|rows| rows.contains(key));
self.depth.expands_row(depth) != toggled
}
}
impl<R: GridRow> ViewSpec<R> {
pub fn expansion(&self) -> &Expansion<R::Key> {
&self.expansion
}
pub_for_tests! {
fn with_expansion(mut self, expansion: Expansion<R::Key>) -> Self {
self.expansion = expansion;
self
}
}
}
#[derive(Clone, Debug, Default)]
pub(crate) struct AggregateValues {
columns: ColumnSet,
values: Vec<OwnedValue>,
}
impl AggregateValues {
pub(crate) fn new(columns: ColumnSet, values: Vec<OwnedValue>) -> Self {
debug_assert!(
columns.is_empty() || values.len() % columns.len() == 0,
"a value per column"
);
Self { columns, values }
}
fn row(&self, ix: usize, row_count: usize) -> AggregateRow<'_> {
let width = self.columns.len();
AggregateRow {
columns: self.columns,
values: self
.values
.get(ix * width..(ix + 1) * width)
.unwrap_or_default(),
row_count,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct AggregateRow<'a> {
columns: ColumnSet,
values: &'a [OwnedValue],
row_count: usize,
}
impl<'a> AggregateRow<'a> {
pub(crate) fn new(columns: ColumnSet, values: &'a [OwnedValue], row_count: usize) -> Self {
debug_assert_eq!(values.len(), columns.len(), "a value per column");
Self {
columns,
values,
row_count,
}
}
pub fn get(&self, column: ColumnIx) -> Option<&'a OwnedValue> {
if !self.columns.contains(column) {
return None;
}
self.values.get(self.columns.rank(column))
}
pub fn columns(&self) -> ColumnSet {
self.columns
}
pub fn row_count(&self) -> usize {
self.row_count
}
pub fn is_empty(&self) -> bool {
self.values.is_empty()
}
}
#[derive(Clone, Debug)]
pub(crate) struct Group {
pub(super) path: GroupPath,
pub(super) parent: Option<GroupId>,
pub(super) end: u32,
pub(super) key_ix: u32,
pub(super) rows: Range<u32>,
pub(super) live: u32,
pub(super) line: u32,
pub(super) expanded: bool,
pub(super) footer: bool,
}
impl Group {
pub(super) fn depth(&self) -> u16 {
self.path.depth()
}
pub(super) fn row_range(&self) -> Range<usize> {
self.rows.start as usize..self.rows.end as usize
}
}
#[derive(Clone, Debug, Default)]
pub(crate) struct GroupData {
pub(super) groups: Vec<Group>,
pub(super) aggregates: AggregateValues,
pub(super) levels: u16,
by_path: OnceLock<FxHashMap<GroupPath, GroupId>>,
}
static NO_GROUPS: GroupData = GroupData {
groups: Vec::new(),
aggregates: AggregateValues {
columns: ColumnSet::EMPTY,
values: Vec::new(),
},
levels: 0,
by_path: OnceLock::new(),
};
impl GroupData {
pub(super) fn new(levels: u16, groups: Vec<Group>, aggregates: AggregateValues) -> Self {
debug_assert_eq!(
aggregates.values.len(),
groups.len() * aggregates.columns.len(),
"a value per aggregated column"
);
Self {
groups,
aggregates,
levels,
by_path: OnceLock::new(),
}
}
pub(super) fn is_leaf(&self, group: &Group) -> bool {
usize::from(group.depth()) + 1 == usize::from(self.levels)
}
fn level_ids(&self) -> Option<impl Iterator<Item = &ColumnId>> {
let first_leaf = self.groups.get(usize::from(self.levels).checked_sub(1)?)?;
Some(first_leaf.path.levels().iter().map(|(id, _)| id))
}
pub(super) fn table<'a>(&'a self, rows: &'a [RowId]) -> GroupTable<'a> {
GroupTable { data: self, rows }
}
}
#[derive(Clone, Copy)]
pub struct GroupTable<'a> {
data: &'a GroupData,
rows: &'a [RowId],
}
impl<'a> GroupTable<'a> {
pub fn len(&self) -> usize {
self.data.groups.len()
}
pub fn is_empty(&self) -> bool {
self.data.groups.is_empty()
}
pub fn get(&self, id: GroupId) -> Option<GroupInfo<'a>> {
(id.ix() < self.len()).then_some(GroupInfo { table: *self, id })
}
pub fn iter(&self) -> impl ExactSizeIterator<Item = GroupInfo<'a>> + 'a {
let table = *self;
(0..self.len()).map(move |ix| GroupInfo {
table,
id: GroupId::new(ix),
})
}
pub fn find(&self, path: &GroupPath) -> Option<GroupInfo<'a>> {
let by_path = self.data.by_path.get_or_init(|| {
self.data
.groups
.iter()
.enumerate()
.map(|(ix, group)| (group.path.clone(), GroupId::new(ix)))
.collect()
});
self.get(*by_path.get(path)?)
}
}
impl fmt::Debug for GroupTable<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
#[derive(Clone, Copy)]
pub struct GroupInfo<'a> {
table: GroupTable<'a>,
id: GroupId,
}
impl<'a> GroupInfo<'a> {
fn group(&self) -> &'a Group {
&self.table.data.groups[self.id.ix()]
}
pub fn id(&self) -> GroupId {
self.id
}
pub fn path(&self) -> &'a GroupPath {
&self.group().path
}
pub fn level_id(&self) -> &'a ColumnId {
let path = self.path();
path.level_id(path.depth())
.expect("a group's path is not the root")
}
pub fn value(&self) -> &'a OwnedValue {
let path = self.path();
path.value(path.depth())
.expect("a group's path is not the root")
}
pub fn depth(&self) -> u16 {
self.path().depth()
}
pub fn parent(&self) -> Option<GroupId> {
self.group().parent
}
pub fn row_count(&self) -> usize {
self.group().live as usize
}
pub fn rows(&self) -> &'a [RowId] {
let rows = &self.group().rows;
&self.table.rows[rows.start as usize..rows.end as usize]
}
pub fn is_expanded(&self) -> bool {
self.group().expanded
}
pub fn has_footer(&self) -> bool {
self.group().footer
}
pub fn aggregates(&self) -> AggregateRow<'a> {
self.table
.data
.aggregates
.row(self.id.ix(), self.row_count())
}
fn line(&self) -> Option<usize> {
let line = self.group().line;
(line != u32::MAX).then_some(line as usize)
}
}
impl fmt::Debug for GroupInfo<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GroupInfo")
.field("id", &self.id)
.field("path", self.path())
.field("row_count", &self.row_count())
.field("is_expanded", &self.is_expanded())
.field("has_footer", &self.has_footer())
.finish()
}
}
#[derive(Clone, Debug, Default)]
pub(crate) enum Lines {
#[default]
Flat,
Built(Box<BuiltLines>),
}
#[derive(Clone, Debug)]
pub(crate) struct BuiltLines {
lines: Vec<DisplayRow>,
row_to_display: Vec<u32>,
shared_display: FxHashMap<RowId, u32>,
structure: BuiltStructure,
}
impl BuiltLines {
fn new(lines: Vec<DisplayRow>, capacity: usize, structure: BuiltStructure) -> Box<Self> {
let (row_to_display, shared_display) = places(
lines
.iter()
.enumerate()
.filter_map(|(display_ix, line)| line.row().map(|id| (display_ix, id))),
capacity,
);
Box::new(Self {
lines,
row_to_display,
shared_display,
structure,
})
}
}
#[derive(Clone, Debug)]
pub(crate) enum BuiltStructure {
Groups(GroupData),
Tree(TreeData),
}
impl Projection {
pub(crate) fn with_lines<R: GridRow>(
snapshot: &StoreSnapshot<R>,
rows: Vec<RowId>,
lines: Vec<DisplayRow>,
structure: BuiltStructure,
) -> Self {
let mut projection = Self::new(snapshot, rows);
projection.lines = Lines::Built(BuiltLines::new(
lines,
projection.row_to_order.len(),
structure,
));
projection
}
pub(crate) fn with_row_count(mut self, row_count: usize) -> Self {
self.row_count = row_count;
self
}
pub(crate) fn with_groups<R: GridRow>(
snapshot: &StoreSnapshot<R>,
rows: Vec<RowId>,
mut groups: GroupData,
) -> Self {
let lines = expand::lay_out(&mut groups, &rows);
Self::with_lines(snapshot, rows, lines, BuiltStructure::Groups(groups))
}
pub(super) fn with_group_layout(&self, mut groups: GroupData) -> Self {
let lines = expand::lay_out(&mut groups, &self.rows);
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: Lines::Built(BuiltLines::new(
lines,
self.row_to_order.len(),
BuiltStructure::Groups(groups),
)),
totals: self.totals.clone(),
summary_stamps: None,
}
}
pub(super) fn with_group_values(
&self,
groups: GroupData,
totals: AggregateValues,
row_count: usize,
store_version: u64,
) -> Self {
let built = self.built().expect("a grouped projection lists its lines");
Self {
store_version,
rows: self.rows.clone(),
row_to_order: self.row_to_order.clone(),
shared_order: self.shared_order.clone(),
row_count,
lines: Lines::Built(Box::new(BuiltLines {
lines: built.lines.clone(),
row_to_display: built.row_to_display.clone(),
shared_display: built.shared_display.clone(),
structure: BuiltStructure::Groups(groups),
})),
totals,
summary_stamps: None,
}
}
pub(super) fn group_data(&self) -> Option<&GroupData> {
match self.built().map(|built| &built.structure) {
Some(BuiltStructure::Groups(groups)) => Some(groups),
_ => None,
}
}
pub(crate) fn with_totals(mut self, totals: AggregateValues) -> Self {
self.totals = totals;
self
}
pub(crate) fn with_store_version(mut self, store_version: u64) -> Self {
self.store_version = store_version;
self
}
fn built(&self) -> Option<&BuiltLines> {
match &self.lines {
Lines::Flat => None,
Lines::Built(built) => Some(built),
}
}
pub fn len(&self) -> usize {
match &self.lines {
Lines::Flat => self.rows.len(),
Lines::Built(built) => built.lines.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn line(&self, display_ix: usize) -> Option<DisplayRow> {
match &self.lines {
Lines::Flat => self
.rows
.get(display_ix)
.map(|&row| DisplayRow::Data { row, depth: 0 }),
Lines::Built(built) => built.lines.get(display_ix).copied(),
}
}
pub fn row(&self, display_ix: usize) -> Option<RowId> {
match &self.lines {
Lines::Flat => self.rows.get(display_ix).copied(),
Lines::Built(built) => built.lines.get(display_ix).and_then(|line| line.row()),
}
}
pub fn display_ix(&self, id: RowId) -> Option<usize> {
match &self.lines {
Lines::Flat => self.order_ix(id),
Lines::Built(built) => match built.row_to_display.get(id.ix()) {
Some(&ix) if ix != u32::MAX && built.lines[ix as usize].row() == Some(id) => {
Some(ix as usize)
}
_ => shared_place(&built.shared_display, id),
},
}
}
pub fn order_ix(&self, id: RowId) -> Option<usize> {
match self.row_to_order.get(id.ix()) {
Some(&ix) if ix != u32::MAX && self.rows[ix as usize] == id => Some(ix as usize),
_ => shared_place(&self.shared_order, id),
}
}
pub fn row_count(&self) -> usize {
self.row_count
}
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn shown_rows(&self) -> ShownRowIds<'_> {
match &self.lines {
Lines::Flat => ShownRowIds::Flat(self.rows.iter()),
Lines::Built(built) => ShownRowIds::Built(built.lines.iter()),
}
}
pub fn groups(&self) -> GroupTable<'_> {
self.group_data().unwrap_or(&NO_GROUPS).table(&self.rows)
}
pub fn group_line(&self, group: GroupId) -> Option<usize> {
self.groups().get(group)?.line()
}
pub fn totals(&self) -> AggregateRow<'_> {
self.totals.row(0, self.row_count)
}
pub fn is_grouped(&self) -> bool {
matches!(
self.built().map(|built| &built.structure),
Some(BuiltStructure::Groups(_))
)
}
pub fn is_tree(&self) -> bool {
matches!(
self.built().map(|built| &built.structure),
Some(BuiltStructure::Tree(_))
)
}
pub(crate) fn tree_data(&self) -> Option<&TreeData> {
match self.built().map(|built| &built.structure) {
Some(BuiltStructure::Tree(tree)) => Some(tree),
_ => None,
}
}
pub fn expanded_lines(&self) -> impl Iterator<Item = DisplayRow> + '_ {
match &self.lines {
Lines::Flat => ExpandedLines::Flat(self.rows.iter()),
Lines::Built(built) => match &built.structure {
BuiltStructure::Groups(groups) => {
ExpandedLines::Groups(expand::ExpandedGroupLines::new(groups, &self.rows))
}
BuiltStructure::Tree(tree) => {
ExpandedLines::Tree(self.rows.iter().enumerate(), tree)
}
},
}
}
}
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) enum ShownRowIds<'a> {
Flat(slice::Iter<'a, RowId>),
Built(slice::Iter<'a, DisplayRow>),
}
impl Iterator for ShownRowIds<'_> {
type Item = RowId;
#[inline]
fn next(&mut self) -> Option<RowId> {
match self {
Self::Flat(rows) => rows.next().copied(),
Self::Built(lines) => lines.find_map(|line| line.row()),
}
}
}
impl FusedIterator for ShownRowIds<'_> {}
enum ExpandedLines<'a> {
Flat(slice::Iter<'a, RowId>),
Groups(expand::ExpandedGroupLines<'a>),
Tree(iter::Enumerate<slice::Iter<'a, RowId>>, &'a TreeData),
}
impl Iterator for ExpandedLines<'_> {
type Item = DisplayRow;
fn next(&mut self) -> Option<DisplayRow> {
match self {
Self::Flat(rows) => rows.next().map(|&row| DisplayRow::Data { row, depth: 0 }),
Self::Groups(lines) => lines.next(),
Self::Tree(rows, tree) => rows.next().map(|(order_ix, &row)| DisplayRow::Data {
row,
depth: tree.depth(order_ix),
}),
}
}
}
pub(super) fn compact<R: GridRow>(
previous: &Projection,
snapshot: &StoreSnapshot<R>,
) -> Projection {
if let Some(groups) = previous.group_data() {
return compact_groups(previous, groups, snapshot);
}
let mut live = 0;
let rows = previous
.rows
.iter()
.copied()
.filter(|&id| is_kept(snapshot, id, &mut live))
.collect();
Projection::new(snapshot, rows)
.with_row_count(live as usize)
.with_totals(previous.totals.clone())
}
#[inline]
fn is_kept<R: GridRow>(snapshot: &StoreSnapshot<R>, id: RowId, live: &mut u32) -> bool {
if snapshot.row(id).is_some() {
*live += 1;
true
} else {
snapshot.fading_row(id).is_some()
}
}
fn compact_groups<R: GridRow>(
previous: &Projection,
data: &GroupData,
snapshot: &StoreSnapshot<R>,
) -> Projection {
let old = &data.groups;
let mut rows = Vec::with_capacity(previous.rows.len());
let mut ranges: Vec<Range<u32>> = vec![0..0; old.len()];
let mut live = vec![0u32; old.len()];
let mut row_count = 0;
for (ix, group) in old.iter().enumerate() {
if data.is_leaf(group) {
let start = rows.len();
let mut group_live = 0;
rows.extend(
previous.rows[group.row_range()]
.iter()
.copied()
.filter(|&id| is_kept(snapshot, id, &mut group_live)),
);
if group_live == 0 {
rows.truncate(start);
}
ranges[ix] = start as u32..rows.len() as u32;
live[ix] = group_live;
row_count += group_live as usize;
}
}
for (ix, group) in old.iter().enumerate().rev() {
if !data.is_leaf(group) {
let end = group.end as usize;
ranges[ix] = ranges[ix + 1].start..ranges[end - 1].end;
let mut child = ix + 1;
while child < end {
live[ix] += live[child];
child = old[child].end as usize;
}
}
}
let mut kept_before = Vec::with_capacity(old.len() + 1);
let mut kept = 0u32;
for range in &ranges {
kept_before.push(kept);
kept += u32::from(!range.is_empty());
}
kept_before.push(kept);
let width = data.aggregates.columns.len();
let mut groups = Vec::with_capacity(kept as usize);
let mut values = Vec::with_capacity(kept as usize * width);
for (ix, group) in old.iter().enumerate() {
if ranges[ix].is_empty() {
continue;
}
groups.push(Group {
path: group.path.clone(),
parent: group.parent.map(|parent| GroupId(kept_before[parent.ix()])),
end: kept_before[group.end as usize],
key_ix: group.key_ix,
rows: ranges[ix].clone(),
live: live[ix],
line: u32::MAX,
expanded: group.expanded,
footer: group.footer,
});
values.extend_from_slice(&data.aggregates.values[ix * width..(ix + 1) * width]);
}
let groups = GroupData::new(
data.levels,
groups,
AggregateValues::new(data.aggregates.columns, values),
);
Projection::with_groups(snapshot, rows, groups)
.with_row_count(row_count)
.with_totals(previous.totals.clone())
}
#[derive(Clone, Debug)]
pub(crate) struct SummaryStamps {
width: usize,
aggregates: Vec<Stamp>,
captions: Vec<Stamp>,
}
impl SummaryStamps {
fn run(&self, ix: usize) -> Option<&[Stamp]> {
let run = self
.aggregates
.get(ix * self.width..(ix + 1) * self.width)?;
run.iter().any(|stamp| stamp.is_set()).then_some(run)
}
}
impl Projection {
pub fn group_stamps(&self, group: GroupId) -> Option<&[Stamp]> {
let stamps = self.summary_stamps.as_deref()?;
if group.ix() >= stamps.captions.len() {
return None;
}
stamps.run(group.ix())
}
pub fn totals_stamps(&self) -> Option<&[Stamp]> {
let stamps = self.summary_stamps.as_deref()?;
stamps.run(stamps.captions.len())
}
pub fn caption_stamp(&self, group: GroupId) -> Option<Stamp> {
let stamps = self.summary_stamps.as_deref()?;
stamps
.captions
.get(group.ix())
.copied()
.filter(|stamp| stamp.is_set())
}
pub_for_tests! {
fn carry_summary_stamps(
&mut self,
previous: &Projection,
now_ms: u32,
policy: HighlightPolicy,
) {
self.summary_stamps = None;
if !self.has_summaries_of(previous) {
return;
}
let columns = self.totals.columns;
let width = (0..128u16)
.rev()
.find(|&ix| columns.contains(ColumnIx::new(ix)))
.map_or(0, |ix| usize::from(ix) + 1);
let hot = |stamp: Option<Stamp>| {
stamp.filter(|&stamp| !policy.is_expired(stamp, now_ms))
.unwrap_or(Stamp::NONE)
};
let stamp_run = |run: &mut [Stamp],
new: AggregateRow<'_>,
old: AggregateRow<'_>,
old_stamps: Option<&[Stamp]>| {
for (ix, stamp) in run.iter_mut().enumerate() {
let column = ColumnIx::new(ix as u16);
*stamp = match (new.get(column), old.get(column)) {
(Some(new), Some(old)) if new != old => Stamp::with_direction(
now_ms,
RowChange::Updated,
direction(new, old),
),
(Some(_), Some(_)) => hot(old_stamps.and_then(|old| old.get(ix).copied())),
_ => Stamp::NONE,
};
}
};
let groups = self.groups();
let old_groups = previous.groups();
let mut aggregates = vec![Stamp::NONE; (groups.len() + 1) * width];
let mut captions = vec![Stamp::NONE; groups.len()];
for group in groups.iter() {
let ix = group.id().ix();
captions[ix] = match old_groups.find(group.path()) {
Some(old) => {
stamp_run(
&mut aggregates[ix * width..(ix + 1) * width],
group.aggregates(),
old.aggregates(),
previous.group_stamps(old.id()),
);
if group.row_count() == old.row_count() {
hot(previous.caption_stamp(old.id()))
} else {
Stamp::new(now_ms, RowChange::Updated)
}
}
None => Stamp::new(now_ms, RowChange::Inserted),
};
}
stamp_run(
&mut aggregates[groups.len() * width..],
self.totals(),
previous.totals(),
previous.totals_stamps(),
);
if aggregates.iter().chain(&captions).any(|stamp| stamp.is_set()) {
self.summary_stamps = Some(Box::new(SummaryStamps {
width,
aggregates,
captions,
}));
}
}
}
fn has_summaries_of(&self, other: &Projection) -> bool {
if self.totals.columns != other.totals.columns {
return false;
}
fn structure(projection: &Projection) -> Option<&BuiltStructure> {
projection.built().map(|built| &built.structure)
}
match (structure(self), structure(other)) {
(None, None) | (Some(BuiltStructure::Tree(_)), Some(BuiltStructure::Tree(_))) => true,
(Some(BuiltStructure::Groups(a)), Some(BuiltStructure::Groups(b))) => {
a.levels == b.levels
&& match (a.level_ids(), b.level_ids()) {
(Some(a), Some(b)) => a.eq(b),
_ => true,
}
}
_ => false,
}
}
}
fn direction(new: &OwnedValue, old: &OwnedValue) -> Option<ChangeDirection> {
let (new, old) = (new.as_cell(), old.as_cell());
if !(new.is_number() && old.is_number()) {
return None;
}
match new.cmp_total(&old) {
std::cmp::Ordering::Greater => Some(ChangeDirection::Up),
std::cmp::Ordering::Less => Some(ChangeDirection::Down),
std::cmp::Ordering::Equal => None,
}
}
#[cfg(test)]
mod tests {
use std::time::Instant;
use super::*;
use crate::{
highlight::HighlightPolicy,
model::{CellValue, GridColumns},
store::{Ingest, Op},
};
fn path(values: &[i64]) -> GroupPath {
const LEVELS: [&str; 3] = ["a", "b", "c"];
GroupPath::new(
values
.iter()
.zip(LEVELS)
.map(|(value, id)| (ColumnId::new_static(id), OwnedValue::Int(*value))),
)
}
#[test]
fn test_group_path() {
let root = GroupPath::default();
assert!(root.is_root() && root.levels().is_empty());
assert_eq!(root.parent(), None);
let top = path(&[1]);
let inner = path(&[1, 2]);
assert_eq!((top.depth(), inner.depth()), (0, 1));
assert_eq!(inner.level_id(1).map(ColumnId::as_str), Some("b"));
assert_eq!(inner.value(0), Some(&OwnedValue::Int(1)));
assert_eq!(inner.value(2), None);
assert_eq!(inner.parent(), Some(top.clone()));
assert_eq!(top.parent(), Some(root.clone()));
assert!(inner.starts_with(&top) && inner.starts_with(&root) && inner.starts_with(&inner));
assert!(!top.starts_with(&inner) && !path(&[2, 2]).starts_with(&top));
let float = GroupPath::new([(ColumnId::new_static("a"), OwnedValue::Float(1.0))]);
assert_eq!(float, top);
assert_eq!(
FxHashSet::from_iter([float])
.get(&top)
.map(GroupPath::depth),
Some(0)
);
}
#[test]
fn test_expansion_rule() {
use ExpandDepth::{All, Captions, Levels};
let cases: [(ExpandDepth, &[(u16, usize, bool)]); 6] = [
(All, &[(0, 1, true), (1, 2, true), (2, 3, true)]),
(Levels(0), &[(0, 1, false), (0, 3, false), (1, 3, false)]),
(Levels(1), &[(0, 1, true), (0, 2, true), (1, 2, false)]),
(Levels(2), &[(0, 3, true), (1, 3, true), (2, 3, false)]),
(Levels(3), &[(0, 3, true), (1, 3, true), (2, 3, true)]),
(
Captions,
&[
(0, 1, false),
(0, 2, true),
(1, 2, false),
(0, 3, true),
(1, 3, true),
(2, 3, false),
],
),
];
for (depth, groups) in cases {
let expansion = Expansion::<u32>::new(depth);
assert_eq!(expansion.depth(), depth);
for &(group_depth, levels, expanded) in groups {
let values: Vec<i64> = (0..=i64::from(group_depth)).collect();
let group = path(&values);
assert_eq!(
expansion.is_group_expanded(&group, levels),
expanded,
"{depth:?}, depth {group_depth} of {levels}"
);
let toggled = expansion
.clone()
.with_group(group.clone(), !expanded, levels);
assert_eq!(toggled.is_group_expanded(&group, levels), !expanded);
assert_eq!(toggled.toggled_groups().count(), 1);
assert_ne!(toggled, expansion);
let back = toggled.with_group(group.clone(), expanded, levels);
assert_eq!(back.is_group_expanded(&group, levels), expanded);
assert_eq!(back.toggled_groups().count(), 0);
assert_eq!(back, expansion);
}
for row_depth in 0..4u16 {
let expected = match depth {
All | Captions => true,
Levels(n) => row_depth < n,
};
assert_eq!(expansion.is_row_expanded(&7, row_depth), expected);
let toggled = expansion.clone().with_row(7, !expected, row_depth);
assert_eq!(toggled.is_row_expanded(&7, row_depth), !expected);
assert!(expansion.is_row_expanded(&8, row_depth) == expected);
assert_eq!(toggled.toggled_rows().collect::<Vec<_>>(), [&7]);
}
}
let collapsed = Expansion::<u32>::default().with_group(path(&[1, 2]), false, 2);
assert!(!collapsed.is_group_expanded(&path(&[1, 2]), 3));
assert!(collapsed.is_group_expanded(&path(&[1, 3]), 3));
let clone = collapsed.clone();
let changed = clone.with_group(path(&[1, 2]), true, 2);
assert!(!collapsed.is_group_expanded(&path(&[1, 2]), 2));
assert!(changed.is_group_expanded(&path(&[1, 2]), 2));
}
#[test]
fn test_expand_depth_serde() {
for (depth, json) in [
(ExpandDepth::All, r#""all""#),
(ExpandDepth::Captions, r#""captions""#),
(ExpandDepth::Levels(0), "0"),
(ExpandDepth::Levels(2), "2"),
] {
assert_eq!(serde_json::to_string(&depth).unwrap(), json);
assert_eq!(serde_json::from_str::<ExpandDepth>(json).unwrap(), depth);
}
for bad in [r#""deep""#, r#""All""#, "-1", "65536", "1.5", "null"] {
assert!(serde_json::from_str::<ExpandDepth>(bad).is_err(), "{bad}");
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct Column;
impl GridColumns for Column {
fn all() -> &'static [Self] {
&[Self]
}
fn id(self) -> &'static str {
"value"
}
fn ix(self) -> ColumnIx {
ColumnIx::new(0)
}
}
struct Row(u32);
impl GridRow for Row {
type Key = u32;
type Column = Column;
fn key(&self) -> &u32 {
&self.0
}
fn value(&self, _: Column) -> CellValue<'_> {
CellValue::Int(self.0.into())
}
}
#[test]
fn test_listed_lines() {
let epoch = Instant::now();
let mut ingest = Ingest::<Row>::new(HighlightPolicy::default(), epoch);
ingest.apply((0..4).map(|id| Op::UpsertSilent(Arc::new(Row(id)))), epoch);
let snapshot = ingest.snapshot();
let ids: Vec<RowId> = (0..4).map(|key| snapshot.row_id(&key).unwrap()).collect();
let mut columns = ColumnSet::EMPTY;
columns.insert(ColumnIx::new(0));
let group =
|path: GroupPath, end: u32, rows: Range<u32>, expanded: bool, footer: bool| Group {
path,
parent: None,
end,
key_ix: end - 1,
live: rows.end - rows.start,
rows,
line: u32::MAX,
expanded,
footer,
};
let groups = GroupData::new(
1,
vec![
group(path(&[1]), 1, 0..2, true, true),
group(path(&[2]), 2, 2..3, false, false),
],
AggregateValues::new(columns, vec![OwnedValue::Int(1), OwnedValue::Int(2)]),
);
let (first, second) = (GroupId::new(0), GroupId::new(1));
let lines = vec![
DisplayRow::GroupCaption {
group: first,
depth: 0,
},
DisplayRow::Data {
row: ids[0],
depth: 1,
},
DisplayRow::Data {
row: ids[1],
depth: 1,
},
DisplayRow::GroupFooter {
group: first,
depth: 0,
},
DisplayRow::GroupCaption {
group: second,
depth: 0,
},
];
let projection = Projection::with_groups(&snapshot, ids[..3].to_vec(), groups)
.with_totals(AggregateValues::new(columns, vec![OwnedValue::Int(3)]));
assert!(projection.is_grouped() && !projection.is_tree());
assert_eq!(projection.len(), 5);
assert_eq!(
(0..5)
.map(|ix| projection.line(ix).unwrap())
.collect::<Vec<_>>(),
lines
);
assert_eq!(projection.row(0), None);
assert_eq!(projection.row(2), Some(ids[1]));
assert_eq!(projection.display_ix(ids[1]), Some(2));
assert_eq!(projection.display_ix(ids[2]), None, "in a collapsed group");
assert_eq!(projection.order_ix(ids[2]), Some(2));
assert!(projection.contains(ids[2]) && !projection.contains(ids[3]));
assert_eq!(projection.row_count(), 3);
assert_eq!(projection.shown_rows().collect::<Vec<_>>(), ids[..2]);
assert_eq!(projection.group_line(first), Some(0));
assert_eq!(projection.group_line(second), Some(4));
assert_eq!(
projection.totals().get(ColumnIx::new(0)),
Some(&OwnedValue::Int(3))
);
assert_eq!(projection.totals().get(ColumnIx::new(1)), None);
assert_eq!(projection.branch(ids[0]), None);
let table = projection.groups();
assert_eq!(table.len(), 2);
let group = table.find(&path(&[2])).expect("found by path");
assert_eq!(group.id(), second);
assert_eq!(group.rows(), &ids[2..3]);
assert_eq!((group.row_count(), group.is_expanded()), (1, false));
assert_eq!(
group.aggregates().get(ColumnIx::new(0)),
Some(&OwnedValue::Int(2))
);
let first = table.get(first).unwrap();
assert_eq!(first.value(), &OwnedValue::Int(1));
assert_eq!(first.level_id().as_str(), "a");
assert_eq!(
(first.depth(), first.parent(), first.has_footer()),
(0, None, true)
);
assert!(table.find(&path(&[3])).is_none());
assert_eq!(
table
.iter()
.map(|group| group.id().ix())
.collect::<Vec<_>>(),
[0, 1]
);
let copy = projection.duplicate();
assert_eq!(copy.len(), 5);
assert_eq!(copy.groups().len(), 2);
}
}