pigeonhole-db 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +16 -16
  3. data/Cargo.toml +5 -5
  4. data/lib/pigeonhole/version.rb +1 -1
  5. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/IO_Event.so +0 -0
  6. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/gem_make.out +6 -6
  7. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/mkmf.log +26 -26
  8. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/gem_make.out +6 -6
  9. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/json/ext/generator.so +0 -0
  10. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/json/ext/parser.so +0 -0
  11. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/mkmf.log +19 -19
  12. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/gem_make.out +9 -9
  13. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/mkmf.log +6 -6
  14. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/prism/prism.so +0 -0
  15. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/gem_make.out +6 -6
  16. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/mkmf.log +7 -7
  17. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/rbs_extension.so +0 -0
  18. data/vendor/bundle/ruby/3.4.0/gems/io-event-1.22.3/ext/Makefile +3 -3
  19. data/vendor/bundle/ruby/3.4.0/gems/io-event-1.22.3/lib/IO_Event.so +0 -0
  20. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/ext/json/ext/generator/Makefile +3 -3
  21. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/ext/json/ext/parser/Makefile +3 -3
  22. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/lib/json/ext/generator.so +0 -0
  23. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/lib/json/ext/parser.so +0 -0
  24. data/vendor/bundle/ruby/3.4.0/gems/prism-1.9.0/ext/prism/Makefile +3 -3
  25. data/vendor/bundle/ruby/3.4.0/gems/prism-1.9.0/lib/prism/prism.so +0 -0
  26. data/vendor/bundle/ruby/3.4.0/gems/rbs-4.2.0/ext/rbs_extension/Makefile +3 -3
  27. data/vendor/bundle/ruby/3.4.0/gems/rbs-4.2.0/lib/rbs_extension.so +0 -0
  28. data/vendor/cache/Cargo.toml +2 -2
  29. data/vendor/cache/src/lib.rs +37 -0
  30. data/vendor/cache/src/loom_tests.rs +32 -0
  31. data/vendor/cache/src/s3fifo.rs +239 -11
  32. data/vendor/cache/src/sync.rs +4 -4
  33. data/vendor/compaction/Cargo.toml +6 -6
  34. data/vendor/compaction/src/cursor.rs +86 -4
  35. data/vendor/compaction/src/job.rs +24 -2
  36. data/vendor/engine/Cargo.toml +11 -11
  37. data/vendor/engine/src/admission.rs +3 -1
  38. data/vendor/engine/src/catalog.rs +39 -4
  39. data/vendor/engine/src/check.rs +59 -20
  40. data/vendor/engine/src/compact.rs +2 -2
  41. data/vendor/engine/src/engine/hooks.rs +144 -2
  42. data/vendor/engine/src/engine/info.rs +6 -0
  43. data/vendor/engine/src/engine.rs +1492 -50
  44. data/vendor/engine/src/external_registry.rs +447 -0
  45. data/vendor/engine/src/feed.rs +313 -84
  46. data/vendor/engine/src/feed_reader/catalog.rs +490 -0
  47. data/vendor/engine/src/feed_reader/history.rs +236 -0
  48. data/vendor/engine/src/feed_reader/read_ahead.rs +241 -0
  49. data/vendor/engine/src/feed_reader/token.rs +232 -0
  50. data/vendor/engine/src/feed_reader.rs +970 -852
  51. data/vendor/engine/src/feed_stop_loom.rs +73 -0
  52. data/vendor/engine/src/flush.rs +29 -4
  53. data/vendor/engine/src/io_count.rs +190 -0
  54. data/vendor/engine/src/large.rs +4 -1
  55. data/vendor/engine/src/lib.rs +16 -2
  56. data/vendor/engine/src/maintenance.rs +2 -2
  57. data/vendor/engine/src/manifest.rs +38 -6
  58. data/vendor/engine/src/nonblocking.rs +3 -3
  59. data/vendor/engine/src/options.rs +26 -0
  60. data/vendor/engine/src/punch.rs +282 -0
  61. data/vendor/engine/src/range_copy.rs +365 -0
  62. data/vendor/engine/src/read/keys.rs +37 -5
  63. data/vendor/engine/src/read/reduce.rs +1 -0
  64. data/vendor/engine/src/read.rs +7 -0
  65. data/vendor/engine/src/row_cache.rs +24 -0
  66. data/vendor/engine/src/shard/tablets.rs +127 -31
  67. data/vendor/engine/src/shard.rs +1006 -33
  68. data/vendor/engine/src/source.rs +105 -12
  69. data/vendor/engine/src/write.rs +310 -4
  70. data/vendor/format/Cargo.toml +1 -1
  71. data/vendor/format/src/shm.rs +10 -0
  72. data/vendor/format/src/superblock.rs +9 -2
  73. data/vendor/format/src/wal.rs +818 -16
  74. data/vendor/io/Cargo.toml +3 -3
  75. data/vendor/io/src/count.rs +76 -0
  76. data/vendor/io/src/file.rs +70 -0
  77. data/vendor/io/src/lib.rs +2 -0
  78. data/vendor/io/src/os/unix.rs +124 -0
  79. data/vendor/io/src/os/windows.rs +51 -0
  80. data/vendor/io/src/pread.rs +26 -0
  81. data/vendor/io/src/sim.rs +81 -9
  82. data/vendor/io/src/sys.rs +53 -0
  83. data/vendor/io/src/uring.rs +28 -0
  84. data/vendor/io/src/vfs.rs +4 -0
  85. data/vendor/memory/Cargo.toml +4 -4
  86. data/vendor/memory/src/hooks.rs +6 -0
  87. data/vendor/memory/src/ivf.rs +67 -1
  88. data/vendor/memory/src/lib.rs +4 -1
  89. data/vendor/memory/src/recall/pool.rs +872 -0
  90. data/vendor/memory/src/recall.rs +351 -273
  91. data/vendor/memory/src/store.rs +15 -0
  92. data/vendor/memtable/Cargo.toml +3 -3
  93. data/vendor/pager/Cargo.toml +3 -3
  94. data/vendor/pager/src/alloc.rs +430 -0
  95. data/vendor/pager/src/lib.rs +114 -0
  96. data/vendor/pigeonhole/Cargo.toml +6 -5
  97. data/vendor/pigeonhole/src/db.rs +91 -2
  98. data/vendor/pigeonhole/src/error.rs +2 -1
  99. data/vendor/pigeonhole/src/feed.rs +210 -6
  100. data/vendor/pigeonhole/src/lib.rs +3 -0
  101. data/vendor/pigeonhole/src/metrics.rs +37 -0
  102. data/vendor/pigeonhole/src/nonblocking.rs +94 -111
  103. data/vendor/pigeonhole/src/options.rs +69 -0
  104. data/vendor/pigeonhole/src/replication.rs +601 -11
  105. data/vendor/pigeonhole/src/write.rs +81 -0
  106. data/vendor/runtime/Cargo.toml +2 -2
  107. data/vendor/shm/Cargo.toml +3 -3
  108. data/vendor/shm/src/region.rs +20 -2
  109. data/vendor/sst/Cargo.toml +4 -4
  110. data/vendor/sst/src/blob.rs +85 -7
  111. data/vendor/sst/src/iter.rs +98 -2
  112. data/vendor/sst/src/lib.rs +11 -2
  113. data/vendor/sst/src/writer.rs +12 -2
  114. data/vendor/text/Cargo.toml +2 -2
  115. data/vendor/text/src/block.rs +638 -0
  116. data/vendor/text/src/error.rs +8 -3
  117. data/vendor/text/src/fuzzy.rs +1 -1
  118. data/vendor/text/src/index.rs +1055 -80
  119. data/vendor/text/src/layout.rs +63 -2
  120. data/vendor/text/src/lib.rs +5 -1
  121. data/vendor/text/src/maxscore.rs +481 -94
  122. data/vendor/text/src/merged.rs +470 -0
  123. data/vendor/text/src/search.rs +1278 -341
  124. data/vendor/text/src/stats.rs +431 -0
  125. data/vendor/vector/Cargo.toml +2 -2
  126. data/vendor/vector/src/hooks.rs +33 -4
  127. data/vendor/vector/src/index.rs +498 -69
  128. data/vendor/wal/Cargo.toml +3 -3
  129. data/vendor/wal/src/feed.rs +313 -19
  130. data/vendor/wal/src/recovery.rs +31 -1
  131. data/vendor/wal/src/stream.rs +28 -0
  132. metadata +15 -1
