use std::sync::{Arc, OnceLock};
use relate_core::{NodeId, Value};
use relate_ir::BinaryOp;
use super::*;
#[derive(Clone, Copy)]
pub enum PropFold {
Count,
Min,
Max,
}
pub trait PropStore: Send + Sync + std::fmt::Debug {
fn point(&self, ts: u64, id: NodeId, kid: u32) -> Option<Value>;
fn point_batch(&self, ts: u64, ids: &[NodeId], kid: u32, out: &mut [Option<Value>]) {
assert_eq!(
ids.len(),
out.len(),
"ids and out lengths must match for point_batch"
);
for (i, &id) in ids.iter().enumerate() {
out[i] = self.point(ts, id, kid);
}
}
fn filter_range(
&self,
_ts: u64,
_ids: &[NodeId],
_kid: u32,
_op: BinaryOp,
_lit: &Value,
_mask: &mut [u64],
) -> Option<()> {
None
}
fn folds_masked(
&self,
_ts: u64,
_ids: &[NodeId],
_mask: &[u64],
_kid: u32,
_wants: &[PropFold],
) -> Option<Vec<Value>> {
None
}
fn grouped_counts_masked(
&self,
_ts: u64,
_ids: &[NodeId],
_mask: &[u64],
_kid: u32,
) -> Option<Vec<(Value, u64)>> {
None
}
fn carries(&self, kid: u32) -> bool;
fn serves_at(&self, ts: u64) -> bool;
fn equality(
&self,
_labels: &[u32],
_kid: u32,
_values: &[Value],
_cap: Option<usize>,
) -> Option<Vec<NodeId>> {
None
}
fn folds(&self, _labels: &[u32], _kid: u32, _wants: &[PropFold]) -> Option<Vec<Value>> {
None
}
fn grouped_counts(&self, _labels: &[u32], _kid: u32) -> Option<Vec<(Value, u64)>> {
None
}
fn on_publication(&self, ts: u64, rows: &[PublishedRow]);
}
pub struct PublishedRow {
pub id: NodeId,
pub labels: Vec<u32>,
pub props: Vec<(u32, Value)>,
pub deleted: bool,
}
impl Graph {
pub(crate) fn point_through(
&self,
store: Option<&dyn PropStore>,
ts: u64,
id: NodeId,
kid: u32,
) -> Option<Value> {
store
.filter(|store| store.carries(kid))
.and_then(|store| store.point(ts, id, kid))
.or_else(|| self.node_prop_row(id, ts, kid))
}
pub(crate) fn point_batch_through(
&self,
store: Option<&dyn PropStore>,
ts: u64,
ids: &[NodeId],
kid: u32,
out: &mut [Option<Value>],
) {
assert_eq!(
ids.len(),
out.len(),
"ids and out lengths must match for point_batch_through"
);
if let Some(store) = store.filter(|store| store.carries(kid)) {
store.point_batch(ts, ids, kid, out);
for (i, &id) in ids.iter().enumerate() {
if out[i].is_none() {
out[i] = self.node_prop_row(id, ts, kid);
}
}
} else {
for (i, &id) in ids.iter().enumerate() {
out[i] = self.node_prop_row(id, ts, kid);
}
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn filter_range_through(
&self,
store: Option<&dyn PropStore>,
ts: u64,
ids: &[NodeId],
kid: u32,
op: BinaryOp,
lit: &Value,
mask: &mut [u64],
) -> Option<()> {
let store = store.filter(|s| s.carries(kid))?;
store.filter_range(ts, ids, kid, op, lit, mask)
}
#[allow(dead_code)]
pub(crate) fn folds_masked_through(
&self,
store: Option<&dyn PropStore>,
ts: u64,
ids: &[NodeId],
mask: &[u64],
kid: u32,
wants: &[PropFold],
) -> Option<Vec<Value>> {
let store = store.filter(|s| s.carries(kid))?;
let served = store.folds_masked(ts, ids, mask, kid, wants)?;
(served.len() == wants.len()).then_some(served)
}
#[allow(dead_code)]
pub(crate) fn grouped_counts_masked_through(
&self,
store: Option<&dyn PropStore>,
ts: u64,
ids: &[NodeId],
mask: &[u64],
kid: u32,
) -> Option<Vec<(Value, u64)>> {
let store = store.filter(|s| s.carries(kid))?;
store.grouped_counts_masked(ts, ids, mask, kid)
}
}
impl PropStore for Graph {
fn point(&self, ts: u64, id: NodeId, kid: u32) -> Option<Value> {
self.point_through(
self.serving_store(ts).map(|store| store.as_ref()),
ts,
id,
kid,
)
}
fn point_batch(&self, ts: u64, ids: &[NodeId], kid: u32, out: &mut [Option<Value>]) {
self.point_batch_through(
self.serving_store(ts).map(|store| store.as_ref()),
ts,
ids,
kid,
out,
);
}
fn filter_range(
&self,
ts: u64,
ids: &[NodeId],
kid: u32,
op: BinaryOp,
lit: &Value,
mask: &mut [u64],
) -> Option<()> {
self.filter_range_through(
self.serving_store(ts).map(|store| store.as_ref()),
ts,
ids,
kid,
op,
lit,
mask,
)
}
fn folds_masked(
&self,
ts: u64,
ids: &[NodeId],
mask: &[u64],
kid: u32,
wants: &[PropFold],
) -> Option<Vec<Value>> {
self.folds_masked_through(
self.serving_store(ts).map(|store| store.as_ref()),
ts,
ids,
mask,
kid,
wants,
)
}
fn grouped_counts_masked(
&self,
ts: u64,
ids: &[NodeId],
mask: &[u64],
kid: u32,
) -> Option<Vec<(Value, u64)>> {
self.grouped_counts_masked_through(
self.serving_store(ts).map(|store| store.as_ref()),
ts,
ids,
mask,
kid,
)
}
fn carries(&self, _kid: u32) -> bool {
true
}
fn serves_at(&self, _ts: u64) -> bool {
true
}
fn on_publication(&self, _ts: u64, _rows: &[PublishedRow]) {}
}
#[derive(Debug, Default)]
pub(crate) struct AttachedStore {
cell: OnceLock<Arc<dyn PropStore>>,
pending: std::sync::Mutex<std::collections::HashSet<NodeId>>,
}
impl AttachedStore {
pub(crate) fn attach(&self, store: Arc<dyn PropStore>) -> bool {
self.cell.set(store).is_ok()
}
pub(crate) fn get(&self) -> Option<&Arc<dyn PropStore>> {
self.cell.get()
}
pub(crate) fn mark_dirty(&self, id: NodeId) {
if self.cell.get().is_some() {
self.pending.lock().unwrap().insert(id);
}
}
pub(crate) fn take_pending(&self) -> Vec<NodeId> {
std::mem::take(&mut *self.pending.lock().unwrap())
.into_iter()
.collect()
}
pub(crate) fn clear(&self) {
self.pending.lock().unwrap().clear();
}
}
impl Graph {
#[doc(hidden)]
pub fn attach_prop_store(&self, store: Arc<dyn PropStore>) -> bool {
self.attached_store.attach(store)
}
#[doc(hidden)]
pub fn interned_id(&self, name: &str) -> Option<u32> {
self.interner.get_id(name)
}
#[doc(hidden)]
pub fn has_attached_prop_store(&self) -> bool {
self.attached_prop_store().is_some()
}
pub(crate) fn attached_prop_store(&self) -> Option<&Arc<dyn PropStore>> {
self.attached_store.get()
}
fn serving_store(&self, ts: u64) -> Option<&Arc<dyn PropStore>> {
self.attached_prop_store()
.filter(|store| store.serves_at(ts))
}
pub(crate) fn serving_store_handle(&self, ts: u64) -> Option<Arc<dyn PropStore>> {
self.serving_store(ts).cloned()
}
pub(crate) fn node_prop_row(&self, id: NodeId, ts: u64, kid: u32) -> Option<Value> {
self.nodes
.get(&id)
.and_then(|vers| visible_version(&vers, ts).and_then(|v| v.live.prop_by_id(kid)))
}
pub(crate) fn prop_store_equality(
&self,
labels: &[String],
key: &str,
values: &[Value],
ts: u64,
cap: Option<usize>,
) -> Option<Vec<NodeId>> {
let store = self.serving_store(ts)?;
let (kid, lids) = self.resolved_serving(store, labels, key)?;
let ids = store.equality(&lids, kid, values, cap)?;
let ascending_dedup = ids.windows(2).all(|pair| pair[0] < pair[1]);
ascending_dedup.then_some(ids)
}
pub(crate) fn prop_store_folds(
&self,
labels: &[String],
key: &str,
wants: &[PropFold],
ts: u64,
) -> Option<Vec<Value>> {
let store = self.serving_store(ts)?;
let (kid, lids) = self.resolved_serving(store, labels, key)?;
let served = store.folds(&lids, kid, wants)?;
(served.len() == wants.len()).then_some(served)
}
pub(crate) fn prop_store_grouped_counts(
&self,
labels: &[String],
key: &str,
ts: u64,
) -> Option<Vec<(Value, u64)>> {
let store = self.serving_store(ts)?;
let (kid, lids) = self.resolved_serving(store, labels, key)?;
store.grouped_counts(&lids, kid)
}
pub(in crate::graph) fn publish_to_prop_store(&self, ts: u64) {
let ids = self.attached_store.take_pending();
let Some(store) = self.attached_prop_store() else {
return;
};
if ids.is_empty() {
return;
}
let mut rows = Vec::with_capacity(ids.len());
for id in ids {
let vers = self.nodes.get(&id);
match vers.as_ref().and_then(|vers| visible_version(vers, ts)) {
Some(v) => rows.push(PublishedRow {
id,
labels: v.live.labels.clone(),
props: v
.live
.props
.iter()
.map(|(kid, value)| (kid, value.clone()))
.collect(),
deleted: false,
}),
None => rows.push(PublishedRow {
id,
labels: Vec::new(),
props: Vec::new(),
deleted: true,
}),
}
}
store.on_publication(ts, &rows);
}
fn resolved_serving(
&self,
store: &Arc<dyn PropStore>,
labels: &[String],
key: &str,
) -> Option<(u32, Vec<u32>)> {
let kid = self.interned_id(key)?;
if !store.carries(kid) {
return None;
}
let lids = self.interned_labels(labels)?;
Some((kid, lids))
}
fn interned_labels(&self, labels: &[String]) -> Option<Vec<u32>> {
labels.iter().map(|label| self.interned_id(label)).collect()
}
}