1use super::{
2 decode::{decode_all, Module},
3 discover_all,
4 table::{SharedExprs, Table, Tables},
5 term::NameId,
6 Env,
7};
8use crate::{
9 error::{Error, Result},
10 olean::Header,
11};
12use hashbrown::HashTable;
13use rustc_hash::{FxHashMap, FxHashSet};
14use std::{
15 fs, mem,
16 path::{Path, PathBuf},
17 time::SystemTime,
18};
19
20pub(super) type Stamp = Vec<(u64, Option<SystemTime>)>;
22
23const PARTS: [&str; 3] = ["olean", "olean.server", "olean.private"];
24
25pub(super) fn stamp(path: &Path) -> Stamp {
26 PARTS
27 .iter()
28 .filter_map(|e| fs::metadata(path.with_extension(e)).ok())
29 .map(|m| (m.len(), m.modified().ok()))
30 .collect()
31}
32
33#[derive(Debug)]
35pub(super) struct Loaded {
36 pub(super) module: String,
37 pub(super) path: PathBuf,
38 pub(super) stamp: Stamp,
39 pub(super) consts: Vec<NameId>,
40 pub(super) recursors: FxHashMap<NameId, Vec<NameId>>,
41 pub(super) skipped: usize,
42 pub(super) header: Header,
43 pub(super) imports: Vec<String>,
44 pub(super) pending: Vec<(NameId, u64)>,
45}
46
47#[derive(Debug)]
49pub(super) struct Live {
50 pub(super) search: Vec<PathBuf>,
51 pub(super) roots: Vec<String>,
52 pub(super) jobs: usize,
53 pub(super) index: Vec<HashTable<u32>>,
54 pub(super) ranks: Vec<Vec<u32>>,
55 pub(super) loaded: Vec<Loaded>,
56}
57
58#[derive(Debug, Default)]
60pub struct Reload {
61 pub modules: Vec<String>,
63 pub consts: Vec<NameId>,
65 pub removed: Vec<NameId>,
67}
68
69impl Reload {
70 #[must_use]
71 pub const fn is_empty(&self) -> bool {
72 self.modules.is_empty() && self.removed.is_empty()
73 }
74}
75
76impl Env {
77 pub fn open(search: &[PathBuf], roots: &[&str], jobs: usize) -> Result<Self> {
80 Self::build(search, roots, jobs, &|_, _| {}, true, false)
81 }
82
83 pub fn reload(&mut self) -> Result<Reload> {
88 let mut live = self
89 .live
90 .take()
91 .ok_or(Error::Misuse("reload needs an environment from Env::open"))?;
92 let r = self.reload_with(&mut live);
93 self.live = Some(live);
94 r
95 }
96
97 fn reload_with(&mut self, live: &mut Live) -> Result<Reload> {
98 let roots: Vec<&str> = live.roots.iter().map(String::as_str).collect();
99 let found = discover_all(&live.search, &roots)?;
100 let stamps: Vec<Stamp> = found.iter().map(|(_, p)| stamp(p)).collect();
101 let fresh: Vec<(usize, &Path)> = {
102 let prev: FxHashMap<&str, &Stamp> = live
103 .loaded
104 .iter()
105 .map(|l| (l.module.as_str(), &l.stamp))
106 .collect();
107 if found.len() == prev.len()
108 && found
109 .iter()
110 .zip(&stamps)
111 .all(|((m, _), s)| prev.get(m.as_str()) == Some(&s))
112 {
113 return Ok(Reload::default());
114 }
115 found
116 .iter()
117 .zip(&stamps)
118 .enumerate()
119 .filter(|(_, ((m, _), s))| prev.get(m.as_str()) != Some(s))
120 .map(|(i, ((_, p), _))| (i, p.as_path()))
121 .collect()
122 };
123 let tables = self.thaw(live);
124 let decoded = decode_all(&tables, &fresh, live.jobs, &|_, _| {}, false);
125 self.freeze(tables, live);
126 let mut decoded: FxHashMap<usize, Module> =
127 fresh.iter().map(|&(i, _)| i).zip(decoded?).collect();
128 let mut prev: FxHashMap<String, Loaded> = mem::take(&mut live.loaded)
129 .into_iter()
130 .map(|l| (l.module.clone(), l))
131 .collect();
132 let imported: FxHashSet<&str> = found.iter().map(|(m, _)| m.as_str()).collect();
133 let mut stale = Vec::new();
134 prev.retain(|m, l| {
135 let keep =
136 imported.contains(m.as_str()) && !fresh.iter().any(|&(i, _)| found[i].0 == *m);
137 if !keep {
138 stale.append(&mut l.consts);
139 }
140 keep
141 });
142 for c in &stale {
143 self.consts.remove(c);
144 }
145 let mut reload = Reload::default();
146 live.loaded = found
147 .into_iter()
148 .zip(stamps)
149 .enumerate()
150 .map(|(i, ((module, path), stamp))| match decoded.remove(&i) {
151 Some(m) => {
152 reload.modules.push(module.clone());
153 reload.consts.extend(m.consts.iter().map(|c| c.name));
154 self.add(module, path, stamp, m)
155 }
156 None => prev.remove(&module).expect("unchanged module was loaded"),
157 })
158 .collect();
159 self.index(&live.loaded);
160 reload.removed = stale
161 .into_iter()
162 .filter(|c| !self.consts.contains_key(c))
163 .collect();
164 Ok(reload)
165 }
166
167 pub(super) fn thaw(&mut self, live: &mut Live) -> Tables {
169 Tables {
170 names: mem::replace(&mut self.names, Table(Vec::new())).thaw(),
171 levels: mem::replace(&mut self.levels, Table(Vec::new())).thaw(),
172 exprs: SharedExprs::thaw(
173 mem::take(&mut self.exprs),
174 mem::take(&mut live.index),
175 mem::take(&mut live.ranks),
176 ),
177 }
178 }
179
180 pub(super) fn freeze(&mut self, tables: Tables, live: &mut Live) {
181 self.names = tables.names.freeze();
182 self.levels = tables.levels.freeze();
183 (self.exprs, live.index, live.ranks) = tables.exprs.freeze();
184 }
185}