@@ -0,0 +1,490 @@
1
+ use super::*;
2
+
3
+ /// A catalog change a feed of every table delivers (D315): one manifest commit's catalog
4
+ /// edits, from its `Catalog` sidecar entry.
5
+ #[derive(Debug, Clone, Default, PartialEq, Eq)]
6
+ #[non_exhaustive]
7
+ pub struct CatalogEvent {
8
+ /// The commit's `seqno_ceiling`: every commit below it comes before the change, every
9
+ /// commit at or above it after.
10
+ pub seqno_ceiling: Seqno,
11
+ /// The commit's `ts_floor`: every default timestamp before the change is at most this.
12
+ pub timestamp: u64,
13
+ /// The root commit's sequence: it orders catalog changes, and a token records the last
14
+ /// one delivered.
15
+ pub sequence: u64,
16
+ /// Its edits, in the order they applied: `CreateTable`, `PutFamily`, `DropTable`.
17
+ pub edits: Vec<pigeonhole_format::manifest::Edit>,
18
+ /// The name of each table the edits name without a `CreateTable` among them, as the
19
+ /// catalog held it before them: every table an event names is named in the event.
20
+ pub names: Vec<(TableId, String)>,
21
+ }
22
+
23
+ /// Whether a `Catalog` sidecar entry's root landed, as the entries after it say (D315
24
+ /// amendment): its Root entry follows (`Yes`), its intent follows or nothing does yet
25
+ /// (`Pending`: read it again next time), or something else follows (`Never`).
26
+ #[derive(Clone, Copy, PartialEq, Eq)]
27
+ pub(super) enum Rooted {
28
+ Yes,
29
+ Pending,
30
+ Never,
31
+ }
32
+
33
+ /// Whether `catalog` reflects every one of a catalog change's `edits`. Ids only grow and are
34
+ /// never reused, and an id is counted only once its change is published: a created table or
35
+ /// family is reflected once the counter passed its id, a drop once the table is gone and its
36
+ /// id was counted (a catalog from before the table's create lacks it too: feed1248's S1 on
37
+ /// #1870). That holds for a prefix of the changes, checked in order, and for each change on
38
+ /// its own.
39
+ pub(super) fn reflects(
40
+ catalog: &crate::catalog::Catalog,
41
+ edits: &[pigeonhole_format::manifest::Edit],
42
+ ) -> bool {
43
+ use pigeonhole_format::manifest::Edit;
44
+ edits.iter().all(|e| match e {
45
+ Edit::CreateTable { table, .. } => table.0 < catalog.counters.next_table,
46
+ Edit::PutFamily { family, .. } => family.0 < catalog.counters.next_family,
47
+ Edit::DropTable { table } => {
48
+ table.0 < catalog.counters.next_table && catalog.table(*table).is_none()
49
+ }
50
+ _ => true,
51
+ })
52
+ }
53
+
54
+ /// The catalog at a feed of every table's position, as its token carries it (D325): an
55
+ /// order-independent hash of its table ids and its id counters. The catalog changes a feed
56
+ /// records are a table created with its families, a family added and a table dropped
57
+ /// (D315). Ids are never reused and the counters only rise, so a create or an added family
58
+ /// no change recorded raises a counter past what the recorded ones account for, and a drop
59
+ /// no change recorded changes the set. The replica's state table (reserved, D315 PR 3) is
60
+ /// left out: it comes and goes without a catalog change.
61
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
62
+ pub(super) struct CatalogModel {
63
+ /// The wrapping sum of xxh3-64 over each table id's little-endian bytes.
64
+ pub(super) tables: u64,
65
+ pub(super) next_table: u32,
66
+ pub(super) next_family: u32,
67
+ }
68
+
69
+ impl CatalogModel {
70
+ /// The bytes it adds to a token.
71
+ pub(super) const LEN: usize = 16;
72
+
73
+ pub(super) fn table_hash(table: TableId) -> u64 {
74
+ pigeonhole_format::checksum::xxh3_64(&table.0.to_le_bytes())
75
+ }
76
+
77
+ pub(super) fn of(catalog: &crate::catalog::Catalog) -> Self {
78
+ let tables = catalog
79
+ .tables()
80
+ .map(|t| t.id)
81
+ .filter(|t| !crate::engine::is_reserved_table(*t))
82
+ .fold(0u64, |h, t| h.wrapping_add(Self::table_hash(t)));
83
+ Self {
84
+ tables,
85
+ next_table: catalog.counters.next_table,
86
+ next_family: catalog.counters.next_family,
87
+ }
88
+ }
89
+
90
+ /// Moves it past a catalog change.
91
+ pub(super) fn apply(&mut self, edits: &[pigeonhole_format::manifest::Edit]) {
92
+ use pigeonhole_format::manifest::Edit;
93
+ for e in edits {
94
+ match e {
95
+ Edit::CreateTable { table, .. } if !crate::engine::is_reserved_table(*table) => {
96
+ self.tables = self.tables.wrapping_add(Self::table_hash(*table));
97
+ self.next_table = self.next_table.max(table.0 + 1);
98
+ }
99
+ Edit::PutFamily { family, .. } => {
100
+ self.next_family = self.next_family.max(family.0 + 1);
101
+ }
102
+ Edit::DropTable { table } if !crate::engine::is_reserved_table(*table) => {
103
+ self.tables = self.tables.wrapping_sub(Self::table_hash(*table));
104
+ }
105
+ _ => {}
106
+ }
107
+ }
108
+ }
109
+
110
+ pub(super) fn encode(&self, out: &mut Vec<u8>) {
111
+ out.extend_from_slice(&self.tables.to_le_bytes());
112
+ out.extend_from_slice(&self.next_table.to_le_bytes());
113
+ out.extend_from_slice(&self.next_family.to_le_bytes());
114
+ }
115
+
116
+ /// One number for the whole model (#1853 part C2): xxh3-64 of its encoding. A replica
117
+ /// compares its own catalog's with its primary's feed's at the same position.
118
+ pub(super) fn digest(&self) -> u64 {
119
+ let mut b = [0u8; Self::LEN];
120
+ b[..8].copy_from_slice(&self.tables.to_le_bytes());
121
+ b[8..12].copy_from_slice(&self.next_table.to_le_bytes());
122
+ b[12..].copy_from_slice(&self.next_family.to_le_bytes());
123
+ pigeonhole_format::checksum::xxh3_64(&b)
124
+ }
125
+
126
+ pub(super) fn decode(b: &[u8]) -> Self {
127
+ Self {
128
+ tables: u64::from_le_bytes(b[..8].try_into().unwrap()),
129
+ next_table: u32::from_le_bytes(b[8..12].try_into().unwrap()),
130
+ next_family: u32::from_le_bytes(b[12..16].try_into().unwrap()),
131
+ }
132
+ }
133
+ }
134
+
135
+ /// The digest of `catalog`'s model (#1853 part C2): what [`FeedReader::catalog_digest`] says
136
+ /// at the same position.
137
+ pub(crate) fn catalog_digest_of(catalog: &crate::catalog::Catalog) -> u64 {
138
+ CatalogModel::of(catalog).digest()
139
+ }
140
+
141
+ /// The catalog model at a feed of every table's start, worked out at open from the catalog
142
+ /// it sees there and the sidecar's catalog changes (D325). `v` is loaded before the sidecar
143
+ /// is read, so it reflects a prefix of the changes, all with their entries in the file.
144
+ /// Changes after the start that `v` reflects are taken back out; changes before the start
145
+ /// that it doesn't reflect yet (a reader process's view can lag, or a change can be
146
+ /// appended but not yet published) are put in.
147
+ pub(super) struct Derive {
148
+ pub(super) v: Arc<crate::catalog::Catalog>,
149
+ /// `v`'s model, with the changes before the start it lacks put in.
150
+ pub(super) model: CatalogModel,
151
+ /// Whether the changes after the start seen so far are all reflected by `v`.
152
+ pub(super) reflecting: bool,
153
+ /// The smallest table and family ids a change after the start that `v` reflects
154
+ /// created, and the largest: the counters at the start were at most the smallest, and
155
+ /// the live ones are one past the largest at least.
156
+ pub(super) created_tables: Option<(u32, u32)>,
157
+ pub(super) created_families: Option<(u32, u32)>,
158
+ /// A token's last catalog change: its model holds those up to it and no later one. A
159
+ /// later change its bound passes over (one appended after the token was taken) is
160
+ /// compared as after the start, then put in.
161
+ pub(super) known: Option<u64>,
162
+ pub(super) late: Vec<Vec<pigeonhole_format::manifest::Edit>>,
163
+ }
164
+
165
+ impl Derive {
166
+ pub(super) fn new(v: Arc<crate::catalog::Catalog>, known: Option<u64>) -> Self {
167
+ let model = CatalogModel::of(&v);
168
+ Self {
169
+ v,
170
+ model,
171
+ reflecting: true,
172
+ created_tables: None,
173
+ created_families: None,
174
+ known,
175
+ late: Vec::new(),
176
+ }
177
+ }
178
+
179
+ /// A change the start passes over: before the start, unless a token's model can't hold
180
+ /// it yet.
181
+ pub(super) fn passed_over(
182
+ &mut self,
183
+ sequence: u64,
184
+ edits: &[pigeonhole_format::manifest::Edit],
185
+ ) {
186
+ if self.known.is_some_and(|k| sequence > k) {
187
+ self.after(edits);
188
+ self.late.push(edits.to_vec());
189
+ } else {
190
+ self.before(edits);
191
+ }
192
+ }
193
+
194
+ /// A change before the start.
195
+ pub(super) fn before(&mut self, edits: &[pigeonhole_format::manifest::Edit]) {
196
+ if !reflects(&self.v, edits) {
197
+ self.model.apply(edits);
198
+ }
199
+ }
200
+
201
+ /// A change after the start, in order (D315: reflected as a prefix).
202
+ pub(super) fn after(&mut self, edits: &[pigeonhole_format::manifest::Edit]) {
203
+ use pigeonhole_format::manifest::Edit;
204
+ self.reflecting = self.reflecting && reflects(&self.v, edits);
205
+ if !self.reflecting {
206
+ return;
207
+ }
208
+ let widen = |r: &mut Option<(u32, u32)>, id: u32| {
209
+ *r = Some(r.map_or((id, id), |(lo, hi)| (lo.min(id), hi.max(id))));
210
+ };
211
+ for e in edits {
212
+ match e {
213
+ Edit::CreateTable { table, .. } if !crate::engine::is_reserved_table(*table) => {
214
+ self.model.tables = self
215
+ .model
216
+ .tables
217
+ .wrapping_sub(CatalogModel::table_hash(*table));
218
+ widen(&mut self.created_tables, table.0);
219
+ }
220
+ Edit::PutFamily { family, .. } => widen(&mut self.created_families, family.0),
221
+ Edit::DropTable { table } if !crate::engine::is_reserved_table(*table) => {
222
+ self.model.tables = self
223
+ .model
224
+ .tables
225
+ .wrapping_add(CatalogModel::table_hash(*table));
226
+ }
227
+ _ => {}
228
+ }
229
+ }
230
+ }
231
+
232
+ /// The model at the start: `claim`'s (a version 3 token's), if the catalog agrees with
233
+ /// it, else the reason it doesn't; with no claim, the one worked out here.
234
+ pub(super) fn finish(
235
+ mut self,
236
+ claim: Option<CatalogModel>,
237
+ ) -> std::result::Result<CatalogModel, String> {
238
+ let late = std::mem::take(&mut self.late);
239
+ let mut model = self.check(claim)?;
240
+ for edits in &late {
241
+ model.apply(edits);
242
+ }
243
+ Ok(model)
244
+ }
245
+
246
+ pub(super) fn check(
247
+ self,
248
+ claim: Option<CatalogModel>,
249
+ ) -> std::result::Result<CatalogModel, String> {
250
+ let live = self.model;
251
+ let Some(claim) = claim else {
252
+ return Ok(CatalogModel {
253
+ tables: live.tables,
254
+ next_table: self
255
+ .created_tables
256
+ .map_or(live.next_table, |(lo, _)| lo.min(live.next_table)),
257
+ next_family: self
258
+ .created_families
259
+ .map_or(live.next_family, |(lo, _)| lo.min(live.next_family)),
260
+ });
261
+ };
262
+ let expect =
263
+ |at: u32, created: Option<(u32, u32)>| created.map_or(at, |(_, hi)| at.max(hi + 1));
264
+ if live.tables != claim.tables {
265
+ return Err(
266
+ "the catalog's tables aren't the token's with the recorded changes \
267
+ since: a table was created or dropped"
268
+ .into(),
269
+ );
270
+ }
271
+ if live.next_table != expect(claim.next_table, self.created_tables) {
272
+ return Err("a table was created".into());
273
+ }
274
+ if live.next_family != expect(claim.next_family, self.created_families) {
275
+ return Err("a family was added".into());
276
+ }
277
+ Ok(claim)
278
+ }
279
+ }
280
+
281
+ impl FeedReader {
282
+ /// A feed of every table (D315): reads the sidecar's new entries and queues the catalog
283
+ /// changes of the feed's history not yet delivered. Called after the frontier F is
284
+ /// loaded: the catalog hold keeps F below a change's ceiling until its entry is appended,
285
+ /// so every change with a ceiling at most F + 1 is in the file by then. At open, `start`
286
+ /// is the catalog a `Now` or `Snapshot` start reflects (the view's, loaded before the
287
+ /// sidecar was read, so no later entry can be reflected): the changes it reflects are
288
+ /// before the start, beside the reader's `catalog_bound` for `Oldest` and `After`. A
289
+ /// token says the last change delivered instead. A passed-over or delivered change's
290
+ /// drops are kept, so a dropped table's later changes are passed over.
291
+ pub(super) fn load_catalog(&mut self, start: Option<&crate::catalog::Catalog>) -> Result<()> {
292
+ // At open, the catalog model at the start is worked out from this first load (D325)
293
+ // and checked against a version 3 token's.
294
+ // A token's start knows the changes up to its last one (`catalog_seq`, set from it).
295
+ let known = self.claim_known;
296
+ let mut derive = self.deriving.take().map(|v| Derive::new(v, known));
297
+ let loaded = self.load_catalog_entries(start, &mut derive);
298
+ if let Some(d) = derive {
299
+ match d.finish(self.claim.take()) {
300
+ Ok(model) => self.model = Some(model),
301
+ Err(why) => {
302
+ if self.expired.is_none() {
303
+ self.expired = Some(expiry(format!(
304
+ "the catalog changed since the token without a record in the \
305
+ feed ({why}): a session that records no catalog changes (a \
306
+ 1.1.x build's, after a rollback) changed the tables, so the feed \
307
+ can't deliver those changes"
308
+ )));
309
+ }
310
+ }
311
+ }
312
+ }
313
+ loaded
314
+ }
315
+
316
+ /// [`load_catalog`](Self::load_catalog)'s read of the sidecar, each catalog change also
317
+ /// given to `derive` at open.
318
+ pub(super) fn load_catalog_entries(
319
+ &mut self,
320
+ start: Option<&crate::catalog::Catalog>,
321
+ derive: &mut Option<Derive>,
322
+ ) -> Result<()> {
323
+ // Before the file is read: a change counted after this is loaded next time.
324
+ if let Some(feed) = &self.writer {
325
+ self.catalog_roots_seen = feed.catalog_roots();
326
+ }
327
+ // Opened once and kept: an idle reader process's poll then costs one length check
328
+ // (simp724's N1 on #1539). The sidecar is only ever appended to, cut or overwritten in
329
+ // place, never replaced by another file.
330
+ let file = match &self.sidecar_file {
331
+ Some(f) => Arc::clone(f),
332
+ None => {
333
+ let path = crate::feed::sidecar_path(&self.shared.path);
334
+ match self.shared.vfs.open(&path, OpenOptions::read()) {
335
+ Ok(f) => {
336
+ self.sidecar_file = Some(Arc::clone(&f));
337
+ f
338
+ }
339
+ Err(e) if e.kind == pigeonhole_io::ErrorKind::NotFound => return Ok(()),
340
+ Err(e) => return Err(e.into()),
341
+ }
342
+ }
343
+ };
344
+ let len = file.len()?;
345
+ let from = self.sidecar_pos.max(FeedHeader::LEN as u64);
346
+ if len <= from {
347
+ return Ok(());
348
+ }
349
+ let mut bytes = vec![0u8; (len - from) as usize];
350
+ file.read_at(&mut bytes, from)?;
351
+ if self.sidecar_pos == 0 {
352
+ let mut head = [0u8; FeedHeader::LEN];
353
+ file.read_at(&mut head, 0)?;
354
+ match FeedHeader::decode(&head) {
355
+ Ok(h) if h.db_id == self.shared.pager.db_id() => {}
356
+ _ => return Ok(()),
357
+ }
358
+ }
359
+ // Up to the first torn entry: the rest is read again once it's whole.
360
+ let (entries, used) = decode_entries_at(&bytes);
361
+ self.sidecar_pos = from + used as u64;
362
+ // Every create and drop the file records, whatever its state (#1853): what explains
363
+ // a table missing from the catalog. Entries past a pending one are read again next
364
+ // time; the sets don't mind.
365
+ for (_, e) in &entries {
366
+ if let FeedEntry::Catalog { edits, .. } = e {
367
+ for edit in edits {
368
+ match edit {
369
+ pigeonhole_format::manifest::Edit::DropTable { table } => {
370
+ self.recorded_drops.insert(*table);
371
+ }
372
+ pigeonhole_format::manifest::Edit::CreateTable { table, .. } => {
373
+ self.recorded_creates.insert(*table);
374
+ }
375
+ _ => {}
376
+ }
377
+ }
378
+ }
379
+ }
380
+ // A catalog change counts once its root commit's Root entry follows it (D315
381
+ // amendment): its entry is appended with the commit's intent, before the commit
382
+ // writes, and a commit that then fails is cut off at the next open, so until the
383
+ // Root is there, it may never have happened. Read from it again next time.
384
+ // A Catalog entry with neither its Root nor its own intent after it can never get its
385
+ // Root: its commit didn't land, and no open resolved it (simp724's F1 on #1526).
386
+ // Waiting there would deliver later commits without the changes after it: expire.
387
+ let rooted: Vec<Rooted> = (0..entries.len())
388
+ .map(
389
+ |i| match (&entries[i].1, entries.get(i + 1).map(|e| &e.1)) {
390
+ (
391
+ FeedEntry::Catalog {
392
+ session, sequence, ..
393
+ },
394
+ Some(FeedEntry::Root {
395
+ session: s,
396
+ sequence: q,
397
+ }),
398
+ ) if s == session && q == sequence => Rooted::Yes,
399
+ (
400
+ FeedEntry::Catalog { sequence, .. },
401
+ Some(FeedEntry::RootIntent { sequence: q, .. }),
402
+ ) if q == sequence => Rooted::Pending,
403
+ // The end of what's whole so far: its intent may still be on its way.
404
+ (FeedEntry::Catalog { .. }, None) => Rooted::Pending,
405
+ _ => Rooted::Never,
406
+ },
407
+ )
408
+ .collect();
409
+ // The start's catalog reflects a prefix of the changes, in order: once one isn't
410
+ // reflected, no later one is.
411
+ let mut reflecting = true;
412
+ for (i, (at, e)) in entries.into_iter().enumerate() {
413
+ let FeedEntry::Catalog {
414
+ session,
415
+ sequence,
416
+ seqno_ceiling,
417
+ timestamp,
418
+ edits,
419
+ names,
420
+ } = e
421
+ else {
422
+ continue;
423
+ };
424
+ if !self.history.holds(session) {
425
+ continue;
426
+ }
427
+ match rooted[i] {
428
+ Rooted::Yes => {}
429
+ Rooted::Pending => {
430
+ // It may be published already, and seen by the start (D325).
431
+ if let Some(d) = derive {
432
+ d.after(&edits);
433
+ }
434
+ self.sidecar_pos = from + at as u64;
435
+ break;
436
+ }
437
+ Rooted::Never => {
438
+ self.expired = Some(expiry(format!(
439
+ "the catalog change of root {sequence} never got its Root entry, and \
440
+ a later entry follows it: later catalog changes can't be delivered"
441
+ )));
442
+ break;
443
+ }
444
+ }
445
+ let before_start = seqno_ceiling <= self.catalog_bound
446
+ || match start {
447
+ Some(catalog) => {
448
+ reflecting = reflecting && reflects(catalog, &edits);
449
+ reflecting
450
+ }
451
+ None => sequence <= self.catalog_seq,
452
+ };
453
+ if before_start {
454
+ // Before the position: in its catalog model (D325).
455
+ match (derive.as_mut(), self.model.as_mut()) {
456
+ (Some(d), _) => d.passed_over(sequence, &edits),
457
+ (None, Some(m)) => m.apply(&edits),
458
+ (None, None) => {}
459
+ }
460
+ self.note_drops(&edits);
461
+ self.catalog_seq = self.catalog_seq.max(sequence);
462
+ self.raise_session(session);
463
+ continue;
464
+ }
465
+ if let Some(d) = derive {
466
+ d.after(&edits);
467
+ }
468
+ self.catalog.push_back((
469
+ CatalogEvent {
470
+ seqno_ceiling,
471
+ timestamp,
472
+ sequence,
473
+ edits,
474
+ names,
475
+ },
476
+ session,
477
+ ));
478
+ }
479
+ Ok(())
480
+ }
481
+
482
+ /// The tables `edits` drop: their later changes are passed over.
483
+ pub(super) fn note_drops(&mut self, edits: &[pigeonhole_format::manifest::Edit]) {
484
+ for e in edits {
485
+ if let pigeonhole_format::manifest::Edit::DropTable { table } = e {
486
+ self.dropped_tables.insert(*table);
487
+ }
488
+ }
489
+ }
490
+ }