Skip to main content

read_decision_model

Function read_decision_model 

Source
pub async fn read_decision_model<S>(
    store: &S,
    query: &Query,
) -> Result<(Vec<SequencedEvent>, Option<SequencePosition>), S::Error>
where S: EventStore,
Expand description

Reads query and reports both the matching events and the position to use in a follow-up AppendCondition.

This is the read half of the DCB command loop: build a decision model from what you can see, then append conditioned on nothing new having appeared. The returned position is the last one observed, or None when nothing matched — which is exactly what AppendCondition::after_every_guard_opt expects.

§Errors

Returns the adapter’s error if the read fails.

Examples found in repository?
examples/transfers-on-sqlite/src/main.rs (line 595)
590async fn fold_balance(events: &SqliteEventStore, account: &str) -> Result<Balance> {
591    let account = AccountId::new(account)?;
592    let mut balance = Balance::new(&account)?;
593
594    let query = balance.query()?;
595    let (found, _) = read_decision_model(events, &query).await?;
596    for event in &found {
597        balance.absorb(event, &Json)?;
598    }
599
600    Ok(balance)
601}
More examples
Hide additional examples
examples/telemetry-across-codecs/src/main.rs (line 213)
206async fn one_fold_reads_both(events: &SqliteEventStore) -> Result<()> {
207    println!("\n== one fold reads both ==");
208
209    let device = DeviceId::new(DEVICE)?;
210    let mut readings = Readings::new(&device)?;
211
212    let query = readings.query()?;
213    let (found, _) = read_decision_model(events, &query).await?;
214    for event in &found {
215        readings.absorb(event, &Json)?;
216    }
217
218    println!(
219        "   the derived query nominated {} event(s), and one codec was named here: Json",
220        found.len()
221    );
222    println!(
223        "   firmware:   {}",
224        readings.firmware.as_deref().unwrap_or("unknown")
225    );
226    println!("   readings:   {:?}", readings.millidegrees);
227
228    // The upcast is the assertion. 21 degrees was written by a binary that had
229    // never heard of a thousandth, and it is 21000 here without anything in
230    // this function converting it.
231    if !readings.millidegrees.contains(&(21 * PER_DEGREE)) {
232        bail!(
233            "the first JSON reading did not arrive upcast: {:?}",
234            readings.millidegrees
235        );
236    }
237    println!(
238        "   21 whole degrees, written by a binary with no concept of a\n   \
239         thousandth, arrives as {} — the upcast is in `decode`, and the fold\n   \
240         below it has one temperature variant and no idea there were ever two",
241        21 * PER_DEGREE
242    );
243
244    Ok(())
245}
examples/two-devices-one-seat/src/rule.rs (line 62)
59    async fn held(&self) -> Result<(ShowLedger, Query), RuleError> {
60        let mut ledger = ShowLedger::new(&self.show);
61        let query = ledger.query().map_err(RuleError::Query)?;
62        let (found, _) = read_decision_model(&self.central, &query)
63            .await
64            .map_err(RuleError::Read)?;
65        for event in &found {
66            ledger.absorb(event, &Json).map_err(RuleError::Decode)?;
67        }
68        Ok((ledger, query))
69    }
examples/two-devices-one-seat/src/scenario.rs (line 401)
398    pub async fn ledger(&self, at: Place) -> Result<ShowLedger, StoryError> {
399        let mut ledger = ShowLedger::new(&self.show);
400        let query = ledger.query()?;
401        let (found, _) = read_decision_model(&self.site(at).events, &query)
402            .await
403            .map_err(|source| StoryError::Read { place: at, source })?;
404        for event in &found {
405            ledger
406                .absorb(event, &Json)
407                .map_err(|source| StoryError::Decode { place: at, source })?;
408        }
409        Ok(ledger)
410    }