11use std:: fs;
2- use std:: io:: { BufReader , ErrorKind , Write } ;
2+ use std:: io:: { ErrorKind , Write } ;
33use std:: path:: PathBuf ;
44
55use serde:: { Deserialize , Serialize } ;
@@ -11,6 +11,7 @@ use crate::records::Check;
1111pub const DB_NAME : & str = "netpulse.store" ;
1212/// Path to the database of netpulse (combine with [DB_NAME])
1313pub const DB_PATH : & str = "/var/lib/netpulse" ;
14+ pub const ZSTD_COMPRESSION_LEVEL : i32 = 4 ;
1415
1516#[ derive( Debug , PartialEq , Eq , Hash , Deserialize , Serialize ) ]
1617pub struct Store {
@@ -33,7 +34,7 @@ impl Store {
3334 . expect ( "the store path has no parent directory" ) ,
3435 ) ?;
3536
36- let mut file = match fs:: File :: options ( )
37+ let file = match fs:: File :: options ( )
3738 . read ( false )
3839 . write ( true )
3940 . append ( false )
@@ -45,9 +46,10 @@ impl Store {
4546 } ;
4647
4748 let store = Store :: new ( ) ;
49+ let mut writer = zstd:: Encoder :: new ( file, ZSTD_COMPRESSION_LEVEL ) ?;
4850
49- file . write_all ( & bincode:: serialize ( & store) ?) ?;
50- file . flush ( ) ?;
51+ writer . write_all ( & bincode:: serialize ( & store) ?) ?;
52+ writer . flush ( ) ?;
5153 Ok ( store)
5254 }
5355
@@ -78,7 +80,7 @@ impl Store {
7880 } ,
7981 } ;
8082
81- let reader = BufReader :: new ( file) ;
83+ let reader = zstd :: Decoder :: new ( file) ? ;
8284
8385 Ok ( bincode:: deserialize_from ( reader) ?)
8486 }
0 commit comments