1use std::collections::BTreeMap;
2
3pub const DEFAULT_PHYSICAL_FORMAT_VERSION: u32 = 2;
4pub const DEFAULT_SUPERBLOCK_COPIES: u8 = 4;
5pub const PHYSICAL_METADATA_PROTOCOL_VERSION: &str = "reddb-physical-v1";
6pub const PHYSICAL_SYSTEM_COLLECTION: &str = "__system__";
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub struct BlockReference {
10 pub index: u64,
11 pub checksum: u128,
12}
13
14#[derive(Debug, Clone, Default)]
15pub struct ManifestPointers {
16 pub oldest: BlockReference,
17 pub newest: BlockReference,
18}
19
20#[derive(Debug, Clone)]
21pub struct SuperblockHeader {
22 pub format_version: u32,
23 pub sequence: u64,
24 pub copies: u8,
25 pub manifest: ManifestPointers,
26 pub free_set: BlockReference,
27 pub collection_roots: BTreeMap<String, u64>,
28}
29
30impl Default for SuperblockHeader {
31 fn default() -> Self {
32 Self {
33 format_version: DEFAULT_PHYSICAL_FORMAT_VERSION,
34 sequence: 0,
35 copies: DEFAULT_SUPERBLOCK_COPIES,
36 manifest: ManifestPointers::default(),
37 free_set: BlockReference::default(),
38 collection_roots: BTreeMap::new(),
39 }
40 }
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub enum ManifestEventKind {
45 Insert,
46 Update,
47 Remove,
48 Checkpoint,
49}
50
51#[derive(Debug, Clone)]
52pub struct ManifestEvent {
53 pub collection: String,
54 pub object_key: String,
55 pub kind: ManifestEventKind,
56 pub block: BlockReference,
57 pub snapshot_min: u64,
58 pub snapshot_max: Option<u64>,
59}
60
61pub fn physical_manifest_block_reference(root: u64, sequence: u64) -> BlockReference {
62 BlockReference {
63 index: root,
64 checksum: ((root as u128) << 64) | sequence as u128,
65 }
66}
67
68pub fn physical_superblock_object_key(sequence: u64) -> String {
69 format!("superblock:{sequence}")
70}
71
72pub fn physical_superblock_checkpoint_event(sequence: u64) -> ManifestEvent {
73 ManifestEvent {
74 collection: PHYSICAL_SYSTEM_COLLECTION.to_string(),
75 object_key: physical_superblock_object_key(sequence),
76 kind: ManifestEventKind::Checkpoint,
77 block: physical_manifest_block_reference(sequence, sequence),
78 snapshot_min: sequence,
79 snapshot_max: None,
80 }
81}
82
83#[derive(Debug, Clone, Default)]
84pub struct SnapshotDescriptor {
85 pub snapshot_id: u64,
86 pub created_at_unix_ms: u128,
87 pub superblock_sequence: u64,
88 pub collection_count: usize,
89 pub total_entities: usize,
90}
91
92#[derive(Debug, Clone)]
93pub struct ExportDescriptor {
94 pub name: String,
95 pub created_at_unix_ms: u128,
96 pub snapshot_id: Option<u64>,
97 pub superblock_sequence: u64,
98 pub data_path: String,
99 pub metadata_path: String,
100 pub collection_count: usize,
101 pub total_entities: usize,
102}
103
104#[derive(Debug, Clone)]
105pub struct PhysicalGraphProjection {
106 pub name: String,
107 pub created_at_unix_ms: u128,
108 pub updated_at_unix_ms: u128,
109 pub state: String,
110 pub source: String,
111 pub node_labels: Vec<String>,
112 pub node_types: Vec<String>,
113 pub edge_labels: Vec<String>,
114 pub last_materialized_sequence: Option<u64>,
115}
116
117#[derive(Debug, Clone)]
118pub struct PhysicalAnalyticsJob {
119 pub id: String,
120 pub kind: String,
121 pub state: String,
122 pub projection: Option<String>,
123 pub created_at_unix_ms: u128,
124 pub updated_at_unix_ms: u128,
125 pub last_run_sequence: Option<u64>,
126 pub metadata: BTreeMap<String, String>,
127}
128
129#[derive(Debug, Clone)]
130pub struct PhysicalTreeDefinition {
131 pub collection: String,
132 pub name: String,
133 pub root_id: u64,
134 pub default_max_children: usize,
135 pub ordered_children: bool,
136 pub ownership: String,
137 pub auto_fix_mode: String,
138 pub created_at_unix_ms: u128,
139 pub updated_at_unix_ms: u128,
140}
141
142#[derive(Debug, Clone)]
143pub struct PersistedPhysicalIndexState {
144 pub name: String,
145 pub kind: String,
146 pub collection: Option<String>,
147 pub enabled: bool,
148 pub entries: usize,
149 pub estimated_memory_bytes: u64,
150 pub last_refresh_ms: Option<u128>,
151 pub backend: String,
152 pub artifact_kind: Option<String>,
153 pub artifact_root_page: Option<u32>,
154 pub artifact_checksum: Option<u64>,
155 pub build_state: String,
156}
157
158#[derive(Debug, Clone, Copy, PartialEq, Eq)]
159pub struct PhysicalPageLocation {
160 pub page_id: u32,
161 pub offset: u32,
162 pub length: u32,
163}
164
165#[derive(Debug, Clone)]
166pub struct PersistedPhysicalHypertableChunk {
167 pub start_ns: u64,
168 pub end_ns_exclusive: u64,
169 pub row_count: u64,
170 pub min_ts_ns: u64,
171 pub max_ts_ns: u64,
172 pub sealed: bool,
173 pub ttl_override_ns: Option<u64>,
174 pub columnar_page: Option<PhysicalPageLocation>,
175}
176
177#[derive(Debug, Clone)]
178pub struct PersistedPhysicalHypertable {
179 pub name: String,
180 pub time_column: String,
181 pub chunk_interval_ns: u64,
182 pub default_ttl_ns: Option<u64>,
183 pub chunks: Vec<PersistedPhysicalHypertableChunk>,
184}
185
186#[derive(Debug, Clone, Default)]
187pub struct PhysicalMetadataDocumentEnvelope {
188 pub protocol_version: String,
189 pub generated_at_unix_ms: u128,
190 pub last_loaded_from: Option<String>,
191 pub last_healed_at_unix_ms: Option<u128>,
192 pub manifest_json: String,
193 pub catalog_json: String,
194 pub manifest_events_json: Vec<String>,
195 pub indexes_json: Vec<String>,
196 pub graph_projections_json: Vec<String>,
197 pub analytics_jobs_json: Vec<String>,
198 pub tree_definitions_json: Vec<String>,
199 pub collection_ttl_defaults_ms: BTreeMap<String, u64>,
200 pub collection_contracts_json: Vec<String>,
201 pub hypertables_json: Vec<String>,
202 pub exports_json: Vec<String>,
203 pub superblock_json: String,
204 pub snapshots_json: Vec<String>,
205}
206
207#[derive(Debug, Clone, PartialEq, Eq)]
208pub struct PhysicalSchemaOptions {
209 pub mode: String,
210 pub data_path: Option<String>,
211 pub read_only: bool,
212 pub create_if_missing: bool,
213 pub verify_checksums: bool,
214 pub durability_mode: Option<String>,
215 pub group_commit_window_ms: Option<u64>,
216 pub group_commit_max_statements: Option<usize>,
217 pub group_commit_max_wal_bytes: Option<u64>,
218 pub auto_checkpoint_pages: u32,
219 pub cache_pages: usize,
220 pub snapshot_retention: Option<usize>,
221 pub export_retention: Option<usize>,
222 pub force_create: bool,
223 pub capabilities: Vec<String>,
224 pub metadata: BTreeMap<String, String>,
225}
226
227#[derive(Debug, Clone, PartialEq, Eq)]
228pub struct PhysicalSchemaManifest {
229 pub format_version: u32,
230 pub created_at_unix_ms: u128,
231 pub updated_at_unix_ms: u128,
232 pub collection_count: usize,
233 pub options: PhysicalSchemaOptions,
234}
235
236#[derive(Debug, Clone, Default, PartialEq, Eq)]
237pub struct PhysicalCatalogCollectionStats {
238 pub entities: usize,
239 pub cross_refs: usize,
240 pub segments: usize,
241}
242
243#[derive(Debug, Clone, Default, PartialEq, Eq)]
244pub struct PhysicalCatalogSnapshot {
245 pub name: String,
246 pub total_entities: usize,
247 pub total_collections: usize,
248 pub updated_at_unix_ms: u128,
249 pub stats_by_collection: BTreeMap<String, PhysicalCatalogCollectionStats>,
250}
251
252#[derive(Debug, Clone, Default, PartialEq, Eq)]
253pub struct PhysicalAnalyticalStorageConfig {
254 pub columnar: bool,
255 pub time_key: String,
256 pub order_by_key: Option<String>,
257}
258
259#[derive(Debug, Clone, Default, PartialEq, Eq)]
260pub struct PhysicalSubscriptionDescriptor {
261 pub name: String,
262 pub source: String,
263 pub target_queue: String,
264 pub ops_filter: Vec<String>,
265 pub where_filter: Option<String>,
266 pub redact_fields: Vec<String>,
267 pub enabled: bool,
268 pub all_tenants: bool,
269}
270
271#[derive(Debug, Clone, Default, PartialEq)]
272pub struct PhysicalAnalyticsViewDescriptor {
273 pub output: String,
274 pub algorithm: Option<String>,
275 pub resolution: Option<f64>,
276 pub max_iterations: Option<i64>,
277 pub tolerance: Option<f64>,
278}
279
280#[derive(Debug, Clone, Default, PartialEq, Eq)]
281pub struct PhysicalDeclaredColumnContract {
282 pub name: String,
283 pub data_type: String,
284 pub sql_type: Option<PhysicalSqlTypeName>,
285 pub not_null: bool,
286 pub default: Option<String>,
287 pub compress: Option<u8>,
288 pub unique: bool,
289 pub primary_key: bool,
290 pub enum_variants: Vec<String>,
291 pub array_element: Option<String>,
292 pub decimal_precision: Option<u8>,
293}
294
295#[derive(Debug, Clone, Default, PartialEq)]
296pub struct PhysicalCollectionContract {
297 pub name: String,
298 pub declared_model: String,
299 pub schema_mode: String,
300 pub origin: String,
301 pub version: u32,
302 pub created_at_unix_ms: u128,
303 pub updated_at_unix_ms: u128,
304 pub default_ttl_ms: Option<u64>,
305 pub vector_dimension: Option<usize>,
306 pub vector_metric: Option<String>,
307 pub context_index_fields: Vec<String>,
308 pub declared_columns: Vec<PhysicalDeclaredColumnContract>,
309 pub table_def_hex: Option<String>,
310 pub timestamps_enabled: bool,
311 pub context_index_enabled: bool,
312 pub metrics_raw_retention_ms: Option<u64>,
313 pub metrics_rollup_policies: Vec<String>,
314 pub metrics_tenant_identity: Option<String>,
315 pub metrics_namespace: Option<String>,
316 pub append_only: bool,
317 pub subscriptions: Vec<PhysicalSubscriptionDescriptor>,
318 pub analytics_config: Vec<PhysicalAnalyticsViewDescriptor>,
319 pub session_key: Option<String>,
320 pub session_gap_ms: Option<u64>,
321 pub retention_duration_ms: Option<u64>,
322 pub analytical_storage: Option<PhysicalAnalyticalStorageConfig>,
323}
324
325#[derive(Debug, Clone, Default, PartialEq, Eq)]
326pub struct PhysicalSqlTypeName {
327 pub name: String,
328 pub modifiers: Vec<PhysicalTypeModifier>,
329}
330
331#[derive(Debug, Clone, PartialEq, Eq)]
332pub enum PhysicalTypeModifier {
333 Number(u32),
334 Ident(String),
335 StringLiteral(String),
336 Type(Box<PhysicalSqlTypeName>),
337}