arete_server/
mutation_batch.rs1use arete_interpreter::Mutation;
4use serde::{Deserialize, Serialize};
5use smallvec::SmallVec;
6use tracing::Span;
7
8#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
14#[serde(rename_all = "camelCase")]
15pub enum SlotIndexDomain {
16 #[default]
18 Legacy,
19 Account,
20 Instruction,
21 Resolver,
22}
23
24#[derive(Debug, Clone, Copy, Default)]
27pub struct SlotContext {
28 pub slot: u64,
30 pub slot_index: u64,
32 pub slot_index_domain: SlotIndexDomain,
34}
35
36impl SlotContext {
37 pub fn with_domain(slot: u64, slot_index: u64, slot_index_domain: SlotIndexDomain) -> Self {
38 Self {
39 slot,
40 slot_index,
41 slot_index_domain,
42 }
43 }
44
45 pub fn new(slot: u64, slot_index: u64) -> Self {
50 Self::with_domain(slot, slot_index, SlotIndexDomain::Legacy)
51 }
52
53 pub fn account(slot: u64, write_version: u64) -> Self {
54 Self::with_domain(slot, write_version, SlotIndexDomain::Account)
55 }
56
57 pub fn instruction(slot: u64, txn_index: u64) -> Self {
58 Self::with_domain(slot, txn_index, SlotIndexDomain::Instruction)
59 }
60
61 pub fn resolver(slot: u64, resolver_index: u64) -> Self {
62 Self::with_domain(slot, resolver_index, SlotIndexDomain::Resolver)
63 }
64
65 pub fn to_seq_string(&self) -> String {
69 format!("{}:{:012}", self.slot, self.slot_index)
70 }
71}
72
73#[derive(Debug)]
78pub struct MutationBatch {
79 pub span: Span,
81 pub mutations: SmallVec<[Mutation; 6]>,
83 pub slot_context: Option<SlotContext>,
85 pub event_context: Option<EventContext>,
87 pub flush_ack: Option<tokio::sync::oneshot::Sender<()>>,
91 pub(crate) snapshot_guard: Option<crate::snapshot::SnapshotProcessingGuard>,
94}
95
96#[derive(Debug, Clone)]
97pub struct EventContext {
98 pub program: String,
99 pub event_kind: String,
100 pub event_type: String,
101 pub account: Option<String>,
102 pub accounts_count: Option<usize>,
103}
104
105impl MutationBatch {
106 pub fn new(mutations: SmallVec<[Mutation; 6]>) -> Self {
107 Self {
108 span: Span::current(),
109 mutations,
110 slot_context: None,
111 event_context: None,
112 flush_ack: None,
113 snapshot_guard: None,
114 }
115 }
116
117 pub fn with_span(span: Span, mutations: SmallVec<[Mutation; 6]>) -> Self {
118 Self {
119 span,
120 mutations,
121 slot_context: None,
122 event_context: None,
123 flush_ack: None,
124 snapshot_guard: None,
125 }
126 }
127
128 pub fn with_slot_context(
129 mutations: SmallVec<[Mutation; 6]>,
130 slot_context: SlotContext,
131 ) -> Self {
132 Self {
133 span: Span::current(),
134 mutations,
135 slot_context: Some(slot_context),
136 event_context: None,
137 flush_ack: None,
138 snapshot_guard: None,
139 }
140 }
141
142 pub fn flush_marker(ack: tokio::sync::oneshot::Sender<()>) -> Self {
145 Self {
146 span: Span::current(),
147 mutations: SmallVec::new(),
148 slot_context: None,
149 event_context: None,
150 flush_ack: Some(ack),
151 snapshot_guard: None,
152 }
153 }
154
155 pub fn with_snapshot_guard(mut self, guard: crate::snapshot::SnapshotProcessingGuard) -> Self {
158 self.snapshot_guard = Some(guard);
159 self
160 }
161
162 pub fn with_event_context(mut self, event_context: EventContext) -> Self {
163 self.event_context = Some(event_context);
164 self
165 }
166
167 pub fn len(&self) -> usize {
168 self.mutations.len()
169 }
170
171 pub fn is_empty(&self) -> bool {
172 self.mutations.is_empty()
173 }
174}