Skip to content

Commit 3df6c22

Browse files
committed
remove need for serde_json
1 parent 047aafd commit 3df6c22

File tree

6 files changed

+52
-26
lines changed

6 files changed

+52
-26
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ env:
1313

1414
jobs:
1515
test:
16-
timeout-minutes: 20
16+
timeout-minutes: 10
1717
strategy:
1818
matrix:
1919
rust_version:
@@ -38,8 +38,10 @@ jobs:
3838
uses: Swatinem/rust-cache@v2
3939
with:
4040
prefix-key: ${{ runner.os }}-cargo
41+
- name: Install cargo-all-features
42+
run: cargo install cargo-all-features
4143
- name: Build
42-
run: cargo build -v
44+
run: cargo build-all-features
4345
- name: Format
4446
run: cargo fmt --all -- --check
4547
- name: Clippy
@@ -49,7 +51,7 @@ jobs:
4951
env:
5052
RUST_LOG: debug
5153
cross:
52-
timeout-minutes: 20
54+
timeout-minutes: 10
5355
name: cross
5456
strategy:
5557
matrix:

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ path = "src/lib.rs"
2121
[features]
2222
default = ["lz4"]
2323
lz4 = ["dep:lz4_flex"]
24+
serde = ["dep:serde"]
2425

2526
[dependencies]
2627
byteorder = "1.5.0"
@@ -31,12 +32,11 @@ min-max-heap = "1.3.0"
3132
path-absolutize = "3.1.1"
3233
quick_cache = "0.4.1"
3334
rand = "0.8.5"
34-
serde = { version = "1.0.197", default-features = false, features = [
35+
serde = { version = "1.0.197", optional = true, features = [
3536
"alloc",
36-
"rc",
3737
"derive",
38+
"rc",
3839
] }
39-
serde_json = { version = "1.0.114" }
4040
tempfile = "3.10.0"
4141

4242
[dev-dependencies]

src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ pub enum Error {
88

99
/// Invalid data format version
1010
InvalidVersion(Option<Version>),
11-
12-
/// CRC check failed
13-
CrcMismatch,
11+
// TODO:
12+
// /// CRC check failed
13+
// CrcMismatch,
1414
}
1515

1616
impl std::fmt::Display for Error {

src/handle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::id::SegmentId;
2-
use serde::{Deserialize, Serialize};
32
use std::hash::Hash;
43

54
/// A value handle points into the value log.
65
#[allow(clippy::module_name_repetitions)]
7-
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
6+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
7+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
88
pub struct ValueHandle {
99
/// Segment ID
1010
pub segment_id: SegmentId,

src/manifest.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use crate::{id::SegmentId, segment::stats::Stats, Segment, SegmentWriter as MultiWriter};
2+
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
23
use std::{
34
collections::HashMap,
45
fs::File,
5-
io::Write,
6+
io::{Cursor, Write},
67
path::{Path, PathBuf},
78
sync::{atomic::AtomicU64, Arc, RwLock},
89
};
910

1011
pub const VLOG_MARKER: &str = ".vlog";
1112
pub const SEGMENTS_FOLDER: &str = "segments";
13+
const MANIFEST_FILE: &str = "vlog_manifest";
1214

1315
/// Atomically rewrites a file
1416
fn rewrite_atomic<P: AsRef<Path>>(path: P, content: &[u8]) -> std::io::Result<()> {
@@ -53,8 +55,6 @@ impl SegmentManifest {
5355
folder: P,
5456
registered_ids: &[u64],
5557
) -> crate::Result<()> {
56-
// TODO:
57-
5858
for dirent in std::fs::read_dir(folder)? {
5959
let dirent = dirent?;
6060

@@ -76,14 +76,32 @@ impl SegmentManifest {
7676
Ok(())
7777
}
7878

79+
/// Parses segment IDs from manifest file
80+
fn load_ids_from_disk<P: AsRef<Path>>(path: P) -> crate::Result<Vec<SegmentId>> {
81+
let path = path.as_ref();
82+
log::debug!("Loading manifest from {}", path.display());
83+
84+
let bytes = std::fs::read(path)?;
85+
86+
let mut ids = vec![];
87+
88+
let mut cursor = Cursor::new(bytes);
89+
90+
let cnt = cursor.read_u64::<BigEndian>()?;
91+
92+
for _ in 0..cnt {
93+
ids.push(cursor.read_u64::<BigEndian>()?);
94+
}
95+
96+
Ok(ids)
97+
}
98+
99+
/// Recovers a value log from disk
79100
pub(crate) fn recover<P: AsRef<Path>>(folder: P) -> crate::Result<Self> {
80101
let folder = folder.as_ref();
81-
let path = folder.join("segments.json");
82-
83-
log::debug!("Loading value log manifest from {}", path.display());
102+
let path = folder.join(MANIFEST_FILE);
84103

85-
let str = std::fs::read_to_string(&path)?;
86-
let ids: Vec<u64> = serde_json::from_str(&str).expect("deserialize error");
104+
let ids = Self::load_ids_from_disk(&path)?;
87105

88106
let segments_folder = folder.join(SEGMENTS_FOLDER);
89107
Self::remove_unfinished_segments(&segments_folder, &ids)?;
@@ -112,7 +130,7 @@ impl SegmentManifest {
112130
}
113131

114132
pub(crate) fn create_new<P: AsRef<Path>>(folder: P) -> crate::Result<Self> {
115-
let path = folder.as_ref().join("segments.json");
133+
let path = folder.as_ref().join(MANIFEST_FILE);
116134

117135
let m = Self(Arc::new(SegmentManifestInner {
118136
path,
@@ -159,10 +177,16 @@ impl SegmentManifest {
159177
let path = path.as_ref();
160178
log::trace!("Writing segment manifest to {}", path.display());
161179

162-
// NOTE: Serialization can't fail here
163-
#[allow(clippy::expect_used)]
164-
let json = serde_json::to_string_pretty(&segment_ids).expect("should serialize");
165-
rewrite_atomic(path, json.as_bytes())?;
180+
let mut bytes = Vec::new();
181+
182+
let cnt = segment_ids.len() as u64;
183+
bytes.write_u64::<BigEndian>(cnt)?;
184+
185+
for id in segment_ids {
186+
bytes.write_u64::<BigEndian>(*id)?;
187+
}
188+
189+
rewrite_atomic(path, &bytes)?;
166190

167191
Ok(())
168192
}

src/segment/stats.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use serde::{Deserialize, Serialize};
21
use std::sync::atomic::AtomicU64;
32

4-
#[derive(Debug, Default, Deserialize, Serialize)]
3+
#[derive(Debug, Default)]
4+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
55
pub struct Stats {
66
pub(crate) item_count: AtomicU64,
77
pub(crate) stale_items: AtomicU64,

0 commit comments

Comments
 (0)