Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(services/fs): Support fs config #4853

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 31 additions & 26 deletions core/src/services/fs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,41 @@ use std::sync::Arc;

use chrono::DateTime;
use log::debug;
use serde::Deserialize;

use crate::raw::*;
use crate::*;

use super::core::*;
use super::lister::FsLister;
use super::reader::FsReader;
use super::writer::FsWriter;
use crate::raw::*;
use crate::*;

/// config for file system
#[derive(Default, Deserialize, Debug)]
#[serde(default)]
#[non_exhaustive]
pub struct FsConfig {
/// root dir for backend
pub root: Option<String>,

/// tmp dir for atomic write
pub atomic_write_dir: Option<String>,
}

/// POSIX file system support.
#[doc = include_str!("docs.md")]
#[derive(Default, Debug)]
pub struct FsBuilder {
root: Option<PathBuf>,
atomic_write_dir: Option<PathBuf>,
config: FsConfig,
}

impl FsBuilder {
/// Set root for backend.
pub fn root(&mut self, root: &str) -> &mut Self {
self.root = if root.is_empty() {
None
} else {
Some(PathBuf::from(root))
};
if !root.is_empty() {
self.config.root = Some(root.to_string());
}

self
}
Expand All @@ -58,11 +69,9 @@ impl FsBuilder {
/// - When append is enabled, we will not use atomic write
/// to avoid data loss and performance issue.
pub fn atomic_write_dir(&mut self, dir: &str) -> &mut Self {
self.atomic_write_dir = if dir.is_empty() {
None
} else {
Some(PathBuf::from(dir))
};
if !dir.is_empty() {
self.config.atomic_write_dir = Some(dir.to_string());
}

self
}
Expand All @@ -73,19 +82,15 @@ impl Builder for FsBuilder {
type Accessor = FsBackend;

fn from_map(map: HashMap<String, String>) -> Self {
let mut builder = FsBuilder::default();

map.get("root").map(|v| builder.root(v));
map.get("atomic_write_dir")
.map(|v| builder.atomic_write_dir(v));

builder
let config = FsConfig::deserialize(ConfigDeserializer::new(map))
.expect("config deserialize must succeed");
FsBuilder { config }
}

fn build(&mut self) -> Result<Self::Accessor> {
debug!("backend build started: {:?}", &self);

let root = match self.root.take() {
let root = match self.config.root.take().map(PathBuf::from) {
Some(root) => Ok(root),
None => Err(Error::new(
ErrorKind::ConfigInvalid,
Expand All @@ -106,7 +111,7 @@ impl Builder for FsBuilder {
}
}

let atomic_write_dir = self.atomic_write_dir.take();
let atomic_write_dir = self.config.atomic_write_dir.take().map(PathBuf::from);

// If atomic write dir is not exist, we must create it.
if let Some(d) = &atomic_write_dir {
Expand Down Expand Up @@ -240,9 +245,9 @@ impl Access for FsBackend {
///
/// There are three ways to get the total file length:
///
/// - call std::fs::metadata directly and than open. (400ns)
/// - open file first, and than use `f.metadata()` (300ns)
/// - open file first, and than use `seek`. (100ns)
/// - call std::fs::metadata directly and then open. (400ns)
/// - open file first, and then use `f.metadata()` (300ns)
/// - open file first, and then use `seek`. (100ns)
///
/// Benchmark could be found [here](https://gist.github.com/Xuanwo/48f9cfbc3022ea5f865388bb62e1a70f)
async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
Expand Down
1 change: 1 addition & 0 deletions core/src/services/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

mod backend;
pub use backend::FsBuilder as Fs;
pub use backend::FsConfig;

mod core;
mod lister;
Expand Down
2 changes: 2 additions & 0 deletions core/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub use self::etcd::EtcdConfig;
mod fs;
#[cfg(feature = "services-fs")]
pub use fs::Fs;
#[cfg(feature = "services-fs")]
pub use fs::FsConfig;

#[cfg(feature = "services-ftp")]
mod ftp;
Expand Down
Loading