@@ -11,9 +11,12 @@ use crate::{
11
11
use async_trait:: async_trait;
12
12
use std:: {
13
13
collections:: BTreeMap ,
14
- fs,
15
14
path:: { Path , PathBuf } ,
16
15
} ;
16
+ use tokio:: {
17
+ fs:: File ,
18
+ io:: { AsyncReadExt , AsyncWriteExt } ,
19
+ } ;
17
20
18
21
type InnerStructure = BTreeMap < ImmutableFileName , HexEncodedDigest > ;
19
22
@@ -32,24 +35,30 @@ impl JsonImmutableFileDigestCacheProvider {
32
35
33
36
#[ cfg( test) ]
34
37
/// [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 {
36
39
let provider = Self :: new ( filepath) ;
37
- provider. write_data ( values) . unwrap ( ) ;
40
+ provider. write_data ( values) . await . unwrap ( ) ;
38
41
provider
39
42
}
40
43
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 ?;
44
51
45
52
Ok ( ( ) )
46
53
}
47
54
48
- fn read_data ( & self ) -> Result < InnerStructure , ImmutableDigesterCacheGetError > {
55
+ async fn read_data ( & self ) -> Result < InnerStructure , ImmutableDigesterCacheGetError > {
49
56
match self . filepath . exists ( ) {
50
57
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) ?;
53
62
Ok ( values)
54
63
}
55
64
false => Ok ( BTreeMap :: new ( ) ) ,
@@ -63,11 +72,11 @@ impl ImmutableFileDigestCacheProvider for JsonImmutableFileDigestCacheProvider {
63
72
& self ,
64
73
digest_per_filenames : Vec < ( ImmutableFileName , HexEncodedDigest ) > ,
65
74
) -> CacheProviderResult < ( ) > {
66
- let mut data = self . read_data ( ) ?;
75
+ let mut data = self . read_data ( ) . await ?;
67
76
for ( filename, digest) in digest_per_filenames {
68
77
data. insert ( filename, digest) ;
69
78
}
70
- self . write_data ( data) ?;
79
+ self . write_data ( data) . await ?;
71
80
72
81
Ok ( ( ) )
73
82
}
@@ -76,7 +85,7 @@ impl ImmutableFileDigestCacheProvider for JsonImmutableFileDigestCacheProvider {
76
85
& self ,
77
86
immutables : Vec < ImmutableFile > ,
78
87
) -> CacheProviderResult < BTreeMap < ImmutableFile , Option < HexEncodedDigest > > > {
79
- let values = self . read_data ( ) ?;
88
+ let values = self . read_data ( ) . await ?;
80
89
let mut result = BTreeMap :: new ( ) ;
81
90
82
91
for immutable in immutables {
@@ -154,7 +163,8 @@ mod tests {
154
163
( "0.chunk" . to_string ( ) , "digest 0" . to_string ( ) ) ,
155
164
( "1.chunk" . to_string ( ) , "digest 1" . to_string ( ) ) ,
156
165
] ) ,
157
- ) ;
166
+ )
167
+ . await ;
158
168
let expected: BTreeMap < _ , _ > = BTreeMap :: from ( [ (
159
169
ImmutableFile :: dummy ( PathBuf :: default ( ) , 0 , "0.chunk" . to_string ( ) ) ,
160
170
Some ( "digest 0" . to_string ( ) ) ,
@@ -176,7 +186,8 @@ mod tests {
176
186
let provider = JsonImmutableFileDigestCacheProvider :: from (
177
187
& file,
178
188
BTreeMap :: from ( [ ( "0.chunk" . to_string ( ) , "digest 0" . to_string ( ) ) ] ) ,
179
- ) ;
189
+ )
190
+ . await ;
180
191
let expected: BTreeMap < _ , _ > = BTreeMap :: from ( [ (
181
192
ImmutableFile :: dummy ( PathBuf :: default ( ) , 2 , "2.chunk" . to_string ( ) ) ,
182
193
None ,
@@ -201,7 +212,8 @@ mod tests {
201
212
( "1.chunk" . to_string ( ) , "keep me" . to_string ( ) ) ,
202
213
( "2.chunk" . to_string ( ) , "keep me too" . to_string ( ) ) ,
203
214
] ) ,
204
- ) ;
215
+ )
216
+ . await ;
205
217
let values_to_store = vec ! [
206
218
( "0.chunk" . to_string( ) , "updated" . to_string( ) ) ,
207
219
( "1.chunk" . to_string( ) , "keep me" . to_string( ) ) ,
0 commit comments