Skip to content

Commit

Permalink
feat: change data_dir to storage's dir (#186)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Jan 2, 2024
1 parent 94d21d2 commit 754678c
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dragonfly-client"
version = "0.1.2"
version = "0.1.3"
authors = ["The Dragonfly Developers"]
homepage = "https://d7y.io/"
repository = "https://github.com/dragonflyoss/client.git"
Expand Down
9 changes: 4 additions & 5 deletions src/bin/dfdaemon/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ async fn main() -> Result<(), anyhow::Error> {
);

// Initialize storage.
let storage =
Storage::new(config.clone(), config.server.data_dir.as_path()).map_err(|err| {
error!("initialize storage failed: {}", err);
err
})?;
let storage = Storage::new(config.clone(), config.storage.dir.as_path()).map_err(|err| {
error!("initialize storage failed: {}", err);
err
})?;
let storage = Arc::new(storage);

// Initialize id generator.
Expand Down
22 changes: 15 additions & 7 deletions src/config/dfdaemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,6 @@ impl Default for Host {
#[derive(Debug, Clone, Validate, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Server {
// data_dir is the directory to store task's metadata and content.
#[serde(default = "super::default_data_dir")]
pub data_dir: PathBuf,

// plugin_dir is the directory to store plugins.
#[serde(default = "default_dfdaemon_plugin_dir")]
pub plugin_dir: PathBuf,
Expand All @@ -211,7 +207,6 @@ pub struct Server {
impl Default for Server {
fn default() -> Self {
Server {
data_dir: super::default_data_dir(),
plugin_dir: default_dfdaemon_plugin_dir(),
cache_dir: default_dfdaemon_cache_dir(),
lock_dir: default_dfdaemon_lock_path(),
Expand Down Expand Up @@ -426,9 +421,22 @@ impl Default for Dynconfig {
}

// Storage is the storage configuration for dfdaemon.
#[derive(Debug, Clone, Default, Validate, Deserialize)]
#[derive(Debug, Clone, Validate, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Storage {}
pub struct Storage {
// dir is the directory to store task's metadata and content.
#[serde(default = "super::default_storage_dir")]
pub dir: PathBuf,
}

// Storage implements Default.
impl Default for Storage {
fn default() -> Self {
Storage {
dir: super::default_storage_dir(),
}
}
}

// Policy is the policy configuration for gc.
#[derive(Debug, Clone, Validate, Deserialize)]
Expand Down
6 changes: 3 additions & 3 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ pub fn default_log_dir() -> PathBuf {
return home::home_dir().unwrap().join(".dragonfly").join("logs");
}

// default_data_dir is the default data directory for client.
pub fn default_data_dir() -> PathBuf {
// default_storage_dir is the default storage directory for client.
pub fn default_storage_dir() -> PathBuf {
#[cfg(target_os = "linux")]
return PathBuf::from("/var/lib/dragonfly/");

#[cfg(target_os = "macos")]
return home::home_dir().unwrap().join(".dragonfly").join("data");
return home::home_dir().unwrap().join(".dragonfly").join("storage");
}

// default_plugin_dir is the default plugin directory for client.
Expand Down
2 changes: 1 addition & 1 deletion src/gc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl GC {

// evict_by_disk_usage evicts the cache by disk usage.
fn evict_by_disk_usage(&self) -> Result<()> {
let stats = fs2::statvfs(self.config.server.data_dir.as_path())?;
let stats = fs2::statvfs(self.config.storage.dir.as_path())?;
let available_space = stats.available_space();
let total_space = stats.total_space();

Expand Down
4 changes: 2 additions & 2 deletions src/storage/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub struct WritePieceResponse {
// Content implements the content storage.
impl Content {
// new returns a new content.
pub fn new(data_dir: &Path) -> Result<Content> {
let dir = data_dir.join(config::NAME).join(DEFAULT_DIR_NAME);
pub fn new(dir: &Path) -> Result<Content> {
let dir = dir.join(config::NAME).join(DEFAULT_DIR_NAME);
fs::create_dir_all(&dir)?;
info!("content initialized directory: {:?}", dir);

Expand Down
4 changes: 2 additions & 2 deletions src/storage/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ pub struct Metadata {
// Metadata implements the metadata storage.
impl Metadata {
// new returns a new metadata.
pub fn new(data_dir: &Path) -> Result<Metadata> {
pub fn new(dir: &Path) -> Result<Metadata> {
// Initialize rocksdb options.
let mut options = Options::default();
options.create_if_missing(true);
Expand All @@ -209,7 +209,7 @@ impl Metadata {
options.set_block_based_table_factory(&block_options);

// Open rocksdb.
let dir = data_dir.join(config::NAME).join(DEFAULT_DIR_NAME);
let dir = dir.join(config::NAME).join(DEFAULT_DIR_NAME);
let cf_names = [TASK_CF_NAME, PIECE_CF_NAME];
let db =
TransactionDB::open_cf(&options, &TransactionDBOptions::default(), &dir, cf_names)?;
Expand Down
6 changes: 3 additions & 3 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ pub struct Storage {
// Storage implements the storage.
impl Storage {
// new returns a new storage.
pub fn new(config: Arc<Config>, data_dir: &Path) -> Result<Self> {
let metadata = metadata::Metadata::new(data_dir)?;
let content = content::Content::new(data_dir)?;
pub fn new(config: Arc<Config>, dir: &Path) -> Result<Self> {
let metadata = metadata::Metadata::new(dir)?;
let content = content::Content::new(dir)?;
Ok(Storage {
config,
metadata,
Expand Down

0 comments on commit 754678c

Please sign in to comment.