Skip to content

Commit

Permalink
don't require db lock on connect / cache.get
Browse files Browse the repository at this point in the history
  • Loading branch information
phiresky committed Jun 4, 2023
1 parent f5285ac commit 5834576
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
Binary file added exampledir/exif.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/caching_writer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{future::Future, pin::Pin};

use anyhow::Result;
use anyhow::{Context, Result};
use async_compression::tokio::write::ZstdEncoder;
use async_stream::stream;

Expand Down Expand Up @@ -64,7 +64,7 @@ pub fn async_read_and_write_to_cache<'a>(
};

// EOF, finish!
on_finish(finish).await
on_finish(finish).await.context("write_to_cache on_finish")
.map_err(to_io_err)?;

};
Expand Down
2 changes: 1 addition & 1 deletion src/preproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ async fn adapt_caching(
let mut cache = cache.context("No cache?")?;
let cache_key = CacheKey::new(&ai.filepath_hint, adapter.as_ref(), &active_adapters)?;
// let dbg_ctx = format!("adapter {}", &adapter.metadata().name);
let cached = cache.get(&cache_key).await?;
let cached = cache.get(&cache_key).await.context("cache.get")?;
match cached {
Some(cached) => Ok(Box::pin(ZstdDecoder::new(Cursor::new(cached)))),
None => {
Expand Down
32 changes: 21 additions & 11 deletions src/preproc_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait PreprocCache {
async fn set(&mut self, key: &CacheKey, value: Vec<u8>) -> Result<()>;
}

async fn pragmas(db: &Connection) -> Result<()> {
async fn connect_pragmas(db: &Connection) -> Result<()> {
// https://phiresky.github.io/blog/2020/sqlite-performance-tuning/
//let want_page_size = 32768;
//db.execute(&format!("pragma page_size = {};", want_page_size))
Expand All @@ -63,9 +63,6 @@ async fn pragmas(db: &Connection) -> Result<()> {
pragma synchronous = off; -- integrity isn't very important here
pragma mmap_size = 30000000000;
pragma application_id = 924716026;
pragma user_version = 2; -- todo: on upgrade clear db if version is unexpected
create table if not exists preproc_cache (
adapter text not null,
adapter_version integer not null,
Expand All @@ -80,23 +77,36 @@ async fn pragmas(db: &Connection) -> Result<()> {
",
)
})
.await?;
/*let jm: String = db
.call(|db| db.pragma_query_value(None, "journal_mode", |r| r.get(0))?)
.await.context("connect_pragmas")?;
let jm: i64 = db
.call(|db| db.pragma_query_value(None, "application_id", |r| r.get(0)))
.await?;
if &jm != "wal" {
anyhow::bail!("journal mode is not wal");
}*/
if jm != 924716026 {
// (probably) newly created db
create_pragmas(db).await.context("create_pragmas")?;
}
Ok(())
}

async fn create_pragmas(db: &Connection) -> Result<()> {
db.call(|db| {
db.execute_batch(
"
pragma application_id = 924716026;
pragma user_version = 2; -- todo: on upgrade clear db if version is unexpected
",
)
})
.await?;
Ok(())
}
struct SqliteCache {
db: Connection,
}
impl SqliteCache {
async fn new(path: &Path) -> Result<SqliteCache> {
let db = Connection::open(path.join("cache.sqlite3")).await?;
pragmas(&db).await?;
connect_pragmas(&db).await?;

Ok(SqliteCache { db })
}
Expand Down

0 comments on commit 5834576

Please sign in to comment.