Skip to content

Commit 5834576

Browse files
committed
don't require db lock on connect / cache.get
1 parent f5285ac commit 5834576

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

exampledir/exif.png

1.92 MB
Loading

src/caching_writer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{future::Future, pin::Pin};
22

3-
use anyhow::Result;
3+
use anyhow::{Context, Result};
44
use async_compression::tokio::write::ZstdEncoder;
55
use async_stream::stream;
66

@@ -64,7 +64,7 @@ pub fn async_read_and_write_to_cache<'a>(
6464
};
6565

6666
// EOF, finish!
67-
on_finish(finish).await
67+
on_finish(finish).await.context("write_to_cache on_finish")
6868
.map_err(to_io_err)?;
6969

7070
};

src/preproc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ async fn adapt_caching(
151151
let mut cache = cache.context("No cache?")?;
152152
let cache_key = CacheKey::new(&ai.filepath_hint, adapter.as_ref(), &active_adapters)?;
153153
// let dbg_ctx = format!("adapter {}", &adapter.metadata().name);
154-
let cached = cache.get(&cache_key).await?;
154+
let cached = cache.get(&cache_key).await.context("cache.get")?;
155155
match cached {
156156
Some(cached) => Ok(Box::pin(ZstdDecoder::new(Cursor::new(cached)))),
157157
None => {

src/preproc_cache.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub trait PreprocCache {
4949
async fn set(&mut self, key: &CacheKey, value: Vec<u8>) -> Result<()>;
5050
}
5151

52-
async fn pragmas(db: &Connection) -> Result<()> {
52+
async fn connect_pragmas(db: &Connection) -> Result<()> {
5353
// https://phiresky.github.io/blog/2020/sqlite-performance-tuning/
5454
//let want_page_size = 32768;
5555
//db.execute(&format!("pragma page_size = {};", want_page_size))
@@ -63,9 +63,6 @@ async fn pragmas(db: &Connection) -> Result<()> {
6363
pragma synchronous = off; -- integrity isn't very important here
6464
pragma mmap_size = 30000000000;
6565
66-
pragma application_id = 924716026;
67-
pragma user_version = 2; -- todo: on upgrade clear db if version is unexpected
68-
6966
create table if not exists preproc_cache (
7067
adapter text not null,
7168
adapter_version integer not null,
@@ -80,23 +77,36 @@ async fn pragmas(db: &Connection) -> Result<()> {
8077
",
8178
)
8279
})
83-
.await?;
84-
/*let jm: String = db
85-
.call(|db| db.pragma_query_value(None, "journal_mode", |r| r.get(0))?)
80+
.await.context("connect_pragmas")?;
81+
let jm: i64 = db
82+
.call(|db| db.pragma_query_value(None, "application_id", |r| r.get(0)))
8683
.await?;
87-
if &jm != "wal" {
88-
anyhow::bail!("journal mode is not wal");
89-
}*/
84+
if jm != 924716026 {
85+
// (probably) newly created db
86+
create_pragmas(db).await.context("create_pragmas")?;
87+
}
9088
Ok(())
9189
}
9290

91+
async fn create_pragmas(db: &Connection) -> Result<()> {
92+
db.call(|db| {
93+
db.execute_batch(
94+
"
95+
pragma application_id = 924716026;
96+
pragma user_version = 2; -- todo: on upgrade clear db if version is unexpected
97+
",
98+
)
99+
})
100+
.await?;
101+
Ok(())
102+
}
93103
struct SqliteCache {
94104
db: Connection,
95105
}
96106
impl SqliteCache {
97107
async fn new(path: &Path) -> Result<SqliteCache> {
98108
let db = Connection::open(path.join("cache.sqlite3")).await?;
99-
pragmas(&db).await?;
109+
connect_pragmas(&db).await?;
100110

101111
Ok(SqliteCache { db })
102112
}

0 commit comments

Comments
 (0)