Skip to content

Commit

Permalink
Attempt to overwrite file when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofer-Julian committed Feb 5, 2024
1 parent 9aad030 commit 3673afe
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/history/file_backed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct FileBackedHistory {
file: Option<PathBuf>,
len_on_disk: usize, // Keep track what was previously written to disk
session: Option<HistorySessionId>,
overwrite: bool, // Will overwrite file on next sync, not just new entries
}

impl Default for FileBackedHistory {
Expand Down Expand Up @@ -217,6 +218,7 @@ impl History for FileBackedHistory {

// Remove the item with the specified id
self.entries.remove(id);
self.overwrite = true;

Ok(())
}
Expand All @@ -226,9 +228,6 @@ impl History for FileBackedHistory {
/// If file would exceed `capacity` truncates the oldest entries.
fn sync(&mut self) -> std::io::Result<()> {
if let Some(fname) = &self.file {
// The unwritten entries
let own_entries = self.entries.range(self.len_on_disk..);

if let Some(base_dir) = fname.parent() {
std::fs::create_dir_all(base_dir)?;
}
Expand All @@ -238,14 +237,39 @@ impl History for FileBackedHistory {
.create(true)
.write(true)
.read(true)
.truncate(self.overwrite)
.open(fname)?,
);
let mut writer_guard = f_lock.write()?;

if self.overwrite {
self.overwrite = false;
let mut writer = BufWriter::new(writer_guard.deref_mut());
for line in &self.entries {
writer.write_all(encode_entry(line).as_bytes())?;
writer.write_all("\n".as_bytes())?;
}
writer.flush()?;
return Ok(());
}

// The unwritten entries
let own_entries = self.entries.range(self.len_on_disk..);

let (mut foreign_entries, truncate) = {
let reader = BufReader::new(writer_guard.deref());
let mut from_file = reader
.lines()
.map(|o| o.map(|i| decode_entry(&i)))
.filter(|e| {
if let Ok(entry) = e {
if entry.starts_with(' ') {
self.overwrite = true;
return false;
}
}
true
})
.collect::<std::io::Result<VecDeque<_>>>()?;
if from_file.len() + own_entries.len() > self.capacity {
(
Expand Down Expand Up @@ -313,6 +337,7 @@ impl FileBackedHistory {
file: None,
len_on_disk: 0,
session: None,
overwrite: false,
})
}

Expand Down

0 comments on commit 3673afe

Please sign in to comment.