ferripfs_config/
datastore.rs1use crate::{Flag, OptionalInteger};
12use serde::{Deserialize, Serialize};
13use std::collections::HashMap;
14
15pub const DEFAULT_DATASTORE_DIRECTORY: &str = "datastore";
17
18pub const DEFAULT_BLOCKS_DIRECTORY: &str = "blocks";
20
21pub const DEFAULT_STORAGE_MAX: &str = "10GB";
23
24pub const DEFAULT_STORAGE_GC_WATERMARK: i64 = 90;
26
27pub const DEFAULT_GC_PERIOD: &str = "1h";
29
30#[derive(Debug, Clone, Default, Serialize, Deserialize)]
32#[serde(rename_all = "PascalCase")]
33pub struct Datastore {
34 #[serde(default)]
36 pub storage_max: String,
37
38 #[serde(default, rename = "StorageGCWatermark")]
40 pub storage_gc_watermark: i64,
41
42 #[serde(default, rename = "GCPeriod")]
44 pub gc_period: String,
45
46 #[serde(default)]
48 pub spec: HashMap<String, serde_json::Value>,
49
50 #[serde(default)]
52 pub hash_on_read: bool,
53
54 #[serde(default)]
56 pub bloom_filter_size: i32,
57
58 #[serde(default, skip_serializing_if = "Option::is_none")]
60 pub block_key_cache_size: Option<OptionalInteger>,
61
62 #[serde(default, skip_serializing_if = "Option::is_none")]
64 pub write_through: Option<Flag>,
65}
66
67impl Datastore {
68 pub fn default_config() -> Self {
70 Self {
71 storage_max: DEFAULT_STORAGE_MAX.to_string(),
72 storage_gc_watermark: DEFAULT_STORAGE_GC_WATERMARK,
73 gc_period: DEFAULT_GC_PERIOD.to_string(),
74 spec: Self::default_spec(),
75 hash_on_read: false,
76 bloom_filter_size: 0,
77 block_key_cache_size: None,
78 write_through: None,
79 }
80 }
81
82 pub fn default_spec() -> HashMap<String, serde_json::Value> {
84 let mut spec = HashMap::new();
85 spec.insert("type".to_string(), serde_json::json!("mount"));
86 spec.insert(
87 "mounts".to_string(),
88 serde_json::json!([
89 {
90 "mountpoint": "/blocks",
91 "type": "measure",
92 "prefix": "flatfs.datastore",
93 "child": {
94 "type": "flatfs",
95 "path": "blocks",
96 "sync": true,
97 "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2"
98 }
99 },
100 {
101 "mountpoint": "/",
102 "type": "measure",
103 "prefix": "leveldb.datastore",
104 "child": {
105 "type": "levelds",
106 "path": "datastore",
107 "compression": "none"
108 }
109 }
110 ]),
111 );
112 spec
113 }
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn test_datastore_defaults() {
122 let ds = Datastore::default_config();
123 assert_eq!(ds.storage_max, "10GB");
124 assert_eq!(ds.storage_gc_watermark, 90);
125 }
126
127 #[test]
128 fn test_default_spec() {
129 let spec = Datastore::default_spec();
130 assert!(spec.contains_key("type"));
131 assert!(spec.contains_key("mounts"));
132 }
133}