Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for creating log file journals directly from buffers #4510

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions lib/journal/src/concrete/log_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct LogFileJournalTx {

#[derive(Debug)]
pub struct LogFileJournalRx {
tx: LogFileJournalTx,
tx: Option<LogFileJournalTx>,
buffer_pos: Mutex<usize>,
buffer: OwnedBuffer,
store: OffloadBackingStore,
Expand Down Expand Up @@ -87,7 +87,7 @@ impl LogFileJournalTx {
}

Ok(LogFileJournalRx {
tx: self.clone(),
tx: Some(self.clone()),
buffer_pos: Mutex::new(buffer_pos),
buffer,
store,
Expand All @@ -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<Self> {
// Move to the end of the file and write the
// magic if one is needed
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -252,8 +270,17 @@ impl ReadableJournal for LogFileJournalRx {
}

fn as_restarted(&self) -> anyhow::Result<Box<DynReadableJournal>> {
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(),
}))
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/virtual-fs/src/mem_fs/offloaded_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading