1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#![allow(dead_code)]
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity Ethereum.
// Parity Ethereum is free software: you can redistribute it and/or modify
// 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.
// Parity Ethereum is distributed in the hope that it will be useful,
// 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
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
//! Indexes all rpc poll requests.
use cfx_types::H128;
use transient_hashmap::{StandardTimer, Timer, TransientHashMap};
pub type PollId = H128;
/// Indexes all poll requests.
///
/// Lazily garbage collects unused polls info.
pub struct PollManager<F, T = StandardTimer>
where T: Timer
{
polls: TransientHashMap<PollId, F, T>,
}
impl<F> PollManager<F, StandardTimer> {
/// Creates new instance of indexer
pub fn new(lifetime: u32) -> Self {
PollManager::new_with_timer(Default::default(), lifetime)
}
}
impl<F, T> PollManager<F, T>
where T: Timer
{
pub fn new_with_timer(timer: T, lifetime: u32) -> Self {
PollManager {
polls: TransientHashMap::new_with_timer(lifetime, timer),
}
}
/// Returns id which can be used for new poll.
///
/// Stores information when last poll happend.
pub fn create_poll(&mut self, filter: F) -> PollId {
self.polls.prune();
let id = loop {
let id = PollId::random();
if self.polls.contains_key(&id) {
continue;
}
break id;
};
self.polls.insert(id, filter);
id
}
// 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();
self.polls.get(id)
}
/// 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)
}
/// Removes poll info.
pub fn remove_poll(&mut self, id: &PollId) -> bool {
self.polls.remove(id).is_some()
}
}
#[cfg(test)]
mod tests {
use super::PollManager;
use std::cell::Cell;
use transient_hashmap::Timer;
struct TestTimer<'a> {
time: &'a Cell<i64>,
}
impl<'a> Timer for TestTimer<'a> {
fn get_time(&self) -> i64 { self.time.get() }
}
#[test]
fn test_poll_indexer() {
let time = Cell::new(0);
let timer = TestTimer { time: &time };
let mut indexer = PollManager::new_with_timer(timer, 60);
let id1 = indexer.create_poll(20);
let id2 = indexer.create_poll(20);
assert_ne!(id1, id2);
time.set(10);
*indexer.poll_mut(&id1).unwrap() = 21;
assert_eq!(*indexer.poll(&id1).unwrap(), 21);
assert_eq!(*indexer.poll(&id2).unwrap(), 20);
time.set(30);
*indexer.poll_mut(&id2).unwrap() = 23;
assert_eq!(*indexer.poll(&id2).unwrap(), 23);
time.set(75);
assert!(indexer.poll(&id1).is_none());
assert_eq!(*indexer.poll(&id2).unwrap(), 23);
indexer.remove_poll(&id2);
assert!(indexer.poll(&id2).is_none());
}
}