From 254a0a9342c84d8318989d2a65a705a122e82bd2 Mon Sep 17 00:00:00 2001 From: PThorpe92 Date: Tue, 2 Sep 2025 09:45:45 -0400 Subject: [PATCH] Apply fix and rename ignore_existing to upsert --- core/storage/page_cache.rs | 44 ++++++++++++++++++++++---------------- core/storage/pager.rs | 12 +++++------ 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/core/storage/page_cache.rs b/core/storage/page_cache.rs index 0cf05841c..ba18b48f4 100644 --- a/core/storage/page_cache.rs +++ b/core/storage/page_cache.rs @@ -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)?; diff --git a/core/storage/pager.rs b/core/storage/pager.rs index 345f8335c..737abf7c1 100644 --- a/core/storage/pager.rs +++ b/core/storage/pager.rs @@ -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(()) }