sokf_core/model/reads.rs
1//! This module records what a check reads from the knowledge outside the
2//! document that the check is about. Each read is a query, recorded against
3//! the check that makes it. From these records, the next run finds the
4//! checks that a change affects (CACHE-1_AC-4).
5
6use std::{
7 cell::RefCell,
8 collections::{BTreeMap, BTreeSet},
9};
10
11use crate::RepoPath;
12
13// sokf:begin query
14/// A `Query` is one fact that a check reads from another document, from a
15/// file, or from a set of documents, such as every document of a type.
16#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
17pub enum Query {
18 /// The query reads the documents in a store that have the given id, and
19 /// the facts of those documents.
20 Doc {
21 /// `knowledge` is the index of the store.
22 knowledge: usize,
23 /// `id` is the id of the documents.
24 id: String,
25 },
26 /// The query reads what is at a path: a document and its facts, a
27 /// schema, an index, an asset, or a file.
28 Path(RepoPath),
29 /// The query reads whether a file or a directory exists at a path, and
30 /// which of the two it is.
31 Exists(RepoPath),
32 /// The query reads the documents of a type in a store.
33 Type {
34 /// `knowledge` is the index of the store.
35 knowledge: usize,
36 /// `type_name` is the name of the type.
37 type_name: String,
38 },
39 /// The query reads the keys with a slug at repository scope.
40 KeyRepository(String),
41 /// The query reads the keys with a slug at knowledge scope, in a store.
42 KeyKnowledge {
43 /// `knowledge` is the index of the store.
44 knowledge: usize,
45 /// `slug` is the slug of the keys.
46 slug: String,
47 },
48 /// The query reads the keys with a slug at document scope, in every
49 /// document of a store.
50 KeyDocument {
51 /// `knowledge` is the index of the store.
52 knowledge: usize,
53 /// `slug` is the slug of the keys.
54 slug: String,
55 },
56 /// The query reads the expanded body of a document, or the text of a
57 /// file.
58 Body(RepoPath),
59 /// The query reads the citations of a document.
60 Citations(RepoPath),
61 /// The query reads the index that sokf expects a directory to have.
62 Directory(RepoPath),
63 /// The query reads the schema for a key in a store. The key is the type
64 /// in the schema's `type` const. A schema without that const has its
65 /// path as the key. Such a schema matches documents only by its globs.
66 /// The store keeps a hash of each schema file whose type or path
67 /// is the key, so that a change to one of those files changes the answer
68 /// to the query.
69 Schema {
70 /// `knowledge` is the index of the store.
71 knowledge: usize,
72 /// `key` is the type that the schema's `type` const holds, or, for a
73 /// schema without that const, the schema's path.
74 key: String,
75 },
76 /// The query reads whether a document plans a file at a path: whether an
77 /// open issue, for example, links the path in its Files section. A check
78 /// of a planned path reads the query of the path and of each directory
79 /// above it, because a plan of a directory plans every path under it.
80 Planned(RepoPath),
81 /// The query reads the identities of every store, current and former.
82 /// A qualified id, a link to another store, or an include from another
83 /// store resolves through them.
84 Identities,
85 /// The query reads the answer for a pair that the formatter left
86 /// unsettled, such as one that it didn't start for. No stat records
87 /// the formatter, so every run checks the readers of this query again.
88 Unsettled,
89 /// The query reads the history of git, for the check of a `verified`
90 /// entry, with the commit that `HEAD` named, or an empty text where it
91 /// named none. No stat records the history, so a run whose `HEAD`
92 /// names another commit checks the readers of this query again
93 /// (CACHE-1_AC-97).
94 History(String),
95 /// The query reads what only the run decides, which no stat records:
96 /// the time of the run, against which an entry has yet to lapse, or a
97 /// `git` that failed. Every run checks the readers of this query again.
98 Run,
99}
100// sokf:end query
101
102impl Query {
103 /// `key` returns the query as one line of text, the form in which the
104 /// store keeps it.
105 pub fn key(&self) -> String {
106 match self {
107 Query::Doc { knowledge, id } => format!("doc:{knowledge}:{id}"),
108 Query::Path(p) => format!("path:{p}"),
109 Query::Exists(p) => format!("exists:{p}"),
110 Query::Type {
111 knowledge,
112 type_name,
113 } => format!("type:{knowledge}:{type_name}"),
114 Query::KeyRepository(slug) => format!("key:repository:{slug}"),
115 Query::KeyKnowledge { knowledge, slug } => format!("key:knowledge:{knowledge}:{slug}"),
116 Query::KeyDocument { knowledge, slug } => format!("key:document:{knowledge}:{slug}"),
117 Query::Body(p) => format!("body:{p}"),
118 Query::Citations(p) => format!("citations:{p}"),
119 Query::Directory(p) => format!("dir:{p}"),
120 Query::Schema { knowledge, key } => format!("{SCHEMA_PREFIX}{knowledge}:{key}"),
121 Query::Planned(p) => format!("planned:{p}"),
122 Query::Identities => "identities".to_string(),
123 Query::Unsettled => "unsettled".to_string(),
124 Query::History(head) => format!("{HISTORY_PREFIX}{head}"),
125 Query::Run => "run".to_string(),
126 }
127 }
128}
129
130/// `SCHEMA_PREFIX` begins the key of every schema query, so that when the
131/// schema formats change, one range scan finds every reader of any schema
132/// (CACHE-1_AC-17).
133pub const SCHEMA_PREFIX: &str = "schema:";
134
135/// `HISTORY_PREFIX` begins the key of every query of the history, so that
136/// one range scan finds each commit that a stored check read
137/// (CACHE-1_AC-97).
138pub const HISTORY_PREFIX: &str = "history:";
139
140/// `Reads` holds the reads of a run: the queries that each reader made.
141#[derive(Debug, Default)]
142pub struct Reads {
143 reader: Option<RepoPath>,
144 queries: BTreeMap<RepoPath, BTreeSet<Query>>,
145}
146
147impl Reads {
148 /// `watch` makes `reader` the reader of every query that follows, and
149 /// records `reader` even when it makes no query.
150 pub fn watch(&mut self, reader: &RepoPath) {
151 self.reader = Some(reader.clone());
152 self.queries.entry(reader.clone()).or_default();
153 }
154
155 /// `unwatch` clears the reader, so that the queries that follow are not
156 /// recorded.
157 pub fn unwatch(&mut self) {
158 self.reader = None;
159 }
160
161 /// `note` records a query against the watched reader, and does nothing
162 /// when no reader is watched.
163 pub fn note(&mut self, query: Query) {
164 if let Some(reader) = &self.reader {
165 self.queries
166 .entry(reader.clone())
167 .or_default()
168 .insert(query);
169 }
170 }
171
172 /// `of` returns the queries of `reader`.
173 pub fn of(&self, reader: &RepoPath) -> BTreeSet<Query> {
174 self.queries.get(reader).cloned().unwrap_or_default()
175 }
176
177 /// `all` returns the queries of every reader.
178 pub fn all(&self) -> &BTreeMap<RepoPath, BTreeSet<Query>> {
179 &self.queries
180 }
181}
182
183/// A `Recorder` holds the reads of a model, and every read of the model goes
184/// through it.
185pub type Recorder = RefCell<Reads>;
186
187#[cfg(test)]
188mod tests {
189 use super::*;
190
191 #[test]
192 fn a_query_has_one_key_and_reads_are_per_reader() {
193 let p = RepoPath::parse("/k/a.md").unwrap();
194 assert_eq!(
195 Query::Doc {
196 knowledge: 0,
197 id: "x".into()
198 }
199 .key(),
200 "doc:0:x"
201 );
202 assert_eq!(Query::Path(p.clone()).key(), "path:/k/a.md");
203 assert_eq!(Query::Exists(p.clone()).key(), "exists:/k/a.md");
204 assert_eq!(
205 Query::Type {
206 knowledge: 1,
207 type_name: "T".into()
208 }
209 .key(),
210 "type:1:T"
211 );
212 assert_eq!(Query::KeyRepository("s".into()).key(), "key:repository:s");
213 assert_eq!(
214 Query::KeyKnowledge {
215 knowledge: 0,
216 slug: "s".into()
217 }
218 .key(),
219 "key:knowledge:0:s"
220 );
221 assert_eq!(
222 Query::KeyDocument {
223 knowledge: 0,
224 slug: "s".into()
225 }
226 .key(),
227 "key:document:0:s"
228 );
229 assert_eq!(Query::Body(p.clone()).key(), "body:/k/a.md");
230 assert_eq!(Query::Citations(p.clone()).key(), "citations:/k/a.md");
231 assert_eq!(Query::Directory(p.clone()).key(), "dir:/k/a.md");
232 assert_eq!(
233 Query::Schema {
234 knowledge: 0,
235 key: "Note".into()
236 }
237 .key(),
238 "schema:0:Note"
239 );
240 assert_eq!(Query::Identities.key(), "identities");
241 let mut reads = Reads::default();
242 reads.note(Query::Exists(p.clone()));
243 assert!(reads.all().is_empty());
244 reads.watch(&p);
245 reads.note(Query::Exists(p.clone()));
246 reads.note(Query::Exists(p.clone()));
247 reads.unwatch();
248 reads.note(Query::Body(p.clone()));
249 assert_eq!(reads.of(&p).len(), 1);
250 assert_eq!(reads.all().len(), 1);
251 }
252}