Skip to main content

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). A view's rows are [`RowRef`]s into [`LiveTable::batches`], so a display
9//! layer (such as `densa-arrow`'s grid) reads values straight from the Arrow arrays. Each
10//! key's row is a [`RecordId`] while the key lives: a selection can follow it through
11//! updates, compactions and re-sorted views ([`View::position`]).
12//!
13//! This crate has no user-interface dependency.
14//!
15//! ```
16//! use std::sync::Arc;
17//! use arrow_array::{ArrayRef, Float64Array, RecordBatch, StringArray};
18//! use densa_data::{CompareOp, Filter, LiveTable, Order, Scalar, ViewSpec};
19//!
20//! let quotes = |symbols: Vec<&str>, prices: Vec<f64>| {
21//!     RecordBatch::try_from_iter([
22//!         ("symbol", Arc::new(StringArray::from(symbols)) as ArrayRef),
23//!         ("price", Arc::new(Float64Array::from(prices)) as ArrayRef),
24//!     ])
25//! };
26//! let mut table = LiveTable::new(quotes(vec![], vec![])?.schema(), "symbol")?;
27//! let expensive = table.add_view(
28//!     ViewSpec::new()
29//!         .filter(Filter::compare("price", CompareOp::Gt, Scalar::Float(100.0)))
30//!         .sort_by("price", Order::Descending),
31//! )?;
32//!
33//! table.upsert(quotes(vec!["AAA", "BBB", "CCC"], vec![150.0, 90.0, 120.0])?)?;
34//! table.upsert(quotes(vec!["BBB"], vec![200.0])?)?; // BBB rises: it enters the view
35//!
36//! let view = table.view(expensive).unwrap();
37//! assert_eq!(view.len(), 3); // BBB 200, AAA 150, CCC 120
38//! # Ok::<(), Box<dyn std::error::Error>>(())
39//! ```
40
41mod dictionary;
42mod error;
43mod filter;
44mod group;
45mod key;
46mod sorted;
47mod table;
48mod value;
49mod view;
50
51pub use arrow_array;
52pub use arrow_schema;
53pub use error::DataError;
54pub use filter::{CompareOp, Filter, Scalar};
55pub use group::{Aggregate, GroupSpec, Groups};
56pub use table::{Changes, GroupsId, LiveTable, RecordId, RowRef, TableOptions, View, ViewId};
57pub use view::{Order, ViewSpec};
58
59/// A row's internal identity: stable for as long as its key is live.
60type RowId = u32;