Skip to main content

sc_network/
utils.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! `sc-network` utilities
20
21use futures::{stream::unfold, FutureExt, Stream, StreamExt};
22use futures_timer::Delay;
23use linked_hash_set::LinkedHashSet;
24
25use std::{hash::Hash, num::NonZeroUsize, time::Duration};
26
27/// Creates a stream that returns a new value every `duration`.
28pub fn interval(duration: Duration) -> impl Stream<Item = ()> + Unpin {
29	unfold((), move |_| Delay::new(duration).map(|_| Some(((), ())))).map(drop)
30}
31
32/// Wrapper around `LinkedHashSet` with bounded growth.
33///
34/// In the limit, for each element inserted the oldest existing element will be removed.
35#[derive(Debug, Clone)]
36pub struct LruHashSet<T: Hash + Eq> {
37	set: LinkedHashSet<T>,
38	limit: NonZeroUsize,
39}
40
41impl<T: Hash + Eq> LruHashSet<T> {
42	/// Create a new `LruHashSet` with the given (exclusive) limit.
43	pub fn new(limit: NonZeroUsize) -> Self {
44		Self { set: LinkedHashSet::new(), limit }
45	}
46
47	/// Check if the set contains the given element without modifying the set.
48	pub fn contains(&self, e: &T) -> bool {
49		self.set.contains(e)
50	}
51
52	/// Remove all elements from the set.
53	pub fn clear(&mut self) {
54		self.set.clear();
55	}
56
57	/// Insert element into the set.
58	///
59	/// Returns `true` if this is a new element to the set, `false` otherwise.
60	/// Maintains the limit of the set by removing the oldest entry if necessary.
61	/// Inserting the same element will update its LRU position.
62	pub fn insert(&mut self, e: T) -> bool {
63		if self.set.insert(e) {
64			if self.set.len() == usize::from(self.limit) {
65				self.set.pop_front(); // remove oldest entry
66			}
67			return true;
68		}
69		false
70	}
71}
72
73#[cfg(test)]
74mod tests {
75	use super::*;
76
77	#[test]
78	fn maintains_limit() {
79		let three = NonZeroUsize::new(3).unwrap();
80		let mut set = LruHashSet::<u8>::new(three);
81
82		// First element.
83		assert!(set.insert(1));
84		assert_eq!(vec![&1], set.set.iter().collect::<Vec<_>>());
85
86		// Second element.
87		assert!(set.insert(2));
88		assert_eq!(vec![&1, &2], set.set.iter().collect::<Vec<_>>());
89
90		// Inserting the same element updates its LRU position.
91		assert!(!set.insert(1));
92		assert_eq!(vec![&2, &1], set.set.iter().collect::<Vec<_>>());
93
94		// We reached the limit. The next element forces the oldest one out.
95		assert!(set.insert(3));
96		assert_eq!(vec![&1, &3], set.set.iter().collect::<Vec<_>>());
97	}
98}