lean_ctx/core/property_graph/
semantic_cache.rs1use rusqlite::{Connection, OptionalExtension, params};
10
11#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct CachedResolution {
14 pub backend: String,
16 pub outcome: String,
19 pub target_file: Option<String>,
20 pub target_line: Option<usize>,
21 pub target_symbol: Option<String>,
22 pub context: String,
26}
27
28pub type SiteKey<'a> = (&'a str, usize, usize, &'a str);
30
31pub(super) fn lookup(
32 conn: &Connection,
33 site: SiteKey<'_>,
34 caller_hash: &str,
35) -> anyhow::Result<Option<CachedResolution>> {
36 let (file, line, col, op) = site;
37 let row = conn
38 .query_row(
39 "SELECT backend, outcome, target_file, target_line, target_symbol, context
40 FROM semantic_resolutions
41 WHERE caller_file = ?1 AND line = ?2 AND col = ?3 AND op = ?4
42 AND caller_hash = ?5",
43 params![file, line as i64, col as i64, op, caller_hash],
44 |r| {
45 Ok(CachedResolution {
46 backend: r.get(0)?,
47 outcome: r.get(1)?,
48 target_file: r.get(2)?,
49 target_line: r.get::<_, Option<i64>>(3)?.map(|l| l as usize),
50 target_symbol: r.get(4)?,
51 context: r.get(5)?,
52 })
53 },
54 )
55 .optional()?;
56 Ok(row)
57}
58
59pub(super) fn store(
60 conn: &Connection,
61 site: SiteKey<'_>,
62 caller_hash: &str,
63 value: &CachedResolution,
64) -> anyhow::Result<()> {
65 let (file, line, col, op) = site;
66 conn.execute(
67 "INSERT INTO semantic_resolutions
68 (caller_file, line, col, op, caller_hash, backend, outcome,
69 target_file, target_line, target_symbol, context)
70 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)
71 ON CONFLICT(caller_file, line, col, op) DO UPDATE SET
72 caller_hash = excluded.caller_hash,
73 backend = excluded.backend,
74 outcome = excluded.outcome,
75 target_file = excluded.target_file,
76 target_line = excluded.target_line,
77 target_symbol = excluded.target_symbol,
78 context = excluded.context",
79 params![
80 file,
81 line as i64,
82 col as i64,
83 op,
84 caller_hash,
85 value.backend,
86 value.outcome,
87 value.target_file,
88 value.target_line.map(|l| l as i64),
89 value.target_symbol,
90 value.context,
91 ],
92 )?;
93 Ok(())
94}
95pub(super) fn any(conn: &Connection) -> anyhow::Result<bool> {
98 Ok(conn.query_row(
99 "SELECT EXISTS(SELECT 1 FROM semantic_resolutions)",
100 [],
101 |r| r.get(0),
102 )?)
103}
104
105pub(super) fn prune(
108 conn: &Connection,
109 live_hashes: &std::collections::HashMap<String, String>,
110) -> anyhow::Result<usize> {
111 let mut stmt =
112 conn.prepare("SELECT DISTINCT caller_file, caller_hash FROM semantic_resolutions")?;
113 let stale: Vec<(String, String)> = stmt
114 .query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?
115 .filter_map(Result::ok)
116 .filter(|(file, hash)| live_hashes.get(file) != Some(hash))
117 .collect();
118 let mut removed = 0;
119 for (file, hash) in stale {
120 removed += conn.execute(
121 "DELETE FROM semantic_resolutions WHERE caller_file = ?1 AND caller_hash = ?2",
122 params![file, hash],
123 )?;
124 }
125 Ok(removed)
126}