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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//! Positions, write tickets and durability receipts.
//!
//! These are the values a caller cannot forge: every constructor is private
//! to this crate. A position comes only from a region (checked against its
//! bounds and alignment), a ticket only from a submitted write, and a receipt
//! only from a completed durable write or a successful barrier.
//!
//! A ticket records the first device flush that can cover its write: the
//! flush issued after the write completed. A receipt records the flush its
//! barrier waited for. The receipt proves the ticket's write durable exactly
//! when the ticket's flush number is not later than the receipt's, which is
//! the join rule the flush domain enforces.
//!
//! # Compile-time guarantees
//!
//! The valid forms compile:
//!
//! ```
//! # use std::path::Path;
//! # use store_io_engine::{Store, StoreOptions};
//! # use store_io_sim::{SimConfig, SimPlatform};
//! # let store = Store::create(SimPlatform::new(SimConfig::volatile(1)), Path::new("/db"), StoreOptions::default()).unwrap();
//! # let pages = store.provision_page_region("p", 1 << 20).unwrap();
//! # let wal = store.provision_append_region("w", 1 << 20).unwrap();
//! let pos = pages.pos(4096)?; // a checked position
//! let ticket = pages.write(pos, &[0u8; 4096])?; // a ticket per write
//! let receipt = pages.sync_through(&ticket)?; // a receipt per barrier
//! assert!(receipt.covers(&ticket));
//! let appended = wal.append(b"x")?;
//! let _durable = wal.sync_through(&appended)?;
//! # Ok::<(), store_io_core::error::Error>(())
//! ```
//!
//! An integer is never a position:
//!
//! ```compile_fail,E0308
//! # use std::path::Path;
//! # use store_io_engine::{Store, StoreOptions};
//! # use store_io_sim::{SimConfig, SimPlatform};
//! # let store = Store::create(SimPlatform::new(SimConfig::volatile(1)), Path::new("/db"), StoreOptions::default()).unwrap();
//! # let pages = store.provision_page_region("p", 1 << 20).unwrap();
//! let _ticket = pages.write(4096, &[0u8; 4096]);
//! ```
//!
//! A position cannot be built by hand:
//!
//! ```compile_fail,E0451
//! # use store_io_engine::RegionPos;
//! let _pos = RegionPos { offset: 4096 };
//! ```
//!
//! A receipt cannot be forged:
//!
//! ```compile_fail,E0451
//! # use store_io_engine::DurableReceipt;
//! let _receipt = DurableReceipt { through: u64::MAX };
//! ```
//!
//! A ticket is not a position, and a receipt is not a ticket:
//!
//! ```compile_fail,E0308
//! # use std::path::Path;
//! # use store_io_engine::{Store, StoreOptions};
//! # use store_io_sim::{SimConfig, SimPlatform};
//! # let store = Store::create(SimPlatform::new(SimConfig::volatile(1)), Path::new("/db"), StoreOptions::default()).unwrap();
//! # let pages = store.provision_page_region("p", 1 << 20).unwrap();
//! let ticket = pages.write(pages.pos(0).unwrap(), &[0u8; 4096]).unwrap();
//! let _again = pages.write(ticket, &[0u8; 4096]);
//! ```
//!
//! ```compile_fail,E0308
//! # use std::path::Path;
//! # use store_io_engine::{Store, StoreOptions};
//! # use store_io_sim::{SimConfig, SimPlatform};
//! # let store = Store::create(SimPlatform::new(SimConfig::volatile(1)), Path::new("/db"), StoreOptions::default()).unwrap();
//! # let wal = store.provision_append_region("w", 1 << 20).unwrap();
//! let (_pos, receipt) = wal.append_durable(b"x").unwrap();
//! let _again = wal.sync_through(&receipt);
//! ```
//!
//! A ticket stands for one write and cannot be copied:
//!
//! ```compile_fail,E0599
//! # use std::path::Path;
//! # use store_io_engine::{Store, StoreOptions};
//! # use store_io_sim::{SimConfig, SimPlatform};
//! # let store = Store::create(SimPlatform::new(SimConfig::volatile(1)), Path::new("/db"), StoreOptions::default()).unwrap();
//! # let wal = store.provision_append_region("w", 1 << 20).unwrap();
//! let ticket = wal.append(b"x").unwrap();
//! let _copy = ticket.clone();
//! ```
//!
//! Dropping a ticket unused is flagged (here made an error):
//!
//! ```compile_fail
//! #![deny(unused_must_use)]
//! # use std::path::Path;
//! # use store_io_engine::{Store, StoreOptions};
//! # use store_io_sim::{SimConfig, SimPlatform};
//! # fn main() -> Result<(), store_io_core::error::Error> {
//! # let store = Store::create(SimPlatform::new(SimConfig::volatile(1)), Path::new("/db"), StoreOptions::default())?;
//! # let wal = store.provision_append_region("w", 1 << 20)?;
//! wal.append(b"x")?;
//! # Ok(())
//! # }
//! ```
use fmt;
use ;
use ;
/// A byte position inside one generation of one region.
///
/// Created by [`crate::PageRegion::pos`] or returned by appends. A position
/// from another region, or from a generation that has since been recycled,
/// is rejected before any I/O.
/// Proof that a write was submitted and completed. Barrier it to make it
/// durable. Not `Clone`: one ticket per write.
/// Unforgeable proof that bytes are durable.
///
/// Its existence means every byte it covers is durable at [`Self::class`] on
/// this device, under the device's documented fault model. The label says
/// why that class is trusted.