crates/kleene-gar/src/ids.rs
//! One id space over a set's vertex types.
//!
//! A vertex type's rows are numbered `0..count` in its own files, so rows of two types share
//! their numbers. Every reader that joins nodes of two types needs one number per node:
//! the set's vertex types in [`GarGraph::vertex_types`] order (the order its
//! `<name>.graph.yml` lists them, the same for every reader and every open), each numbered from the sum of
//! the counts before it (type `t`'s row `r` is id `base(t) + r`). [`GarGraph::id_space`]
//! gives the numbering, [`GarGraph::vertex_ids`] a type's ids as a column (to key its
//! properties by). An edge type's adjacency stays in its types' rows (shared from its
//! files); kleene-plan's `Source::GraphAt` reads it in the id space by the two bases.
use std::sync::Arc;
use arrow_array::{ArrayRef, UInt64Array};
use datafusion::error::{DataFusionError, Result};
use crate::graph::GarGraph;
/// The numbering of a set's vertex types: per type (in the set's order) its base and count.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IdSpace {
types: Vec<(String, u64, u64)>,
}
impl IdSpace {
/// The numbering of types `(name, count)` in this order, each from the sum of the counts
/// before it.
pub fn new<'a>(types: impl IntoIterator<Item = (&'a str, u64)>) -> Self {
let mut base = 0;
let types = types
.into_iter()
.map(|(t, n)| {
let b = base;
base += n;
(t.to_string(), b, n)
})
.collect();
IdSpace { types }
}
/// `(type, base, count)` in order.
pub fn types(&self) -> impl Iterator<Item = (&str, u64, u64)> {
self.types.iter().map(|(t, b, n)| (t.as_str(), *b, *n))
}
/// Type `t`'s base and count.
pub fn of(&self, t: &str) -> Option<(u64, u64)> {
self.types
.iter()
.find(|x| x.0 == t)
.map(|&(_, b, n)| (b, n))
}
/// Type `t`'s base.
pub fn base(&self, t: &str) -> Option<u64> {
self.of(t).map(|x| x.0)
}
/// The number of ids (every type's rows).
pub fn total(&self) -> u64 {
self.types.last().map_or(0, |t| t.1 + t.2)
}
/// The id of row `row` of type `t`.
pub fn id(&self, t: &str, row: u64) -> Option<u64> {
let (b, n) = self.of(t)?;
(row < n).then_some(b + row)
}
/// The place (in order) and row of id `id`, by a binary search over the bases (the
/// bases rise): for an id space of many rows (one per part of a split).
pub fn position(&self, id: u64) -> Option<(usize, u64)> {
// The last row whose base is at or below `id` (an empty row shares its base with the
// next one and is passed over).
let k = self.types.partition_point(|t| t.1 <= id).checked_sub(1)?;
let (_, b, n) = &self.types[k];
(id < b + n).then_some((k, id - b))
}
/// The type and row of id `id`.
pub fn locate(&self, id: u64) -> Option<(&str, u64)> {
self.types
.iter()
.find(|&&(_, b, n)| id >= b && id < b + n)
.map(|(t, b, _)| (t.as_str(), id - b))
}
}
impl GarGraph {
/// The set's id space: its vertex types in order, each numbered from the sum of the
/// counts before it.
pub fn id_space(&self) -> Result<IdSpace> {
let types: Vec<(&str, u64)> = self
.vertex_types()
.iter()
.map(|t| Ok((t.as_str(), self.vertex_count(t)? as u64)))
.collect::<Result<_>>()?;
Ok(IdSpace::new(types))
}
/// The ids of vertex type `t`'s rows, in row order (`base..base + count`): the key
/// column of its properties in the id space.
pub fn vertex_ids(&self, t: &str) -> Result<ArrayRef> {
let (b, n) = self
.id_space()?
.of(t)
.ok_or_else(|| DataFusionError::Plan(format!("no vertex type `{t}`")))?;
Ok(Arc::new(UInt64Array::from_iter_values(b..b + n)))
}
}