paper/core/index/
connection.rs1use anyhow::{Context, Result};
2use rusqlite::OptionalExtension;
3use std::path::Path;
4use std::sync::Once;
5
6static REGISTER_VEC: Once = Once::new();
7
8#[allow(clippy::missing_transmute_annotations)]
13pub fn register_sqlite_vec() {
14 REGISTER_VEC.call_once(|| unsafe {
15 rusqlite::ffi::sqlite3_auto_extension(Some(std::mem::transmute(
19 sqlite_vec::sqlite3_vec_init as *const (),
20 )));
21 });
22}
23
24pub struct Connection {
27 conn: rusqlite::Connection,
28}
29
30impl Connection {
31 pub fn open(path: &Path) -> Result<Self> {
39 register_sqlite_vec();
40 let existed = path.exists();
41 if let Some(parent) = path.parent() {
42 std::fs::create_dir_all(parent)
43 .with_context(|| format!("creating db directory {}", parent.display()))?;
44 }
45 let conn = rusqlite::Connection::open(path)
46 .with_context(|| format!("opening {}", path.display()))?;
47 if !existed {
48 let quick_check: String = conn.query_row("PRAGMA quick_check", [], |row| row.get(0))?;
50 if quick_check != "ok" {
51 anyhow::bail!("database quick_check failed: {quick_check}");
52 }
53 }
54 Self::finish(conn, !existed) }
56
57 pub fn open_or_create(path: &Path) -> Result<Self> {
63 Self::open(path)
64 }
65
66 pub fn open_in_memory() -> Result<Self> {
67 register_sqlite_vec();
68 let conn = rusqlite::Connection::open_in_memory()?;
69 Self::finish(conn, false) }
71
72 fn finish(conn: rusqlite::Connection, allow_page_size: bool) -> Result<Self> {
76 conn.busy_timeout(std::time::Duration::from_secs(5))?;
77 conn.execute_batch(
79 "PRAGMA journal_mode = WAL;
80 PRAGMA synchronous = NORMAL;
81 PRAGMA foreign_keys = ON;
82 PRAGMA mmap_size = 268435456;
83 PRAGMA wal_autocheckpoint = 1000;",
84 )?;
85 if allow_page_size {
86 conn.execute_batch("PRAGMA page_size = 8192;")?;
89 }
90 Ok(Self { conn })
91 }
92
93 pub fn inner(&self) -> &rusqlite::Connection {
94 &self.conn
95 }
96
97 pub fn inner_mut(&mut self) -> &mut rusqlite::Connection {
98 &mut self.conn
99 }
100
101 pub fn execute_batch(&self, sql: &str) -> Result<()> {
102 self.conn.execute_batch(sql)?;
103 Ok(())
104 }
105
106 pub fn query_i64(&self, sql: &str) -> Result<i64> {
108 Ok(self.conn.query_row(sql, [], |row| row.get(0))?)
109 }
110
111 pub fn query_row<T, P: rusqlite::Params>(
112 &self,
113 sql: &str,
114 params: P,
115 f: impl FnOnce(&rusqlite::Row<'_>) -> rusqlite::Result<T>,
116 ) -> Result<T> {
117 Ok(self.conn.query_row(sql, params, f)?)
118 }
119
120 pub fn stored_embedding_model(&self) -> Result<Option<String>> {
122 let model: Option<String> = self
123 .conn
124 .query_row(
125 "SELECT value FROM index_meta WHERE key = 'embedding_model'",
126 [],
127 |row| row.get(0),
128 )
129 .optional()?;
130 Ok(model)
131 }
132}