1use std::collections::BTreeMap;
2use std::sync::Arc;
3
4use shard_stream_core::{LogicalOffset, ShardId, TopicPartition};
5
6use crate::{
7 CompressionCohortId, CompressionPlacementId, DictionaryId, TelemetryError, TelemetryResult,
8};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum CompressionCodec {
13 Zstd,
15}
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
19pub struct BlockId(u64);
20
21impl BlockId {
22 #[must_use]
24 pub const fn new(value: u64) -> Self {
25 Self(value)
26 }
27
28 #[must_use]
30 pub const fn get(self) -> u64 {
31 self.0
32 }
33}
34
35#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct BlockDescriptor {
38 pub block_id: BlockId,
40 pub stream_shard_id: ShardId,
42 pub topic_partition: TopicPartition,
44 pub source_compression_cohort: CompressionCohortId,
46 pub placement_id: CompressionPlacementId,
48 pub dictionary_id: Option<DictionaryId>,
50 pub compression_codec: CompressionCodec,
52 pub compression_level: i32,
54 pub first_offset: LogicalOffset,
56 pub last_offset: LogicalOffset,
58 pub record_count: u32,
60 pub source_bytes: u64,
62 pub structural_bytes: u64,
64 pub stored_bytes: u64,
66 pub min_timestamp_unix_nanos: u64,
68 pub max_timestamp_unix_nanos: u64,
70 pub compression_temperature: u16,
72 pub compression_shape_hash: u64,
74 pub compression_temperature_variance_q8: u16,
76 pub max_compression_temperature_deviation: u8,
78 pub object_key: Option<Arc<str>>,
80 pub object_offset: Option<u64>,
82}
83
84#[derive(Debug, Default)]
86pub struct BlockCatalog {
87 next_block_id: u64,
88 blocks: BTreeMap<BlockId, BlockDescriptor>,
89 staged_payloads: BTreeMap<BlockId, Arc<[u8]>>,
90}
91
92impl BlockCatalog {
93 #[must_use]
95 pub fn len(&self) -> usize {
96 self.blocks.len()
97 }
98
99 #[must_use]
101 pub fn is_empty(&self) -> bool {
102 self.blocks.is_empty()
103 }
104
105 #[must_use]
107 pub fn get(&self, block_id: BlockId) -> Option<&BlockDescriptor> {
108 self.blocks.get(&block_id)
109 }
110
111 pub fn iter(&self) -> impl Iterator<Item = &BlockDescriptor> {
113 self.blocks.values()
114 }
115
116 #[must_use]
119 pub fn staged_payload(&self, block_id: BlockId) -> Option<Arc<[u8]>> {
120 self.staged_payloads.get(&block_id).cloned()
121 }
122
123 pub(crate) fn seal(
124 &mut self,
125 mut block: BlockDescriptor,
126 payload: Arc<[u8]>,
127 ) -> BlockDescriptor {
128 let block_id = BlockId::new(self.next_block_id);
129 self.next_block_id = self.next_block_id.wrapping_add(1);
130 block.block_id = block_id;
131 self.blocks.insert(block_id, block.clone());
132 self.staged_payloads.insert(block_id, payload);
133 block
134 }
135
136 pub fn mark_offloaded(
138 &mut self,
139 block_id: BlockId,
140 object_key: impl Into<Arc<str>>,
141 ) -> TelemetryResult<()> {
142 self.mark_offloaded_range(block_id, object_key, 0)
143 }
144
145 pub fn mark_offloaded_range(
147 &mut self,
148 block_id: BlockId,
149 object_key: impl Into<Arc<str>>,
150 object_offset: u64,
151 ) -> TelemetryResult<()> {
152 let block = self
153 .blocks
154 .get_mut(&block_id)
155 .ok_or_else(|| TelemetryError::UnknownBlock(block_id.get()))?;
156 block.object_key = Some(object_key.into());
157 block.object_offset = Some(object_offset);
158 self.staged_payloads.remove(&block_id);
159 Ok(())
160 }
161}