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
use super::*;
use std::fs::File;
use std::io::BufWriter;
#[derive(Debug)]
pub struct Logger {
writer: BufWriter<File>,
offset: u64,
bytes_written: u64
}
impl Logger {
pub fn new(log: Log) -> Result<Logger, DatabaseError> {
log.restore_index().and_then(|index| {
log.open_writer().and_then(|writer| {
Ok(Logger {
writer: writer,
offset: index.line_count() + 1,
bytes_written: index.byte_count()
})
})
})
}
pub fn log(&mut self, event: Event) -> Result<u64, DatabaseError> {
match event.validated() {
Ok(event) => {
let event_id = self.offset;
let mut event = event.with_id(event_id);
if event.timestamp == 0 {
event = event.with_current_timestamp();
}
let event_string = event.to_tab_separated_string();
match self.writer.write_line(&event_string) {
Ok(bytes_written) => {
self.offset += 1;
self.bytes_written += bytes_written as u64;
Ok(event_id)
},
Err(err) => Err(DatabaseError::from_io_error(err))
}
},
Err(err) => Err(DatabaseError::ValidationError(err))
}
}
pub fn bytes_written(&self) -> u64 {
self.bytes_written
}
}
#[cfg(test)]
mod tests {
use super::super::*;
use exar_testkit::*;
use std::io::{BufRead, BufReader};
fn create_log() -> Log {
let ref collection_name = random_collection_name();
Log::new("", collection_name, 100)
}
#[test]
fn test_constructor() {
let log = create_log();
let event = Event::new("data", vec!["tag1", "tag2"]);
let mut logger = Logger::new(log.clone()).expect("Unable to create logger");
assert_eq!(logger.writer.get_ref().metadata().unwrap().is_file(), true);
assert_eq!(logger.offset, 1);
assert_eq!(logger.bytes_written, 0);
assert_eq!(logger.log(event), Ok(1));
let logger = Logger::new(log.clone()).expect("Unable to create logger");
assert_eq!(logger.writer.get_ref().metadata().unwrap().is_file(), true);
assert_eq!(logger.offset, 2);
assert_eq!(logger.bytes_written, 31);
assert!(log.remove().is_ok());
}
#[test]
fn test_constructor_failure() {
let ref collection_name = invalid_collection_name();
let log = Log::new("", collection_name, 10);
assert!(Logger::new(log.clone()).is_err());
assert!(log.remove().is_err());
}
#[test]
fn test_log() {
let log = create_log();
let event = Event::new("data", vec!["tag1", "tag2"]);
let mut logger = Logger::new(log.clone()).expect("Unable to create logger");
assert_eq!(logger.log(event.clone()), Ok(1));
assert_eq!(logger.offset, 2);
assert_eq!(logger.bytes_written, 31);
assert_eq!(logger.log(event.clone()), Ok(2));
assert_eq!(logger.offset, 3);
assert_eq!(logger.bytes_written, 62);
let reader = log.open_reader().expect("Unable to open reader");
let mut lines = BufReader::new(reader).lines();
let line = lines.next().expect("Unable to read next line")
.expect("Unable to read next line");
let event = Event::from_tab_separated_str(&line).expect("Unable to decode event");
assert_eq!(event.id, 1);
assert_eq!(event.data, "data");
assert_eq!(event.tags, vec!["tag1", "tag2"]);
assert!(event.timestamp > 0);
let line = lines.next().expect("Unable to read next line")
.expect("Unable to read next line");
let event = Event::from_tab_separated_str(&line).expect("Unable to decode event");
assert_eq!(event.id, 2);
assert_eq!(event.data, "data");
assert_eq!(event.tags, vec!["tag1", "tag2"]);
assert!(event.timestamp > 0);
assert!(lines.next().is_none());
assert!(log.remove().is_ok());
}
#[test]
fn test_event_validation_failure() {
let log = create_log();
let event = Event::new("data", vec![]);
let mut logger = Logger::new(log.clone()).expect("Unable to create logger");
let expected_validation_error = ValidationError::new("event must contain at least one tag");
assert_eq!(logger.log(event.clone()), Err(DatabaseError::ValidationError(expected_validation_error)));
assert!(log.remove().is_ok());
}
}