pub struct MemoryProjectionStore { /* private fields */ }memory only.Expand description
The reference implementation of ProjectionStore.
It exists for the same three reasons
MemoryEventStore does, in the same order of
importance:
- it is the oracle the conformance suite is validated against, so that a failing adapter is known to be the adapter’s fault and not the suite’s;
- it makes this crate’s examples runnable, so the documentation cannot drift from the API;
- it lets application code be written and tested before any real adapter exists.
It is deliberately obvious rather than fast. When a conformance rule
fails, the adapter is presumed wrong and this store is presumed right, so its
correctness has to be readable in one sitting: one lock over one struct
holding both halves, and commit takes
that lock exactly once. Splitting it into more than one guarded section is
precisely the shape a store that writes the checkpoint without its read model
fails for.
§One end of the batch-shape axis
This store applies on write: a batch is a materialised delta layered over
committed state, so it can be read back through before it commits, and
ProjectionProbe::READS_THROUGH_BATCH is true here. The other end of that
axis — a batch that buffers and replays at commit, where read-through is
impossible and declining it is the conformant answer — is a separate
implementation. Nothing a conformance rule needs may assume read-through
beyond that constant.
The probe’s name is deliberately not a link, for the reason the crate root
gives twice: this page renders whenever memory is on, ProjectionProbe
exists only under conformance, and rustdoc::broken_intra_doc_links is
deny — so a link here is a hard error on the crate’s default feature
set, which is what a consumer builds.
§Examples
The whole loop: open a batch, write through it, commit the rows and the
checkpoint as one unit, read both back, then return the projection to
Checkpoint::NeverRun with the caller’s own deletes.
use happenstance_core::{
Authority, Checkpoint, MemoryProjectionStore, ProjectionId, ProjectionStore,
SequencePosition,
};
let store = MemoryProjectionStore::new();
let id = ProjectionId::new("van_stock")?;
// Never run: the enum says so, and no `Option` is involved.
assert_eq!(store.checkpoint(&id).await?, Checkpoint::NeverRun);
// `begin` never yields and never fails here: opening a buffer is free.
let mut batch = store.begin().await?;
batch.write("depot-7", 12);
// Nothing is visible yet — not the row, not the checkpoint.
assert_eq!(store.get("depot-7"), None);
assert_eq!(store.checkpoint(&id).await?, Checkpoint::NeverRun);
// One unit of work: the row and the checkpoint, or neither.
store
.commit(batch, &id, SequencePosition::FIRST, Authority::Live)
.await?;
assert_eq!(store.get("depot-7"), Some(12));
assert_eq!(
store.checkpoint(&id).await?,
Checkpoint::Live { through: SequencePosition::FIRST },
);
// The dual: the caller's own batch carries the deletes, because the port
// has no idea what the read model is.
let mut clearing = store.begin().await?;
clearing.delete_all();
store.reset(clearing, &id).await?;
assert_eq!(store.get("depot-7"), None);
assert_eq!(store.checkpoint(&id).await?, Checkpoint::NeverRun);Implementations§
Source§impl MemoryProjectionStore
impl MemoryProjectionStore
Sourcepub fn open(&self) -> MemoryProjectionBatch
pub fn open(&self) -> MemoryProjectionBatch
Opens a batch. The inherent twin of
begin, so an application author can
drive this store without importing the port.
Trait Implementations§
Source§impl Debug for MemoryProjectionStore
impl Debug for MemoryProjectionStore
Source§impl Default for MemoryProjectionStore
Hand-written, and not #[derive(Default)], for the same reason
MemoryEventStore’s is hand-written: stamp is
this instance’s identity, and the derive would fill it with 0 without ever
calling the counter, which starts at 1. Two default() stores would then
share identity 0, each would accept the other’s batch, and
CommitError::ForeignBatch — the check the oracle exists to be right
about — would be unreachable through that constructor.
commit_rejects_a_foreign_batch_from_default_stores and its reset twin in
tests/projection_memory.rs are what keep the derive from coming back.
impl Default for MemoryProjectionStore
Hand-written, and not #[derive(Default)], for the same reason
MemoryEventStore’s is hand-written: stamp is
this instance’s identity, and the derive would fill it with 0 without ever
calling the counter, which starts at 1. Two default() stores would then
share identity 0, each would accept the other’s batch, and
CommitError::ForeignBatch — the check the oracle exists to be right
about — would be unreachable through that constructor.
commit_rejects_a_foreign_batch_from_default_stores and its reset twin in
tests/projection_memory.rs are what keep the derive from coming back.
Source§impl ProjectionProbe for MemoryProjectionStore
impl ProjectionProbe for MemoryProjectionStore
Source§const READS_THROUGH_BATCH: bool = true
Available on crate feature conformance only.
const READS_THROUGH_BATCH: bool = true
conformance only.true, because this store applies on write and a batch can therefore be
read back through before it commits.
Source§async fn probe_write(
&self,
batch: &mut Self::Batch,
key: &str,
value: u64,
) -> Result<(), Self::Error>
async fn probe_write( &self, batch: &mut Self::Batch, key: &str, value: u64, ) -> Result<(), Self::Error>
conformance only.Source§async fn probe_delete_all(
&self,
batch: &mut Self::Batch,
) -> Result<(), Self::Error>
async fn probe_delete_all( &self, batch: &mut Self::Batch, ) -> Result<(), Self::Error>
conformance only.