Skip to main content

sim_incremental_core/
observation.rs

1//! Dependency observation records captured during query execution.
2
3use crate::ValueFingerprint;
4
5/// A monotone revision stamp for memoized values and external observations.
6#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
7pub struct Revision(u64);
8
9impl Revision {
10    /// The initial revision assigned to unseen external observations.
11    pub const ZERO: Self = Self(0);
12
13    /// Creates a revision from raw stamp bits.
14    #[must_use]
15    pub const fn new(value: u64) -> Self {
16        Self(value)
17    }
18
19    /// Returns the raw revision bits.
20    #[must_use]
21    pub const fn get(self) -> u64 {
22        self.0
23    }
24}
25
26/// The reason a query depends on a key.
27#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
28pub enum ObservationKind {
29    /// A query read another query's value.
30    Read,
31    /// A query observed that a name was absent.
32    Missing,
33    /// A query observed a directory or collection listing.
34    Listing,
35    /// A query observed policy or authority state.
36    Policy,
37    /// A query observed an external backend epoch.
38    Epoch,
39    /// A domain-specific observation class.
40    Custom(&'static str),
41}
42
43/// One dependency observation captured by a query frame.
44#[derive(Clone, Debug, Eq, Hash, PartialEq)]
45pub struct Observation<K> {
46    key: K,
47    kind: ObservationKind,
48    revision: Revision,
49    fingerprint: Option<ValueFingerprint>,
50}
51
52impl<K> Observation<K> {
53    /// Creates an observation from a key, kind, revision, and optional value
54    /// fingerprint.
55    #[must_use]
56    pub fn new(
57        key: K,
58        kind: ObservationKind,
59        revision: Revision,
60        fingerprint: Option<ValueFingerprint>,
61    ) -> Self {
62        Self {
63            key,
64            kind,
65            revision,
66            fingerprint,
67        }
68    }
69
70    /// Creates a query-read observation.
71    #[must_use]
72    pub fn read(key: K, revision: Revision, fingerprint: ValueFingerprint) -> Self {
73        Self::new(key, ObservationKind::Read, revision, Some(fingerprint))
74    }
75
76    /// Returns the observed key.
77    #[must_use]
78    pub fn key(&self) -> &K {
79        &self.key
80    }
81
82    /// Returns the observation kind.
83    #[must_use]
84    pub fn kind(&self) -> &ObservationKind {
85        &self.kind
86    }
87
88    /// Returns the revision captured by the observation.
89    #[must_use]
90    pub fn revision(&self) -> Revision {
91        self.revision
92    }
93
94    /// Returns the captured value fingerprint when this observation has one.
95    #[must_use]
96    pub fn fingerprint(&self) -> Option<ValueFingerprint> {
97        self.fingerprint
98    }
99}