From b42a1ef2728d786fa7fa5ab2d3d3897e5cec9ea7 Mon Sep 17 00:00:00 2001 From: meteorgan Date: Sat, 12 Jul 2025 17:50:54 +0800 Subject: [PATCH] minor improvements based on PR comments --- core/lib.rs | 13 ++++--------- core/storage/pager.rs | 11 ++++++----- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/core/lib.rs b/core/lib.rs index 540f81761..0e763a67a 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -179,7 +179,6 @@ impl Database { ) -> Result> { let wal_path = format!("{path}-wal"); let maybe_shared_wal = WalFileShared::open_shared_if_exists(&io, wal_path.as_str())?; - let db_size = db_file.size()?; let mv_store = if enable_mvcc { Some(Rc::new(MvStore::new( @@ -189,12 +188,9 @@ impl Database { } else { None }; - let wal_has_frames = maybe_shared_wal - .as_ref() - .is_some_and(|wal| unsafe { &*wal.get() }.max_frame.load(Ordering::SeqCst) > 0); - // No pages in DB file or WAL -> empty database - let db_state = if db_size == 0 && !wal_has_frames { + let db_size = db_file.size()?; + let db_state = if db_size == 0 { DB_STATE_UNINITIALIZED } else { DB_STATE_INITIALIZED @@ -228,10 +224,9 @@ impl Database { .expect("lock on schema should succeed first try"); let syms = conn.syms.borrow(); + let pager = conn.pager.borrow().clone(); - if let Err(LimboError::ExtensionError(e)) = - schema.make_from_btree(None, conn.pager.clone(), &syms) - { + if let Err(LimboError::ExtensionError(e)) = schema.make_from_btree(None, pager, &syms) { // this means that a vtab exists and we no longer have the module loaded. we print // a warning to the user to load the module eprintln!("Warning: {e}"); diff --git a/core/storage/pager.rs b/core/storage/pager.rs index 2b2d2cd32..9d9a927d5 100644 --- a/core/storage/pager.rs +++ b/core/storage/pager.rs @@ -9,7 +9,7 @@ use crate::types::CursorResult; use crate::Completion; use crate::{Buffer, Connection, LimboError, Result}; use parking_lot::RwLock; -use std::cell::{OnceCell, RefCell, UnsafeCell}; +use std::cell::{Cell, OnceCell, RefCell, UnsafeCell}; use std::collections::HashSet; use std::rc::Rc; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -225,7 +225,7 @@ pub struct Pager { /// Cache page_size and reserved_space at Pager init and reuse for subsequent /// `usable_space` calls. TODO: Invalidate reserved_space when we add the functionality /// to change it. - page_size: RefCell>, + page_size: Cell>, reserved_space: OnceCell, } @@ -291,7 +291,7 @@ impl Pager { db_state, init_lock, allocate_page1_state, - page_size: RefCell::new(None), + page_size: Cell::new(None), reserved_space: OnceCell::new(), }) } @@ -586,7 +586,7 @@ impl Pager { pub fn usable_space(&self) -> usize { let page_size = *self .page_size - .borrow_mut() + .get() .get_or_insert_with(|| header_accessor::get_page_size(self).unwrap_or_default()); let reserved_space = *self @@ -598,6 +598,7 @@ impl Pager { /// Set the initial page size for the database. Should only be called before the database is initialized pub fn set_initial_page_size(&self, size: u32) { + assert_eq!(self.db_state.load(Ordering::SeqCst), DB_STATE_UNINITIALIZED); self.page_size.replace(Some(size)); } @@ -1063,7 +1064,7 @@ impl Pager { self.db_state.store(DB_STATE_INITIALIZING, Ordering::SeqCst); let mut default_header = DatabaseHeader::default(); default_header.database_size += 1; - if let Some(size) = *self.page_size.borrow() { + if let Some(size) = self.page_size.get() { default_header.update_page_size(size); } let page = allocate_page(1, &self.buffer_pool, 0);