Skip to main content

SyncEngine

Struct SyncEngine 

Source
pub struct SyncEngine { /* private fields */ }
Expand description

The sync engine orchestrates synchronization between local state and a remote sequencer.

This is the Rust equivalent of the JS SyncEngine class, providing:

  • Event recording to the outbox
  • Push (outbox -> remote) via a Transport
  • Pull (remote -> buffer) via a Transport
  • Conflict resolution during pull
  • Status reporting

§Examples

use stateset_sync::{SyncEngine, SyncConfig, SyncEvent};
use serde_json::json;

let config = SyncConfig::new("agent-1", "tenant-1", "store-1");
let mut engine = SyncEngine::new(config).expect("valid sync config");

let seq = engine.record(SyncEvent::new("order.created", "order", "ORD-1", json!({"total": 99})));
assert!(seq.is_ok());
assert_eq!(engine.pending_count(), 1);

Implementations§

Source§

impl SyncEngine

Source

pub fn new(config: SyncConfig) -> Result<Self, SyncError>

Create a new SyncEngine with the given configuration.

§Errors

Returns SyncError::InvalidConfig for invalid settings or SyncError::Storage when durable outbox initialization fails.

Source

pub fn with_strategy( config: SyncConfig, strategy: ConflictStrategy, ) -> Result<Self, SyncError>

Create a SyncEngine with a custom conflict resolution strategy.

§Errors

Returns SyncError::InvalidConfig for invalid settings or SyncError::Storage when durable outbox initialization fails.

Source

pub fn try_new(config: SyncConfig) -> Result<Self, SyncError>

Fallible constructor that validates config and initializes persistence.

§Errors

Returns SyncError::InvalidConfig for invalid settings or SyncError::Storage when durable outbox initialization fails.

Source

pub fn try_with_strategy( config: SyncConfig, strategy: ConflictStrategy, ) -> Result<Self, SyncError>

Fallible constructor with explicit conflict strategy.

§Errors

Returns SyncError::InvalidConfig for invalid settings or SyncError::Storage when durable outbox initialization fails.

Source

pub fn record(&mut self, event: SyncEvent) -> Result<u64, SyncError>

Record an event into the outbox for later push.

Returns the assigned local sequence number.

§Errors

Returns SyncError::OutboxFull if the outbox is at capacity.

Source

pub async fn push( &mut self, transport: &dyn Transport, ) -> Result<PushResult, SyncError>

Push pending events from the outbox to the remote via the given transport.

Drains up to batch_size events from the outbox.

§Errors

Returns SyncError::Transport if the transport operation fails.

Source

pub async fn pull( &mut self, transport: &dyn Transport, ) -> Result<PullResult, SyncError>

Pull events from the remote sequencer into the local buffer.

Pulled events are added to the event buffer. If conflicts are detected (same entity_type + entity_id in both outbox and pulled), they are resolved using the configured strategy.

§Errors

Returns SyncError::Transport if the transport operation fails.

Source

pub fn status(&self) -> SyncStatus

Get the current sync status.

Source

pub fn pending_count(&self) -> usize

Return the number of events pending in the outbox.

Source

pub fn buffered_count(&self) -> usize

Return the number of events currently in the pull buffer.

Source

pub fn drain_buffer(&mut self) -> Vec<SyncEvent>

Drain all events from the pull buffer.

Source

pub const fn state(&self) -> &SyncState

Return a reference to the current sync state.

Source

pub const fn config(&self) -> &SyncConfig

Return a reference to the sync configuration.

Source

pub const fn resolver(&self) -> &ConflictResolver

Return a reference to the conflict resolver.

Source

pub async fn full_sync( &mut self, transport: &dyn Transport, ) -> Result<(PushResult, PullResult), SyncError>

Perform a full sync: push first, then pull.

§Errors

Returns the first error encountered during push or pull.

Trait Implementations§

Source§

impl Debug for SyncEngine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.