Skip to main content

feldera_ir/
lir.rs

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