1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use crate::serde_utils::{bytes_serde, option_bytes_serde};
use crate::transaction::TransactionMarker;
use bytes::Bytes;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
/// Represents a single message in Rivven
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
/// Unique offset within the partition
pub offset: u64,
/// Message key (optional, used for partitioning)
#[serde(with = "option_bytes_serde")]
pub key: Option<Bytes>,
/// Message payload
#[serde(with = "bytes_serde")]
pub value: Bytes,
/// Timestamp when message was created
pub timestamp: DateTime<Utc>,
/// Optional headers for metadata
pub headers: Vec<(String, Vec<u8>)>,
/// Producer ID (for transactional/idempotent messages)
/// None for non-transactional messages
#[serde(default)]
pub producer_id: Option<u64>,
/// Producer epoch (for fencing)
#[serde(default)]
pub producer_epoch: Option<u16>,
/// Transaction marker (Some for control records, None for data records)
/// Control records mark transaction boundaries (COMMIT/ABORT)
#[serde(default)]
pub transaction_marker: Option<TransactionMarker>,
/// Whether this message is part of an ongoing transaction
/// Used for read_committed filtering
#[serde(default)]
pub is_transactional: bool,
}
impl Message {
/// Create a new message
pub fn new(value: Bytes) -> Self {
Self {
offset: 0,
key: None,
value,
timestamp: Utc::now(),
headers: Vec::new(),
producer_id: None,
producer_epoch: None,
transaction_marker: None,
is_transactional: false,
}
}
/// Create a message with a key
pub fn with_key(key: Bytes, value: Bytes) -> Self {
Self {
offset: 0,
key: Some(key),
value,
timestamp: Utc::now(),
headers: Vec::new(),
producer_id: None,
producer_epoch: None,
transaction_marker: None,
is_transactional: false,
}
}
/// Create a transactional message
pub fn transactional(value: Bytes, producer_id: u64, producer_epoch: u16) -> Self {
Self {
offset: 0,
key: None,
value,
timestamp: Utc::now(),
headers: Vec::new(),
producer_id: Some(producer_id),
producer_epoch: Some(producer_epoch),
transaction_marker: None,
is_transactional: true,
}
}
/// Create a transactional message with a key
pub fn transactional_with_key(
key: Bytes,
value: Bytes,
producer_id: u64,
producer_epoch: u16,
) -> Self {
Self {
offset: 0,
key: Some(key),
value,
timestamp: Utc::now(),
headers: Vec::new(),
producer_id: Some(producer_id),
producer_epoch: Some(producer_epoch),
transaction_marker: None,
is_transactional: true,
}
}
/// Create a transaction control record (COMMIT or ABORT marker)
pub fn control_record(
marker: TransactionMarker,
producer_id: u64,
producer_epoch: u16,
) -> Self {
Self {
offset: 0,
key: None,
value: Bytes::new(), // Control records have empty value
timestamp: Utc::now(),
headers: Vec::new(),
producer_id: Some(producer_id),
producer_epoch: Some(producer_epoch),
transaction_marker: Some(marker),
is_transactional: true,
}
}
/// Check if this is a control record (transaction marker)
pub fn is_control_record(&self) -> bool {
self.transaction_marker.is_some()
}
/// Check if this message is from a committed transaction
/// Note: This is set after transaction completion, not during write
pub fn is_committed(&self) -> bool {
!self.is_transactional || matches!(self.transaction_marker, Some(TransactionMarker::Commit))
}
/// Add a header to the message
pub fn add_header(mut self, key: String, value: Vec<u8>) -> Self {
self.headers.push((key, value));
self
}
/// Mark as transactional
pub fn with_producer(
mut self,
producer_id: u64,
producer_epoch: u16,
transactional: bool,
) -> Self {
self.producer_id = Some(producer_id);
self.producer_epoch = Some(producer_epoch);
self.is_transactional = transactional;
self
}
/// Serialize to bytes (allocates a new Vec).
///
/// For the hot path (segment append), prefer `postcard::to_extend` directly
/// to avoid intermediate allocations — see `Segment::append()`.
pub fn to_bytes(&self) -> crate::Result<Vec<u8>> {
Ok(postcard::to_allocvec(self)?)
}
/// Deserialize from bytes
pub fn from_bytes(data: &[u8]) -> crate::Result<Self> {
Ok(postcard::from_bytes(data)?)
}
}