summaryrefslogtreecommitdiff
path: root/cli/cache/incremental.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-05-29 14:38:18 -0400
committerGitHub <noreply@github.com>2024-05-29 18:38:18 +0000
commit94f040ac2867706d261e2fe1ec8bc2c4263eb6ab (patch)
tree5eaed4d41efd8d25da839bc29b8d32554e1a0fca /cli/cache/incremental.rs
parentfada25b0dd593efee496dabb48ed9cb7a9cb6647 (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/incremental.rs')
-rw-r--r--cli/cache/incremental.rs89
1 files changed, 42 insertions, 47 deletions
diff --git a/cli/cache/incremental.rs b/cli/cache/incremental.rs
index a8bfb0371..2d31b4125 100644
--- a/cli/cache/incremental.rs
+++ b/cli/cache/incremental.rs
@@ -6,23 +6,23 @@ use std::path::PathBuf;
use deno_core::error::AnyError;
use deno_core::parking_lot::Mutex;
-use deno_core::serde_json;
use deno_core::unsync::spawn;
use deno_core::unsync::JoinHandle;
use deno_runtime::deno_webstorage::rusqlite::params;
-use serde::Serialize;
use super::cache_db::CacheDB;
use super::cache_db::CacheDBConfiguration;
+use super::cache_db::CacheDBHash;
use super::cache_db::CacheFailure;
-use super::common::FastInsecureHasher;
pub static INCREMENTAL_CACHE_DB: CacheDBConfiguration = CacheDBConfiguration {
- table_initializer: "CREATE TABLE IF NOT EXISTS incrementalcache (
- file_path TEXT PRIMARY KEY,
- state_hash TEXT NOT NULL,
- source_hash TEXT NOT NULL
- );",
+ table_initializer: concat!(
+ "CREATE TABLE IF NOT EXISTS incrementalcache (",
+ "file_path TEXT PRIMARY KEY,",
+ "state_hash INTEGER NOT NULL,",
+ "source_hash INTEGER NOT NULL",
+ ");"
+ ),
on_version_change: "DELETE FROM incrementalcache;",
preheat_queries: &[],
// If the cache fails, just ignore all caching attempts
@@ -34,7 +34,7 @@ pub static INCREMENTAL_CACHE_DB: CacheDBConfiguration = CacheDBConfiguration {
pub struct IncrementalCache(IncrementalCacheInner);
impl IncrementalCache {
- pub fn new<TState: Serialize>(
+ pub fn new<TState: std::hash::Hash>(
db: CacheDB,
state: &TState,
initial_file_paths: &[PathBuf],
@@ -56,24 +56,23 @@ impl IncrementalCache {
}
enum ReceiverMessage {
- Update(PathBuf, u64),
+ Update(PathBuf, CacheDBHash),
Exit,
}
struct IncrementalCacheInner {
- previous_hashes: HashMap<PathBuf, u64>,
+ previous_hashes: HashMap<PathBuf, CacheDBHash>,
sender: tokio::sync::mpsc::UnboundedSender<ReceiverMessage>,
handle: Mutex<Option<JoinHandle<()>>>,
}
impl IncrementalCacheInner {
- pub fn new<TState: Serialize>(
+ pub fn new<TState: std::hash::Hash>(
db: CacheDB,
state: &TState,
initial_file_paths: &[PathBuf],
) -> Self {
- let state_hash =
- FastInsecureHasher::hash(serde_json::to_string(state).unwrap());
+ let state_hash = CacheDBHash::from_source(state);
let sql_cache = SqlIncrementalCache::new(db, state_hash);
Self::from_sql_incremental_cache(sql_cache, initial_file_paths)
}
@@ -113,13 +112,13 @@ impl IncrementalCacheInner {
pub fn is_file_same(&self, file_path: &Path, file_text: &str) -> bool {
match self.previous_hashes.get(file_path) {
- Some(hash) => *hash == FastInsecureHasher::hash(file_text),
+ Some(hash) => *hash == CacheDBHash::from_source(file_text),
None => false,
}
}
pub fn update_file(&self, file_path: &Path, file_text: &str) {
- let hash = FastInsecureHasher::hash(file_text);
+ let hash = CacheDBHash::from_source(file_text);
if let Some(previous_hash) = self.previous_hashes.get(file_path) {
if *previous_hash == hash {
return; // do not bother updating the db file because nothing has changed
@@ -146,15 +145,15 @@ struct SqlIncrementalCache {
/// A hash of the state used to produce the formatting/linting other than
/// the CLI version. This state is a hash of the configuration and ensures
/// we format/lint a file when the configuration changes.
- state_hash: u64,
+ state_hash: CacheDBHash,
}
impl SqlIncrementalCache {
- pub fn new(conn: CacheDB, state_hash: u64) -> Self {
+ pub fn new(conn: CacheDB, state_hash: CacheDBHash) -> Self {
Self { conn, state_hash }
}
- pub fn get_source_hash(&self, path: &Path) -> Option<u64> {
+ pub fn get_source_hash(&self, path: &Path) -> Option<CacheDBHash> {
match self.get_source_hash_result(path) {
Ok(option) => option,
Err(err) => {
@@ -171,7 +170,7 @@ impl SqlIncrementalCache {
fn get_source_hash_result(
&self,
path: &Path,
- ) -> Result<Option<u64>, AnyError> {
+ ) -> Result<Option<CacheDBHash>, AnyError> {
let query = "
SELECT
source_hash
@@ -183,10 +182,10 @@ impl SqlIncrementalCache {
LIMIT 1";
let res = self.conn.query_row(
query,
- params![path.to_string_lossy(), self.state_hash.to_string()],
+ params![path.to_string_lossy(), self.state_hash],
|row| {
- let hash: String = row.get(0)?;
- Ok(hash.parse::<u64>()?)
+ let hash: CacheDBHash = row.get(0)?;
+ Ok(hash)
},
)?;
Ok(res)
@@ -195,7 +194,7 @@ impl SqlIncrementalCache {
pub fn set_source_hash(
&self,
path: &Path,
- source_hash: u64,
+ source_hash: CacheDBHash,
) -> Result<(), AnyError> {
let sql = "
INSERT OR REPLACE INTO
@@ -204,11 +203,7 @@ impl SqlIncrementalCache {
(?1, ?2, ?3)";
self.conn.execute(
sql,
- params![
- path.to_string_lossy(),
- &self.state_hash.to_string(),
- &source_hash,
- ],
+ params![path.to_string_lossy(), self.state_hash, source_hash],
)?;
Ok(())
}
@@ -223,51 +218,51 @@ mod test {
#[test]
pub fn sql_cache_general_use() {
let conn = CacheDB::in_memory(&INCREMENTAL_CACHE_DB, "1.0.0");
- let cache = SqlIncrementalCache::new(conn, 1);
+ let cache = SqlIncrementalCache::new(conn, CacheDBHash::new(1));
let path = PathBuf::from("/mod.ts");
assert_eq!(cache.get_source_hash(&path), None);
- cache.set_source_hash(&path, 2).unwrap();
- assert_eq!(cache.get_source_hash(&path), Some(2));
+ cache.set_source_hash(&path, CacheDBHash::new(2)).unwrap();
+ assert_eq!(cache.get_source_hash(&path), Some(CacheDBHash::new(2)));
// try changing the cli version (should clear)
let conn = cache.conn.recreate_with_version("2.0.0");
- let mut cache = SqlIncrementalCache::new(conn, 1);
+ let mut cache = SqlIncrementalCache::new(conn, CacheDBHash::new(1));
assert_eq!(cache.get_source_hash(&path), None);
// add back the file to the cache
- cache.set_source_hash(&path, 2).unwrap();
- assert_eq!(cache.get_source_hash(&path), Some(2));
+ cache.set_source_hash(&path, CacheDBHash::new(2)).unwrap();
+ assert_eq!(cache.get_source_hash(&path), Some(CacheDBHash::new(2)));
// try changing the state hash
- cache.state_hash = 2;
+ cache.state_hash = CacheDBHash::new(2);
assert_eq!(cache.get_source_hash(&path), None);
- cache.state_hash = 1;
+ cache.state_hash = CacheDBHash::new(1);
// should return now that everything is back
- assert_eq!(cache.get_source_hash(&path), Some(2));
+ assert_eq!(cache.get_source_hash(&path), Some(CacheDBHash::new(2)));
// recreating the cache should not remove the data because the CLI version and state hash is the same
let conn = cache.conn.recreate_with_version("2.0.0");
- let cache = SqlIncrementalCache::new(conn, 1);
- assert_eq!(cache.get_source_hash(&path), Some(2));
+ let cache = SqlIncrementalCache::new(conn, CacheDBHash::new(1));
+ assert_eq!(cache.get_source_hash(&path), Some(CacheDBHash::new(2)));
// now try replacing and using another path
- cache.set_source_hash(&path, 3).unwrap();
- cache.set_source_hash(&path, 4).unwrap();
+ cache.set_source_hash(&path, CacheDBHash::new(3)).unwrap();
+ cache.set_source_hash(&path, CacheDBHash::new(4)).unwrap();
let path2 = PathBuf::from("/mod2.ts");
- cache.set_source_hash(&path2, 5).unwrap();
- assert_eq!(cache.get_source_hash(&path), Some(4));
- assert_eq!(cache.get_source_hash(&path2), Some(5));
+ cache.set_source_hash(&path2, CacheDBHash::new(5)).unwrap();
+ assert_eq!(cache.get_source_hash(&path), Some(CacheDBHash::new(4)));
+ assert_eq!(cache.get_source_hash(&path2), Some(CacheDBHash::new(5)));
}
#[tokio::test]
pub async fn incremental_cache_general_use() {
let conn = CacheDB::in_memory(&INCREMENTAL_CACHE_DB, "1.0.0");
- let sql_cache = SqlIncrementalCache::new(conn, 1);
+ let sql_cache = SqlIncrementalCache::new(conn, CacheDBHash::new(1));
let file_path = PathBuf::from("/mod.ts");
let file_text = "test";
- let file_hash = FastInsecureHasher::hash(file_text);
+ let file_hash = CacheDBHash::from_source(file_text);
sql_cache.set_source_hash(&file_path, file_hash).unwrap();
let cache = IncrementalCacheInner::from_sql_incremental_cache(
sql_cache,