| 1 | //! A streaming port of `superfly/ltx` v0.5.2 `Compactor`. |
| 2 | //! |
| 3 | //! The compactor keeps one decompressed page per input. It does not build a |
| 4 | //! database-sized page map. Inputs must be ordered by transaction range. |
| 5 | |
| 6 | use crate::codec::{Decoder, Encoder}; |
| 7 | use crate::error::{Error, Result}; |
| 8 | use crate::ltx::{Header, PageHeader, Trailer, VERSION}; |
| 9 | use crate::TXID; |
| 10 | use std::io::{Read, Write}; |
| 11 | use std::sync::atomic::{AtomicU32, Ordering}; |
| 12 | |
| 13 | /// The current progress of a compaction. |
| 14 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 15 | pub struct CompactorStatus { |
| 16 | /// The last page number processed by the merge. |
| 17 | pub page_number: u32, |
| 18 | /// The final input's commit page count. |
| 19 | pub total: u32, |
| 20 | } |
| 21 | |
| 22 | impl CompactorStatus { |
| 23 | pub fn is_zero(self) -> bool { |
| 24 | self.page_number == 0 && self.total == 0 |
| 25 | } |
| 26 | |
| 27 | pub fn fraction(self) -> f64 { |
| 28 | if self.total == 0 { |
| 29 | 0.0 |
| 30 | } else { |
| 31 | f64::from(self.page_number) / f64::from(self.total) |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /// Merges ordered LTX inputs into one byte-compatible LTX output. |
| 37 | pub struct Compactor<W, R> { |
| 38 | encoder: Encoder<W>, |
| 39 | inputs: Vec<CompactorInput<R>>, |
| 40 | page_number: AtomicU32, |
| 41 | total: AtomicU32, |
| 42 | |
| 43 | /// Flags to set on the compacted LTX header. |
| 44 | pub header_flags: u32, |
| 45 | /// Allows gaps in the ordered input transaction ranges. |
| 46 | pub allow_non_contiguous_txids: bool, |
| 47 | } |
| 48 | |
| 49 | impl<W: Write, R: Read> Compactor<W, R> { |
| 50 | pub fn new(writer: W, readers: Vec<R>) -> Self { |
| 51 | Self { |
| 52 | encoder: Encoder::new_block(writer), |
| 53 | inputs: readers |
| 54 | .into_iter() |
| 55 | .map(|reader| CompactorInput { |
| 56 | decoder: Decoder::new(reader), |
| 57 | page: None, |
| 58 | data: Vec::new(), |
| 59 | }) |
| 60 | .collect(), |
| 61 | page_number: AtomicU32::new(0), |
| 62 | total: AtomicU32::new(0), |
| 63 | header_flags: 0, |
| 64 | allow_non_contiguous_txids: false, |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | pub fn header(&self) -> Header { |
| 69 | self.encoder.header |
| 70 | } |
| 71 | |
| 72 | pub fn trailer(&self) -> Trailer { |
| 73 | self.encoder.trailer |
| 74 | } |
| 75 | |
| 76 | pub fn status(&self) -> CompactorStatus { |
| 77 | CompactorStatus { |
| 78 | page_number: self.page_number.load(Ordering::Relaxed), |
| 79 | total: self.total.load(Ordering::Relaxed), |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | pub fn writer(&self) -> &W { |
| 84 | &self.encoder.writer |
| 85 | } |
| 86 | |
| 87 | pub fn writer_mut(&mut self) -> &mut W { |
| 88 | &mut self.encoder.writer |
| 89 | } |
| 90 | |
| 91 | pub fn into_writer(self) -> W { |
| 92 | self.encoder.writer |
| 93 | } |
| 94 | |
| 95 | pub fn compact(&mut self) -> Result<()> { |
| 96 | if self.inputs.is_empty() { |
| 97 | return Err(Error::LTXCorrupted); |
| 98 | } |
| 99 | |
| 100 | for input in &mut self.inputs { |
| 101 | input.decoder.decode_header()?; |
| 102 | } |
| 103 | |
| 104 | for index in 1..self.inputs.len() { |
| 105 | let previous = self.inputs[index - 1].decoder.header; |
| 106 | let current = self.inputs[index].decoder.header; |
| 107 | if previous.page_size != current.page_size { |
| 108 | return Err(Error::LTXCorrupted); |
| 109 | } |
| 110 | if !self.allow_non_contiguous_txids |
| 111 | && !is_contiguous(previous.max_txid, current.min_txid, current.max_txid) |
| 112 | { |
| 113 | return Err(Error::LTXCorrupted); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | let first = self.inputs[0].decoder.header; |
| 118 | let last = self.inputs[self.inputs.len() - 1].decoder.header; |
| 119 | self.encoder.encode_header(Header { |
| 120 | version: VERSION, |
| 121 | flags: self.header_flags, |
| 122 | page_size: first.page_size, |
| 123 | commit: last.commit, |
| 124 | min_txid: first.min_txid, |
| 125 | max_txid: last.max_txid, |
| 126 | timestamp: last.timestamp, |
| 127 | pre_apply_checksum: first.pre_apply_checksum, |
| 128 | ..Header::default() |
| 129 | })?; |
| 130 | self.total.store(last.commit, Ordering::Relaxed); |
| 131 | |
| 132 | for input in &mut self.inputs { |
| 133 | input.data.resize(first.page_size as usize, 0); |
| 134 | } |
| 135 | |
| 136 | loop { |
| 137 | let Some(page_number) = self.fill_page_buffers()? else { |
| 138 | break; |
| 139 | }; |
| 140 | self.write_page_buffer(page_number)?; |
| 141 | self.page_number.store(page_number, Ordering::Relaxed); |
| 142 | } |
| 143 | |
| 144 | for input in &mut self.inputs { |
| 145 | input.decoder.close()?; |
| 146 | } |
| 147 | |
| 148 | let post_apply_checksum = self.inputs[self.inputs.len() - 1] |
| 149 | .decoder |
| 150 | .trailer |
| 151 | .post_apply_checksum; |
| 152 | self.encoder.close(post_apply_checksum) |
| 153 | } |
| 154 | |
| 155 | fn fill_page_buffers(&mut self) -> Result<Option<u32>> { |
| 156 | let mut minimum = None; |
| 157 | for input in &mut self.inputs { |
| 158 | if input.page.is_none() { |
| 159 | input.page = input.decoder.decode_page(&mut input.data)?; |
| 160 | } |
| 161 | if let Some(page) = input.page { |
| 162 | minimum = Some(minimum.map_or(page.pgno, |value: u32| value.min(page.pgno))); |
| 163 | } |
| 164 | } |
| 165 | Ok(minimum) |
| 166 | } |
| 167 | |
| 168 | fn write_page_buffer(&mut self, page_number: u32) -> Result<()> { |
| 169 | let commit = self.encoder.header.commit; |
| 170 | let mut written = false; |
| 171 | for input in self.inputs.iter_mut().rev() { |
| 172 | let Some(page) = input.page else { |
| 173 | continue; |
| 174 | }; |
| 175 | if page.pgno != page_number { |
| 176 | continue; |
| 177 | } |
| 178 | input.page = None; |
| 179 | if written || page_number > commit { |
| 180 | continue; |
| 181 | } |
| 182 | written = true; |
| 183 | self.encoder.encode_page(page, &input.data)?; |
| 184 | } |
| 185 | Ok(()) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | fn is_contiguous(previous_max: TXID, min: TXID, max: TXID) -> bool { |
| 190 | min.0 <= previous_max.0.wrapping_add(1) && max > previous_max |
| 191 | } |
| 192 | |
| 193 | struct CompactorInput<R> { |
| 194 | decoder: Decoder<R>, |
| 195 | page: Option<PageHeader>, |
| 196 | data: Vec<u8>, |
| 197 | } |