Skip to main content

SendAdjudicate

Trait SendAdjudicate 

pub trait SendAdjudicate: Send {
    type Error: Error + Classify + 'static;

    // Required method
    fn compensation(
        &self,
        group: &EventGroup,
        earlier: &[EventGroup],
    ) -> impl Future<Output = Result<Vec<Event>, Self::Error>> + Send;
}
Expand description

A merge rule: what one direction of an edge writes in answer to a group it ingests (SY-7).

compensation is asked once per incoming group, in batch order, before anything in the batch is written. What it returns is minted by the receiving store as its own events and lands in the same atomic write as the group (SY-2); an empty answer lands the group unjudged. A group the store already holds every event of is a skip, and its compensation is not written again (SY-11), so a rule may answer a re-delivered group as it answered the first delivery.

A rule holds whatever it reads its judgment from, typically its own read handle onto the receiving store, so the runner’s bounds on the store do not grow. It judges by fact family (SY-35); the group’s guard is evidence it may read, never an instruction (SY-6).

§Examples

A rule that reverses the second claim on a seat in one batch, judging from earlier alone:

use core::convert::Infallible;

use happenstance_core::{Event, EventId, RecordedAt, SequencePosition, StoreId};
use happenstance_sync::runner::Adjudicate;
use happenstance_sync::{EventGroup, ReplicatedEvent};

/// Holds the reversal it answers with, built once.
struct FirstClaimWins {
    reversal: Event,
}

fn claims(group: &EventGroup) -> bool {
    group
        .events
        .iter()
        .any(|event| event.event.event_type().as_str() == "SeatClaimed")
}

impl Adjudicate for FirstClaimWins {
    type Error = Infallible;

    async fn compensation(
        &self,
        group: &EventGroup,
        earlier: &[EventGroup],
    ) -> Result<Vec<Event>, Infallible> {
        if claims(group) && earlier.iter().any(claims) {
            return Ok(vec![self.reversal.clone()]);
        }
        Ok(Vec::new())
    }
}

let claim = |origin: u8| -> Result<EventGroup, Box<dyn core::error::Error>> {
    let event = Event::new("SeatClaimed", &b"{}"[..])?;
    let id = EventId::new(StoreId::from_bytes([origin; 16]), SequencePosition::FIRST);
    let at = RecordedAt::from_millis(1_700_000_000_000);
    Ok(EventGroup::new(None, vec![ReplicatedEvent::new(id, at, event)]))
};
let first = claim(1)?;
let second = claim(2)?;
let rule = FirstClaimWins {
    reversal: Event::new("ClaimReversed", &b"{}"[..])?,
};

assert!(rule.compensation(&first, &[]).await?.is_empty());
let earlier = [first];
assert_eq!(rule.compensation(&second, &earlier).await?.len(), 1);

Required Associated Types§

type Error: Error + Classify + 'static

How judging fails, stating a class so the runner reacts to it as to any other failure (SY-36).

Required Methods§

fn compensation( &self, group: &EventGroup, earlier: &[EventGroup], ) -> impl Future<Output = Result<Vec<Event>, Self::Error>> + Send

The compensation group lands with: empty to land it unjudged.

earlier is the groups of the same batch that land before group, in the same ingest call, and so are not yet in the receiving store.

§Errors

The rule’s own error, when it cannot decide: say, its read of the receiving store failed. Nothing in the batch is written, and its class says what the runner does next.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§