sokf_core/cache/
markers.rs1use rusqlite::params;
7
8use super::{Cache, conn::Conn, sync::SyncState};
9use crate::{
10 FileKind, RepoPath,
11 markers::{IgnoreFault, MarkerRow, RowKind},
12};
13
14fn encode(kind: &RowKind) -> (i64, &str, &str) {
17 match kind {
18 RowKind::Marker { rel, target } => (0, rel, target),
19 RowKind::Malformed(tag) => (1, tag, ""),
20 RowKind::Rel(tag) => (6, tag, ""),
21 RowKind::Ignore(IgnoreFault::BeginInside) => (3, "", ""),
22 RowKind::Ignore(IgnoreFault::EndOutside) => (4, "", ""),
23 RowKind::Ignore(IgnoreFault::NeverEnds) => (5, "", ""),
24 }
25}
26
27fn decode(code: i64, rel: String, target: String) -> Option<RowKind> {
30 Some(match code {
31 0 => RowKind::Marker { rel, target },
32 1 => RowKind::Malformed(rel),
33 3 => RowKind::Ignore(IgnoreFault::BeginInside),
34 4 => RowKind::Ignore(IgnoreFault::EndOutside),
35 5 => RowKind::Ignore(IgnoreFault::NeverEnds),
36 6 => RowKind::Rel(rel),
37 _ => return None,
38 })
39}
40
41impl Cache {
42 pub fn outside_to_read(&self) -> Vec<RepoPath> {
51 let state = self.state.borrow();
52 let mut paths: Vec<RepoPath> = state
53 .known
54 .keys()
55 .filter(|p| {
56 Self::listed(&state, p).is_some_and(|(kind, root)| {
57 root.is_none() && super::sync::kind_of(kind) == FileKind::Candidate
58 })
59 })
60 .cloned()
61 .collect();
62 paths.sort();
63 paths
64 }
65
66 pub fn unread(&self, path: &RepoPath) {
70 self.state.borrow_mut().candidates.remove(path);
71 }
72
73 pub fn record_markers(&self, path: &RepoPath, rows: Vec<MarkerRow>) {
77 self.state.borrow_mut().markers.insert(path.clone(), rows);
78 }
79
80 pub fn stored_markers(&self, path: &RepoPath) -> Vec<MarkerRow> {
84 let mut out: Vec<MarkerRow> = self
85 .conn()
86 .rows(
87 "SELECT line, kind, rel, target FROM markers WHERE path = ?1",
88 [path.as_str()],
89 |r| {
90 let line = r.get::<_, usize>(0)?;
91 let kind = decode(r.get(1)?, r.get(2)?, r.get(3)?);
92 Ok(kind.map(|kind| MarkerRow { line, kind }))
93 },
94 )
95 .into_iter()
96 .flatten()
97 .collect();
98 out.sort();
99 out
100 }
101
102 pub fn has_markers(&self, path: &RepoPath) -> bool {
105 self.conn()
106 .query_row(
107 "SELECT 1 FROM markers WHERE path = ?1 LIMIT 1",
108 [path.as_str()],
109 |_| Ok(()),
110 )
111 .is_ok()
112 }
113
114 pub(super) fn commit_markers(conn: &Conn, state: &SyncState) -> rusqlite::Result<()> {
118 let mut delete = conn.prepare("DELETE FROM markers WHERE path = ?1")?;
119 for path in &state.removed {
120 delete.execute([path.as_str()])?;
121 }
122 let mut put = conn.prepare(
123 "INSERT INTO markers (path, line, kind, rel, target) VALUES (?1, ?2, ?3, ?4, ?5)",
124 )?;
125 for (path, rows) in &state.markers {
126 delete.execute([path.as_str()])?;
127 for row in rows {
128 let (code, rel, target) = encode(&row.kind);
129 put.execute(params![path.as_str(), row.line, code, rel, target])?;
130 }
131 }
132 Ok(())
133 }
134}