raft_engine/env/
mod.rs

1// Copyright (c) 2017-present, PingCAP, Inc. Licensed under Apache-2.0.
2
3use std::io::{Read, Result, Seek, Write};
4use std::path::Path;
5use std::sync::Arc;
6
7mod default;
8mod log_fd;
9mod obfuscated;
10
11pub use default::DefaultFileSystem;
12pub use obfuscated::ObfuscatedFileSystem;
13
14#[derive(Clone, Copy, PartialEq, Eq, Debug)]
15pub enum Permission {
16    ReadOnly,
17    ReadWrite,
18}
19
20/// FileSystem
21pub trait FileSystem: Send + Sync {
22    type Handle: Send + Sync + Handle;
23    type Reader: Seek + Read + Send;
24    type Writer: Seek + Write + Send + WriteExt;
25
26    fn create<P: AsRef<Path>>(&self, path: P) -> Result<Self::Handle>;
27
28    fn open<P: AsRef<Path>>(&self, path: P, perm: Permission) -> Result<Self::Handle>;
29
30    fn delete<P: AsRef<Path>>(&self, path: P) -> Result<()>;
31
32    fn rename<P: AsRef<Path>>(&self, src_path: P, dst_path: P) -> Result<()>;
33
34    /// Reuses file at `src_path` as a new file at `dst_path`. The default
35    /// implementation simply renames the file.
36    fn reuse<P: AsRef<Path>>(&self, src_path: P, dst_path: P) -> Result<()> {
37        self.rename(src_path, dst_path)
38    }
39
40    #[inline]
41    fn reuse_and_open<P: AsRef<Path>>(&self, src_path: P, dst_path: P) -> Result<Self::Handle> {
42        self.reuse(src_path.as_ref(), dst_path.as_ref())?;
43        self.open(dst_path, Permission::ReadWrite)
44    }
45
46    /// Deletes user implemented metadata associated with `path`. Returns
47    /// `true` if any metadata is deleted.
48    ///
49    /// In older versions of Raft Engine, physical files are deleted without
50    /// going through user implemented cleanup procedure. This method is used to
51    /// detect and cleanup the user metadata that is no longer mapped to a
52    /// physical file.
53    fn delete_metadata<P: AsRef<Path>>(&self, _path: P) -> Result<()> {
54        Ok(())
55    }
56
57    /// Returns whether there is any user metadata associated with given `path`.
58    fn exists_metadata<P: AsRef<Path>>(&self, _path: P) -> bool {
59        false
60    }
61
62    fn new_reader(&self, handle: Arc<Self::Handle>) -> Result<Self::Reader>;
63
64    fn new_writer(&self, handle: Arc<Self::Handle>) -> Result<Self::Writer>;
65}
66
67pub trait Handle {
68    fn truncate(&self, offset: usize) -> Result<()>;
69
70    /// Returns the current size of this file.
71    fn file_size(&self) -> Result<usize>;
72
73    fn sync(&self) -> Result<()>;
74}
75
76/// WriteExt is writer extension api
77pub trait WriteExt {
78    fn truncate(&mut self, offset: usize) -> Result<()>;
79    fn allocate(&mut self, offset: usize, size: usize) -> Result<()>;
80}
81
82#[cfg(test)]
83mod tests {
84    use super::Permission;
85
86    #[test]
87    fn test_copy_permission() {
88        let perm = Permission::ReadWrite;
89        let perm1 = perm;
90        assert_eq!(perm, Permission::ReadWrite);
91        assert_eq!(perm1, Permission::ReadWrite);
92    }
93}