Skip to main content

rskit_storage/store/local/
config.rs

1//! Local store configuration.
2
3use std::path::PathBuf;
4use std::sync::atomic::{AtomicU64, Ordering};
5use std::time::{SystemTime, UNIX_EPOCH};
6
7use serde::{Deserialize, Serialize};
8
9static NEXT_DEFAULT_ROOT_ID: AtomicU64 = AtomicU64::new(0);
10
11/// Configuration for the local file store.
12#[derive(Debug, Clone, Deserialize, Serialize)]
13pub struct LocalStoreConfig {
14    /// Root directory that all stored keys and generated parent directories must stay under.
15    pub root_dir: PathBuf,
16    /// Whether to auto-create the root directory if it doesn't exist.
17    pub auto_create: bool,
18}
19
20impl Default for LocalStoreConfig {
21    fn default() -> Self {
22        Self {
23            root_dir: default_local_root_dir(),
24            auto_create: true,
25        }
26    }
27}
28
29fn default_local_root_dir() -> PathBuf {
30    let sequence = NEXT_DEFAULT_ROOT_ID.fetch_add(1, Ordering::Relaxed);
31    let nanos = SystemTime::now()
32        .duration_since(UNIX_EPOCH)
33        .map_or(0, |duration| duration.as_nanos());
34    std::env::temp_dir().join(format!(
35        "rskit-storage-{}-{nanos}-{sequence}",
36        std::process::id()
37    ))
38}