flams_math_archives/backend/
sandbox.rs1use std::{
2 hint::unreachable_unchecked,
3 path::{Path, PathBuf},
4};
5
6use ftml_ontology::{
7 narrative::{DocumentRange, documents::Document},
8 utils::{Css, RefTree},
9};
10use ftml_uris::{ArchiveId, DocumentUri, UriPath, UriWithArchive};
11
12use crate::{
13 Archive, LocalArchive, MathArchive,
14 backend::{GlobalBackend, LocalBackend},
15 manager::{ArchiveGroup, ArchiveManager, ArchiveOrGroup, ArchiveTree},
16 utils::{
17 AsyncEngine,
18 errors::{ArtifactSaveError, BackendError, FileError},
19 path_ext::{PathExt, RelPath},
20 },
21};
22
23#[derive(Debug, Clone)]
24pub enum SandboxedRepository {
25 Copy(ArchiveId),
26 Git {
27 id: ArchiveId,
28 branch: Box<str>,
29 commit: flams_backend_types::git::Commit,
30 remote: Box<str>,
31 },
32}
33
34impl SandboxedRepository {
35 #[inline]
36 #[must_use]
37 pub const fn id(&self) -> &ArchiveId {
38 match self {
39 Self::Copy(id) | Self::Git { id, .. } => id,
40 }
41 }
42}
43
44#[derive(Debug)]
45pub(super) struct SandboxedBackendI {
46 path: Box<Path>,
47 span: tracing::Span,
48 pub(super) repos: parking_lot::RwLock<Vec<SandboxedRepository>>,
49 manager: ArchiveManager,
50}
51
52#[derive(Debug, Clone)]
53pub struct SandboxedBackend(pub(super) triomphe::Arc<SandboxedBackendI>);
54impl Drop for SandboxedBackendI {
55 fn drop(&mut self) {
56 let _ = std::fs::remove_dir_all(&self.path);
57 }
58}
59
60impl SandboxedBackend {
61 pub fn load_all(&self) {
62 self.0.manager.load(&[&self.0.path]);
63 }
64 #[inline]
65 #[must_use]
66 pub fn root(&self) -> &Path {
67 &self.0.path
68 }
69
70 #[inline]
71 #[must_use]
72 pub fn get_repos(&self) -> Vec<SandboxedRepository> {
73 self.0.repos.read().clone()
74 }
75
76 #[inline]
77 pub fn with_repos<R>(&self, f: impl FnOnce(&[SandboxedRepository]) -> R) -> R {
78 let inner = self.0.repos.read();
79 f(inner.as_slice())
80 }
81
82 #[inline]
83 #[must_use]
84 pub fn path_for(&self, id: &ArchiveId) -> PathBuf {
85 self.0.path.join(id.as_ref())
86 }
87
88 pub fn new(name: &str, temp_dir: &Path) -> Self {
89 let p = temp_dir.join(name);
90 let i = SandboxedBackendI {
91 span: tracing::info_span!(target:"sandbox","sandbox",path=%p.display()),
92 path: p.into(),
93 repos: parking_lot::RwLock::new(Vec::new()),
94 #[cfg(not(feature = "rocksdb"))]
95 manager: ArchiveManager::default(),
96 #[cfg(feature = "rocksdb")]
97 manager: ArchiveManager::new(&temp_dir.join(".rdf")),
98 };
99 Self(triomphe::Arc::new(i))
100 }
101
102 #[inline]
103 pub fn clear(&self) {
104 self.0.repos.write().clear();
105 }
106
107 #[tracing::instrument(level = "info",
110 parent = &self.0.span,
111 target = "sandbox",
112 name = "migrating",
113 fields(path = %self.0.path.display()),
114 skip_all
115 )]
116 pub fn migrate<A: AsyncEngine>(&self) -> Result<usize, FileError> {
117 let mut count = 0;
118 let cnt = &mut count;
119 self.0.manager.reinit::<Result<(), FileError>>(
120 move |sandbox| {
121 GlobalBackend.reinit::<Result<(), FileError>>(
122 |_| {
123 sandbox.top.clear();
124 let Some(main) = crate::mathhub::mathhubs().first() else {
125 unreachable!()
126 };
127 for a in std::mem::take(&mut sandbox.archives) {
128 *cnt += 1;
129 let Archive::Local(a) = a else {
130 unsafe { unreachable_unchecked() }
132 };
133 let source = a.path();
134 let target = main.join(a.id().as_ref());
135
136 if let Some(p) = target.parent() {
137 std::fs::create_dir_all(p)
138 .map_err(|e| FileError::Creation(p.to_path_buf(), e))?;
139 }
140 let safe_target = unsafe { target.parent().unwrap_unchecked() }.join(
143 format!(".{}.tmp", target.file_name().expect("weird fs").display()),
144 );
145 if safe_target.exists() {
146 std::fs::remove_dir_all(&safe_target)
147 .map_err(|e| FileError::Rename(safe_target.clone(), e))?;
148 }
149 source.rename_safe(&safe_target)?;
150 if target.exists() {
151 std::fs::remove_dir_all(&target)
152 .map_err(|e| FileError::Rename(target.clone(), e))?;
153 }
154 std::fs::rename(&safe_target, target)
155 .map_err(|e| FileError::Rename(safe_target.clone(), e))?;
156 }
157 Ok(())
158 },
159 crate::mathhub::mathhubs(),
160 )
161 },
162 &[&*self.0.path],
163 )?;
164
165 #[cfg(feature = "rdf")]
166 A::background(|| {
167 GlobalBackend
168 .triple_store()
169 .load_archives(&GlobalBackend.all_archives());
170 });
171 Ok(count)
172 }
173
174 #[tracing::instrument(level = "info",
176 parent = &self.0.span,
177 target = "sandbox",
178 name = "adding",
179 fields(repository = ?sb),
180 skip_all
181 )]
182 pub fn add(&self, sb: SandboxedRepository, then: impl FnOnce()) {
183 let mut repos = self.0.repos.write();
184 let id = sb.id();
185 if let Some(i) = repos.iter().position(|r| r.id() == id) {
186 repos.remove(i);
187 }
188 self.require_meta_infs(
189 id,
190 &mut repos,
191 |_, _| {},
192 |_, _, _| {
193 tracing::error!("A group with id {id} already exists!");
194 },
195 || {},
196 );
197 let id = sb.id().clone();
198 repos.push(sb);
199 drop(repos);
200 then();
201 let manifest = LocalArchive::manifest_of(&self.0.path.join(id.as_ref()))
202 .expect("archive does not exist");
203 self.0.manager.load_one(&manifest, RelPath::from_id(&id));
204 }
205
206 fn require_meta_infs(
207 &self,
208 id: &ArchiveId,
209 repos: &mut Vec<SandboxedRepository>,
210 then: impl FnOnce(&LocalArchive, &mut Vec<SandboxedRepository>),
211 group: impl FnOnce(&ArchiveGroup, &ArchiveTree, &mut Vec<SandboxedRepository>),
212 else_: impl FnOnce(),
213 ) {
214 if repos.iter().any(|r| r.id() == id) {
215 return;
216 }
217 GlobalBackend.with_tree(move |t| {
218 let mut steps = id.steps();
219 let Some(mut current) = steps.next() else {
220 tracing::error!("empty archive ID");
221 return;
222 };
223 let mut ls = &t.top;
224 loop {
225 let Some(a) = ls.iter().find(|a| a.id().last() == current) else {
226 else_();
227 return;
228 };
229 match a {
230 ArchiveOrGroup::Archive(_) => {
231 if steps.next().is_some() {
232 else_();
233 return;
234 }
235 let Some(Archive::Local(a)) = t.get(id) else {
236 else_();
237 return;
238 };
239 then(a, repos);
240 return;
241 }
242 ArchiveOrGroup::Group(g) => {
243 let Some(next) = steps.next() else {
244 group(g, t, repos);
245 return;
246 };
247 if let Some(ArchiveOrGroup::Archive(a)) =
248 g.children.iter().find(|a| a.id().is_meta())
249 && !repos.iter().any(|r| r.id() == a)
250 {
251 let Some(Archive::Local(a)) = t.get(a) else {
252 else_();
253 return;
254 };
255 repos.push(SandboxedRepository::Copy(a.id().clone()));
256 if self.copy_archive(a).is_ok()
257 && let Some(manifest) =
258 LocalArchive::manifest_of(&self.0.path.join(a.id().as_ref()))
259 {
260 self.0.manager.load_one(&manifest, RelPath::from_id(a.id()));
261 }
262 }
263 current = next;
264 ls = &g.children;
265 }
266 }
267 }
268 });
269 }
270
271 #[tracing::instrument(level = "info",
273 parent = &self.0.span,
274 target = "sandbox",
275 name = "require",
276 skip(self)
277 )]
278 pub fn require(&self, id: &ArchiveId) {
279 let mut repos = self.0.repos.write();
281 self.require_meta_infs(
282 id,
283 &mut repos,
284 |a, repos| {
285 if !repos.iter().any(|r| r.id() == id) {
286 repos.push(SandboxedRepository::Copy(id.clone()));
287 self.copy_archive(a);
288 }
289 },
290 |g, t, repos| {
291 for a in g.dfs() {
292 if let ArchiveOrGroup::Archive(id) = a
293 && let Some(Archive::Local(a)) = t.get(id)
294 && !repos.iter().any(|r| r.id() == id)
295 {
296 repos.push(SandboxedRepository::Copy(id.clone()));
297 self.copy_archive(a);
298 if let Some(manifest) =
299 LocalArchive::manifest_of(&self.0.path.join(id.as_ref()))
300 {
301 self.0.manager.load_one(&manifest, RelPath::from_id(&id));
302 }
303 }
304 }
305 },
306 || tracing::error!("could not find archive {id}"),
307 );
308 drop(repos);
309
310 let manifest = LocalArchive::manifest_of(&self.0.path.join(id.as_ref()))
311 .expect("archive does not exist");
312 self.0.manager.load_one(&manifest, RelPath::from_id(&id));
313 }
314
315 pub fn maybe_copy(&self, archive: &LocalArchive) {
317 if !self.0.repos.read().iter().any(|a| a.id() == archive.id()) {
318 self.0
319 .repos
320 .write()
321 .push(SandboxedRepository::Copy(archive.id().clone()));
322 let _ = self.copy_archive(archive);
323 }
324 }
325
326 fn copy_archive(&self, a: &LocalArchive) -> Result<(), FileError> {
327 let path = a.path();
328 let target = self.0.path.join(a.id().as_ref());
329 if target.exists() {
330 return Err(FileError::AlreadyExists);
331 }
332 tracing::info!("copying archive {} to {}", a.id(), target.display());
333 path.copy_dir_all(&target)
334 }
335}
336
337impl LocalBackend for SandboxedBackend {
338 type ArchiveIter<'a> =
339 std::iter::Chain<std::slice::Iter<'a, Archive>, std::slice::Iter<'a, Archive>>;
340
341 fn save(
342 &self,
343 in_doc: &ftml_uris::DocumentUri,
344 rel_path: Option<&UriPath>,
345 log: crate::artifacts::FileOrString,
346 from: crate::formats::BuildTargetId,
347 result: Option<Box<dyn crate::artifacts::Artifact>>,
348 ) -> std::result::Result<(), crate::utils::errors::ArtifactSaveError> {
349 self.0
350 .manager
351 .with_buildable_archive(in_doc.archive_id(), |a| {
352 let Some(a) = a else {
353 return Err(ArtifactSaveError::NoArchive);
354 };
355 a.save(
356 in_doc,
357 rel_path,
358 log,
359 from,
360 result,
361 #[cfg(feature = "rdf")]
362 self.0.manager.triple_store(),
363 #[cfg(feature = "rdf")]
364 false,
365 )
366 })
367 }
368
369 fn get_document(&self, uri: &DocumentUri) -> Result<Document, BackendError> {
370 self.0
371 .manager
372 .get_document(uri)
373 .or_else(|_| GlobalBackend.get_document(uri))
374 }
375
376 #[allow(clippy::future_not_send)]
377 fn get_document_async<A: AsyncEngine>(
378 &self,
379 uri: &DocumentUri,
380 ) -> impl Future<Output = Result<Document, BackendError>> + Send + use<A>
381 where
382 Self: Sized,
383 {
384 let mgr = self.0.manager.get_document_async::<A>(uri);
385 let uri = uri.clone();
386 async move {
387 if let Ok(d) = mgr.await {
388 return Ok(d);
389 }
390 GlobalBackend.get_document_async::<A>(&uri).await
391 }
392 }
393
394 fn get_module(
395 &self,
396 uri: &ftml_uris::ModuleUri,
397 ) -> Result<ftml_ontology::domain::modules::ModuleLike, crate::utils::errors::BackendError>
398 {
399 self.0
400 .manager
401 .get_module(uri)
402 .or_else(|_| GlobalBackend.get_module(uri))
403 }
404
405 fn get_module_async<A: AsyncEngine>(
406 &self,
407 uri: &ftml_uris::ModuleUri,
408 ) -> impl Future<Output = Result<ftml_ontology::domain::modules::ModuleLike, BackendError>>
409 + Send
410 + use<A>
411 where
412 Self: Sized,
413 {
414 let uri = uri.clone();
415 let mgr = self.0.manager.get_module_async::<A>(&uri);
416 async move {
417 if let Ok(d) = mgr.await {
418 return Ok(d);
419 }
420 GlobalBackend.get_module_async::<A>(&uri).await
421 }
422 }
423
424 fn with_archive_or_group<R>(
425 &self,
426 id: &ArchiveId,
427 f: impl FnOnce(Option<&ArchiveOrGroup>) -> R,
428 ) -> R
429 where
430 Self: Sized,
431 {
432 match self.0.manager.with_archive_or_group(id, |a| {
433 if a.is_some() {
434 either::Left(f(a))
435 } else {
436 either::Right(f)
437 }
438 }) {
439 either::Left(v) => v,
440 either::Right(f) => GlobalBackend.with_archive_or_group(id, f),
441 }
442 }
443
444 fn with_archive<R>(&self, id: &ArchiveId, f: impl FnOnce(Option<&Archive>) -> R) -> R
445 where
446 Self: Sized,
447 {
448 match self.0.manager.with_archive(id, |a| {
449 if a.is_some() {
450 either::Left(f(a))
451 } else {
452 either::Right(f)
453 }
454 }) {
455 either::Left(v) => v,
456 either::Right(f) => GlobalBackend.with_archive(id, f),
457 }
458 }
459
460 fn with_archives<R>(&self, f: impl FnOnce(Self::ArchiveIter<'_>) -> R) -> R
461 where
462 Self: Sized,
463 {
464 self.0
465 .manager
466 .with_archives(|t1| GlobalBackend.with_archives(|t2| f(t1.iter().chain(t2.iter()))))
467 }
468
469 fn get_html_full(&self, uri: &DocumentUri) -> Result<Box<str>, BackendError> {
470 self.0
471 .manager
472 .get_html_full(uri)
473 .or_else(|_| GlobalBackend.get_html_full(uri))
474 }
475
476 fn get_html_body(&self, uri: &DocumentUri) -> Result<(Box<[Css]>, Box<str>), BackendError> {
477 self.0
478 .manager
479 .get_html_body(uri)
480 .or_else(|_| GlobalBackend.get_html_body(uri))
481 }
482
483 fn get_html_body_async<A: AsyncEngine>(
484 &self,
485 uri: &ftml_uris::DocumentUri,
486 ) -> impl Future<Output = Result<(Box<[ftml_ontology::utils::Css]>, Box<str>), BackendError>>
487 + Send
488 + use<A>
489 where
490 Self: Sized,
491 {
492 let mgr = self.0.manager.get_html_body_async::<A>(uri);
493 let uri = uri.clone();
494 async move {
495 if let Ok(d) = mgr.await {
496 return Ok(d);
497 }
498 GlobalBackend.get_html_body_async::<A>(&uri).await
499 }
500 }
501
502 fn get_html_body_inner(
503 &self,
504 uri: &DocumentUri,
505 ) -> Result<(Box<[Css]>, Box<str>), BackendError> {
506 self.0
507 .manager
508 .get_html_body_inner(uri)
509 .or_else(|_| GlobalBackend.get_html_body_inner(uri))
510 }
511
512 fn get_html_body_inner_async<A: AsyncEngine>(
513 &self,
514 uri: &ftml_uris::DocumentUri,
515 ) -> impl Future<Output = Result<(Box<[ftml_ontology::utils::Css]>, Box<str>), BackendError>>
516 + Send
517 + use<A>
518 where
519 Self: Sized,
520 {
521 let mgr = self.0.manager.get_html_body_inner_async::<A>(uri);
522 let uri = uri.clone();
523 async move {
524 if let Ok(d) = mgr.await {
525 return Ok(d);
526 }
527 GlobalBackend.get_html_body_inner_async::<A>(&uri).await
528 }
529 }
530
531 fn get_html_fragment(
532 &self,
533 uri: &DocumentUri,
534 range: DocumentRange,
535 ) -> Result<(Box<[Css]>, Box<str>), BackendError> {
536 self.0
537 .manager
538 .get_html_fragment(uri, range)
539 .or_else(|_| GlobalBackend.get_html_fragment(uri, range))
540 }
541
542 fn get_html_fragment_async<A: AsyncEngine>(
543 &self,
544 uri: &ftml_uris::DocumentUri,
545 range: ftml_ontology::narrative::DocumentRange,
546 ) -> impl Future<Output = Result<(Box<[ftml_ontology::utils::Css]>, Box<str>), BackendError>>
547 + Send
548 + use<A> {
549 let mgr = self.0.manager.get_html_fragment_async::<A>(uri, range);
550 let uri = uri.clone();
551 async move {
552 if let Ok(d) = mgr.await {
553 return Ok(d);
554 }
555 GlobalBackend
556 .get_html_fragment_async::<A>(&uri, range)
557 .await
558 }
559 }
560
561 fn get_reference<T: bincode::Decode<()>>(
562 &self,
563 rf: &ftml_ontology::narrative::DocDataRef<T>,
564 ) -> Result<T, BackendError>
565 where
566 Self: Sized,
567 {
568 self.0
569 .manager
570 .get_reference(rf)
571 .or_else(|_| GlobalBackend.get_reference(rf))
572 }
573
574 #[cfg(feature = "rdf")]
575 #[inline]
576 fn get_notations<E: AsyncEngine>(
577 &self,
578 uri: &ftml_uris::SymbolUri,
579 ) -> impl Iterator<
580 Item = (
581 ftml_uris::DocumentElementUri,
582 ftml_ontology::narrative::elements::Notation,
583 ),
584 >
585 where
586 Self: Sized,
587 {
588 GlobalBackend.get_notations::<E>(uri)
589 }
590
591 #[cfg(feature = "rdf")]
592 #[inline]
593 fn get_var_notations<E: AsyncEngine>(
594 &self,
595 uri: &ftml_uris::DocumentElementUri,
596 ) -> impl Iterator<
597 Item = (
598 ftml_uris::DocumentElementUri,
599 ftml_ontology::narrative::elements::Notation,
600 ),
601 >
602 where
603 Self: Sized,
604 {
605 GlobalBackend.get_var_notations::<E>(uri)
606 }
607}