reddb-io-server 1.2.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
Documentation
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use super::*;

impl RedDBRuntime {
    pub fn create_export(&self, name: impl Into<String>) -> RedDBResult<ExportDescriptor> {
        self.inner
            .db
            .create_named_export(name)
            .map_err(|err| RedDBError::Internal(err.to_string()))
    }

    pub fn graph_projections(&self) -> RedDBResult<Vec<PhysicalGraphProjection>> {
        Ok(self.inner.db.declared_graph_projections())
    }

    pub fn operational_graph_projections(&self) -> Vec<PhysicalGraphProjection> {
        self.inner.db.operational_graph_projections()
    }

    pub fn graph_projection_named(&self, name: &str) -> RedDBResult<RuntimeGraphProjection> {
        let status = self
            .graph_projection_statuses()
            .into_iter()
            .find(|status| status.name == name)
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))?;
        if !status.declared {
            return Err(RedDBError::Catalog(format!(
                "graph projection '{name}' is not declared"
            )));
        }
        if !status.operational {
            return Err(RedDBError::Catalog(format!(
                "graph projection '{name}' is declared but not operationally materialized"
            )));
        }
        if status.lifecycle_state == "stale" {
            return Err(RedDBError::Catalog(format!(
                "graph projection '{name}' is stale and must be rematerialized before use"
            )));
        }
        let projection = self
            .operational_graph_projections()
            .into_iter()
            .find(|projection| projection.name == name)
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))?;
        Ok(RuntimeGraphProjection {
            node_labels: (!projection.node_labels.is_empty()).then_some(projection.node_labels),
            node_types: (!projection.node_types.is_empty()).then_some(projection.node_types),
            edge_labels: (!projection.edge_labels.is_empty()).then_some(projection.edge_labels),
        })
    }

    pub fn save_graph_projection(
        &self,
        name: impl Into<String>,
        projection: RuntimeGraphProjection,
        source: Option<String>,
    ) -> RedDBResult<PhysicalGraphProjection> {
        self.inner
            .db
            .save_graph_projection(
                name,
                projection.node_labels.unwrap_or_default(),
                projection.node_types.unwrap_or_default(),
                projection.edge_labels.unwrap_or_default(),
                source.unwrap_or_else(|| "runtime".to_string()),
            )
            .map_err(|err| RedDBError::Internal(err.to_string()))
    }

    pub fn materialize_graph_projection(&self, name: &str) -> RedDBResult<PhysicalGraphProjection> {
        self.inner
            .db
            .materialize_graph_projection(name)
            .map_err(|err| RedDBError::Internal(err.to_string()))?
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))
    }

    pub fn mark_graph_projection_materializing(
        &self,
        name: &str,
    ) -> RedDBResult<PhysicalGraphProjection> {
        self.inner
            .db
            .mark_graph_projection_materializing(name)
            .map_err(|err| RedDBError::Internal(err.to_string()))?
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))
    }

    pub fn fail_graph_projection(&self, name: &str) -> RedDBResult<PhysicalGraphProjection> {
        self.inner
            .db
            .fail_graph_projection(name)
            .map_err(|err| RedDBError::Internal(err.to_string()))?
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))
    }

    pub fn mark_graph_projection_stale(&self, name: &str) -> RedDBResult<PhysicalGraphProjection> {
        self.inner
            .db
            .mark_graph_projection_stale(name)
            .map_err(|err| RedDBError::Internal(err.to_string()))?
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))
    }

    pub fn analytics_jobs(&self) -> RedDBResult<Vec<PhysicalAnalyticsJob>> {
        Ok(self.inner.db.declared_analytics_jobs())
    }

    pub fn operational_analytics_jobs(&self) -> Vec<PhysicalAnalyticsJob> {
        self.inner.db.operational_analytics_jobs()
    }

    pub fn save_analytics_job(
        &self,
        kind: impl Into<String>,
        projection_name: Option<String>,
        metadata: std::collections::BTreeMap<String, String>,
    ) -> RedDBResult<PhysicalAnalyticsJob> {
        self.inner
            .db
            .save_analytics_job(kind, projection_name, metadata)
            .map_err(|err| RedDBError::Internal(err.to_string()))
    }

    pub fn start_analytics_job(
        &self,
        kind: impl Into<String>,
        projection_name: Option<String>,
        metadata: std::collections::BTreeMap<String, String>,
    ) -> RedDBResult<PhysicalAnalyticsJob> {
        if let Some(projection_name) = projection_name.as_deref() {
            let status = self
                .graph_projection_statuses()
                .into_iter()
                .find(|status| status.name == projection_name)
                .ok_or_else(|| RedDBError::NotFound(projection_name.to_string()))?;
            if !status.declared {
                return Err(RedDBError::Catalog(format!(
                    "graph projection '{projection_name}' is not declared"
                )));
            }
            if !status.operational {
                return Err(RedDBError::Catalog(format!(
                    "graph projection '{projection_name}' is declared but not operationally materialized"
                )));
            }
            if status.lifecycle_state == "stale" {
                return Err(RedDBError::Catalog(format!(
                    "graph projection '{projection_name}' is stale and must be rematerialized before analytics jobs can start against it"
                )));
            }
        }
        self.inner
            .db
            .start_analytics_job(kind, projection_name, metadata)
            .map_err(|err| RedDBError::Internal(err.to_string()))
    }

    pub fn queue_analytics_job(
        &self,
        kind: impl Into<String>,
        projection_name: Option<String>,
        metadata: std::collections::BTreeMap<String, String>,
    ) -> RedDBResult<PhysicalAnalyticsJob> {
        if let Some(projection_name) = projection_name.as_deref() {
            let status = self
                .graph_projection_statuses()
                .into_iter()
                .find(|status| status.name == projection_name)
                .ok_or_else(|| RedDBError::NotFound(projection_name.to_string()))?;
            if !status.declared {
                return Err(RedDBError::Catalog(format!(
                    "graph projection '{projection_name}' is not declared"
                )));
            }
            if !status.operational {
                return Err(RedDBError::Catalog(format!(
                    "graph projection '{projection_name}' is declared but not operationally materialized"
                )));
            }
            if status.lifecycle_state == "stale" {
                return Err(RedDBError::Catalog(format!(
                    "graph projection '{projection_name}' is stale and must be rematerialized before analytics jobs can be queued against it"
                )));
            }
        }
        self.inner
            .db
            .queue_analytics_job(kind, projection_name, metadata)
            .map_err(|err| RedDBError::Internal(err.to_string()))
    }

    pub fn fail_analytics_job(
        &self,
        kind: impl Into<String>,
        projection_name: Option<String>,
        metadata: std::collections::BTreeMap<String, String>,
    ) -> RedDBResult<PhysicalAnalyticsJob> {
        self.inner
            .db
            .fail_analytics_job(kind, projection_name, metadata)
            .map_err(|err| RedDBError::Internal(err.to_string()))
    }

    pub fn mark_analytics_job_stale(
        &self,
        kind: impl Into<String>,
        projection_name: Option<String>,
        metadata: std::collections::BTreeMap<String, String>,
    ) -> RedDBResult<PhysicalAnalyticsJob> {
        self.inner
            .db
            .mark_analytics_job_stale(kind, projection_name, metadata)
            .map_err(|err| RedDBError::Internal(err.to_string()))
    }

    pub fn complete_analytics_job(
        &self,
        kind: impl Into<String>,
        projection_name: Option<String>,
        metadata: std::collections::BTreeMap<String, String>,
    ) -> RedDBResult<PhysicalAnalyticsJob> {
        self.inner
            .db
            .record_analytics_job(kind, projection_name, metadata)
            .map_err(|err| RedDBError::Internal(err.to_string()))
    }

    pub fn record_analytics_job(
        &self,
        kind: impl Into<String>,
        projection_name: Option<String>,
        metadata: std::collections::BTreeMap<String, String>,
    ) -> RedDBResult<PhysicalAnalyticsJob> {
        if let Some(projection_name) = projection_name.as_deref() {
            let status = self
                .graph_projection_statuses()
                .into_iter()
                .find(|status| status.name == projection_name)
                .ok_or_else(|| RedDBError::NotFound(projection_name.to_string()))?;
            if !status.declared {
                return Err(RedDBError::Catalog(format!(
                    "graph projection '{projection_name}' is not declared"
                )));
            }
            if !status.operational {
                return Err(RedDBError::Catalog(format!(
                    "graph projection '{projection_name}' is declared but not operationally materialized"
                )));
            }
            if status.lifecycle_state == "stale" {
                return Err(RedDBError::Catalog(format!(
                    "graph projection '{projection_name}' is stale and must be rematerialized before analytics jobs can complete against it"
                )));
            }
        }
        self.inner
            .db
            .record_analytics_job(kind, projection_name, metadata)
            .map_err(|err| RedDBError::Internal(err.to_string()))
    }

    pub fn resolve_graph_projection(
        &self,
        projection_name: Option<&str>,
        inline: Option<RuntimeGraphProjection>,
    ) -> RedDBResult<Option<RuntimeGraphProjection>> {
        let named = match projection_name {
            Some(name) => Some(self.graph_projection_named(name)?),
            None => None,
        };
        Ok(merge_runtime_projection(named, inline))
    }

    pub fn apply_retention_policy(&self) -> RedDBResult<()> {
        self.inner
            .db
            .enforce_retention_policy()
            .map_err(|err| RedDBError::Internal(err.to_string()))?;
        self.enforce_metrics_raw_retention()?;
        self.invalidate_result_cache();
        Ok(())
    }

    fn enforce_metrics_raw_retention(&self) -> RedDBResult<()> {
        let now_ns = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos()
            .min(u128::from(u64::MAX)) as u64;
        let store = self.inner.db.store();

        for contract in self
            .inner
            .db
            .collection_contracts()
            .into_iter()
            .filter(|contract| contract.declared_model == crate::catalog::CollectionModel::Metrics)
        {
            let Some(raw_retention_ms) = contract.metrics_raw_retention_ms else {
                continue;
            };
            let cutoff_ns = now_ns.saturating_sub(raw_retention_ms.saturating_mul(1_000_000));
            let Some(manager) = store.get_collection(&contract.name) else {
                continue;
            };
            let expired = manager.query_all(|entity| match &entity.data {
                crate::storage::EntityData::TimeSeries(point) => point.timestamp_ns < cutoff_ns,
                _ => false,
            });
            for entity in expired {
                store
                    .delete(&contract.name, entity.id)
                    .map_err(|err| RedDBError::Internal(err.to_string()))?;
            }
        }

        Ok(())
    }

    pub fn indexes(&self) -> Vec<crate::PhysicalIndexState> {
        self.inner.db.operational_indexes()
    }

    pub fn declared_indexes(&self) -> Vec<crate::PhysicalIndexState> {
        self.inner.db.declared_indexes()
    }

    pub fn declared_indexes_for_collection(
        &self,
        collection: &str,
    ) -> Vec<crate::PhysicalIndexState> {
        self.inner
            .db
            .declared_indexes()
            .into_iter()
            .filter(|index| index.collection.as_deref() == Some(collection))
            .collect()
    }

    pub fn index_statuses(&self) -> Vec<crate::catalog::CatalogIndexStatus> {
        self.inner.db.index_statuses()
    }

    pub fn graph_projection_statuses(&self) -> Vec<crate::catalog::CatalogGraphProjectionStatus> {
        self.inner
            .db
            .catalog_model_snapshot()
            .graph_projection_statuses
    }

    pub fn analytics_job_statuses(&self) -> Vec<crate::catalog::CatalogAnalyticsJobStatus> {
        self.inner
            .db
            .catalog_model_snapshot()
            .analytics_job_statuses
    }

    pub fn indexes_for_collection(&self, collection: &str) -> Vec<crate::PhysicalIndexState> {
        self.inner
            .db
            .operational_indexes()
            .into_iter()
            .filter(|index| index.collection.as_deref() == Some(collection))
            .collect()
    }

    pub fn set_index_enabled(
        &self,
        name: &str,
        enabled: bool,
    ) -> RedDBResult<crate::PhysicalIndexState> {
        self.check_write(crate::runtime::write_gate::WriteKind::Maintenance)?;
        self.inner
            .db
            .set_index_enabled(name, enabled)
            .map_err(|err| RedDBError::Internal(err.to_string()))?
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))
    }

    pub fn mark_index_building(&self, name: &str) -> RedDBResult<crate::PhysicalIndexState> {
        self.inner
            .db
            .mark_index_building(name)
            .map_err(|err| RedDBError::Internal(err.to_string()))?
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))
    }

    pub fn fail_index(&self, name: &str) -> RedDBResult<crate::PhysicalIndexState> {
        self.inner
            .db
            .fail_index(name)
            .map_err(|err| RedDBError::Internal(err.to_string()))?
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))
    }

    pub fn mark_index_stale(&self, name: &str) -> RedDBResult<crate::PhysicalIndexState> {
        self.inner
            .db
            .mark_index_stale(name)
            .map_err(|err| RedDBError::Internal(err.to_string()))?
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))
    }

    pub fn mark_index_ready(&self, name: &str) -> RedDBResult<crate::PhysicalIndexState> {
        self.inner
            .db
            .mark_index_ready(name)
            .map_err(|err| RedDBError::Internal(err.to_string()))?
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))
    }

    pub fn warmup_index_with_lifecycle(
        &self,
        name: &str,
    ) -> RedDBResult<crate::PhysicalIndexState> {
        self.mark_index_building(name)?;
        match self.warmup_index(name) {
            Ok(index) => Ok(index),
            Err(err) => {
                let _ = self.fail_index(name);
                Err(err)
            }
        }
    }

    pub fn warmup_index(&self, name: &str) -> RedDBResult<crate::PhysicalIndexState> {
        self.inner
            .db
            .warmup_index(name)
            .map_err(|err| RedDBError::Internal(err.to_string()))?
            .ok_or_else(|| RedDBError::NotFound(name.to_string()))
    }

    pub fn rebuild_indexes(
        &self,
        collection: Option<&str>,
    ) -> RedDBResult<Vec<crate::PhysicalIndexState>> {
        self.inner
            .db
            .rebuild_index_registry(collection)
            .map_err(|err| RedDBError::Internal(err.to_string()))
    }

    pub fn rebuild_indexes_with_lifecycle(
        &self,
        collection: Option<&str>,
    ) -> RedDBResult<Vec<crate::PhysicalIndexState>> {
        let target_names: Vec<String> = match collection {
            Some(collection) => self
                .declared_indexes_for_collection(collection)
                .into_iter()
                .map(|index| index.name)
                .collect(),
            None => self
                .declared_indexes()
                .into_iter()
                .map(|index| index.name)
                .collect(),
        };

        let mut marked_building = Vec::new();
        for name in target_names {
            if self.mark_index_building(&name).is_ok() {
                marked_building.push(name);
            }
        }

        match self.rebuild_indexes(collection) {
            Ok(indexes) => Ok(indexes),
            Err(err) => {
                for name in marked_building {
                    let _ = self.fail_index(&name);
                }
                Err(err)
            }
        }
    }
}