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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! Contains the [`LogIdMap`] definition used to capture [`LogId`]s and their [`LogIdEntry`]s.

use std::{
    collections::HashMap,
    sync::{RwLock, RwLockWriteGuard},
};

use once_cell::sync::Lazy;

use crate::{
    id_entry::LogIdEntry,
    log_id::{LogId, INVALID_LOG_ID},
};

/// Map to capture [`LogId`]s, and combine all informations set
/// for  a [`LogId`] inside a [`LogIdEntry`].
pub struct LogIdMap {
    /// Map to capture entries for set [`LogId`]s.
    /// Multiple entries per [`LogId`] might be possible
    /// if the [`LogId`] is used at multiple positions.
    pub(crate) map: RwLock<HashMap<LogId, Vec<LogIdEntry>>>,
    /// Used to keep track of the last [`LogId`] that was
    /// entered in the map, and got marked as `drainable`.
    pub(crate) last_finalized_id: RwLock<LogId>,
}

pub(crate) static LOG_ID_MAP: Lazy<LogIdMap> = Lazy::new(LogIdMap::new);

impl Default for LogIdMap {
    fn default() -> Self {
        Self::new()
    }
}

/// Drain global [`LogIdMap`].  Returning all `drainable` entries of all captured [`LogId`]s of the map so far.
pub fn drain_map() -> Option<HashMap<LogId, Vec<LogIdEntry>>> {
    LOG_ID_MAP.drain_map()
}

impl LogIdMap {
    /// Create a new [`LogIdMap`].
    pub fn new() -> Self {
        LogIdMap {
            map: RwLock::new(HashMap::new()),
            last_finalized_id: RwLock::new(INVALID_LOG_ID),
        }
    }

    /// Returns the last [`LogId`] that was
    /// entered in the map, and got marked as `drainable`.
    pub fn get_last_finalized_id(&self) -> LogId {
        match self.last_finalized_id.read() {
            Ok(last) => *last,
            Err(_) => INVALID_LOG_ID,
        }
    }

    /// Drain this [`LogIdMap`]. Returning all `drainable` entries of all captured [`LogId`]s of the map so far.
    pub fn drain_map(&self) -> Option<HashMap<LogId, Vec<LogIdEntry>>> {
        if let Ok(mut last) = self.last_finalized_id.write() {
            *last = INVALID_LOG_ID;
        }

        //Note: Due to RWLock, mutable access to map is fine
        match self.map.write() {
            Ok(mut map) => {
                let mut safe_map = HashMap::new();
                let mut keys = Vec::new();
                for key in (*map).keys() {
                    keys.push(*key);
                }

                for id in keys {
                    let drain_res = drain_entries(&mut map, id);
                    if let Some(entries) = drain_res.0 {
                        safe_map.insert(id, entries);
                    }
                }

                if safe_map.is_empty() {
                    None
                } else {
                    Some(safe_map)
                }
            }
            Err(_) => None,
        }
    }

    /// Returns all captured entries marked as `drainable` for the given [`LogId`].
    ///
    /// # Arguments
    ///
    /// * `id` - the [`LogId`] used to search for map entries
    pub fn get_entries(&self, id: LogId) -> Option<Vec<LogIdEntry>> {
        match self.map.read() {
            Ok(map) => match (*map).get(&id) {
                Some(entries) => {
                    let mut safe_entries: Vec<LogIdEntry> = Vec::new();
                    for entry in entries {
                        if entry.drainable() {
                            safe_entries.push((*entry).clone());
                        }
                    }
                    if safe_entries.is_empty() {
                        None
                    } else {
                        Some(safe_entries)
                    }
                }
                None => None,
            },
            Err(_) => None,
        }
    }

    /// Returns all captured entries for the given [`LogId`].
    /// Entries must not be marked as `drainable`.
    /// Therefore, not all information might have been captured for an entry.
    ///
    /// # Arguments
    ///
    /// * `id` - the [`LogId`] used to search for map entries
    pub fn get_entries_unsafe(&self, id: LogId) -> Option<Vec<LogIdEntry>> {
        match self.map.read() {
            Ok(map) => match (*map).get(&id) {
                Some(entries) => {
                    let mut safe_entries: Vec<LogIdEntry> = Vec::new();
                    for entry in entries {
                        safe_entries.push((*entry).clone());
                    }
                    if safe_entries.is_empty() {
                        None
                    } else {
                        Some(safe_entries)
                    }
                }
                None => None,
            },
            Err(_) => None,
        }
    }

    /// Drains all captured entries marked as `drainable` for the given [`LogId`].
    /// Non-drainable entries remain in the map.
    ///
    /// # Arguments
    ///
    /// * `id` - the [`LogId`] used to search for map entries
    pub fn drain_entries(&self, id: LogId) -> Option<Vec<LogIdEntry>> {
        let mut drained = false;
        let res = match self.map.write() {
            Ok(mut map) => {
                let drain_res = drain_entries(&mut map, id);
                drained = drain_res.1;
                drain_res.0
            }
            Err(_) => None,
        };

        if drained {
            if let Ok(mut last) = self.last_finalized_id.write() {
                if *last == id {
                    *last = INVALID_LOG_ID;
                }
            }
        }

        res
    }
}

/// Function to drain `drainable` map entries using an aquired write-lock.
fn drain_entries(
    write_lock_map: &mut RwLockWriteGuard<HashMap<LogId, Vec<LogIdEntry>>>,
    id: LogId,
) -> (Option<Vec<LogIdEntry>>, bool) {
    let mut drained = false;
    match (*write_lock_map).remove(&id) {
        Some(mut entries) => {
            let mut safe_entries = Vec::new();
            let mut i = 0;
            while i < entries.len() {
                if entries[i].drainable() {
                    drained = true;
                    safe_entries.push(entries.remove(i));
                } else {
                    i += 1;
                }
            }

            if !entries.is_empty() {
                write_lock_map.insert(id, entries);
            }
            if safe_entries.is_empty() {
                (None, drained)
            } else {
                (Some(safe_entries), drained)
            }
        }
        None => (None, drained),
    }
}