Skip to content

Commit

Permalink
Disable read-ahead on Windows (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc authored Jan 5, 2024
1 parent 70e0929 commit 6698b38
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ snap = "1"
loom = { version = "0.5.1", optional = true }
siphasher = "0.3.10"

[target.'cfg(windows)'.dependencies]
winapi = "0.3.9"

[dev-dependencies]
env_logger = { version = "0.10.0", default-features = false, features = ["auto-color", "humantime"] }
fdlimit = "0.2.1"
Expand Down
20 changes: 20 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ use crate::{
};
use std::sync::atomic::{AtomicU64, Ordering};

trait OpenOptionsExt {
fn disable_read_ahead(&mut self) -> &mut Self;
}

impl OpenOptionsExt for std::fs::OpenOptions {
#[cfg(not(windows))]
fn disable_read_ahead(&mut self) -> &mut Self {
// Not supported
self
}

#[cfg(windows)]
fn disable_read_ahead(&mut self) -> &mut Self {
use std::os::windows::fs::OpenOptionsExt;
self.custom_flags(winapi::um::winbase::FILE_FLAG_RANDOM_ACCESS)
}
}

#[cfg(target_os = "linux")]
fn disable_read_ahead(file: &std::fs::File) -> std::io::Result<()> {
use std::os::unix::io::AsRawFd;
Expand Down Expand Up @@ -82,6 +100,7 @@ impl TableFile {
let file = try_io!(std::fs::OpenOptions::new()
.read(true)
.write(true)
.disable_read_ahead()
.open(filepath.as_path()));
try_io!(disable_read_ahead(&file));
let len = try_io!(file.metadata()).len();
Expand Down Expand Up @@ -111,6 +130,7 @@ impl TableFile {
.create(true)
.read(true)
.write(true)
.disable_read_ahead()
.open(self.path.as_path()));
try_io!(disable_read_ahead(&file));
Ok(file)
Expand Down

0 comments on commit 6698b38

Please sign in to comment.