Skip to content

Commit

Permalink
refactor: simplify record, restrict record's visibility
Browse files Browse the repository at this point in the history
Signed-off-by: MrCroxx <[email protected]>
  • Loading branch information
MrCroxx committed Jan 8, 2025
1 parent 309ea6c commit e093ec0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 52 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ members = [
]

[workspace.package]
version = "0.13.1"
version = "0.14.0-dev"
edition = "2021"
rust-version = "1.81.0"
repository = "https://github.com/foyer-rs/foyer"
Expand Down Expand Up @@ -53,10 +53,10 @@ opentelemetry_0_26 = { package = "opentelemetry", version = "0.26" }
prometheus-client_0_22 = { package = "prometheus-client", version = "0.22" }

# foyer components
foyer-common = { version = "0.13.1", path = "foyer-common" }
foyer-memory = { version = "0.13.1", path = "foyer-memory" }
foyer-storage = { version = "0.13.1", path = "foyer-storage" }
foyer = { version = "0.13.1", path = "foyer" }
foyer-common = { version = "0.14.0-dev", path = "foyer-common" }
foyer-memory = { version = "0.14.0-dev", path = "foyer-memory" }
foyer-storage = { version = "0.14.0-dev", path = "foyer-storage" }
foyer = { version = "0.14.0-dev", path = "foyer" }

[workspace.lints.rust]
missing_docs = "warn"
Expand Down
7 changes: 4 additions & 3 deletions foyer-memory/src/eviction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ use std::sync::Arc;

use itertools::Itertools;

use super::{Eviction, Op};
use crate::Record;

use crate::{
eviction::{Eviction, Op},
record::Record,
};
#[expect(dead_code)]
pub trait OpExt: Eviction {
fn acquire_immutable(&self, record: &Arc<Record<Self>>) {
Expand Down
2 changes: 1 addition & 1 deletion foyer-memory/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ pub use crate::{
error::{Error, Result},
eviction::{fifo::FifoConfig, lfu::LfuConfig, lru::LruConfig, s3fifo::S3FifoConfig, Eviction, Op},
raw::{FetchMark, FetchState, Weighter},
record::{CacheHint, Record},
record::CacheHint,
};
52 changes: 9 additions & 43 deletions foyer-memory/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ pub struct Record<E>
where
E: Eviction,
{
data: Option<Data<E>>,
data: Data<E>,
state: UnsafeCell<E::State>,
/// Reference count used in the in-memory cache.
refs: AtomicUsize,
flags: AtomicU64,
}
Expand All @@ -81,11 +82,7 @@ where
E: Eviction,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut s = f.debug_struct("Record");
if let Some(data) = self.data.as_ref() {
s.field("hash", &data.hash);
}
s.finish()
f.debug_struct("Record").field("hash", &self.data.hash).finish()

Check warning on line 85 in foyer-memory/src/record.rs

View check run for this annotation

Codecov / codecov/patch

foyer-memory/src/record.rs#L85

Added line #L85 was not covered by tests
}
}

Expand All @@ -99,67 +96,36 @@ where
/// Create a record with data.
pub fn new(data: Data<E>) -> Self {
Record {
data: Some(data),
data,
state: Default::default(),
refs: AtomicUsize::new(0),
flags: AtomicU64::new(0),
}
}

/// Create a record without data.
pub fn empty() -> Self {
Record {
data: None,
state: Default::default(),
refs: AtomicUsize::new(0),
flags: AtomicU64::new(0),
}
}

/// Wrap the data in the record.
///
/// # Safety
///
/// Panics if the record is already wrapping data.
pub fn insert(&mut self, data: Data<E>) {
assert!(self.data.replace(data).is_none());
}

/// Unwrap the inner data.
///
/// # Safety
///
/// Panics if the record is not wrapping data.
pub fn take(&mut self) -> Data<E> {
self.state = Default::default();
self.refs.store(0, Ordering::SeqCst);
self.flags.store(0, Ordering::SeqCst);
self.data.take().unwrap()
}

/// Get the immutable reference of the record key.
pub fn key(&self) -> &E::Key {
&self.data.as_ref().unwrap().key
&self.data.key
}

/// Get the immutable reference of the record value.
pub fn value(&self) -> &E::Value {
&self.data.as_ref().unwrap().value
&self.data.value
}

/// Get the immutable reference of the record hint.
pub fn hint(&self) -> &E::Hint {
&self.data.as_ref().unwrap().hint
&self.data.hint
}

/// Get the record hash.
pub fn hash(&self) -> u64 {
self.data.as_ref().unwrap().hash
self.data.hash
}

/// Get the record weight.
pub fn weight(&self) -> usize {
self.data.as_ref().unwrap().weight
self.data.weight
}

/// Get the record state wrapped with [`UnsafeCell`].
Expand Down

0 comments on commit e093ec0

Please sign in to comment.