datrs / random-access-disk

Continuously read/write to disk using random offsets and lengths .
Apache License 2.0
27 stars 13 forks source link

Add method to acquire BufReader #50

Open DrSensor opened 9 months ago

DrSensor commented 9 months ago

Choose one: is this a 🐛 bug fix, a 🙋 feature, or a 🔦 documentation change? 🙋 feature

Checklist

Context

I have a case where I store RandomAccessDisk in struct like this

struct LSPBackend {
  files: HashMap<Url, RandomAccessDisk>, // I store the file instead of path to make it efficient
  // ...other stuff
}

It's quite common to do buffered read so I expose reader() method. Accessing the underlying fs::File is possible by calling reader().get_ref() or reader().get_mut().

The reader() method open up the possibility for this pattern

let line = 10;
let column = 10;
let length = 5;

let mut file = RandomAccessDisk::open("text.db").await.unwrap();

let offset = column + (
    file.reader()
        .lines()
        .take(line - 1)
        .fold(column, |acc, line| acc + line.unwrap().len() as u64)
        .await
);

let _ = file.del(offset, length).await;

Semver Changes

3.1.0

ttiurani commented 9 months ago

Thanks for the PR! This is possible, if not a bit unconventional, because you can of course already read with read() but still ok.

However, I think this needs tests. Especially this should have a test where you first create a sparse file (with a large enough deleted section that a hole is actually created) and then take out this BufReader and read over the hole. It most likely does work and gives zeroes, but should IMO still be verified.