Apply fix and rename ignore_existing to upsert

This commit is contained in:
PThorpe92
2025-09-02 09:45:45 -04:00
parent 3a0b9b360a
commit 254a0a9342
2 changed files with 31 additions and 25 deletions

View File

@@ -195,11 +195,7 @@ impl PageCache {
}
#[inline]
pub fn insert_ignore_existing(
&mut self,
key: PageCacheKey,
value: PageRef,
) -> Result<(), CacheError> {
pub fn upsert_page(&mut self, key: PageCacheKey, value: PageRef) -> Result<(), CacheError> {
self._insert(key, value, true)
}
@@ -207,23 +203,35 @@ impl PageCache {
&mut self,
key: PageCacheKey,
value: PageRef,
ignore_exists: bool,
update_in_place: bool,
) -> Result<(), CacheError> {
trace!("insert(key={:?})", key);
// Check first if page already exists in cache
if let Some(slot) = self.map.borrow().get(&key) {
if !ignore_exists {
let existing = self.entries.borrow()[slot]
.page
.as_ref()
.expect("map points to empty slot")
.clone();
turso_assert!(
Arc::ptr_eq(&existing, &value),
"Attempted to insert different page with same key: {key:?}"
);
return Err(CacheError::KeyExists);
let slot = { self.map.borrow().get(&key) };
if let Some(slot) = slot {
{
let existing = &mut self.entries.borrow_mut()[slot];
existing.ref_bit.set(true);
if update_in_place {
// update it in place
existing.page = Some(value);
} else {
// the page we are inserting must match the existing page
turso_assert!(
Arc::ptr_eq(
existing.page.as_ref().expect("map points to empty slot"),
&value
),
"Attempted to insert different page with same key: {key:?}"
);
}
}
self.move_to_front(slot);
return if update_in_place {
Ok(())
} else {
Err(CacheError::KeyExists)
};
}
// Key doesn't exist, proceed with new entry
self.make_room_for(1)?;

View File

@@ -2090,13 +2090,11 @@ impl Pager {
// FIXME: use specific page key for writer instead of max frame, this will make readers not conflict
assert!(page.is_dirty());
cache
.insert_ignore_existing(page_key, page.clone())
.map_err(|e| {
LimboError::InternalError(format!(
"Failed to insert loaded page {id} into cache: {e:?}"
))
})?;
cache.upsert_page(page_key, page.clone()).map_err(|e| {
LimboError::InternalError(format!(
"Failed to insert loaded page {id} into cache: {e:?}"
))
})?;
page.set_loaded();
Ok(())
}