use super::Cache;
#[test]
fn row_cache_when_given_a_block_subslice_stores_a_detached_copy() {
let cache = Cache::with_capacity_bytes(1024 * 1024);
let id = crate::table::GlobalTableId::from((0, 0));
let block = crate::Slice::from(vec![7_u8; 4096]);
let block_range = {
let start = block.as_ptr() as usize;
start..start + block.len()
};
let value = block.slice(0..64);
assert!(
block_range.contains(&(value.as_ptr() as usize)),
"precondition: a subslice past the inline threshold points into the block's own buffer",
);
cache.insert_row(
id,
1,
crate::InternalValue {
key: crate::key::InternalKey::new(
crate::UserKey::from(&b"k"[..]),
1,
crate::ValueType::Value,
),
value,
},
);
let Some(got) = cache.get_row(id, 1, b"k") else {
panic!("the row was just inserted, so the lookup must hit");
};
assert!(
!block_range.contains(&(got.value.as_ptr() as usize)),
"the cached row still points into the block it was read from, so it \
keeps the whole block alive while being charged only its own bytes",
);
assert_eq!(&*got.value, &[7_u8; 64][..]);
}
#[test]
fn metadata_priority_defaults_on_and_toggles() {
assert!(Cache::with_capacity_bytes(1024).metadata_priority());
let off = Cache::with_capacity_bytes(1024).with_metadata_priority(false);
assert!(!off.metadata_priority());
assert!(off.with_metadata_priority(true).metadata_priority());
}
#[test]
fn a_blob_lookup_under_a_conflicting_key_misses_rather_than_serving_the_other_value() {
use crate::vlog::ValueHandle;
let cache = Cache::with_capacity_bytes(1024 * 1024);
let vhandle = ValueHandle {
blob_file_id: 7,
offset: 4096,
on_disk_size: 5,
};
cache.insert_blob(
0,
&vhandle,
b"real-key",
crate::UserValue::from(&b"value"[..]),
);
assert_eq!(
cache.get_blob(0, &vhandle, b"real-key").as_deref(),
Some(&b"value"[..]),
"the owning key must still hit",
);
assert!(
cache.get_blob(0, &vhandle, b"other-key").is_none(),
"a conflicting key must not be served the value at that offset",
);
}
#[test]
fn a_blob_lookup_with_a_conflicting_size_misses_rather_than_serving_the_value() {
use crate::vlog::ValueHandle;
let cache = Cache::with_capacity_bytes(1024 * 1024);
let stored = ValueHandle {
blob_file_id: 7,
offset: 4096,
on_disk_size: 5,
};
cache.insert_blob(0, &stored, b"key", crate::UserValue::from(&b"value"[..]));
assert_eq!(
cache.get_blob(0, &stored, b"key").as_deref(),
Some(&b"value"[..]),
"the handle it was stored under must still hit",
);
let conflicting = ValueHandle {
on_disk_size: 9,
..stored
};
assert!(
cache.get_blob(0, &conflicting, b"key").is_none(),
"a handle declaring a different size must not be served the cached value",
);
}