link_cli/engine/
causal_named.rs1use super::causal_session::CausalWorkspace;
4use crate::{
5 link_storage::ChangeObserver, query_options::QueryLimits, GenericLink, LinkError, LinksStorage,
6 NamedTypeLinks,
7};
8use anyhow::Result;
9
10pub struct CausalNamedLinks<'a> {
11 pub(super) workspace: &'a mut CausalWorkspace,
12}
13
14fn address(value: u128) -> Result<u64> {
15 Ok(u64::try_from(value).map_err(|_| LinkError::AddressOutOfRange(value))?)
16}
17
18fn wide(link: GenericLink<u64>) -> GenericLink<u128> {
19 GenericLink::new(link.index.into(), link.source.into(), link.target.into())
20}
21
22impl NamedTypeLinks<u128> for CausalNamedLinks<'_> {
23 fn query_limits(&self) -> Option<QueryLimits> {
24 Some(QueryLimits::causal())
25 }
26 fn reserve_query_allocations(
27 &mut self,
28 processor: &crate::GenericQueryProcessor<u128>,
29 restriction: &crate::lino_link::LinoLink,
30 substitution: &crate::lino_link::LinoLink,
31 ) -> Result<Option<std::collections::HashSet<u128>>> {
32 self.workspace
33 .reserve_query(processor, restriction, substitution)
34 }
35 fn create(&mut self, source: u128, target: u128) -> u128 {
36 self.try_create(source, target)
37 .expect("allocation failed; use try_create")
38 }
39 fn try_create(&mut self, source: u128, target: u128) -> Result<u128> {
40 let source = address(source)?;
41 let target = address(target)?;
42 Ok(self.workspace.create_link(source, target)?.into())
43 }
44 fn ensure_created(&mut self, id: u128) -> u128 {
45 self.try_ensure_created(id)
46 .expect("allocation failed; use try_ensure_created")
47 }
48 fn try_ensure_created(&mut self, id: u128) -> Result<u128> {
49 Ok(self.workspace.ensure_link_created(address(id)?)?.into())
50 }
51 fn get_link(&mut self, id: u128) -> Option<GenericLink<u128>> {
52 self.workspace.get_link(u64::try_from(id).ok()?).map(wide)
53 }
54 fn exists(&mut self, id: u128) -> bool {
55 self.get_link(id).is_some()
56 }
57 fn update_observed(
58 &mut self,
59 id: u128,
60 source: u128,
61 target: u128,
62 observer: ChangeObserver<'_, u128>,
63 ) -> Result<GenericLink<u128>> {
64 let id = address(id)?;
65 let source = address(source)?;
66 let target = address(target)?;
67 let before = wide(self.workspace.update_link(id, source, target)?);
68 observer(before, wide(GenericLink::new(id, source, target)));
69 Ok(before)
70 }
71 fn delete_observed(
72 &mut self,
73 id: u128,
74 observer: ChangeObserver<'_, u128>,
75 ) -> Result<GenericLink<u128>> {
76 let before = wide(self.workspace.delete_link(address(id)?)?);
77 observer(before, GenericLink::null());
78 Ok(before)
79 }
80 fn all_links(&mut self) -> Vec<GenericLink<u128>> {
81 self.workspace.all_links().into_iter().map(wide).collect()
82 }
83 fn search(&mut self, source: u128, target: u128) -> Option<u128> {
84 self.all_links()
85 .into_iter()
86 .filter(|link| link.source == source && link.target == target)
87 .map(|link| link.index)
88 .min()
89 }
90 fn get_or_create(&mut self, source: u128, target: u128) -> u128 {
91 self.try_get_or_create(source, target)
92 .expect("allocation failed; use try_get_or_create")
93 }
94 fn get_name(&mut self, id: u128) -> Result<Option<String>> {
95 Ok(self.workspace.get_name(address(id)?))
96 }
97 fn format_reference(&mut self, id: u128) -> Result<String> {
98 Ok(self
99 .get_name(id)?
100 .map(|name| links_notation::encode_reference_literal(&name))
101 .unwrap_or_else(|| id.to_string()))
102 }
103 fn set_name(&mut self, id: u128, name: &str) -> Result<u128> {
104 self.workspace
105 .set_name(address(id)?, Some(name.to_owned()))?;
106 Ok(id)
107 }
108 fn get_by_name(&mut self, name: &str) -> Result<Option<u128>> {
109 Ok(self.workspace.get_by_name(name).map(u128::from))
110 }
111 fn remove_name(&mut self, id: u128) -> Result<()> {
112 Ok(self.workspace.set_name(address(id)?, None)?)
113 }
114 fn save(&mut self) -> Result<()> {
115 Ok(self.workspace.query_save()?)
116 }
117}