Skip to main content

relate_executor/graph/
access.rs

1//! The graph's read accessors: the version walk every `*_at` read takes,
2//! the value materializers, the wholesale id enumerators, and the
3//! source-scoped pull identity (ADR-0080).
4
5use relate_core::{NodeId, RelId, Value};
6
7use crate::redo::PullKind;
8
9use super::chain::{Chain, VerRef};
10use super::id_vec::IdVec;
11use super::Graph;
12
13/// The visible version at `ts` — the one walk every `*_at` read takes
14/// over a version chain (LABS-337's extraction of the previously
15/// five-times-spelled rev-find).
16pub(super) fn visible_version<Live>(vers: &Chain<Live>, ts: u64) -> Option<VerRef<'_, Live>> {
17    vers.visible_at(ts)
18}
19
20/// The LIVE version — the last `xmax == 0` one, not a ts-probe: the
21/// maintained-statistics paths run inside the write window, where a rel
22/// created earlier in the same batch is invisible at `commit_ts` and one
23/// deleted in the batch keeps a live-looking pre-delete version. The
24/// `xmax == 0` convention is the one every live-version reader in the graph
25/// module uses (`live_label_ids`, the rebuild's live-rel scan, and the
26/// constraint_keys family's snapshots and backfills).
27pub(super) fn live_version<Live>(vers: &Chain<Live>) -> Option<VerRef<'_, Live>> {
28    vers.live()
29}
30
31/// The ids of `map` visible at `ts`, ascending — the one collector behind
32/// the wholesale node and relationship enumerators.
33fn visible_ids<Live>(map: &IdVec<Chain<Live>>, ts: u64) -> Vec<i64> {
34    let mut ids = Vec::new();
35    map.for_each(|id, vers| {
36        if visible_version(vers, ts).is_some() {
37            ids.push(id);
38        }
39    });
40    ids.sort();
41    ids
42}
43
44impl Graph {
45    pub fn node_value(&self, id: NodeId) -> Value {
46        self.node(id).map(Value::from_node).unwrap_or(Value::Null)
47    }
48
49    pub fn rel_value(&self, id: RelId) -> Value {
50        self.rel(id)
51            .map(Value::from_relationship)
52            .unwrap_or(Value::Null)
53    }
54
55    pub fn pull_identity_get(&self, source: &str, kind: PullKind, remote_id: &str) -> Option<i64> {
56        self.pull_identity.get(source, kind, remote_id)
57    }
58
59    pub fn pull_identity_put(
60        &self,
61        source: String,
62        kind: PullKind,
63        remote_id: String,
64        local_id: i64,
65    ) {
66        self.pull_identity.put(source, kind, remote_id, local_id);
67    }
68
69    pub fn drop_pull_identity(&self, kind: PullKind, local_id: i64) {
70        self.pull_identity.remove(kind, local_id);
71    }
72
73    pub fn all_rel_ids(&self) -> Vec<RelId> {
74        self.all_rel_ids_at(self.read_ts())
75    }
76
77    pub fn all_rel_ids_at(&self, ts: u64) -> Vec<RelId> {
78        visible_ids(&self.rels, ts)
79    }
80
81    pub fn all_node_ids(&self) -> Vec<NodeId> {
82        self.all_node_ids_at(self.read_ts())
83    }
84
85    pub fn all_node_ids_at(&self, ts: u64) -> Vec<NodeId> {
86        visible_ids(&self.nodes, ts)
87    }
88}