Skip to content

Commit

Permalink
feat: add disk cache value crc (apache#447)
Browse files Browse the repository at this point in the history
* add disk cache value crc

* add ut
  • Loading branch information
jiacai2050 authored and Rachelint committed Dec 6, 2022
1 parent 7b16e09 commit f609f7d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/object_store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition.workspace = true
async-trait = { workspace = true }
bytes = { workspace = true }
common_util = { workspace = true }
crc = "3.0.0"
futures = { workspace = true }
upstream = { package = "object_store", version = "0.5.1" }
oss-rust-sdk = "0.4.0"
Expand Down
19 changes: 16 additions & 3 deletions components/object_store/src/disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{collections::BTreeMap, fmt::Display, ops::Range, sync::Arc};
use async_trait::async_trait;
use bytes::{Bytes, BytesMut};
use common_util::time::current_as_rfc3339;
use crc::{Crc, CRC_32_ISCSI};
use futures::stream::BoxStream;
use log::{debug, error, info};
use lru::LruCache;
Expand All @@ -30,6 +31,7 @@ use upstream::{

const MANIFEST_FILE: &str = "manifest.json";
const CURRENT_VERSION: usize = 1;
pub const CASTAGNOLI: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);

#[derive(Debug, Snafu)]
enum Error {
Expand Down Expand Up @@ -188,10 +190,10 @@ impl DiskCache {
file: file_path.clone(),
})?;

let bytes = value.to_vec();
let pb_bytes = proto::oss_cache::Bytes {
// TODO: CRC checking
crc: 0,
value: value.to_vec(),
crc: CASTAGNOLI.checksum(&bytes),
value: bytes,
};

file.write_all(&pb_bytes.encode_to_vec())
Expand Down Expand Up @@ -221,6 +223,7 @@ impl DiskCache {
let bytes = proto::oss_cache::Bytes::decode(&*buf).with_context(|| DecodeCache {
file: file_path.clone(),
})?;
// TODO: CRC checking

Ok(bytes.value.into())
}
Expand Down Expand Up @@ -769,4 +772,14 @@ mod test {
}
};
}

#[test]
fn test_disk_cache_bytes_crc() {
let testcases = vec![("abc", 910901175), ("hello ceresdb", 2026251212)];

for (input, expect) in testcases {
let actual = CASTAGNOLI.checksum(input.as_bytes());
assert_eq!(actual, expect);
}
}
}

0 comments on commit f609f7d

Please sign in to comment.