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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use std::cmp::Ordering;
use std::fmt::{self, Display};
use fail::fail_point;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::ToPrimitive;
use serde_repr::{Deserialize_repr, Serialize_repr};
use strum::EnumIter;
use crate::Result;
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum LogQueue {
Append = 0,
Rewrite = 1,
}
pub type FileSeq = u64;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct FileId {
pub queue: LogQueue,
pub seq: FileSeq,
}
impl FileId {
pub fn new(queue: LogQueue, seq: FileSeq) -> Self {
Self { queue, seq }
}
#[cfg(test)]
pub fn dummy(queue: LogQueue) -> Self {
Self { queue, seq: 0 }
}
}
impl std::cmp::Ord for FileId {
fn cmp(&self, other: &Self) -> Ordering {
match (self.queue, other.queue) {
(LogQueue::Append, LogQueue::Rewrite) => Ordering::Greater,
(LogQueue::Rewrite, LogQueue::Append) => Ordering::Less,
_ => self.seq.cmp(&other.seq),
}
}
}
impl std::cmp::PartialOrd for FileId {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct FileBlockHandle {
pub id: FileId,
pub offset: u64,
pub len: usize,
}
impl FileBlockHandle {
#[cfg(test)]
pub fn dummy(queue: LogQueue) -> Self {
Self {
id: FileId::dummy(queue),
offset: 0,
len: 0,
}
}
}
#[repr(u64)]
#[derive(
Clone,
Copy,
Debug,
Eq,
PartialEq,
FromPrimitive,
ToPrimitive,
Serialize_repr,
Deserialize_repr,
EnumIter,
)]
pub enum Version {
V1 = 1,
V2 = 2,
}
impl Version {
pub fn has_log_signing(&self) -> bool {
fail_point!("pipe_log::version::force_enable_log_signing", |_| { true });
match self {
Version::V1 => false,
Version::V2 => true,
}
}
}
impl Default for Version {
fn default() -> Self {
Version::V1
}
}
impl Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_u64().unwrap())
}
}
#[derive(Debug, Clone)]
pub struct LogFileContext {
pub id: FileId,
pub version: Version,
}
impl LogFileContext {
pub fn new(id: FileId, version: Version) -> Self {
Self { id, version }
}
pub fn get_signature(&self) -> Option<u32> {
if self.version.has_log_signing() {
Some(self.id.seq as u32)
} else {
None
}
}
}
pub trait ReactiveBytes {
fn as_bytes(&mut self, ctx: &LogFileContext) -> &[u8];
}
impl<T> ReactiveBytes for &T
where
T: AsRef<[u8]> + ?Sized,
{
fn as_bytes(&mut self, _ctx: &LogFileContext) -> &[u8] {
(*self).as_ref()
}
}
pub trait PipeLog: Sized {
fn read_bytes(&self, handle: FileBlockHandle) -> Result<Vec<u8>>;
fn append<T: ReactiveBytes + ?Sized>(
&self,
queue: LogQueue,
bytes: &mut T,
) -> Result<FileBlockHandle>;
fn sync(&self, queue: LogQueue) -> Result<()>;
fn file_span(&self, queue: LogQueue) -> (FileSeq, FileSeq);
fn file_at(&self, queue: LogQueue, mut position: f64) -> FileSeq {
if position > 1.0 {
position = 1.0;
} else if position < 0.0 {
position = 0.0;
}
let (first, active) = self.file_span(queue);
let count = active - first + 1;
first + (count as f64 * position) as u64
}
fn total_size(&self, queue: LogQueue) -> usize;
fn rotate(&self, queue: LogQueue) -> Result<()>;
fn purge_to(&self, file_id: FileId) -> Result<usize>;
}