nodedb_lite/sync/array/inbound/
dispatcher.rs1use std::collections::{BTreeMap, HashMap};
9use std::sync::{Arc, Mutex};
10
11use nodedb_array::sync::apply::ApplyOutcome;
12use nodedb_array::sync::op::ArrayOp;
13use nodedb_array::sync::snapshot::{SnapshotChunk, SnapshotHeader};
14
15use crate::error::LiteError;
16use crate::storage::engine::StorageEngine;
17use crate::sync::array::catchup::CatchupTracker;
18use crate::sync::array::pending::PendingQueue;
19use crate::sync::array::replica_state::ReplicaState;
20use crate::sync::array::schema_registry::SchemaRegistry;
21
22use super::apply::LiteApplyEngine;
23use super::outcome::InboundOutcome;
24
25pub(super) struct SnapshotAssembly {
29 pub(super) header: SnapshotHeader,
30 pub(super) chunks: BTreeMap<u32, SnapshotChunk>,
31}
32
33impl SnapshotAssembly {
34 pub(super) fn new(header: SnapshotHeader) -> Self {
35 Self {
36 header,
37 chunks: BTreeMap::new(),
38 }
39 }
40}
41
42pub struct ArrayInbound<S: StorageEngine> {
51 pub(super) engine: Arc<LiteApplyEngine<S>>,
52 pub(super) schemas: Arc<SchemaRegistry<S>>,
53 pub(super) replica: Arc<ReplicaState>,
54 pub(super) pending: Arc<PendingQueue<S>>,
55 pub(super) catchup: Arc<CatchupTracker<S>>,
58 pub(super) snapshots: Mutex<HashMap<(String, [u8; 18]), SnapshotAssembly>>,
61}
62
63impl<S: StorageEngine> ArrayInbound<S> {
64 pub fn new(
66 engine: Arc<LiteApplyEngine<S>>,
67 schemas: Arc<SchemaRegistry<S>>,
68 replica: Arc<ReplicaState>,
69 pending: Arc<PendingQueue<S>>,
70 catchup: Arc<CatchupTracker<S>>,
71 ) -> Self {
72 Self {
73 engine,
74 schemas,
75 replica,
76 pending,
77 catchup,
78 snapshots: Mutex::new(HashMap::new()),
79 }
80 }
81
82 pub fn replica_id(&self) -> u64 {
86 self.replica.replica_id().as_u64()
87 }
88
89 pub(super) fn apply_single_op(&self, op: &ArrayOp) -> Result<ApplyOutcome, LiteError> {
92 let mut engine_ref: &LiteApplyEngine<S> = &self.engine;
93 nodedb_array::sync::apply::apply_op(&mut engine_ref, op).map_err(|e| LiteError::Storage {
94 detail: format!("apply: {e}"),
95 })
96 }
97}
98
99pub(super) fn map_apply_outcome(outcome: ApplyOutcome) -> InboundOutcome {
101 match outcome {
102 ApplyOutcome::Applied => InboundOutcome::Applied,
103 ApplyOutcome::Idempotent => InboundOutcome::Idempotent,
104 ApplyOutcome::Rejected(r) => InboundOutcome::Rejected(r),
105 }
106}