From 0735b7a635879c37077268ed67cc9061a41ee487 Mon Sep 17 00:00:00 2001 From: Arseniy Volynets <vol0ncar@yandex.ru> Date: Mon, 6 May 2024 12:59:47 +0000 Subject: [PATCH] fix: use new mutex api for storage traits --- src/sql/storage.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/sql/storage.rs b/src/sql/storage.rs index 50d6e296b7..098ffebd82 100644 --- a/src/sql/storage.rs +++ b/src/sql/storage.rs @@ -16,6 +16,8 @@ use sbroad::executor::ir::{ConnectionType, ExecutionPlan, QueryType}; use sbroad::executor::lru::{Cache, EvictFn, LRUCache, DEFAULT_CAPACITY}; use sbroad::executor::protocol::{Binary, RequiredData, SchemaInfo}; use sbroad::ir::value::Value; +use sbroad::utils::MutexLike; +use tarantool::fiber::Mutex; use crate::sql::router::{get_table_version, VersionMap}; use crate::traft::node; @@ -28,8 +30,8 @@ use std::{any::Any, cell::RefCell, rc::Rc}; use super::{router::calculate_bucket_id, DEFAULT_BUCKET_COUNT}; thread_local!( - static STATEMENT_CACHE: Rc<RefCell<PicoStorageCache>> = Rc::new( - RefCell::new(PicoStorageCache::new(DEFAULT_CAPACITY, Some(Box::new(unprepare))).unwrap()) + static STATEMENT_CACHE: Rc<Mutex<PicoStorageCache>> = Rc::new( + Mutex::new(PicoStorageCache::new(DEFAULT_CAPACITY, Some(Box::new(unprepare))).unwrap()) ) ); @@ -37,7 +39,7 @@ thread_local!( pub struct StorageRuntime { pub metadata: RefCell<StorageMetadata>, bucket_count: u64, - cache: Rc<RefCell<PicoStorageCache>>, + cache: Rc<Mutex<PicoStorageCache>>, } pub struct PicoStorageCache(LRUCache<SmolStr, (PreparedStmt, VersionMap)>); @@ -130,28 +132,16 @@ impl StorageCache for PicoStorageCache { impl QueryCache for StorageRuntime { type Cache = PicoStorageCache; - fn cache(&self) -> &RefCell<Self::Cache> { - &self.cache + fn cache(&self) -> &impl MutexLike<<Self as QueryCache>::Cache> { + &*self.cache } fn cache_capacity(&self) -> Result<usize, SbroadError> { - Ok(self - .cache() - .try_borrow() - .map_err(|e| { - SbroadError::FailedTo( - Action::Borrow, - Some(Entity::Cache), - format_smolstr!("{e:?}"), - ) - })? - .capacity()) + Ok(self.cache().lock().capacity()) } fn clear_cache(&self) -> Result<(), SbroadError> { - *self.cache.try_borrow_mut().map_err(|e| { - SbroadError::FailedTo(Action::Clear, Some(Entity::Cache), format_smolstr!("{e:?}")) - })? = Self::Cache::new(DEFAULT_CAPACITY, None)?; + *self.cache.lock() = Self::Cache::new(self.cache_capacity()?, Some(Box::new(unprepare)))?; Ok(()) } -- GitLab