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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use amico_core::types::AgentEvent;
use chrono::Utc;
use std::collections::HashMap;
use std::time::Duration;
/// Struct representing an event pool in the system.
#[derive(Debug)]
pub struct EventPool {
/// Key is the Event ID, value is the Event itself.
events_map: HashMap<u32, AgentEvent>,
/// The next ID candidate if `free_list` is empty.
next_id: u32,
/// A list (stack) of IDs that were removed and can be reused.
free_list: Vec<u32>,
/// The default expiry time for events in seconds
default_expiry_time: u64,
}
impl EventPool {
/// Creates a new EventPool.
pub fn new(default_expiry_time: u64) -> Self {
Self {
events_map: HashMap::new(),
next_id: 0,
free_list: Vec::new(),
default_expiry_time,
}
}
/// Updates the events map and retrieves all events that hasn't expired as a cloned vector and .
/// (Depending on requirements, returning references or an iterator might be preferable.)
pub fn get_events(&mut self) -> Vec<AgentEvent> {
let now = Utc::now();
// Use drain_filter() to remove expired events and recycle IDs
let mut expired_ids = Vec::new();
self.events_map.retain(|id, event| {
if let Some(expiry_time) = event.expiry_time {
if expiry_time < now {
expired_ids.push(*id);
return false;
}
}
true
});
self.free_list.extend(expired_ids);
// return all non-expired events
self.events_map.values().cloned().collect()
}
/// Inserts multiple new events into the event pool.
/// Each new Event will be assigned a unique ID from this pool.
///
/// # Arguments
///
/// * `events` - A vector of events to be inserted.
///
/// # Returns
///
/// * `Result<(), EventPoolError>` - Returns `Ok(())` if all events are successfully inserted,
/// otherwise returns an `EventPoolError`.
pub fn extend_events(&mut self, events: Vec<AgentEvent>) -> Result<(), EventPoolError> {
let now = Utc::now();
let default_expiry = now + Duration::from_secs(self.default_expiry_time);
// Optimization: Pre-allocate capacity for the HashMap to reduce reallocations
self.events_map.reserve(events.len());
for mut event in events {
let id = self.get_new_event_id()?;
event.id = id;
if event.expiry_time.is_none() {
event.expiry_time = Some(default_expiry);
}
self.events_map.insert(id, event);
}
Ok(())
}
/// Removes events corresponding to the given list of event IDs.
/// Freed IDs are pushed back into `free_list`.
pub fn remove_events(&mut self, event_ids: Vec<u32>) -> Result<(), EventPoolError> {
let mut not_found_ids = Vec::new();
for id in event_ids {
if self.events_map.remove(&id).is_some() {
self.free_list.push(id);
} else {
not_found_ids.push(id);
}
}
if !not_found_ids.is_empty() {
return Err(EventPoolError::SomeEventIdsNotFound(not_found_ids));
}
Ok(())
}
/// Generates a new unique event ID in O(1) time, reusing old IDs if available.
fn get_new_event_id(&mut self) -> Result<u32, EventPoolError> {
// If we have some freed IDs, reuse one.
if let Some(reused_id) = self.free_list.pop() {
return Ok(reused_id);
}
// Ensure we don't overflow the ID space.
if self.next_id == u32::MAX {
return Err(EventPoolError::NoAvailableEventIds);
}
// Otherwise, use the next_id.
let id = self.next_id;
// Increment next_id for the next allocation.
self.next_id += 1;
Ok(id)
}
}
/// Errors that can occur in event pool.
#[derive(thiserror::Error, Debug)]
pub enum EventPoolError {
/// Error when the there is no available event IDs left.
#[error("No available event IDs left")]
NoAvailableEventIds,
/// Error when No such event ID.
#[error("Event ID not found: {0}")]
EventIdNotFound(u32),
/// Error when the event IDs are not found.
#[error("Event IDs not found: {0:?}")]
SomeEventIdsNotFound(Vec<u32>),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_event_pool() -> Result<(), EventPoolError> {
// Create a new EventPool
let mut event_pool = EventPool::new(1);
// Add two events to the event pool
event_pool.extend_events(vec![
AgentEvent::new("ExampleEvent", "ExampleSource"),
AgentEvent::new("ExampleEvent2", "ExampleSource2").lifetime(Duration::from_secs(3)),
])?;
assert_eq!(event_pool.get_events().len(), 2);
// Wait for 2 seconds
std::thread::sleep(std::time::Duration::from_secs(2));
assert_eq!(event_pool.get_events().len(), 1);
// remove the last event
event_pool.remove_events(vec![1])?;
Ok(())
}
}