1use crate::primitives::fixed::{
4 get_bool, get_i8, get_i32, get_i64, put_bool, put_i8, put_i32, put_i64,
5};
6use crate::primitives::string_bytes::{
7 compact_nullable_bytes_len, get_compact_nullable_bytes_owned, get_nullable_bytes_owned,
8 nullable_bytes_len, put_compact_nullable_bytes, put_nullable_bytes,
9};
10use crate::primitives::string_bytes::{
11 compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
12 string_len,
13};
14use crate::tagged_fields::{
15 WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len,
16};
17use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
18use bytes::{Buf, BufMut};
19pub const MIN_VERSION: i16 = 0;
20pub const MAX_VERSION: i16 = 0;
21pub const FLEXIBLE_MIN: i16 = 0;
22#[inline]
23fn is_flexible(version: i16) -> bool {
24 version >= FLEXIBLE_MIN
25}
26#[derive(Debug, Clone, PartialEq, Eq, Default)]
27pub struct RemoteLogSegmentMetadataRecord {
28 pub remote_log_segment_id: RemoteLogSegmentIdEntry,
29 pub start_offset: i64,
30 pub end_offset: i64,
31 pub broker_id: i32,
32 pub max_timestamp_ms: i64,
33 pub event_timestamp_ms: i64,
34 pub segment_leader_epochs: Vec<SegmentLeaderEpochEntry>,
35 pub segment_size_in_bytes: i32,
36 pub custom_metadata: Option<::bytes::Bytes>,
37 pub remote_log_segment_state: i8,
38 pub txn_index_empty: bool,
39 pub unknown_tagged_fields: UnknownTaggedFields,
40}
41impl Encode for RemoteLogSegmentMetadataRecord {
42 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
43 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
44 return Err(ProtocolError::SchemaMismatch(
45 "RemoteLogSegmentMetadataRecord version out of range",
46 ));
47 }
48 let flex = is_flexible(version);
49 if version >= 0 {
50 self.remote_log_segment_id.encode(buf, version)?;
51 }
52 if version >= 0 {
53 put_i64(buf, self.start_offset);
54 }
55 if version >= 0 {
56 put_i64(buf, self.end_offset);
57 }
58 if version >= 0 {
59 put_i32(buf, self.broker_id);
60 }
61 if version >= 0 {
62 put_i64(buf, self.max_timestamp_ms);
63 }
64 if version >= 0 {
65 put_i64(buf, self.event_timestamp_ms);
66 }
67 if version >= 0 {
68 {
69 crate::primitives::array::put_array_len(
70 buf,
71 (self.segment_leader_epochs).len(),
72 flex,
73 );
74 for it in &self.segment_leader_epochs {
75 it.encode(buf, version)?;
76 }
77 }
78 }
79 if version >= 0 {
80 put_i32(buf, self.segment_size_in_bytes);
81 }
82 if version >= 0 {
83 if flex {
84 put_compact_nullable_bytes(buf, self.custom_metadata.as_deref());
85 } else {
86 put_nullable_bytes(buf, self.custom_metadata.as_deref());
87 }
88 }
89 if version >= 0 {
90 put_i8(buf, self.remote_log_segment_state);
91 }
92 if flex {
93 let mut tagged = WriteTaggedFields::new();
94 if !(crate::codegen_helpers::is_default(&self.txn_index_empty)) {
95 let payload = encode_to_bytes(1, |b| {
96 put_bool(b, self.txn_index_empty);
97 Ok(())
98 });
99 tagged.add(0, payload);
100 }
101 tagged.write(buf, &self.unknown_tagged_fields);
102 }
103 Ok(())
104 }
105 fn encoded_len(&self, version: i16) -> usize {
106 let flex = is_flexible(version);
107 let mut n: usize = 0;
108 if version >= 0 {
109 n += self.remote_log_segment_id.encoded_len(version);
110 }
111 if version >= 0 {
112 n += 8;
113 }
114 if version >= 0 {
115 n += 8;
116 }
117 if version >= 0 {
118 n += 4;
119 }
120 if version >= 0 {
121 n += 8;
122 }
123 if version >= 0 {
124 n += 8;
125 }
126 if version >= 0 {
127 n += {
128 let prefix = crate::primitives::array::array_len_prefix_len(
129 (self.segment_leader_epochs).len(),
130 flex,
131 );
132 let body: usize = (self.segment_leader_epochs)
133 .iter()
134 .map(|it| it.encoded_len(version))
135 .sum();
136 prefix + body
137 };
138 }
139 if version >= 0 {
140 n += 4;
141 }
142 if version >= 0 {
143 n += if flex {
144 compact_nullable_bytes_len(self.custom_metadata.as_deref())
145 } else {
146 nullable_bytes_len(self.custom_metadata.as_deref())
147 };
148 }
149 if version >= 0 {
150 n += 1;
151 }
152 if flex {
153 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
154 if !(crate::codegen_helpers::is_default(&self.txn_index_empty)) {
155 known_pairs.push((0, 1));
156 }
157 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
158 }
159 n
160 }
161}
162impl Decode<'_> for RemoteLogSegmentMetadataRecord {
163 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
164 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
165 return Err(ProtocolError::SchemaMismatch(
166 "RemoteLogSegmentMetadataRecord version out of range",
167 ));
168 }
169 let flex = is_flexible(version);
170 let mut out = Self::default();
171 if version >= 0 {
172 out.remote_log_segment_id = RemoteLogSegmentIdEntry::decode(buf, version)?;
173 }
174 if version >= 0 {
175 out.start_offset = get_i64(buf)?;
176 }
177 if version >= 0 {
178 out.end_offset = get_i64(buf)?;
179 }
180 if version >= 0 {
181 out.broker_id = get_i32(buf)?;
182 }
183 if version >= 0 {
184 out.max_timestamp_ms = get_i64(buf)?;
185 }
186 if version >= 0 {
187 out.event_timestamp_ms = get_i64(buf)?;
188 }
189 if version >= 0 {
190 out.segment_leader_epochs = {
191 let n = crate::primitives::array::get_array_len(buf, flex)?;
192 let mut v = Vec::with_capacity(n);
193 for _ in 0..n {
194 v.push(SegmentLeaderEpochEntry::decode(buf, version)?);
195 }
196 v
197 };
198 }
199 if version >= 0 {
200 out.segment_size_in_bytes = get_i32(buf)?;
201 }
202 if version >= 0 {
203 out.custom_metadata = if flex {
204 get_compact_nullable_bytes_owned(buf)?
205 } else {
206 get_nullable_bytes_owned(buf)?
207 };
208 }
209 if version >= 0 {
210 out.remote_log_segment_state = get_i8(buf)?;
211 }
212 if flex {
213 let mut tag_txn_index_empty = None;
214 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
215 0 => {
216 tag_txn_index_empty = Some({
217 let b: &mut &[u8] = payload;
218 get_bool(b)?
219 });
220 Ok(true)
221 }
222 _ => Ok(false),
223 })?;
224 if let Some(v) = tag_txn_index_empty {
225 out.txn_index_empty = v;
226 }
227 }
228 Ok(out)
229 }
230}
231#[cfg(test)]
232impl RemoteLogSegmentMetadataRecord {
233 #[must_use]
234 pub fn populated(version: i16) -> Self {
235 let mut m = Self::default();
236 if version >= 0 {
237 m.remote_log_segment_id = RemoteLogSegmentIdEntry::populated(version);
238 }
239 if version >= 0 {
240 m.start_offset = 1i64;
241 }
242 if version >= 0 {
243 m.end_offset = 1i64;
244 }
245 if version >= 0 {
246 m.broker_id = 1i32;
247 }
248 if version >= 0 {
249 m.max_timestamp_ms = 1i64;
250 }
251 if version >= 0 {
252 m.event_timestamp_ms = 1i64;
253 }
254 if version >= 0 {
255 m.segment_leader_epochs = vec![SegmentLeaderEpochEntry::populated(version)];
256 }
257 if version >= 0 {
258 m.segment_size_in_bytes = 1i32;
259 }
260 if version >= 0 {
261 m.custom_metadata = Some(::bytes::Bytes::from_static(b"x"));
262 }
263 if version >= 0 {
264 m.remote_log_segment_state = 1i8;
265 }
266 if version >= 0 {
267 m.txn_index_empty = true;
268 }
269 m
270 }
271}
272#[derive(Debug, Clone, PartialEq, Eq, Default)]
273pub struct RemoteLogSegmentIdEntry {
274 pub topic_id_partition: TopicIdPartitionEntry,
275 pub id: crate::primitives::uuid::Uuid,
276 pub unknown_tagged_fields: UnknownTaggedFields,
277}
278impl Encode for RemoteLogSegmentIdEntry {
279 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
280 let flex = version >= 0;
281 if version >= 0 {
282 self.topic_id_partition.encode(buf, version)?;
283 }
284 if version >= 0 {
285 crate::primitives::uuid::put_uuid(buf, self.id);
286 }
287 if flex {
288 let tagged = WriteTaggedFields::new();
289 tagged.write(buf, &self.unknown_tagged_fields);
290 }
291 Ok(())
292 }
293 fn encoded_len(&self, version: i16) -> usize {
294 let flex = version >= 0;
295 let mut n: usize = 0;
296 if version >= 0 {
297 n += self.topic_id_partition.encoded_len(version);
298 }
299 if version >= 0 {
300 n += 16;
301 }
302 if flex {
303 let known_pairs: Vec<(u32, usize)> = Vec::new();
304 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
305 }
306 n
307 }
308}
309impl Decode<'_> for RemoteLogSegmentIdEntry {
310 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
311 let flex = version >= 0;
312 let mut out = Self::default();
313 if version >= 0 {
314 out.topic_id_partition = TopicIdPartitionEntry::decode(buf, version)?;
315 }
316 if version >= 0 {
317 out.id = crate::primitives::uuid::get_uuid(buf)?;
318 }
319 if flex {
320 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
321 }
322 Ok(out)
323 }
324}
325#[cfg(test)]
326impl RemoteLogSegmentIdEntry {
327 #[must_use]
328 pub fn populated(version: i16) -> Self {
329 let mut m = Self::default();
330 if version >= 0 {
331 m.topic_id_partition = TopicIdPartitionEntry::populated(version);
332 }
333 if version >= 0 {
334 m.id = crate::primitives::uuid::Uuid([1u8; 16]);
335 }
336 m
337 }
338}
339#[derive(Debug, Clone, PartialEq, Eq, Default)]
340pub struct TopicIdPartitionEntry {
341 pub name: String,
342 pub id: crate::primitives::uuid::Uuid,
343 pub partition: i32,
344 pub unknown_tagged_fields: UnknownTaggedFields,
345}
346impl Encode for TopicIdPartitionEntry {
347 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
348 let flex = version >= 0;
349 if version >= 0 {
350 if flex {
351 put_compact_string(buf, &self.name);
352 } else {
353 put_string(buf, &self.name);
354 }
355 }
356 if version >= 0 {
357 crate::primitives::uuid::put_uuid(buf, self.id);
358 }
359 if version >= 0 {
360 put_i32(buf, self.partition);
361 }
362 if flex {
363 let tagged = WriteTaggedFields::new();
364 tagged.write(buf, &self.unknown_tagged_fields);
365 }
366 Ok(())
367 }
368 fn encoded_len(&self, version: i16) -> usize {
369 let flex = version >= 0;
370 let mut n: usize = 0;
371 if version >= 0 {
372 n += if flex {
373 compact_string_len(&self.name)
374 } else {
375 string_len(&self.name)
376 };
377 }
378 if version >= 0 {
379 n += 16;
380 }
381 if version >= 0 {
382 n += 4;
383 }
384 if flex {
385 let known_pairs: Vec<(u32, usize)> = Vec::new();
386 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
387 }
388 n
389 }
390}
391impl Decode<'_> for TopicIdPartitionEntry {
392 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
393 let flex = version >= 0;
394 let mut out = Self::default();
395 if version >= 0 {
396 out.name = if flex {
397 get_compact_string_owned(buf)?
398 } else {
399 get_string_owned(buf)?
400 };
401 }
402 if version >= 0 {
403 out.id = crate::primitives::uuid::get_uuid(buf)?;
404 }
405 if version >= 0 {
406 out.partition = get_i32(buf)?;
407 }
408 if flex {
409 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
410 }
411 Ok(out)
412 }
413}
414#[cfg(test)]
415impl TopicIdPartitionEntry {
416 #[must_use]
417 pub fn populated(version: i16) -> Self {
418 let mut m = Self::default();
419 if version >= 0 {
420 m.name = "x".to_string();
421 }
422 if version >= 0 {
423 m.id = crate::primitives::uuid::Uuid([1u8; 16]);
424 }
425 if version >= 0 {
426 m.partition = 1i32;
427 }
428 m
429 }
430}
431#[derive(Debug, Clone, PartialEq, Eq, Default)]
432pub struct SegmentLeaderEpochEntry {
433 pub leader_epoch: i32,
434 pub offset: i64,
435 pub unknown_tagged_fields: UnknownTaggedFields,
436}
437impl Encode for SegmentLeaderEpochEntry {
438 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
439 let flex = version >= 0;
440 if version >= 0 {
441 put_i32(buf, self.leader_epoch);
442 }
443 if version >= 0 {
444 put_i64(buf, self.offset);
445 }
446 if flex {
447 let tagged = WriteTaggedFields::new();
448 tagged.write(buf, &self.unknown_tagged_fields);
449 }
450 Ok(())
451 }
452 fn encoded_len(&self, version: i16) -> usize {
453 let flex = version >= 0;
454 let mut n: usize = 0;
455 if version >= 0 {
456 n += 4;
457 }
458 if version >= 0 {
459 n += 8;
460 }
461 if flex {
462 let known_pairs: Vec<(u32, usize)> = Vec::new();
463 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
464 }
465 n
466 }
467}
468impl Decode<'_> for SegmentLeaderEpochEntry {
469 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
470 let flex = version >= 0;
471 let mut out = Self::default();
472 if version >= 0 {
473 out.leader_epoch = get_i32(buf)?;
474 }
475 if version >= 0 {
476 out.offset = get_i64(buf)?;
477 }
478 if flex {
479 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
480 }
481 Ok(out)
482 }
483}
484#[cfg(test)]
485impl SegmentLeaderEpochEntry {
486 #[must_use]
487 pub fn populated(version: i16) -> Self {
488 let mut m = Self::default();
489 if version >= 0 {
490 m.leader_epoch = 1i32;
491 }
492 if version >= 0 {
493 m.offset = 1i64;
494 }
495 m
496 }
497}
498#[must_use]
501#[allow(unused_comparisons)]
502pub fn default_json(version: i16) -> ::serde_json::Value {
503 let mut obj = ::serde_json::Map::new();
504 obj.insert("remoteLogSegmentId".to_string(), {
505 let mut m = ::serde_json::Map::new();
506 m.insert("topicIdPartition".to_string(), {
507 let mut m = ::serde_json::Map::new();
508 m.insert(
509 "name".to_string(),
510 ::serde_json::Value::String(String::new()),
511 );
512 m.insert(
513 "id".to_string(),
514 ::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()),
515 );
516 m.insert("partition".to_string(), ::serde_json::json!(0));
517 ::serde_json::Value::Object(m)
518 });
519 m.insert(
520 "id".to_string(),
521 ::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()),
522 );
523 ::serde_json::Value::Object(m)
524 });
525 obj.insert("startOffset".to_string(), ::serde_json::json!(0));
526 obj.insert("endOffset".to_string(), ::serde_json::json!(0));
527 obj.insert("brokerId".to_string(), ::serde_json::json!(0));
528 obj.insert("maxTimestampMs".to_string(), ::serde_json::json!(0));
529 obj.insert("eventTimestampMs".to_string(), ::serde_json::json!(0));
530 obj.insert(
531 "segmentLeaderEpochs".to_string(),
532 ::serde_json::Value::Array(vec![]),
533 );
534 obj.insert("segmentSizeInBytes".to_string(), ::serde_json::json!(0));
535 obj.insert("customMetadata".to_string(), ::serde_json::Value::Null);
536 obj.insert("remoteLogSegmentState".to_string(), ::serde_json::json!(0));
537 obj.insert(
538 "txnIndexEmpty".to_string(),
539 ::serde_json::Value::Bool(false),
540 );
541 ::serde_json::Value::Object(obj)
542}