diff --git a/Cargo.toml b/Cargo.toml index 5626bbac..9c38189a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/file.rs b/src/file.rs index 9136a2c9..e6e46c06 100644 --- a/src/file.rs +++ b/src/file.rs @@ -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; @@ -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(); @@ -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)