-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some update for JSegcache #452
base: master
Are you sure you want to change the base?
Changes from all commits
9a956d5
80dcd62
331e4c1
5a15cb2
fe8b0ff
4b9bf76
87096f2
5c1a71a
491060d
f370786
14cb8ec
2a5e5e6
c3c5dd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ mod reserved; | |
#[cfg(any(feature = "magic", feature = "debug"))] | ||
pub(crate) use header::ITEM_MAGIC_SIZE; | ||
|
||
use crate::hashtable::FREQ_MASK; | ||
use crate::SegError; | ||
use crate::Value; | ||
|
||
|
@@ -21,13 +22,14 @@ pub(crate) use reserved::ReservedItem; | |
/// Items are the base unit of data stored within the cache. | ||
pub struct Item { | ||
cas: u32, | ||
age: u32, | ||
raw: RawItem, | ||
} | ||
|
||
impl Item { | ||
/// Creates a new `Item` from its parts | ||
pub(crate) fn new(raw: RawItem, cas: u32) -> Self { | ||
Item { cas, raw } | ||
pub(crate) fn new(raw: RawItem, age: u32, cas: u32) -> Self { | ||
Item { cas, age, raw } | ||
} | ||
|
||
/// If the `magic` or `debug` features are enabled, this allows for checking | ||
|
@@ -56,6 +58,10 @@ impl Item { | |
self.cas | ||
} | ||
|
||
pub fn age(&self) -> u32 { | ||
self.age | ||
} | ||
|
||
/// Borrow the optional data | ||
pub fn optional(&self) -> Option<&[u8]> { | ||
self.raw.optional() | ||
|
@@ -83,6 +89,104 @@ impl std::fmt::Debug for Item { | |
} | ||
} | ||
|
||
/// Items are the base unit of data stored within the cache. | ||
pub struct RichItem { | ||
item: Item, | ||
item_info: u64, | ||
item_info_ptr: *const u64, | ||
} | ||
|
||
impl RichItem { | ||
/// Creates a new `Item` from its parts | ||
pub(crate) fn new( | ||
raw: RawItem, | ||
age: u32, | ||
cas: u32, | ||
item_info: u64, | ||
item_info_ptr: *const u64, | ||
) -> Self { | ||
let item = Item::new(raw, age, cas); | ||
RichItem { | ||
item, | ||
item_info, | ||
item_info_ptr, | ||
} | ||
} | ||
|
||
/// If the `magic` or `debug` features are enabled, this allows for checking | ||
/// that the magic bytes at the start of an item match the expected value. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the magic bytes are incorrect, indicating that the data has | ||
/// become corrupted or the item was loaded from the wrong offset. | ||
pub(crate) fn check_magic(&self) { | ||
self.item.raw.check_magic() | ||
} | ||
|
||
/// Borrow the item key | ||
pub fn key(&self) -> &[u8] { | ||
self.item.raw.key() | ||
} | ||
|
||
/// Borrow the item value | ||
pub fn value(&self) -> Value { | ||
self.item.raw.value() | ||
} | ||
|
||
/// CAS value for the item | ||
pub fn cas(&self) -> u32 { | ||
self.item.cas | ||
} | ||
|
||
pub fn age(&self) -> u32 { | ||
self.item.age | ||
} | ||
|
||
pub fn item(&self) -> &Item { | ||
&self.item | ||
} | ||
|
||
pub fn item_mut(&mut self) -> &mut Item { | ||
&mut self.item | ||
} | ||
|
||
// used to support multi readers and single writer | ||
// return true, if the item is evicted/updated since being | ||
// read from the hash table | ||
pub fn is_not_changed(&self) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't love the |
||
unsafe { return self.item_info == *self.item_info_ptr & !FREQ_MASK } | ||
} | ||
|
||
/// Borrow the optional data | ||
pub fn optional(&self) -> Option<&[u8]> { | ||
self.item.raw.optional() | ||
} | ||
|
||
/// Perform a wrapping addition on the value. Returns an error if the item | ||
/// is not a numeric type. | ||
pub fn wrapping_add(&mut self, rhs: u64) -> Result<(), SegError> { | ||
self.item.raw.wrapping_add(rhs) | ||
} | ||
|
||
/// Perform a saturating subtraction on the value. Returns an error if the | ||
/// item is not a numeric type. | ||
pub fn saturating_sub(&mut self, rhs: u64) -> Result<(), SegError> { | ||
self.item.raw.saturating_sub(rhs) | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for RichItem { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { | ||
f.debug_struct("Item") | ||
.field("cas", &self.cas()) | ||
.field("raw", &self.item.raw) | ||
.field("item_info", &self.item_info) | ||
.field("item_info_ptr", &self.item_info_ptr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this print the ptr address? Is that really useful? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it is not useful, printing the value itself is not very useful as well. I can print all three (two value, one pointer) or we can remove the print. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like printing if it's stale or not would maybe have value here? |
||
.finish() | ||
} | ||
} | ||
|
||
pub fn size_of(value: &Value) -> usize { | ||
match value { | ||
Value::Bytes(v) => v.len(), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason this can't be:
It seems like re-using the
Item
type and having the wrapper type contain the additional fields for "rich" functionality would be a better way to go hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, your proposal is better, I will change it :)