use crate::node::{InputStreams, Node, NodeExecutionError, OutputStreams};
use crate::nodes::common::BaseNode;
use async_trait::async_trait;
use std::any::Any;
use std::collections::HashMap;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::pin::Pin;
use std::sync::Arc;
use tokio_stream::{StreamExt, wrappers::ReceiverStream};
#[derive(Debug, Clone)]
struct ArcWrapper(Arc<dyn Any + Send + Sync>);
impl PartialEq for ArcWrapper {
fn eq(&self, other: &Self) -> bool {
if let (Ok(a), Ok(b)) = (
self.0.clone().downcast::<i32>(),
other.0.clone().downcast::<i32>(),
) {
return *a == *b;
}
if let (Ok(a), Ok(b)) = (
self.0.clone().downcast::<i64>(),
other.0.clone().downcast::<i64>(),
) {
return *a == *b;
}
if let (Ok(a), Ok(b)) = (
self.0.clone().downcast::<u32>(),
other.0.clone().downcast::<u32>(),
) {
return *a == *b;
}
if let (Ok(a), Ok(b)) = (
self.0.clone().downcast::<u64>(),
other.0.clone().downcast::<u64>(),
) {
return *a == *b;
}
if let (Ok(a), Ok(b)) = (
self.0.clone().downcast::<String>(),
other.0.clone().downcast::<String>(),
) {
return *a == *b;
}
if let (Ok(a), Ok(b)) = (
self.0.clone().downcast::<bool>(),
other.0.clone().downcast::<bool>(),
) {
return *a == *b;
}
Arc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for ArcWrapper {}
impl Hash for ArcWrapper {
fn hash<H: Hasher>(&self, state: &mut H) {
if let Ok(val) = self.0.clone().downcast::<i32>() {
return (*val).hash(state);
}
if let Ok(val) = self.0.clone().downcast::<i64>() {
return (*val).hash(state);
}
if let Ok(val) = self.0.clone().downcast::<u32>() {
return (*val).hash(state);
}
if let Ok(val) = self.0.clone().downcast::<u64>() {
return (*val).hash(state);
}
if let Ok(val) = self.0.clone().downcast::<String>() {
return (*val).hash(state);
}
if let Ok(val) = self.0.clone().downcast::<bool>() {
return (*val).hash(state);
}
(Arc::as_ptr(&self.0) as *const () as usize).hash(state);
}
}
pub struct DistinctNode {
pub(crate) base: BaseNode,
}
impl DistinctNode {
pub fn new(name: String) -> Self {
Self {
base: BaseNode::new(
name,
vec!["configuration".to_string(), "in".to_string()],
vec!["out".to_string(), "error".to_string()],
),
}
}
}
#[async_trait]
impl Node for DistinctNode {
fn name(&self) -> &str {
self.base.name()
}
fn set_name(&mut self, name: &str) {
self.base.set_name(name);
}
fn input_port_names(&self) -> &[String] {
self.base.input_port_names()
}
fn output_port_names(&self) -> &[String] {
self.base.output_port_names()
}
fn has_input_port(&self, name: &str) -> bool {
self.base.has_input_port(name)
}
fn has_output_port(&self, name: &str) -> bool {
self.base.has_output_port(name)
}
fn execute(
&self,
mut inputs: InputStreams,
) -> Pin<
Box<dyn std::future::Future<Output = Result<OutputStreams, NodeExecutionError>> + Send + '_>,
> {
Box::pin(async move {
let _config_stream = inputs.remove("configuration");
let in_stream = inputs.remove("in").ok_or("Missing 'in' input")?;
let (out_tx, out_rx) = tokio::sync::mpsc::channel(10);
let (error_tx, error_rx) = tokio::sync::mpsc::channel(10);
let out_tx_clone = out_tx.clone();
let _error_tx_clone = error_tx.clone();
tokio::spawn(async move {
let mut seen_items: HashSet<ArcWrapper> = HashSet::new();
let mut in_stream = in_stream;
while let Some(item) = in_stream.next().await {
let wrapper = ArcWrapper(item.clone());
if !seen_items.contains(&wrapper) {
let _ = out_tx_clone.send(item).await;
seen_items.insert(wrapper);
}
}
});
let mut outputs = HashMap::new();
outputs.insert(
"out".to_string(),
Box::pin(ReceiverStream::new(out_rx))
as Pin<Box<dyn tokio_stream::Stream<Item = Arc<dyn Any + Send + Sync>> + Send>>,
);
outputs.insert(
"error".to_string(),
Box::pin(ReceiverStream::new(error_rx))
as Pin<Box<dyn tokio_stream::Stream<Item = Arc<dyn Any + Send + Sync>> + Send>>,
);
Ok(outputs)
})
}
}