-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement 'Seen' node, in conjunction with SSE allows you to receive …
…only new entries
- Loading branch information
Showing
9 changed files
with
181 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use std::{collections::HashSet, slice, sync::Arc}; | ||
|
||
use anyhow::anyhow; | ||
use async_trait::async_trait; | ||
use parking_lot::Mutex; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::node::{Data, DataKind, NodeTrait, IO}; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct Seen { | ||
#[serde(default)] | ||
store: Store, | ||
|
||
#[serde(skip)] | ||
input: Arc<IO>, | ||
#[serde(skip)] | ||
output: Arc<IO>, | ||
} | ||
|
||
impl Seen { | ||
pub fn new() -> Self { | ||
Self { | ||
store: Store::default(), | ||
|
||
output: Arc::default(), | ||
input: Arc::default(), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl NodeTrait for Seen { | ||
fn inputs(&self) -> &[Arc<IO>] { | ||
slice::from_ref(&self.input) | ||
} | ||
|
||
fn outputs(&self) -> &[Arc<IO>] { | ||
slice::from_ref(&self.input) | ||
} | ||
|
||
fn input_types(&self) -> &[DataKind] { | ||
&[DataKind::Feed] | ||
} | ||
|
||
fn output_types(&self) -> &[DataKind] { | ||
&[DataKind::Feed] | ||
} | ||
|
||
#[tracing::instrument(name = "seen_node")] | ||
async fn run(&self) -> anyhow::Result<()> { | ||
let Some(Data::Feed(mut atom)) = self.input.get() else { | ||
return Err(anyhow!("Input data not available")); | ||
}; | ||
|
||
if let Store::Internal(seen) = &self.store { | ||
Check failure on line 56 in src/flow/seen.rs GitHub Actions / all
Check failure on line 56 in src/flow/seen.rs GitHub Actions / all (ubuntu-latest, stable)
|
||
let mut seen = seen.lock(); | ||
|
||
// seen.retain(|id| atom.entries.iter().any(|i| i.id.eq(id))); | ||
atom.entries.retain(|item| seen.insert(item.id.clone())); | ||
} | ||
|
||
if !atom.entries.is_empty() { | ||
self.output.accept(atom)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn set_input(&mut self, _index: usize, input: Arc<IO>) { | ||
self.input = input; | ||
} | ||
fn set_output(&mut self, _index: usize, output: Arc<IO>) { | ||
self.output = output; | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
enum Store { | ||
Internal(#[serde(skip)] Mutex<HashSet<String>>), | ||
// External, | ||
} | ||
|
||
impl Default for Store { | ||
fn default() -> Self { | ||
Self::Internal(Mutex::default()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters