liquid_cache_common/
io_mode.rs

1use std::{fmt::Display, str::FromStr};
2
3use serde::Serialize;
4
5/// Mode in which Disk IO is done (direct IO or page cache)
6#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize)]
7pub enum IoMode {
8    /// Uses io_uring and bypass the page cache (uses direct IO), only available on Linux
9    #[serde(rename = "uring-direct")]
10    UringDirect,
11
12    /// Uses io_uring and uses OS's page cache, only available on Linux
13    #[serde(rename = "uring")]
14    Uring,
15
16    /// Uses io_uring on the calling thread and blocks until completion.
17    #[cfg_attr(target_os = "linux", default)]
18    #[serde(rename = "uring-blocking")]
19    UringBlocking,
20
21    /// Uses rust's std::fs::File, this is blocking IO.
22    /// On Linux, this is essentially `pread/pwrite`
23    /// This is the default on non-Linux platforms.
24    #[cfg_attr(not(target_os = "linux"), default)]
25    #[serde(rename = "std-blocking")]
26    StdBlocking,
27
28    /// Uses tokio's async IO, this is non-blocking IO, but quite slow: <https://github.com/tokio-rs/tokio/issues/3664>
29    #[serde(rename = "tokio")]
30    TokioIO,
31
32    /// Use rust's std::fs::File, but will try to `spawn_blocking`, just like `object_store` does:
33    /// <https://github.com/apache/arrow-rs-object-store/blob/28b2fc563feb44bb3d15718cf58036772334a704/src/local.rs#L440-L448>
34    #[serde(rename = "std-spawn-blocking")]
35    StdSpawnBlocking,
36}
37
38impl Display for IoMode {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        write!(
41            f,
42            "{}",
43            match self {
44                IoMode::Uring => "uring",
45                IoMode::UringDirect => "uring-direct",
46                IoMode::UringBlocking => "uring-blocking",
47                IoMode::StdBlocking => "std-blocking",
48                IoMode::TokioIO => "tokio",
49                IoMode::StdSpawnBlocking => "std-spawn-blocking",
50            }
51        )
52    }
53}
54
55impl FromStr for IoMode {
56    type Err = String;
57
58    fn from_str(s: &str) -> Result<Self, Self::Err> {
59        Ok(match s {
60            "uring-direct" => IoMode::UringDirect,
61            "uring" => IoMode::Uring,
62            "uring-blocking" => IoMode::UringBlocking,
63            "std-blocking" => IoMode::StdBlocking,
64            "tokio" => IoMode::TokioIO,
65            "std-spawn-blocking" => IoMode::StdSpawnBlocking,
66            _ => return Err(format!("Invalid IO mode: {s}")),
67        })
68    }
69}