1use std::collections::BTreeSet;
6
7use rusqlite::{Connection, OpenFlags, params};
8
9use super::{Cache, conn::Conn};
10use crate::{
11 RepoPath,
12 model::{Fallback, Fields, SchemaMeta},
13};
14
15#[derive(Debug)]
18pub struct StoreView {
19 conn: Conn,
20 removed: BTreeSet<RepoPath>,
23}
24
25impl Cache {
26 pub fn view(&self) -> Option<Box<dyn Fallback>> {
29 let file = self.file()?;
30 let conn = Connection::open_with_flags(file, OpenFlags::SQLITE_OPEN_READ_ONLY).ok()?;
31 conn.busy_timeout(std::time::Duration::from_secs(5)).ok()?;
32 Some(Box::new(StoreView {
33 conn: Conn::new(conn, self.conn().counter()),
34 removed: self.removed_set(),
35 }))
36 }
37}
38
39impl StoreView {
40 fn live(&self, path: &str) -> Option<RepoPath> {
41 let p = RepoPath::parse(path)?;
42 (!self.removed.contains(&p)).then_some(p)
43 }
44}
45
46impl Fallback for StoreView {
47 fn ids(&self, knowledge: usize, id: &str) -> Vec<RepoPath> {
48 self.conn
49 .rows(
50 "SELECT path FROM ids WHERE store = ?1 AND id = ?2 ORDER BY path",
51 params![knowledge, id],
52 |r| r.get::<_, String>(0),
53 )
54 .iter()
55 .filter_map(|p| self.live(p))
56 .collect()
57 }
58
59 fn ids_of_type(&self, knowledge: usize, type_name: &str) -> Vec<(RepoPath, String)> {
60 self.conn
61 .rows(
62 "SELECT path, id FROM documents WHERE store = ?1 AND type = ?2 AND id IS NOT NULL \
63 ORDER BY path",
64 params![knowledge, type_name],
65 |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)),
66 )
67 .into_iter()
68 .filter_map(|(p, id)| Some((self.live(&p)?, id)))
69 .collect()
70 }
71
72 fn is_document(&self, path: &RepoPath) -> bool {
73 if self.removed.contains(path) {
74 return false;
75 }
76 self.conn
77 .query_row(
78 "SELECT 1 FROM documents WHERE path = ?1",
79 [path.as_str()],
80 |_| Ok(()),
81 )
82 .is_ok()
83 }
84
85 fn fields(&self, path: &RepoPath) -> Option<Fields> {
86 if self.removed.contains(path) {
87 return None;
88 }
89 self.conn
90 .query_row(
91 "SELECT id, title, description FROM documents WHERE path = ?1",
92 [path.as_str()],
93 |r| {
94 Ok(Fields {
95 id: r.get(0)?,
96 title: r.get(1)?,
97 description: r.get(2)?,
98 })
99 },
100 )
101 .ok()
102 }
103
104 fn broken(&self, path: &RepoPath) -> bool {
105 if self.removed.contains(path) {
106 return false;
107 }
108 self.conn
109 .query_row(
110 "SELECT broken FROM documents WHERE path = ?1",
111 [path.as_str()],
112 |r| r.get::<_, i64>(0),
113 )
114 .is_ok_and(|b| b != 0)
115 }
116
117 fn schema_meta(&self, path: &RepoPath) -> Option<SchemaMeta> {
118 type Row = (String, String, String, String, usize, String);
119 if self.removed.contains(path) {
120 return None;
121 }
122 let (key, globs, asset_globs, uses, line, archived): Row = self
123 .conn
124 .query_row(
125 "SELECT key, globs, asset_globs, uses, line, archived FROM schemas WHERE path = ?1",
126 [path.as_str()],
127 |r| {
128 Ok((
129 r.get(0)?,
130 r.get(1)?,
131 r.get(2)?,
132 r.get(3)?,
133 r.get(4)?,
134 r.get(5)?,
135 ))
136 },
137 )
138 .ok()?;
139 let lines = |s: String| -> Vec<String> {
140 s.lines()
141 .filter(|l| !l.is_empty())
142 .map(str::to_string)
143 .collect()
144 };
145 Some(SchemaMeta {
146 id: self.fields(path).and_then(|f| f.id),
147 type_const: (!key.starts_with('/')).then_some(key),
148 globs: lines(globs),
149 asset_globs: lines(asset_globs),
150 uses: lines(uses),
151 body_line: line,
152 archived: crate::model::Archived::decode(&archived),
153 })
154 }
155
156 fn scalars_of_type(
157 &self,
158 knowledge: usize,
159 type_name: &str,
160 ) -> Vec<(RepoPath, Vec<(String, String)>)> {
161 self.conn
162 .rows(
163 "SELECT path, scalars FROM documents WHERE store = ?1 AND type = ?2 ORDER BY path",
164 params![knowledge, type_name],
165 |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)),
166 )
167 .into_iter()
168 .filter_map(|(p, scalars)| {
169 Some((
170 self.live(&p)?,
171 crate::model::archive::decode_scalars(&scalars),
172 ))
173 })
174 .collect()
175 }
176
177 fn broken_in(&self, knowledge: usize) -> Vec<RepoPath> {
178 self.conn
179 .rows(
180 "SELECT path FROM documents WHERE store = ?1 AND broken = 1 ORDER BY path",
181 [knowledge],
182 |r| r.get::<_, String>(0),
183 )
184 .iter()
185 .filter_map(|p| self.live(p))
186 .collect()
187 }
188}