|
@@ -1,5 +1,5 @@
|
|
//! Broadcaster implementation
|
|
//! Broadcaster implementation
|
|
-use crate::{worker::Worker, Filter, Transaction};
|
|
|
|
|
|
+use crate::{worker::Worker, Filter, FilterableValue, Transaction};
|
|
use async_trait::async_trait;
|
|
use async_trait::async_trait;
|
|
use std::{
|
|
use std::{
|
|
collections::HashMap,
|
|
collections::HashMap,
|
|
@@ -16,7 +16,7 @@ use tokio::sync::{
|
|
/// This structure broadcasts the transactions to all subscribers in a separated working thread.
|
|
/// This structure broadcasts the transactions to all subscribers in a separated working thread.
|
|
pub struct Broadcaster {
|
|
pub struct Broadcaster {
|
|
subscribers: RwLock<HashMap<usize, Sender<Transaction>>>,
|
|
subscribers: RwLock<HashMap<usize, Sender<Transaction>>>,
|
|
- subscriptions: RwLock<HashMap<Filter, Vec<usize>>>,
|
|
|
|
|
|
+ subscriptions: RwLock<HashMap<FilterableValue, Vec<(Filter, usize)>>>,
|
|
is_there_any_subscriber: AtomicBool,
|
|
is_there_any_subscriber: AtomicBool,
|
|
index: AtomicUsize,
|
|
index: AtomicUsize,
|
|
}
|
|
}
|
|
@@ -25,7 +25,7 @@ impl Default for Broadcaster {
|
|
fn default() -> Self {
|
|
fn default() -> Self {
|
|
Self {
|
|
Self {
|
|
subscribers: RwLock::new(HashMap::<usize, Sender<_>>::new()),
|
|
subscribers: RwLock::new(HashMap::<usize, Sender<_>>::new()),
|
|
- subscriptions: RwLock::new(HashMap::<Filter, Vec<_>>::new()),
|
|
|
|
|
|
+ subscriptions: RwLock::new(HashMap::<FilterableValue, Vec<_>>::new()),
|
|
is_there_any_subscriber: false.into(),
|
|
is_there_any_subscriber: false.into(),
|
|
index: 0.into(),
|
|
index: 0.into(),
|
|
}
|
|
}
|
|
@@ -52,10 +52,16 @@ impl Broadcaster {
|
|
self.is_there_any_subscriber
|
|
self.is_there_any_subscriber
|
|
.store(true, std::sync::atomic::Ordering::Release);
|
|
.store(true, std::sync::atomic::Ordering::Release);
|
|
|
|
|
|
- if let Some(previous_values) = listeners.get_mut(&filter) {
|
|
|
|
- previous_values.push(sender_index);
|
|
|
|
- } else {
|
|
|
|
- listeners.insert(filter, vec![sender_index]);
|
|
|
|
|
|
+ let (primary_filter, filter) = filter.get_primary_filter();
|
|
|
|
+ let key_filter: Vec<FilterableValue> = primary_filter.into();
|
|
|
|
+ println!("{:#?}", key_filter);
|
|
|
|
+
|
|
|
|
+ for key_filter in key_filter {
|
|
|
|
+ if let Some(previous_values) = listeners.get_mut(&key_filter) {
|
|
|
|
+ previous_values.push((filter.clone(), sender_index));
|
|
|
|
+ } else {
|
|
|
|
+ listeners.insert(key_filter, vec![(filter.clone(), sender_index)]);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
receiver
|
|
receiver
|
|
@@ -78,15 +84,21 @@ impl Worker for Broadcaster {
|
|
let mut subscriptions_to_reindex = vec![];
|
|
let mut subscriptions_to_reindex = vec![];
|
|
let mut senders_to_remove = vec![];
|
|
let mut senders_to_remove = vec![];
|
|
|
|
|
|
- for (filter, listeners) in listeners.iter() {
|
|
|
|
- if filter.matches(&transaction.transaction, &transaction.revision) {
|
|
|
|
- for sender_index in listeners.iter() {
|
|
|
|
|
|
+ for primary_filter in transaction.get_filterable_fields() {
|
|
|
|
+ let listeners = if let Some(listeners) = listeners.get(&primary_filter) {
|
|
|
|
+ listeners
|
|
|
|
+ } else {
|
|
|
|
+ continue;
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ for (filter, sender_index) in listeners {
|
|
|
|
+ if filter.matches(&transaction.transaction, &transaction.revision) {
|
|
if let Some(Err(TrySendError::Closed(_))) = senders
|
|
if let Some(Err(TrySendError::Closed(_))) = senders
|
|
.get(sender_index)
|
|
.get(sender_index)
|
|
.map(|sender| sender.try_send(transaction.clone()))
|
|
.map(|sender| sender.try_send(transaction.clone()))
|
|
{
|
|
{
|
|
senders_to_remove.push(*sender_index);
|
|
senders_to_remove.push(*sender_index);
|
|
- subscriptions_to_reindex.push(filter.clone());
|
|
|
|
|
|
+ subscriptions_to_reindex.push(primary_filter.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -103,13 +115,15 @@ impl Worker for Broadcaster {
|
|
senders.remove(to_remove);
|
|
senders.remove(to_remove);
|
|
}
|
|
}
|
|
|
|
|
|
- for to_rebuild in &subscriptions_to_reindex {
|
|
|
|
- if let Some(list_of_senders) = listeners.get_mut(to_rebuild) {
|
|
|
|
- *list_of_senders = list_of_senders
|
|
|
|
- .iter()
|
|
|
|
- .filter(|x| senders.contains_key(*x))
|
|
|
|
- .copied()
|
|
|
|
- .collect::<Vec<_>>();
|
|
|
|
|
|
+ for to_rebuild in subscriptions_to_reindex {
|
|
|
|
+ if let Some(list_of_senders) = listeners.remove(&to_rebuild) {
|
|
|
|
+ listeners.insert(
|
|
|
|
+ to_rebuild,
|
|
|
|
+ list_of_senders
|
|
|
|
+ .into_iter()
|
|
|
|
+ .filter(|x| senders.contains_key(&x.1))
|
|
|
|
+ .collect::<Vec<_>>(),
|
|
|
|
+ );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|