-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add crc value, blob cache & index writer trait
- Loading branch information
1 parent
246d941
commit d599e9a
Showing
15 changed files
with
238 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::ValueHandle; | ||
use quick_cache::{sync::Cache, Weighter}; | ||
use std::sync::Arc; | ||
|
||
type CacheKey = ValueHandle; | ||
type Item = Arc<[u8]>; | ||
|
||
#[derive(Clone)] | ||
struct BlobWeighter; | ||
|
||
impl Weighter<CacheKey, Item> for BlobWeighter { | ||
fn weight(&self, _: &CacheKey, blob: &Item) -> u32 { | ||
// TODO: quick_cache only supports u32 as weight...? | ||
blob.len() as u32 | ||
} | ||
} | ||
|
||
/// Blob cache, in which blobs are cached in-memory | ||
/// after being retrieved from disk | ||
/// | ||
/// This speeds up consecutive accesses to the same blobs, improving | ||
/// read performance for hot data. | ||
pub struct BlobCache { | ||
data: Cache<CacheKey, Item, BlobWeighter>, | ||
capacity: u64, | ||
} | ||
|
||
impl std::fmt::Debug for BlobCache { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "BlobCache<cap: {} bytes>", self.capacity) | ||
} | ||
} | ||
|
||
impl BlobCache { | ||
/// Creates a new block cache with roughly `n` bytes of capacity | ||
#[must_use] | ||
pub fn with_capacity_bytes(bytes: u64) -> Self { | ||
Self { | ||
data: Cache::with_weighter(10_000, bytes, BlobWeighter), | ||
capacity: bytes, | ||
} | ||
} | ||
|
||
pub fn insert(&self, handle: CacheKey, value: Arc<[u8]>) { | ||
self.data.insert(handle, value); | ||
} | ||
|
||
pub fn get(&self, handle: &CacheKey) -> Option<Item> { | ||
self.data.get(handle) | ||
} | ||
|
||
/// Returns the cache capacity in bytes | ||
#[must_use] | ||
pub fn capacity(&self) -> u64 { | ||
self.capacity | ||
} | ||
|
||
/// Returns the number of cached blocks | ||
#[must_use] | ||
pub fn len(&self) -> usize { | ||
self.data.len() | ||
} | ||
|
||
/// Returns `true` if there are no cached blocks | ||
#[must_use] | ||
pub fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.