Skip to content

Commit 05b3e60

Browse files
committed
Use async for json digests cache provider IOs
1 parent e404f98 commit 05b3e60

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

mithril-common/src/digesters/cache/json_provider.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ use crate::{
1111
use async_trait::async_trait;
1212
use std::{
1313
collections::BTreeMap,
14-
fs,
1514
path::{Path, PathBuf},
1615
};
16+
use tokio::{
17+
fs::File,
18+
io::{AsyncReadExt, AsyncWriteExt},
19+
};
1720

1821
type InnerStructure = BTreeMap<ImmutableFileName, HexEncodedDigest>;
1922

@@ -32,24 +35,30 @@ impl JsonImmutableFileDigestCacheProvider {
3235

3336
#[cfg(test)]
3437
/// [Test Only] Build a new [JsonImmutableFileDigestCacheProvider] that contains the given values.
35-
pub fn from(filepath: &Path, values: InnerStructure) -> Self {
38+
pub async fn from(filepath: &Path, values: InnerStructure) -> Self {
3639
let provider = Self::new(filepath);
37-
provider.write_data(values).unwrap();
40+
provider.write_data(values).await.unwrap();
3841
provider
3942
}
4043

41-
fn write_data(&self, values: InnerStructure) -> Result<(), ImmutableDigesterCacheStoreError> {
42-
let file = fs::File::create(&self.filepath)?;
43-
serde_json::to_writer(&file, &values)?;
44+
async fn write_data(
45+
&self,
46+
values: InnerStructure,
47+
) -> Result<(), ImmutableDigesterCacheStoreError> {
48+
let mut file = File::create(&self.filepath).await?;
49+
file.write_all(serde_json::to_string_pretty(&values)?.as_bytes())
50+
.await?;
4451

4552
Ok(())
4653
}
4754

48-
fn read_data(&self) -> Result<InnerStructure, ImmutableDigesterCacheGetError> {
55+
async fn read_data(&self) -> Result<InnerStructure, ImmutableDigesterCacheGetError> {
4956
match self.filepath.exists() {
5057
true => {
51-
let file = fs::File::open(&self.filepath)?;
52-
let values: InnerStructure = serde_json::from_reader(&file)?;
58+
let mut file = File::open(&self.filepath).await?;
59+
let mut json_string = String::new();
60+
file.read_to_string(&mut json_string).await?;
61+
let values: InnerStructure = serde_json::from_str(&json_string)?;
5362
Ok(values)
5463
}
5564
false => Ok(BTreeMap::new()),
@@ -63,11 +72,11 @@ impl ImmutableFileDigestCacheProvider for JsonImmutableFileDigestCacheProvider {
6372
&self,
6473
digest_per_filenames: Vec<(ImmutableFileName, HexEncodedDigest)>,
6574
) -> CacheProviderResult<()> {
66-
let mut data = self.read_data()?;
75+
let mut data = self.read_data().await?;
6776
for (filename, digest) in digest_per_filenames {
6877
data.insert(filename, digest);
6978
}
70-
self.write_data(data)?;
79+
self.write_data(data).await?;
7180

7281
Ok(())
7382
}
@@ -76,7 +85,7 @@ impl ImmutableFileDigestCacheProvider for JsonImmutableFileDigestCacheProvider {
7685
&self,
7786
immutables: Vec<ImmutableFile>,
7887
) -> CacheProviderResult<BTreeMap<ImmutableFile, Option<HexEncodedDigest>>> {
79-
let values = self.read_data()?;
88+
let values = self.read_data().await?;
8089
let mut result = BTreeMap::new();
8190

8291
for immutable in immutables {
@@ -154,7 +163,8 @@ mod tests {
154163
("0.chunk".to_string(), "digest 0".to_string()),
155164
("1.chunk".to_string(), "digest 1".to_string()),
156165
]),
157-
);
166+
)
167+
.await;
158168
let expected: BTreeMap<_, _> = BTreeMap::from([(
159169
ImmutableFile::dummy(PathBuf::default(), 0, "0.chunk".to_string()),
160170
Some("digest 0".to_string()),
@@ -176,7 +186,8 @@ mod tests {
176186
let provider = JsonImmutableFileDigestCacheProvider::from(
177187
&file,
178188
BTreeMap::from([("0.chunk".to_string(), "digest 0".to_string())]),
179-
);
189+
)
190+
.await;
180191
let expected: BTreeMap<_, _> = BTreeMap::from([(
181192
ImmutableFile::dummy(PathBuf::default(), 2, "2.chunk".to_string()),
182193
None,
@@ -201,7 +212,8 @@ mod tests {
201212
("1.chunk".to_string(), "keep me".to_string()),
202213
("2.chunk".to_string(), "keep me too".to_string()),
203214
]),
204-
);
215+
)
216+
.await;
205217
let values_to_store = vec![
206218
("0.chunk".to_string(), "updated".to_string()),
207219
("1.chunk".to_string(), "keep me".to_string()),

0 commit comments

Comments
 (0)