diff options
author | haturau <135221985+haturatu@users.noreply.github.com> | 2024-10-13 10:44:12 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-13 10:44:12 +0900 |
commit | cedfd657a6a2f04d841db2b3fc3d7694de95eada (patch) | |
tree | 0d5d1c95cfd35bc46c230ea71f4dbf23aa0aaeea /ext/cache/sqlite.rs | |
parent | 4da77059dfd5b8d7591aa8e3b9f04386fbdce221 (diff) | |
parent | 64c304a45265705832ebb4ab4e9ef19f899ac911 (diff) |
Merge branch 'denoland:main' into main
Diffstat (limited to 'ext/cache/sqlite.rs')
-rw-r--r-- | ext/cache/sqlite.rs | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/ext/cache/sqlite.rs b/ext/cache/sqlite.rs index c3c55dd5e..e4991c32f 100644 --- a/ext/cache/sqlite.rs +++ b/ext/cache/sqlite.rs @@ -30,6 +30,7 @@ use crate::serialize_headers; use crate::vary_header_matches; use crate::Cache; use crate::CacheDeleteRequest; +use crate::CacheError; use crate::CacheMatchRequest; use crate::CacheMatchResponseMeta; use crate::CachePutRequest; @@ -102,7 +103,7 @@ impl Cache for SqliteBackedCache { /// Open a cache storage. Internally, this creates a row in the /// sqlite db if the cache doesn't exist and returns the internal id /// of the cache. - async fn storage_open(&self, cache_name: String) -> Result<i64, AnyError> { + async fn storage_open(&self, cache_name: String) -> Result<i64, CacheError> { let db = self.connection.clone(); let cache_storage_dir = self.cache_storage_dir.clone(); spawn_blocking(move || { @@ -121,14 +122,14 @@ impl Cache for SqliteBackedCache { )?; let responses_dir = get_responses_dir(cache_storage_dir, cache_id); std::fs::create_dir_all(responses_dir)?; - Ok::<i64, AnyError>(cache_id) + Ok::<i64, CacheError>(cache_id) }) .await? } /// Check if a cache with the provided name exists. /// Note: this doesn't check the disk, it only checks the sqlite db. - async fn storage_has(&self, cache_name: String) -> Result<bool, AnyError> { + async fn storage_has(&self, cache_name: String) -> Result<bool, CacheError> { let db = self.connection.clone(); spawn_blocking(move || { let db = db.lock(); @@ -140,13 +141,16 @@ impl Cache for SqliteBackedCache { Ok(count > 0) }, )?; - Ok::<bool, AnyError>(cache_exists) + Ok::<bool, CacheError>(cache_exists) }) .await? } /// Delete a cache storage. Internally, this deletes the row in the sqlite db. - async fn storage_delete(&self, cache_name: String) -> Result<bool, AnyError> { + async fn storage_delete( + &self, + cache_name: String, + ) -> Result<bool, CacheError> { let db = self.connection.clone(); let cache_storage_dir = self.cache_storage_dir.clone(); spawn_blocking(move || { @@ -167,7 +171,7 @@ impl Cache for SqliteBackedCache { std::fs::remove_dir_all(cache_dir)?; } } - Ok::<bool, AnyError>(maybe_cache_id.is_some()) + Ok::<bool, CacheError>(maybe_cache_id.is_some()) }) .await? } @@ -176,10 +180,12 @@ impl Cache for SqliteBackedCache { &self, request_response: CachePutRequest, resource: Option<Rc<dyn Resource>>, - ) -> Result<(), AnyError> { + ) -> Result<(), CacheError> { let db = self.connection.clone(); let cache_storage_dir = self.cache_storage_dir.clone(); - let now = SystemTime::now().duration_since(UNIX_EPOCH)?; + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("SystemTime is before unix epoch"); if let Some(resource) = resource { let body_key = hash(&format!( @@ -193,7 +199,11 @@ impl Cache for SqliteBackedCache { let mut file = tokio::fs::File::create(response_path).await?; let mut buf = BufMutView::new(64 * 1024); loop { - let (size, buf2) = resource.clone().read_byob(buf).await?; + let (size, buf2) = resource + .clone() + .read_byob(buf) + .await + .map_err(CacheError::Other)?; if size == 0 { break; } @@ -224,7 +234,7 @@ impl Cache for SqliteBackedCache { request: CacheMatchRequest, ) -> Result< Option<(CacheMatchResponseMeta, Option<CacheResponseResource>)>, - AnyError, + CacheError, > { let db = self.connection.clone(); let cache_storage_dir = self.cache_storage_dir.clone(); @@ -290,19 +300,17 @@ impl Cache for SqliteBackedCache { } Err(err) => return Err(err.into()), }; - return Ok(Some((cache_meta, Some(CacheResponseResource::new(file))))); - } - Some((cache_meta, None)) => { - return Ok(Some((cache_meta, None))); + Ok(Some((cache_meta, Some(CacheResponseResource::new(file))))) } - None => return Ok(None), + Some((cache_meta, None)) => Ok(Some((cache_meta, None))), + None => Ok(None), } } async fn delete( &self, request: CacheDeleteRequest, - ) -> Result<bool, AnyError> { + ) -> Result<bool, CacheError> { let db = self.connection.clone(); spawn_blocking(move || { // TODO(@satyarohith): remove the response body from disk if one exists @@ -311,17 +319,17 @@ impl Cache for SqliteBackedCache { "DELETE FROM request_response_list WHERE cache_id = ?1 AND request_url = ?2", (request.cache_id, &request.request_url), )?; - Ok::<bool, AnyError>(rows_effected > 0) + Ok::<bool, CacheError>(rows_effected > 0) }) .await? } } async fn insert_cache_asset( - db: Arc<Mutex<rusqlite::Connection>>, + db: Arc<Mutex<Connection>>, put: CachePutRequest, response_body_key: Option<String>, -) -> Result<Option<String>, deno_core::anyhow::Error> { +) -> Result<Option<String>, CacheError> { spawn_blocking(move || { let maybe_response_body = { let db = db.lock(); @@ -339,7 +347,7 @@ async fn insert_cache_asset( response_body_key, put.response_status, put.response_status_text, - SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(), + SystemTime::now().duration_since(UNIX_EPOCH).expect("SystemTime is before unix epoch").as_secs(), ), |row| { let response_body_key: Option<String> = row.get(0)?; @@ -347,7 +355,7 @@ async fn insert_cache_asset( }, )? }; - Ok::<Option<String>, AnyError>(maybe_response_body) + Ok::<Option<String>, CacheError>(maybe_response_body) }).await? } |