use std::collections::BTreeSet;
use crate::{Cache, Model, RepoPath, RepoRoot, model::Query};
pub(super) fn additional_rows(model: &Model) -> Vec<crate::cache::AdditionalRow> {
model
.dispatch
.iter()
.map(|(path, schema)| crate::cache::AdditionalRow {
path: path.clone(),
schema: schema.map(|r| model.schema(r).doc.path.clone()),
})
.collect()
}
pub(super) fn scope_name(scope: &crate::grammar::KeyScope) -> String {
match scope {
crate::grammar::KeyScope::Document => "document".to_string(),
crate::grammar::KeyScope::Host(h) => h.clone(),
}
}
pub(super) fn key_query(scope: &str, slug: &str, knowledge: usize) -> String {
match scope {
"repository" => Query::KeyRepository(slug.to_string()).key(),
"knowledge" => Query::KeyKnowledge {
knowledge,
slug: slug.to_string(),
}
.key(),
_ => Query::KeyDocument {
knowledge,
slug: slug.to_string(),
}
.key(),
}
}
pub(super) fn schema_rows(model: &Model, cache: &Cache) -> Vec<crate::cache::SchemaRow> {
let mut rows = Vec::new();
for (k, knowledge) in model.knowledges.iter().enumerate() {
for (i, schema) in knowledge.schemas.iter().enumerate() {
rows.push(crate::cache::SchemaRow {
knowledge: k,
path: schema.doc.path.clone(),
key: knowledge.schema_key(i),
hash: cache.hash_of(&schema.doc.path).unwrap_or(0),
globs: schema.globs.join("\n"),
asset_globs: schema.asset_globs.join("\n"),
uses: schema.uses.join("\n"),
line: schema.doc.body_line,
archived: schema.archived.encode(),
});
}
}
rows
}
pub(super) fn changed_queries(
model: &Model,
cache: &Cache,
root: &RepoRoot,
dirs: &mut BTreeSet<RepoPath>,
) -> BTreeSet<String> {
let mut q: BTreeSet<String> = BTreeSet::new();
let mut ids: BTreeSet<(usize, String)> = BTreeSet::new();
let changed = cache.changed_set();
let touch = |dirs: &mut BTreeSet<RepoPath>, path: &RepoPath| {
if let Some(parent) = path.parent() {
dirs.insert(parent);
}
};
for (k, doc) in model.documents_and_schemas() {
if !changed.contains(&doc.path) {
continue;
}
touch(dirs, &doc.path);
q.insert(Query::Body(doc.path.clone()).key());
if let Some(old) = cache.old_row(&doc.path) {
let scalars =
crate::model::archive::encode_scalars(&crate::model::archive::scalars(doc));
if old.visible != doc.visible_hash() || old.scalars != scalars {
doc_and_type(&mut q, k, old.id.as_deref(), old.type_name.as_deref());
doc_and_type(&mut q, k, doc.id(), doc.type_name());
q.insert(Query::Path(doc.path.clone()).key());
if old.id != doc.core.id {
ids.extend(old.id.iter().map(|id| (k, id.clone())));
ids.extend(doc.id().map(|id| (k, id.to_string())));
}
}
} else {
doc_and_type(&mut q, k, doc.id(), doc.type_name());
q.insert(Query::Path(doc.path.clone()).key());
q.insert(Query::Exists(doc.path.clone()).key());
ids.extend(doc.id().map(|id| (k, id.to_string())));
}
}
for path in cache.removed() {
touch(dirs, &path);
file_queries(&mut q, &path, true);
if let Some(old) = cache.old_row(&path) {
doc_and_type(
&mut q,
old.knowledge,
old.id.as_deref(),
old.type_name.as_deref(),
);
ids.extend(old.id.map(|id| (old.knowledge, id)));
}
for target in cache.old_citation_targets(&path) {
q.insert(Query::Citations(target).key());
}
for (scope, slug, k) in cache.old_keys_of(&path) {
q.insert(key_query(&scope, &slug, k));
}
for target in cache.old_plans_of(&path) {
q.insert(Query::Planned(target.path).key());
}
}
for path in &changed {
let file = model.tree.file(path);
if file
.is_some_and(|f| matches!(f.kind, crate::FileKind::Document | crate::FileKind::Schema))
{
continue;
}
if file.is_some_and(|f| f.root.is_some()) {
touch(dirs, path);
}
file_queries(&mut q, path, cache.is_new(path));
}
for (path, new) in cache.pending_candidates() {
file_queries(&mut q, &path, new);
}
let rows = asset_rows(model);
for path in cache.assets_changed(&rows) {
touch(dirs, &path);
q.insert(Query::Path(path).key());
}
cache.record_assets(rows);
for path in cache.probes_changed(root) {
q.insert(Query::Exists(path).key());
}
let (added, removed) = cache.dirs_changed();
for dir in added {
touch(dirs, &dir);
dirs.insert(dir);
}
for dir in removed {
touch(dirs, &dir);
}
for (k, id) in ids {
for path in model.ids(k, &id) {
touch(dirs, &path);
}
}
unsettled_queries(model, cache, &mut q, dirs);
history_queries(model, cache, &mut q);
q
}
fn history_queries(model: &Model, cache: &Cache, q: &mut BTreeSet<String>) {
q.insert(Query::Run.key());
let stored = cache.history_queries();
if stored.is_empty() {
return;
}
let now = Query::History(model.history().head().to_string()).key();
q.extend(stored.into_iter().filter(|key| *key != now));
}
fn unsettled_queries(
model: &Model,
cache: &Cache,
q: &mut BTreeSet<String>,
dirs: &mut BTreeSet<RepoPath>,
) {
let unsettled = BTreeSet::from([Query::Unsettled.key()]);
for reader in cache.readers_of(&unsettled) {
if model
.tree
.file(&reader)
.is_some_and(|f| f.kind == crate::FileKind::Index)
&& let Some(dir) = reader.parent()
{
dirs.insert(dir);
}
}
q.extend(unsettled);
}
fn file_queries(q: &mut BTreeSet<String>, path: &RepoPath, exists: bool) {
q.insert(Query::Body(path.clone()).key());
q.insert(Query::Path(path.clone()).key());
if exists {
q.insert(Query::Exists(path.clone()).key());
}
}
fn doc_and_type(q: &mut BTreeSet<String>, k: usize, id: Option<&str>, type_name: Option<&str>) {
if let Some(id) = id {
q.insert(
Query::Doc {
knowledge: k,
id: id.to_string(),
}
.key(),
);
}
if let Some(t) = type_name {
q.insert(
Query::Type {
knowledge: k,
type_name: t.to_string(),
}
.key(),
);
}
}
fn asset_rows(model: &Model) -> Vec<crate::cache::AssetRow> {
let mut rows = Vec::new();
for (k, knowledge) in model.knowledges.iter().enumerate() {
for asset in &knowledge.assets {
rows.push(crate::cache::AssetRow {
knowledge: k,
asset: asset.path.clone(),
sidecar: asset.has_sidecar.then(|| asset.sidecar_path()),
});
}
}
rows
}