use crate::{Flag, OptionalInteger};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub const DEFAULT_DATASTORE_DIRECTORY: &str = "datastore";
pub const DEFAULT_BLOCKS_DIRECTORY: &str = "blocks";
pub const DEFAULT_STORAGE_MAX: &str = "10GB";
pub const DEFAULT_STORAGE_GC_WATERMARK: i64 = 90;
pub const DEFAULT_GC_PERIOD: &str = "1h";
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Datastore {
#[serde(default)]
pub storage_max: String,
#[serde(default, rename = "StorageGCWatermark")]
pub storage_gc_watermark: i64,
#[serde(default, rename = "GCPeriod")]
pub gc_period: String,
#[serde(default)]
pub spec: HashMap<String, serde_json::Value>,
#[serde(default)]
pub hash_on_read: bool,
#[serde(default)]
pub bloom_filter_size: i32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub block_key_cache_size: Option<OptionalInteger>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub write_through: Option<Flag>,
}
impl Datastore {
pub fn default_config() -> Self {
Self {
storage_max: DEFAULT_STORAGE_MAX.to_string(),
storage_gc_watermark: DEFAULT_STORAGE_GC_WATERMARK,
gc_period: DEFAULT_GC_PERIOD.to_string(),
spec: Self::default_spec(),
hash_on_read: false,
bloom_filter_size: 0,
block_key_cache_size: None,
write_through: None,
}
}
pub fn default_spec() -> HashMap<String, serde_json::Value> {
let mut spec = HashMap::new();
spec.insert("type".to_string(), serde_json::json!("mount"));
spec.insert(
"mounts".to_string(),
serde_json::json!([
{
"mountpoint": "/blocks",
"type": "measure",
"prefix": "flatfs.datastore",
"child": {
"type": "flatfs",
"path": "blocks",
"sync": true,
"shardFunc": "/repo/flatfs/shard/v1/next-to-last/2"
}
},
{
"mountpoint": "/",
"type": "measure",
"prefix": "leveldb.datastore",
"child": {
"type": "levelds",
"path": "datastore",
"compression": "none"
}
}
]),
);
spec
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_datastore_defaults() {
let ds = Datastore::default_config();
assert_eq!(ds.storage_max, "10GB");
assert_eq!(ds.storage_gc_watermark, 90);
}
#[test]
fn test_default_spec() {
let spec = Datastore::default_spec();
assert!(spec.contains_key("type"));
assert!(spec.contains_key("mounts"));
}
}