From 3f09e2bd345d03f1bc8972bad9580162d058efd6 Mon Sep 17 00:00:00 2001 From: Johnathan Sharratt Date: Thu, 21 Mar 2024 23:27:04 +1100 Subject: [PATCH 1/2] Added support for creating log file journals directly from buffers --- lib/journal/src/concrete/log_file.rs | 35 ++++++++++++++++++--- lib/virtual-fs/src/mem_fs/offloaded_file.rs | 4 +++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/journal/src/concrete/log_file.rs b/lib/journal/src/concrete/log_file.rs index 19a7d7ea494..eb8fae2def0 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) -> Box { + // 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 + Box::new(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() From 078fe5857c347cab7cc027161201b9f07c373c7b Mon Sep 17 00:00:00 2001 From: Johnathan Sharratt Date: Thu, 21 Mar 2024 23:59:55 +1100 Subject: [PATCH 2/2] Made the output a concrete type so that its easier to use --- lib/journal/src/concrete/log_file.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/journal/src/concrete/log_file.rs b/lib/journal/src/concrete/log_file.rs index eb8fae2def0..864bd0e313e 100644 --- a/lib/journal/src/concrete/log_file.rs +++ b/lib/journal/src/concrete/log_file.rs @@ -145,7 +145,7 @@ impl LogFileJournal { } /// Create a new journal from a buffer - pub fn from_buffer(buffer: OwnedBuffer) -> Box { + pub fn from_buffer(buffer: OwnedBuffer) -> RecombinedJournal { // Create the rx let rx = LogFileJournalRx { tx: None, @@ -158,7 +158,7 @@ impl LogFileJournal { let tx = UnsupportedJournal::default(); // Now recombine - Box::new(RecombinedJournal::new(Box::new(tx), Box::new(rx))) + RecombinedJournal::new(Box::new(tx), Box::new(rx)) } }