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
|
//! Different aggregators for output.
//!
//! An aggregator is something that "controls the order" of the output. Aggregators can either save
//! all items that they're and output them once the search is finished, or write them straight
//! to the output stream.
//!
//! Aggregators must be shareable across threads, as the search will be multi-threaded. This is why
//! an Aggregator must make sure that the data is protected by a mutex or similar.
use super::{super::LogResult, formats::Format, sorting::Sorting};
use std::{io::Write, sync::Mutex};
pub trait Aggregator: Sync {
fn push_item(&self, item: LogResult, format: &dyn Format, stream: &mut dyn Write);
// When the `unsized_locals` feature is stable, we could rewrite this to finish(self, ...).
fn finish(self: Box<Self>, format: &dyn Format, stream: &mut dyn Write);
}
/// An aggregator that just pushes through each item to the output stream without any sorting or
/// whatsoever.
pub struct WriteThrough;
impl Aggregator for WriteThrough {
fn push_item(&self, item: LogResult, format: &dyn Format, stream: &mut dyn Write) {
let text = format.format_result(&item);
stream.write_all(text.as_bytes()).unwrap();
stream.flush().unwrap();
}
fn finish(self: Box<Self>, _: &dyn Format, _: &mut dyn Write) {}
}
/// An aggregator that keeps all found logs in memory and sorts them before outputting them.
#[derive(Debug)]
pub struct SortedOutput {
sorting: Sorting,
items: Mutex<Vec<LogResult>>,
}
impl SortedOutput {
pub fn new(sorting: Sorting) -> Self {
SortedOutput {
sorting,
items: Mutex::new(vec![]),
}
}
}
impl Aggregator for SortedOutput {
fn push_item(&self, item: LogResult, _: &dyn Format, _: &mut dyn Write) {
self.items.lock().unwrap().push(item)
}
fn finish(self: Box<Self>, format: &dyn Format, stream: &mut dyn Write) {
let SortedOutput { sorting, items } = *self;
let mut items = items.into_inner().unwrap();
items.sort_unstable_by(|a, b| sorting.cmp(a, b));
for item in items {
let text = format.format_result(&item);
stream.write_all(text.as_bytes()).unwrap();
}
stream.flush().unwrap();
}
}
|