densa_data/lib.rs
1//! Live Arrow tables: rows identified by a key, updated by record batches, and views that
2//! stay sorted and filtered as the rows change.
3//!
4//! [`LiveTable`] takes Arrow [`RecordBatch`](arrow_array::RecordBatch)es from a feed, a
5//! query engine or a file, and upserts them by a key column; [`ViewSpec`] describes a view
6//! (a [`Filter`] and sort columns), which the table keeps up to date incrementally instead of
7//! recomputing it; [`GroupSpec`] does the same for grouped aggregates (count, sum, min, max,
8//! mean), grouped by key columns or by buckets of one (counts per minute,
9//! [`GroupSpec::group_by_time`]). A view's rows are [`RowRef`]s into
10//! [`LiveTable::batches`], so a display layer (such as `densa-arrow`'s grid) reads values
11//! straight from the Arrow arrays. Each key's row is a [`RecordId`] while the key lives: a
12//! selection can follow it through updates, compactions and re-sorted views
13//! ([`View::position`]). [`LiveTable::snapshot`] hands the live rows to anything else that
14//! reads Arrow, such as a SQL engine. A [`Window`] keeps a table to its recent rows, by a
15//! time column.
16//!
17//! This crate has no user-interface dependency.
18//!
19//! ```
20//! use std::sync::Arc;
21//! use arrow_array::{ArrayRef, Float64Array, RecordBatch, StringArray};
22//! use densa_data::{CompareOp, Filter, LiveTable, Order, Scalar, ViewSpec};
23//!
24//! let quotes = |symbols: Vec<&str>, prices: Vec<f64>| {
25//! RecordBatch::try_from_iter([
26//! ("symbol", Arc::new(StringArray::from(symbols)) as ArrayRef),
27//! ("price", Arc::new(Float64Array::from(prices)) as ArrayRef),
28//! ])
29//! };
30//! let mut table = LiveTable::new(quotes(vec![], vec![])?.schema(), "symbol")?;
31//! let expensive = table.add_view(
32//! ViewSpec::new()
33//! .filter(Filter::compare("price", CompareOp::Gt, Scalar::Float(100.0)))
34//! .sort_by("price", Order::Descending),
35//! )?;
36//!
37//! table.upsert(quotes(vec!["AAA", "BBB", "CCC"], vec![150.0, 90.0, 120.0])?)?;
38//! table.upsert(quotes(vec!["BBB"], vec![200.0])?)?; // BBB rises: it enters the view
39//!
40//! let view = table.view(expensive).unwrap();
41//! assert_eq!(view.len(), 3); // BBB 200, AAA 150, CCC 120
42//! # Ok::<(), Box<dyn std::error::Error>>(())
43//! ```
44
45mod bucket;
46mod dictionary;
47mod error;
48mod filter;
49mod group;
50mod key;
51mod snapshot;
52mod sorted;
53mod table;
54mod value;
55mod view;
56mod window;
57
58pub use arrow_array;
59pub use arrow_schema;
60pub use error::DataError;
61pub use filter::{CompareOp, Filter, Scalar};
62pub use group::{Aggregate, GroupSpec, Groups};
63pub use table::{Changes, GroupsId, LiveTable, RecordId, RowRef, TableOptions, View, ViewId};
64pub use view::{Order, ViewSpec};
65pub use window::Window;
66
67/// A row's internal identity: stable for as long as its key is live.
68type RowId = u32;