|
| 1 | +// Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0. |
| 2 | + |
| 3 | +use std::{collections::HashMap, hash::Hash, sync::RwLock}; |
| 4 | + |
| 5 | +use tokio::sync::mpsc::Sender; |
| 6 | + |
| 7 | +type Notifier<T> = Sender<T>; |
| 8 | + |
| 9 | +#[derive(Debug)] |
| 10 | +struct Notifiers<T> { |
| 11 | + notifiers: RwLock<Vec<Notifier<T>>>, |
| 12 | +} |
| 13 | + |
| 14 | +impl<T> Notifiers<T> { |
| 15 | + pub fn new(notifier: Notifier<T>) -> Self { |
| 16 | + let notifiers = vec![notifier]; |
| 17 | + Self { |
| 18 | + notifiers: RwLock::new(notifiers), |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + pub fn add_notifier(&self, notifier: Notifier<T>) { |
| 23 | + self.notifiers.write().unwrap().push(notifier); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Debug)] |
| 28 | +pub struct RequestNotifiers<K, T> |
| 29 | +where |
| 30 | + K: PartialEq + Eq + Hash, |
| 31 | +{ |
| 32 | + notifiers_by_key: RwLock<HashMap<K, Notifiers<T>>>, |
| 33 | +} |
| 34 | + |
| 35 | +impl<K, T> Default for RequestNotifiers<K, T> |
| 36 | +where |
| 37 | + K: PartialEq + Eq + Hash, |
| 38 | +{ |
| 39 | + fn default() -> Self { |
| 40 | + Self { |
| 41 | + notifiers_by_key: RwLock::new(HashMap::new()), |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<K, T> RequestNotifiers<K, T> |
| 47 | +where |
| 48 | + K: PartialEq + Eq + Hash, |
| 49 | +{ |
| 50 | + /// Insert a notifier for the given key. |
| 51 | + pub fn insert_notifier(&self, key: K, notifier: Notifier<T>) -> RequestResult { |
| 52 | + // First try to read the notifiers, if the key exists, add the notifier to the |
| 53 | + // notifiers. |
| 54 | + let notifiers_by_key = self.notifiers_by_key.read().unwrap(); |
| 55 | + if let Some(notifiers) = notifiers_by_key.get(&key) { |
| 56 | + notifiers.add_notifier(notifier); |
| 57 | + return RequestResult::Wait; |
| 58 | + } |
| 59 | + drop(notifiers_by_key); |
| 60 | + |
| 61 | + // If the key does not exist, try to write the notifiers. |
| 62 | + let mut notifiers_by_key = self.notifiers_by_key.write().unwrap(); |
| 63 | + // double check, if the key exists, add the notifier to the notifiers. |
| 64 | + if let Some(notifiers) = notifiers_by_key.get(&key) { |
| 65 | + notifiers.add_notifier(notifier); |
| 66 | + return RequestResult::Wait; |
| 67 | + } |
| 68 | + |
| 69 | + //the key is not existed, insert the key and the notifier. |
| 70 | + notifiers_by_key.insert(key, Notifiers::new(notifier)); |
| 71 | + RequestResult::First |
| 72 | + } |
| 73 | + |
| 74 | + /// Take the notifiers for the given key, and remove the key from the map. |
| 75 | + pub fn take_notifiers(&self, key: &K) -> Option<Vec<Notifier<T>>> { |
| 76 | + self.notifiers_by_key |
| 77 | + .write() |
| 78 | + .unwrap() |
| 79 | + .remove(key) |
| 80 | + .map(|notifiers| notifiers.notifiers.into_inner().unwrap()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +pub enum RequestResult { |
| 85 | + // The first request for this key, need to handle this request. |
| 86 | + First, |
| 87 | + // There are other requests for this key, just wait for the result. |
| 88 | + Wait, |
| 89 | +} |
0 commit comments