diff --git a/lib/journal/src/concrete/log_file.rs b/lib/journal/src/concrete/log_file.rs index 19a7d7ea494..864bd0e313e 100644 --- a/lib/journal/src/concrete/log_file.rs +++ b/lib/journal/src/concrete/log_file.rs @@ -44,7 +44,7 @@ pub struct LogFileJournalTx { #[derive(Debug)] pub struct LogFileJournalRx { - tx: LogFileJournalTx, + tx: Option, buffer_pos: Mutex, buffer: OwnedBuffer, store: OffloadBackingStore, @@ -87,7 +87,7 @@ impl LogFileJournalTx { } Ok(LogFileJournalRx { - tx: self.clone(), + tx: Some(self.clone()), buffer_pos: Mutex::new(buffer_pos), buffer, store, @@ -113,6 +113,7 @@ impl LogFileJournal { self.rx.backing_store() } + /// Create a new journal from a file pub fn from_file(mut file: std::fs::File) -> anyhow::Result { // Move to the end of the file and write the // magic if one is needed @@ -142,6 +143,23 @@ impl LogFileJournal { Ok(Self { rx, tx }) } + + /// Create a new journal from a buffer + pub fn from_buffer(buffer: OwnedBuffer) -> RecombinedJournal { + // Create the rx + let rx = LogFileJournalRx { + tx: None, + buffer_pos: Mutex::new(0), + buffer: buffer.clone(), + store: OffloadBackingStore::from_buffer(buffer), + }; + + // Create an unsupported write journal + let tx = UnsupportedJournal::default(); + + // Now recombine + RecombinedJournal::new(Box::new(tx), Box::new(rx)) + } } impl WritableJournal for LogFileJournalTx { @@ -252,8 +270,17 @@ impl ReadableJournal for LogFileJournalRx { } fn as_restarted(&self) -> anyhow::Result> { - let ret = self.tx.as_rx()?; - Ok(Box::new(ret)) + if let Some(tx) = &self.tx { + let ret = tx.as_rx()?; + Ok(Box::new(ret)) + } else { + Ok(Box::new(LogFileJournalRx { + tx: None, + buffer_pos: Mutex::new(0), + buffer: self.buffer.clone(), + store: self.store.clone(), + })) + } } } diff --git a/lib/virtual-fs/src/mem_fs/offloaded_file.rs b/lib/virtual-fs/src/mem_fs/offloaded_file.rs index a4308be32bd..356a7e739b5 100644 --- a/lib/virtual-fs/src/mem_fs/offloaded_file.rs +++ b/lib/virtual-fs/src/mem_fs/offloaded_file.rs @@ -108,6 +108,10 @@ impl OffloadBackingStore { Self::new(buffer, Some(file)) } + pub fn from_buffer(buffer: OwnedBuffer) -> Self { + Self::new(buffer, None) + } + pub fn owned_buffer(&self) -> OwnedBuffer { let guard = self.state.lock().unwrap(); guard.mmap_offload.clone()