feldera_ir/
lir.rs

1use std::io::Write;
2
3use serde::{Deserialize, Serialize};
4use zip::{write::FileOptions, ZipWriter};
5
6use crate::MirNodeId;
7
8#[derive(Clone, Debug, Default, Serialize, Deserialize)]
9#[repr(transparent)]
10pub struct LirNodeId(String);
11
12impl LirNodeId {
13    pub fn new(id: &str) -> Self {
14        Self(id.to_string())
15    }
16}
17
18#[derive(Clone, Debug, Default, Serialize, Deserialize)]
19#[repr(transparent)]
20pub struct LirStreamId(usize);
21
22impl LirStreamId {
23    pub fn new(id: usize) -> Self {
24        Self(id)
25    }
26}
27
28#[derive(Clone, Debug, Default, Serialize, Deserialize)]
29pub struct LirNode {
30    pub id: LirNodeId,
31
32    /// Node type, e.g., map, join, etc.
33    pub operation: String,
34
35    /// The list of Mir node ids that this LIR node implementa completely or partially.
36    #[serde(default)]
37    pub implements: Vec<MirNodeId>,
38}
39
40#[derive(Clone, Debug, Default, Serialize, Deserialize)]
41pub struct LirEdge {
42    /// Stream id if this edge is a stream edge. None if this is a dependency edge.
43    /// Dependency edges connect operators that implement a single logical function,
44    /// e.g., exchange sender and receiver or the input and output halves of Z1.
45    pub stream_id: Option<LirStreamId>,
46    pub from: LirNodeId,
47    pub to: LirNodeId,
48}
49
50#[derive(Clone, Debug, Default, Serialize, Deserialize)]
51pub struct LirCircuit {
52    pub nodes: Vec<LirNode>,
53    pub edges: Vec<LirEdge>,
54}
55
56impl LirCircuit {
57    pub fn as_json(&self) -> String {
58        serde_json::to_string(self).unwrap()
59    }
60
61    pub fn as_zip(&self) -> Vec<u8> {
62        let json = self.as_json();
63        let json = json.as_bytes();
64
65        let mut zip = ZipWriter::new(std::io::Cursor::new(Vec::with_capacity(65536)));
66        zip.start_file("ir.json", FileOptions::default()).unwrap();
67        zip.write_all(json).unwrap();
68        zip.finish().unwrap().into_inner()
69    }
70}