diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-05-29 14:38:18 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-29 18:38:18 +0000 |
commit | 94f040ac2867706d261e2fe1ec8bc2c4263eb6ab (patch) | |
tree | 5eaed4d41efd8d25da839bc29b8d32554e1a0fca /cli/cache/module_info.rs | |
parent | fada25b0dd593efee496dabb48ed9cb7a9cb6647 (diff) |
fix: bump cache sqlite dbs to v2 for WAL journal mode change (#24030)
In https://github.com/denoland/deno/pull/23955 we changed the sqlite db
journal mode to WAL. This causes issues when someone is running an old
version of Deno using TRUNCATE and a new version because the two fight
against each other.
Diffstat (limited to 'cli/cache/module_info.rs')
-rw-r--r-- | cli/cache/module_info.rs | 105 |
1 files changed, 41 insertions, 64 deletions
diff --git a/cli/cache/module_info.rs b/cli/cache/module_info.rs index 959f84451..4dbb01c37 100644 --- a/cli/cache/module_info.rs +++ b/cli/cache/module_info.rs @@ -12,8 +12,8 @@ use deno_runtime::deno_webstorage::rusqlite::params; use super::cache_db::CacheDB; use super::cache_db::CacheDBConfiguration; +use super::cache_db::CacheDBHash; use super::cache_db::CacheFailure; -use super::FastInsecureHasher; use super::ParsedSourceCache; const SELECT_MODULE_INFO: &str = " @@ -28,40 +28,19 @@ WHERE LIMIT 1"; pub static MODULE_INFO_CACHE_DB: CacheDBConfiguration = CacheDBConfiguration { - table_initializer: "CREATE TABLE IF NOT EXISTS moduleinfocache ( - specifier TEXT PRIMARY KEY, - media_type TEXT NOT NULL, - source_hash TEXT NOT NULL, - module_info TEXT NOT NULL - );", + table_initializer: concat!( + "CREATE TABLE IF NOT EXISTS moduleinfocache (", + "specifier TEXT PRIMARY KEY,", + "media_type INTEGER NOT NULL,", + "source_hash INTEGER NOT NULL,", + "module_info TEXT NOT NULL", + ");" + ), on_version_change: "DELETE FROM moduleinfocache;", preheat_queries: &[SELECT_MODULE_INFO], on_failure: CacheFailure::InMemory, }; -#[derive(Debug)] -pub struct ModuleInfoCacheSourceHash(String); - -impl ModuleInfoCacheSourceHash { - pub fn new(hash: u64) -> Self { - Self(hash.to_string()) - } - - pub fn from_source(source: &[u8]) -> Self { - Self::new(FastInsecureHasher::hash(source)) - } - - pub fn as_str(&self) -> &str { - &self.0 - } -} - -impl From<ModuleInfoCacheSourceHash> for String { - fn from(source_hash: ModuleInfoCacheSourceHash) -> String { - source_hash.0 - } -} - /// A cache of `deno_graph::ModuleInfo` objects. Using this leads to a considerable /// performance improvement because when it exists we can skip parsing a module for /// deno_graph. @@ -91,7 +70,7 @@ impl ModuleInfoCache { &self, specifier: &ModuleSpecifier, media_type: MediaType, - expected_source_hash: &ModuleInfoCacheSourceHash, + expected_source_hash: CacheDBHash, ) -> Result<Option<ModuleInfo>, AnyError> { let query = SELECT_MODULE_INFO; let res = self.conn.query_row( @@ -99,7 +78,7 @@ impl ModuleInfoCache { params![ &specifier.as_str(), serialize_media_type(media_type), - expected_source_hash.as_str(), + expected_source_hash, ], |row| { let module_info: String = row.get(0)?; @@ -114,7 +93,7 @@ impl ModuleInfoCache { &self, specifier: &ModuleSpecifier, media_type: MediaType, - source_hash: &ModuleInfoCacheSourceHash, + source_hash: CacheDBHash, module_info: &ModuleInfo, ) -> Result<(), AnyError> { let sql = " @@ -127,7 +106,7 @@ impl ModuleInfoCache { params![ specifier.as_str(), serialize_media_type(media_type), - source_hash.as_str(), + source_hash, &serde_json::to_string(&module_info)?, ], )?; @@ -159,11 +138,11 @@ impl<'a> deno_graph::ModuleAnalyzer for ModuleInfoCacheModuleAnalyzer<'a> { media_type: MediaType, ) -> Result<ModuleInfo, deno_ast::ParseDiagnostic> { // attempt to load from the cache - let source_hash = ModuleInfoCacheSourceHash::from_source(source.as_bytes()); + let source_hash = CacheDBHash::from_source(&source); match self.module_info_cache.get_module_info( specifier, media_type, - &source_hash, + source_hash, ) { Ok(Some(info)) => return Ok(info), Ok(None) => {} @@ -193,7 +172,7 @@ impl<'a> deno_graph::ModuleAnalyzer for ModuleInfoCacheModuleAnalyzer<'a> { if let Err(err) = self.module_info_cache.set_module_info( specifier, media_type, - &source_hash, + source_hash, &module_info, ) { log::debug!( @@ -207,27 +186,25 @@ impl<'a> deno_graph::ModuleAnalyzer for ModuleInfoCacheModuleAnalyzer<'a> { } } -// todo(dsherret): change this to be stored as an integer next time -// the cache version is bumped -fn serialize_media_type(media_type: MediaType) -> &'static str { +fn serialize_media_type(media_type: MediaType) -> i64 { use MediaType::*; match media_type { - JavaScript => "1", - Jsx => "2", - Mjs => "3", - Cjs => "4", - TypeScript => "5", - Mts => "6", - Cts => "7", - Dts => "8", - Dmts => "9", - Dcts => "10", - Tsx => "11", - Json => "12", - Wasm => "13", - TsBuildInfo => "14", - SourceMap => "15", - Unknown => "16", + JavaScript => 1, + Jsx => 2, + Mjs => 3, + Cjs => 4, + TypeScript => 5, + Mts => 6, + Cts => 7, + Dts => 8, + Dmts => 9, + Dcts => 10, + Tsx => 11, + Json => 12, + Wasm => 13, + TsBuildInfo => 14, + SourceMap => 15, + Unknown => 16, } } @@ -250,7 +227,7 @@ mod test { .get_module_info( &specifier1, MediaType::JavaScript, - &ModuleInfoCacheSourceHash::new(1) + CacheDBHash::new(1) ) .unwrap(), None @@ -274,7 +251,7 @@ mod test { .set_module_info( &specifier1, MediaType::JavaScript, - &ModuleInfoCacheSourceHash::new(1), + CacheDBHash::new(1), &module_info, ) .unwrap(); @@ -283,7 +260,7 @@ mod test { .get_module_info( &specifier1, MediaType::JavaScript, - &ModuleInfoCacheSourceHash::new(1) + CacheDBHash::new(1) ) .unwrap(), Some(module_info.clone()) @@ -293,7 +270,7 @@ mod test { .get_module_info( &specifier2, MediaType::JavaScript, - &ModuleInfoCacheSourceHash::new(1) + CacheDBHash::new(1) ) .unwrap(), None, @@ -304,7 +281,7 @@ mod test { .get_module_info( &specifier1, MediaType::TypeScript, - &ModuleInfoCacheSourceHash::new(1) + CacheDBHash::new(1) ) .unwrap(), None, @@ -315,7 +292,7 @@ mod test { .get_module_info( &specifier1, MediaType::JavaScript, - &ModuleInfoCacheSourceHash::new(2) + CacheDBHash::new(2) ) .unwrap(), None, @@ -330,7 +307,7 @@ mod test { .get_module_info( &specifier1, MediaType::JavaScript, - &ModuleInfoCacheSourceHash::new(1) + CacheDBHash::new(1) ) .unwrap(), Some(module_info) @@ -345,7 +322,7 @@ mod test { .get_module_info( &specifier1, MediaType::JavaScript, - &ModuleInfoCacheSourceHash::new(1) + CacheDBHash::new(1) ) .unwrap(), None, |