2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-02-23 18:51:29 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-02-23 18:51:29 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-02-23 18:51:29 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-23 18:51:29 +01:00
|
|
|
|
|
|
|
//! Indexes all rpc poll requests.
|
|
|
|
|
|
|
|
use transient_hashmap::{TransientHashMap, Timer, StandardTimer};
|
|
|
|
|
|
|
|
pub type PollId = usize;
|
|
|
|
|
|
|
|
/// Indexes all poll requests.
|
2016-03-02 05:46:38 +01:00
|
|
|
///
|
2016-02-23 18:51:29 +01:00
|
|
|
/// Lazily garbage collects unused polls info.
|
2016-03-02 05:46:38 +01:00
|
|
|
pub struct PollManager<F, T = StandardTimer> where T: Timer {
|
2016-03-11 12:31:42 +01:00
|
|
|
polls: TransientHashMap<PollId, F, T>,
|
2016-03-10 15:35:36 +01:00
|
|
|
next_available_id: PollId,
|
2016-02-23 18:51:29 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 05:46:38 +01:00
|
|
|
impl<F> PollManager<F, StandardTimer> {
|
2018-06-18 13:42:54 +02:00
|
|
|
/// Creates new instance of indexer
|
|
|
|
pub fn new(lifetime: u32) -> Self {
|
|
|
|
PollManager::new_with_timer(Default::default(), lifetime)
|
2016-02-23 18:51:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 05:46:38 +01:00
|
|
|
impl<F, T> PollManager<F, T> where T: Timer {
|
2016-03-11 12:31:42 +01:00
|
|
|
|
2018-06-18 13:42:54 +02:00
|
|
|
pub fn new_with_timer(timer: T, lifetime: u32) -> Self {
|
2016-03-02 05:46:38 +01:00
|
|
|
PollManager {
|
2018-06-18 13:42:54 +02:00
|
|
|
polls: TransientHashMap::new_with_timer(lifetime, timer),
|
2016-03-02 14:03:43 +01:00
|
|
|
next_available_id: 0,
|
2016-02-23 18:51:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 05:46:38 +01:00
|
|
|
/// Returns id which can be used for new poll.
|
|
|
|
///
|
2016-02-23 18:51:29 +01:00
|
|
|
/// Stores information when last poll happend.
|
2016-03-11 12:31:42 +01:00
|
|
|
pub fn create_poll(&mut self, filter: F) -> PollId {
|
|
|
|
self.polls.prune();
|
|
|
|
|
2016-02-23 18:51:29 +01:00
|
|
|
let id = self.next_available_id;
|
2016-03-11 12:31:42 +01:00
|
|
|
self.polls.insert(id, filter);
|
|
|
|
|
2016-02-23 18:51:29 +01:00
|
|
|
self.next_available_id += 1;
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
2016-03-11 12:31:42 +01:00
|
|
|
// Implementation is always using `poll_mut`
|
|
|
|
/// Get a reference to stored poll filter
|
|
|
|
pub fn poll(&mut self, id: &PollId) -> Option<&F> {
|
|
|
|
self.polls.prune();
|
2016-02-23 18:51:29 +01:00
|
|
|
self.polls.get(id)
|
|
|
|
}
|
|
|
|
|
2016-03-11 12:31:42 +01:00
|
|
|
/// Get a mutable reference to stored poll filter
|
|
|
|
pub fn poll_mut(&mut self, id: &PollId) -> Option<&mut F> {
|
|
|
|
self.polls.prune();
|
|
|
|
self.polls.get_mut(id)
|
2016-03-10 15:35:36 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 18:51:29 +01:00
|
|
|
/// Removes poll info.
|
2018-03-31 11:06:16 +02:00
|
|
|
pub fn remove_poll(&mut self, id: &PollId) -> bool {
|
|
|
|
self.polls.remove(id).is_some()
|
2016-02-23 18:51:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-03-11 12:31:42 +01:00
|
|
|
use std::cell::Cell;
|
2016-02-23 18:51:29 +01:00
|
|
|
use transient_hashmap::Timer;
|
2016-03-02 05:46:38 +01:00
|
|
|
use v1::helpers::PollManager;
|
2016-02-23 18:51:29 +01:00
|
|
|
|
|
|
|
struct TestTimer<'a> {
|
2016-03-11 12:31:42 +01:00
|
|
|
time: &'a Cell<i64>,
|
2016-02-23 18:51:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Timer for TestTimer<'a> {
|
|
|
|
fn get_time(&self) -> i64 {
|
2016-03-11 12:31:42 +01:00
|
|
|
self.time.get()
|
2016-02-23 18:51:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_poll_indexer() {
|
2016-03-11 12:31:42 +01:00
|
|
|
let time = Cell::new(0);
|
2016-02-23 18:51:29 +01:00
|
|
|
let timer = TestTimer {
|
2016-03-02 14:03:43 +01:00
|
|
|
time: &time,
|
2016-02-23 18:51:29 +01:00
|
|
|
};
|
|
|
|
|
2018-06-18 13:42:54 +02:00
|
|
|
let mut indexer = PollManager::new_with_timer(timer,60);
|
2016-03-11 12:31:42 +01:00
|
|
|
assert_eq!(indexer.create_poll(20), 0);
|
|
|
|
assert_eq!(indexer.create_poll(20), 1);
|
2016-02-23 18:51:29 +01:00
|
|
|
|
2016-03-11 12:31:42 +01:00
|
|
|
time.set(10);
|
|
|
|
*indexer.poll_mut(&0).unwrap() = 21;
|
|
|
|
assert_eq!(*indexer.poll(&0).unwrap(), 21);
|
|
|
|
assert_eq!(*indexer.poll(&1).unwrap(), 20);
|
2016-02-23 18:51:29 +01:00
|
|
|
|
2016-03-11 12:31:42 +01:00
|
|
|
time.set(30);
|
|
|
|
*indexer.poll_mut(&1).unwrap() = 23;
|
|
|
|
assert_eq!(*indexer.poll(&1).unwrap(), 23);
|
2016-02-23 18:51:29 +01:00
|
|
|
|
2016-03-11 12:31:42 +01:00
|
|
|
time.set(75);
|
|
|
|
assert!(indexer.poll(&0).is_none());
|
|
|
|
assert_eq!(*indexer.poll(&1).unwrap(), 23);
|
2016-02-23 18:51:29 +01:00
|
|
|
|
|
|
|
indexer.remove_poll(&1);
|
2016-03-11 12:31:42 +01:00
|
|
|
assert!(indexer.poll(&1).is_none());
|
2016-03-10 15:35:36 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 18:51:29 +01:00
|
|
|
}
|