use std::marker::PhantomData;
use crate::{
adaptive::{AdaptivePolicy, EncodeDecision},
error::DeltaError,
migration::MigrationRegistry,
packet::{DecodeConfig, Packet},
schema::DeltaState,
sync::{GenericApplyResult, GenericDecoder, GenericEncoder},
};
pub struct StatePublisher<T: DeltaState> {
encoder: GenericEncoder<T>,
}
impl<T: DeltaState> Default for StatePublisher<T> {
fn default() -> Self {
Self {
encoder: GenericEncoder::default(),
}
}
}
impl<T: DeltaState> StatePublisher<T> {
pub fn new() -> Self {
Self::default()
}
pub fn builder() -> PublisherBuilder<T> {
PublisherBuilder::default()
}
pub fn with_policy(policy: AdaptivePolicy) -> Self {
Self {
encoder: GenericEncoder::with_policy(policy),
}
}
pub fn encode(&mut self, state: &T) -> Result<Vec<u8>, DeltaError> {
self.update(state)?.to_bytes()
}
pub fn update(&mut self, state: &T) -> Result<Packet, DeltaError> {
self.encoder.encode(state)
}
pub fn force_snapshot(&mut self, state: &T) -> Result<Packet, DeltaError> {
self.encoder.force_snapshot(state)
}
pub fn recovery_snapshot(&self, state: &T) -> Result<Packet, DeltaError> {
self.encoder.recovery_snapshot(state)
}
pub fn sequence(&self) -> u64 {
self.encoder.sequence()
}
pub fn last_decision(&self) -> Option<EncodeDecision> {
self.encoder.last_decision()
}
}
pub struct PublisherBuilder<T: DeltaState> {
policy: AdaptivePolicy,
_marker: PhantomData<T>,
}
impl<T: DeltaState> Default for PublisherBuilder<T> {
fn default() -> Self {
Self {
policy: AdaptivePolicy::default(),
_marker: PhantomData,
}
}
}
impl<T: DeltaState> PublisherBuilder<T> {
pub fn adaptive_policy(mut self, policy: AdaptivePolicy) -> Self {
self.policy = policy;
self
}
pub fn compression(mut self, enabled: bool) -> Self {
self.policy.enable_zstd = enabled;
self
}
pub fn zstd_level(mut self, level: i32) -> Self {
self.policy.zstd_level = level;
self
}
pub fn build(self) -> StatePublisher<T> {
StatePublisher::with_policy(self.policy)
}
}
pub struct StateSubscriber<T: DeltaState> {
decoder: GenericDecoder<T>,
decode_config: DecodeConfig,
}
impl<T: DeltaState> Default for StateSubscriber<T> {
fn default() -> Self {
let decode_config = DecodeConfig::default();
Self {
decoder: GenericDecoder::with_decode_config(decode_config),
decode_config,
}
}
}
impl<T: DeltaState> StateSubscriber<T> {
pub fn new() -> Self {
Self::default()
}
pub fn with_decode_config(decode_config: DecodeConfig) -> Self {
Self {
decoder: GenericDecoder::with_decode_config(decode_config),
decode_config,
}
}
pub fn receive(&mut self, bytes: &[u8]) -> Result<GenericApplyResult<T>, DeltaError> {
let packet = Packet::from_bytes_with_config(bytes, &self.decode_config)?;
self.apply(packet)
}
pub fn apply(&mut self, packet: Packet) -> Result<GenericApplyResult<T>, DeltaError> {
self.decoder.apply_packet(packet)
}
pub fn apply_with_migrations(
&mut self,
packet: Packet,
migrations: &MigrationRegistry,
) -> Result<GenericApplyResult<T>, DeltaError> {
self.decoder
.apply_packet_with_migrations(packet, migrations)
}
pub fn state(&self) -> Option<&T> {
self.decoder.state()
}
pub fn sequence(&self) -> Option<u64> {
self.decoder.sequence()
}
pub fn reset(&mut self) {
self.decoder = GenericDecoder::with_decode_config(self.decode_config);
}
}
pub type Publisher<T> = StatePublisher<T>;
pub type Subscriber<T> = StateSubscriber<T>;