use serde::{Deserialize, Serialize};
use crate::concrete::ComponentPackage;
use crate::node::NodeConfig;
pub mod transient;
pub use transient::TransientSnapshot;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NodeSnapshot {
pub incarnation: u64,
pub config: NodeConfigSnapshot,
pub graphs: Vec<NamedGraphSnapshot>,
pub components: Vec<NamedComponentSnapshot>,
pub transient: TransientSnapshot,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct NodeConfigSnapshot {
#[serde(default)]
pub peer_id: Vec<u8>,
pub cycle_op_budget: Option<usize>,
#[serde(default)]
pub max_pending_async: Option<usize>,
#[serde(default)]
pub max_outbound_queue: Option<usize>,
pub bus_capacity: usize,
}
impl From<&NodeConfig> for NodeConfigSnapshot {
fn from(c: &NodeConfig) -> Self {
Self {
peer_id: c.peer_id.to_bytes(),
cycle_op_budget: c.cycle_op_budget,
max_pending_async: c.max_pending_async,
max_outbound_queue: c.max_outbound_queue,
bus_capacity: c.bus_capacity,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NamedGraphSnapshot {
pub name: String,
pub function_proto_bytes: Vec<u8>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NamedComponentSnapshot {
pub type_name: String,
pub instance_id: u32,
pub package: ComponentPackage,
pub state_bytes: Vec<u8>,
}
impl NodeSnapshot {
pub fn encode(&self) -> Vec<u8> {
bincode::serialize(self).expect("NodeSnapshot serde is infallible for valid types")
}
pub fn decode(bytes: &[u8]) -> Result<Self, bincode::Error> {
bincode::deserialize(bytes)
}
}