Skip to content

Commit fcc197b

Browse files
committed
feat(store)!: use zstd compression
1 parent 288fe53 commit fcc197b

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ daemonize = "0.5"
1919
bincode = "1.3.3"
2020
flagset = { version = "0.4.6", features = ["serde"] }
2121
thiserror = "2.0.0"
22+
zstd = "0.13.2"
2223

2324
[[bin]] # client
2425
name = "netpulse"

src/store.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::fs;
2-
use std::io::{BufReader, ErrorKind, Write};
2+
use std::io::{ErrorKind, Write};
33
use std::path::PathBuf;
44

55
use serde::{Deserialize, Serialize};
@@ -11,6 +11,7 @@ use crate::records::Check;
1111
pub const DB_NAME: &str = "netpulse.store";
1212
/// Path to the database of netpulse (combine with [DB_NAME])
1313
pub const DB_PATH: &str = "/var/lib/netpulse";
14+
pub const ZSTD_COMPRESSION_LEVEL: i32 = 4;
1415

1516
#[derive(Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
1617
pub 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

Comments
 (0)