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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use std::{fmt::Debug, hash::Hash, io::Write, path::Path, sync::Mutex};
use peace_resource_rt::type_reg::{
common::UnknownEntriesSome,
untagged::{DataTypeWrapper, TypeMapOpt, TypeReg},
};
use peace_rt_model_core::{Error, NativeError};
use serde::{de::DeserializeOwned, Serialize};
use tokio::{
fs::File,
io::{AsyncReadExt, BufReader, BufWriter},
};
use tokio_util::io::SyncIoBridge;
/// Wrapper around file system operations.
#[derive(Clone, Debug)]
pub struct Storage;
impl Storage {
/// Reads the file at the given path to string.
///
/// Note: This does not check the file size, so it will use as much memory
/// as the file size.
///
/// # Parameters
///
/// * `file_path`: Path to the file to read to string.
pub async fn read_to_string(&self, file_path: &Path) -> Result<String, Error> {
if file_path.exists() {
let mut file = File::open(file_path).await.map_err(
// Tests currently don't cover file system failure cases,
// e.g. disk space limits.
#[cfg_attr(coverage_nightly, coverage(off))]
|error| {
let path = file_path.to_path_buf();
Error::Native(NativeError::FileOpen { path, error })
},
)?;
let mut buffer = String::new();
file.read_to_string(&mut buffer).await.map_err(
#[cfg_attr(coverage_nightly, coverage(off))]
|error| {
Error::Native(NativeError::FileRead {
error,
path: file_path.to_path_buf(),
})
},
)?;
Ok(buffer)
} else {
Err(Error::ItemNotExists {
path: file_path.to_path_buf(),
})
}
}
/// Reads a serializable item from the given path.
///
/// # Parameters
///
/// * `thread_name`: Name of the thread to use to do the read operation.
/// * `file_path`: Path to the file to read the serialized item.
/// * `f_map_err`: Maps the deserialization error (if any) to an [`Error`].
pub async fn serialized_read<T, F>(
&self,
thread_name: String,
file_path: &Path,
f_map_err: F,
) -> Result<T, Error>
where
T: Serialize + DeserializeOwned + Send + Sync,
F: FnOnce(serde_yaml::Error) -> Error + Send,
{
if file_path.exists() {
let t = self
.read_with_sync_api(thread_name, file_path, |file| {
serde_yaml::from_reader::<_, T>(file).map_err(f_map_err)
})
.await?;
Ok(t)
} else {
Err(Error::ItemNotExists {
path: file_path.to_path_buf(),
})
}
}
/// Reads a serializable item from the given path if the file exists.
///
/// # Parameters
///
/// * `thread_name`: Name of the thread to use to do the read operation.
/// * `file_path`: Path to the file to read the serialized item.
/// * `f_map_err`: Maps the deserialization error (if any) to an [`Error`].
pub async fn serialized_read_opt<T, F>(
&self,
thread_name: String,
file_path: &Path,
f_map_err: F,
) -> Result<Option<T>, Error>
where
T: DeserializeOwned + Send + Sync,
F: FnOnce(serde_yaml::Error) -> Error + Send,
{
if file_path.exists() {
let t = self
.read_with_sync_api(thread_name, file_path, |file| {
serde_yaml::from_reader::<_, T>(file).map_err(f_map_err)
})
.await?;
Ok(Some(t))
} else {
Ok(None)
}
}
/// Deserializes a typemap from the given path if the file exists.
///
/// # Parameters
///
/// * `thread_name`: Name of the thread to use to do the read operation.
/// * `type_reg`: Type registry with the stateful deserialization mappings.
/// * `file_path`: Path to the file to read the serialized item.
/// * `f_map_err`: Maps the deserialization error (if any) to an [`Error`].
pub async fn serialized_typemap_read_opt<K, BoxDT, F>(
&self,
thread_name: String,
type_reg: &TypeReg<K, BoxDT>,
file_path: &Path,
f_map_err: F,
) -> Result<Option<TypeMapOpt<K, BoxDT, UnknownEntriesSome<serde_yaml::Value>>>, Error>
where
K: Clone + Debug + DeserializeOwned + Eq + Hash + Send + Sync + 'static,
BoxDT: DataTypeWrapper + Send + 'static,
F: FnOnce(serde_yaml::Error) -> Error + Send,
{
if file_path.exists() {
let type_map_opt = self
.read_with_sync_api(thread_name, file_path, |file| {
let deserializer = serde_yaml::Deserializer::from_reader(file);
let type_map_opt = type_reg
.deserialize_map_opt_with_unknowns::<'_, serde_yaml::Value, _, _>(
deserializer,
)
.map_err(f_map_err)?;
Result::<_, Error>::Ok(type_map_opt)
})
.await?;
Ok(Some(type_map_opt))
} else {
Ok(None)
}
}
/// Writes a serializable item to the given path.
///
/// # Parameters
///
/// * `thread_name`: Name of the thread to use to do the write operation.
/// * `file_path`: Path to the file to store the serialized item.
/// * `t`: Item to serialize.
/// * `f_map_err`: Maps the serialization error (if any) to an [`Error`].
pub async fn serialized_write<T, F>(
&self,
thread_name: String,
file_path: &Path,
t: &T,
f_map_err: F,
) -> Result<(), Error>
where
T: Serialize + Send + Sync,
F: FnOnce(serde_yaml::Error) -> Error + Send,
{
self.write_with_sync_api(thread_name, file_path, |file| {
serde_yaml::to_writer(file, t).map_err(f_map_err)
})
.await?;
Ok(())
}
/// Serializes an item to a string.
///
/// # Parameters
///
/// * `t`: Item to serialize.
/// * `f_map_err`: Maps the serialization error (if any) to an [`Error`].
pub fn serialized_write_string<T, F>(&self, t: &T, f_map_err: F) -> Result<String, Error>
where
T: Serialize + Send + Sync,
F: FnOnce(serde_yaml::Error) -> Error + Send,
{
serde_yaml::to_string(t).map_err(f_map_err)
}
/// Reads from a file, bridging to libraries that take a synchronous `Write`
/// type.
///
/// This method buffers the write, and calls flush on the buffer when the
/// passed in closure returns.
pub async fn read_with_sync_api<'f, F, T, E>(
&self,
thread_name: String,
file_path: &Path,
f: F,
) -> Result<T, E>
where
F: FnOnce(&mut SyncIoBridge<BufReader<File>>) -> Result<T, E> + Send + 'f,
T: Send,
E: From<Error> + Send,
{
let file = File::open(file_path).await.map_err(
// Tests currently don't cover file system failure cases,
// e.g. disk space limits.
#[cfg_attr(coverage_nightly, coverage(off))]
|error| {
let path = file_path.to_path_buf();
Error::Native(NativeError::FileOpen { path, error })
},
)?;
let mut sync_io_bridge = SyncIoBridge::new(BufReader::new(file));
// `tokio::task::spawn_blocking` doesn't work because it needs the closure's
// environment to be `'static`
let t = std::thread::scope(move |s| {
std::thread::Builder::new()
.name(thread_name)
.spawn_scoped(s, move || f(&mut sync_io_bridge))
.map_err(NativeError::StorageSyncThreadSpawn)
.map_err(Error::Native)?
.join()
.map_err(Mutex::new)
.map_err(NativeError::StorageSyncThreadJoin)
.map_err(Error::Native)?
})?;
Ok(t)
}
/// Writes to a file, bridging to libraries that take a synchronous `Write`
/// type.
///
/// This method buffers the write, and calls flush on the buffer when the
/// passed in closure returns.
///
/// # Parameters
///
/// * `thread_name`: Name of the thread to use to do the write operation.
/// * `file_path`: Path to the file to store the serialized item.
/// * `f`: Function that is given the `Write` implementation to call the
/// sync API with.
pub async fn write_with_sync_api<'f, F, T>(
&self,
thread_name: String,
file_path: &Path,
f: F,
) -> Result<T, Error>
where
F: FnOnce(&mut SyncIoBridge<BufWriter<File>>) -> Result<T, Error> + Send + 'f,
T: Send,
{
let file = File::create(file_path).await.map_err(
// Tests currently don't cover file system failure cases,
// e.g. disk space limits.
#[cfg_attr(coverage_nightly, coverage(off))]
|error| {
let path = file_path.to_path_buf();
NativeError::FileCreate { path, error }
},
)?;
let mut sync_io_bridge = SyncIoBridge::new(BufWriter::new(file));
// `tokio::task::spawn_blocking` doesn't work because it needs the closure's
// environment to be `'static`
let t = std::thread::scope(move |s| {
std::thread::Builder::new()
.name(thread_name)
.spawn_scoped(s, move || {
let t = f(&mut sync_io_bridge)?;
sync_io_bridge.flush().map_err(
// Tests currently don't cover file system failure cases,
// e.g. disk space limits.
#[cfg_attr(coverage_nightly, coverage(off))]
|error| {
let path = file_path.to_path_buf();
NativeError::FileWrite { path, error }
},
)?;
Result::<_, Error>::Ok(t)
})
.map_err(NativeError::StorageSyncThreadSpawn)
.map_err(Error::Native)?
.join()
.map_err(Mutex::new)
.map_err(NativeError::StorageSyncThreadJoin)
.map_err(Error::Native)?
})?;
Ok(t)
}
}