Skip to main content

crisol_core/data/
lineage.rs

1//! Serializable data lineage: where a dataset came from and what changed it.
2
3use std::collections::BTreeMap;
4
5/// One transformation applied to a dataset.
6///
7/// # Example
8///
9/// ```
10/// use crisol_core::data::LineageStep;
11///
12/// let step = LineageStep::new("input.csv", "csv");
13/// assert_eq!(step.operation, "csv");
14/// ```
15#[non_exhaustive]
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub struct LineageStep {
19    /// Source name or path.
20    pub source: String,
21    /// Operation, such as `csv`, `filter`, or `join`.
22    pub operation: String,
23    /// Operation parameters.
24    pub parameters: BTreeMap<String, String>,
25}
26
27impl LineageStep {
28    /// A step with no parameters.
29    pub fn new(source: impl Into<String>, operation: impl Into<String>) -> Self {
30        Self {
31            source: source.into(),
32            operation: operation.into(),
33            parameters: BTreeMap::new(),
34        }
35    }
36
37    /// Add a parameter.
38    pub fn parameter(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
39        self.parameters.insert(key.into(), value.into());
40        self
41    }
42}
43
44/// The ordered history of a dataset.
45///
46/// # Example
47///
48/// ```
49/// use crisol_core::data::Lineage;
50///
51/// let lineage = Lineage::new();
52/// assert!(lineage.steps.is_empty());
53/// ```
54#[non_exhaustive]
55#[derive(Clone, Debug, Default, PartialEq, Eq)]
56#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
57pub struct Lineage {
58    /// Steps in application order.
59    pub steps: Vec<LineageStep>,
60}
61
62impl Lineage {
63    /// An empty lineage.
64    pub fn new() -> Self {
65        Self::default()
66    }
67
68    /// Append a step.
69    pub fn record(&mut self, step: LineageStep) {
70        self.steps.push(step);
71    }
72
73    /// The distinct sources named by the steps.
74    pub fn sources(&self) -> Vec<&str> {
75        let mut sources: Vec<&str> = self.steps.iter().map(|step| step.source.as_str()).collect();
76        sources.sort_unstable();
77        sources.dedup();
78        sources
79    }
80
81    /// The distinct operations named by the steps.
82    pub fn operations(&self) -> Vec<&str> {
83        let mut operations: Vec<&str> = self.steps.iter().map(|step| step.operation.as_str()).collect();
84        operations.sort_unstable();
85        operations.dedup();
86        operations
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    #[test]
95    fn lineage_records_sources_and_operations() {
96        let mut lineage = Lineage::new();
97        lineage.record(LineageStep::new("input.csv", "csv").parameter("delimiter", ","));
98        lineage.record(LineageStep::new("input.csv", "filter").parameter("where", "v > 0"));
99        assert_eq!(lineage.sources(), ["input.csv"]);
100        assert_eq!(lineage.operations(), ["csv", "filter"]);
101        assert_eq!(lineage.steps[1].parameters["where"], "v > 0");
102    }
103}