-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathconfig.rs
More file actions
234 lines (203 loc) · 6.58 KB
/
Copy pathconfig.rs
File metadata and controls
234 lines (203 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Storage configuration types.
//!
//! This module provides configuration structures for different storage backends,
//! allowing services to configure storage type (InMemory or SlateDB) via config files
//! or environment variables.
use serde::{Deserialize, Serialize};
/// Top-level storage configuration.
///
/// Defaults to `SlateDb` with a local `/tmp/opendata-storage` directory.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
pub enum StorageConfig {
InMemory,
SlateDb(SlateDbStorageConfig),
}
impl Default for StorageConfig {
fn default() -> Self {
StorageConfig::SlateDb(SlateDbStorageConfig {
path: "data".to_string(),
object_store: ObjectStoreConfig::Local(LocalObjectStoreConfig {
path: ".data".to_string(),
}),
settings_path: None,
})
}
}
/// SlateDB-specific configuration.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SlateDbStorageConfig {
/// Path prefix for SlateDB data in the object store.
pub path: String,
/// Object store provider configuration.
pub object_store: ObjectStoreConfig,
/// Optional path to SlateDB settings file (TOML/YAML/JSON).
///
/// If not provided, uses SlateDB's `Settings::load()` which checks for
/// `SlateDb.toml`, `SlateDb.json`, `SlateDb.yaml` in the working directory
/// and merges any `SLATEDB_` prefixed environment variables.
#[serde(skip_serializing_if = "Option::is_none")]
pub settings_path: Option<String>,
}
impl Default for SlateDbStorageConfig {
fn default() -> Self {
Self {
path: "data".to_string(),
object_store: ObjectStoreConfig::default(),
settings_path: None,
}
}
}
/// Object store provider configuration for SlateDB.
#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
pub enum ObjectStoreConfig {
/// In-memory object store (useful for testing and development).
#[default]
InMemory,
/// AWS S3 object store.
Aws(AwsObjectStoreConfig),
/// Local filesystem object store.
Local(LocalObjectStoreConfig),
}
/// AWS S3 object store configuration.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AwsObjectStoreConfig {
/// AWS region (e.g., "us-west-2").
pub region: String,
/// S3 bucket name.
pub bucket: String,
}
/// Local filesystem object store configuration.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LocalObjectStoreConfig {
/// Path to the local directory for storage.
pub path: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_default_to_slatedb_with_local_data_dir() {
// given/when
let config = StorageConfig::default();
// then
match config {
StorageConfig::SlateDb(slate_config) => {
assert_eq!(slate_config.path, "data");
assert_eq!(
slate_config.object_store,
ObjectStoreConfig::Local(LocalObjectStoreConfig {
path: ".data".to_string()
})
);
}
_ => panic!("Expected SlateDb config as default"),
}
}
#[test]
fn should_deserialize_in_memory_config() {
// given
let yaml = r#"type: InMemory"#;
// when
let config: StorageConfig = serde_yaml::from_str(yaml).unwrap();
// then
assert_eq!(config, StorageConfig::InMemory);
}
#[test]
fn should_deserialize_slatedb_config_with_local_object_store() {
// given
let yaml = r#"
type: SlateDb
path: my-data
object_store:
type: Local
path: /tmp/slatedb
"#;
// when
let config: StorageConfig = serde_yaml::from_str(yaml).unwrap();
// then
match config {
StorageConfig::SlateDb(slate_config) => {
assert_eq!(slate_config.path, "my-data");
assert_eq!(
slate_config.object_store,
ObjectStoreConfig::Local(LocalObjectStoreConfig {
path: "/tmp/slatedb".to_string()
})
);
assert!(slate_config.settings_path.is_none());
}
_ => panic!("Expected SlateDb config"),
}
}
#[test]
fn should_deserialize_slatedb_config_with_aws_object_store() {
// given
let yaml = r#"
type: SlateDb
path: my-data
object_store:
type: Aws
region: us-west-2
bucket: my-bucket
settings_path: slatedb.toml
"#;
// when
let config: StorageConfig = serde_yaml::from_str(yaml).unwrap();
// then
match config {
StorageConfig::SlateDb(slate_config) => {
assert_eq!(slate_config.path, "my-data");
assert_eq!(
slate_config.object_store,
ObjectStoreConfig::Aws(AwsObjectStoreConfig {
region: "us-west-2".to_string(),
bucket: "my-bucket".to_string()
})
);
assert_eq!(slate_config.settings_path, Some("slatedb.toml".to_string()));
}
_ => panic!("Expected SlateDb config"),
}
}
#[test]
fn should_deserialize_slatedb_config_with_in_memory_object_store() {
// given
let yaml = r#"
type: SlateDb
path: test-data
object_store:
type: InMemory
"#;
// when
let config: StorageConfig = serde_yaml::from_str(yaml).unwrap();
// then
match config {
StorageConfig::SlateDb(slate_config) => {
assert_eq!(slate_config.path, "test-data");
assert_eq!(slate_config.object_store, ObjectStoreConfig::InMemory);
}
_ => panic!("Expected SlateDb config"),
}
}
#[test]
fn should_serialize_slatedb_config() {
// given
let config = StorageConfig::SlateDb(SlateDbStorageConfig {
path: "my-data".to_string(),
object_store: ObjectStoreConfig::Local(LocalObjectStoreConfig {
path: "/tmp/slatedb".to_string(),
}),
settings_path: None,
});
// when
let yaml = serde_yaml::to_string(&config).unwrap();
// then
assert!(yaml.contains("type: SlateDb"));
assert!(yaml.contains("path: my-data"));
assert!(yaml.contains("type: Local"));
// settings_path should be omitted when None
assert!(!yaml.contains("settings_path"));
}
}