Skip to main content

link_cli/engine/
named.rs

1//! Adapter to the CLI/query/protocol surface. The raw engine's constraints
2//! remain commit-time policies; these operations do not merge or cascade.
3use super::EngineTransaction;
4use crate::query_address::QueryAddress;
5use crate::{GenericLink as Link, LinksStorage, NamedTypeLinks, TransitionLogStore};
6use anyhow::Result;
7
8impl<T: QueryAddress, L: TransitionLogStore> NamedTypeLinks<T> for EngineTransaction<T, L> {
9    fn reserve_query_allocations(
10        &mut self,
11        processor: &crate::GenericQueryProcessor<T>,
12        restriction: &crate::lino_link::LinoLink,
13        substitution: &crate::lino_link::LinoLink,
14    ) -> Result<Option<std::collections::HashSet<T>>> {
15        self.reserve_query(processor, restriction, substitution)
16    }
17    fn create(&mut self, source: T, target: T) -> T {
18        self.create_link(source, target)
19            .expect("allocation failed; use try_create")
20    }
21    fn try_create(&mut self, source: T, target: T) -> Result<T> {
22        Ok(self.create_link(source, target)?)
23    }
24    fn ensure_created(&mut self, id: T) -> T {
25        self.ensure_link_created(id)
26            .expect("allocation failed; use try_ensure_created")
27    }
28    fn try_ensure_created(&mut self, id: T) -> Result<T> {
29        Ok(self.ensure_link_created(id)?)
30    }
31    fn get_link(&mut self, id: T) -> Option<Link<T>> {
32        LinksStorage::get_link(self, id)
33    }
34    fn exists(&mut self, id: T) -> bool {
35        self.link_exists(id)
36    }
37    fn update_observed(
38        &mut self,
39        id: T,
40        source: T,
41        target: T,
42        observer: crate::link_storage::ChangeObserver<'_, T>,
43    ) -> Result<Link<T>> {
44        let before = self.update_link(id, source, target)?;
45        observer(before, Link::new(id, source, target));
46        Ok(before)
47    }
48    fn delete_observed(
49        &mut self,
50        id: T,
51        observer: crate::link_storage::ChangeObserver<'_, T>,
52    ) -> Result<Link<T>> {
53        let before = self.delete_link(id)?;
54        observer(before, Link::null());
55        Ok(before)
56    }
57    fn all_links(&mut self) -> Vec<Link<T>> {
58        LinksStorage::all_links(self)
59    }
60    fn search(&mut self, source: T, target: T) -> Option<T> {
61        self.search_link(source, target)
62    }
63    fn get_or_create(&mut self, source: T, target: T) -> T {
64        self.get_or_create_link(source, target)
65            .expect("allocation failed; use fallible LinksStorage")
66    }
67    fn get_name(&mut self, id: T) -> Result<Option<String>> {
68        Ok(EngineTransaction::get_name(self, id))
69    }
70    fn set_name(&mut self, id: T, name: &str) -> Result<T> {
71        EngineTransaction::set_name(self, id, Some(name.into()))?;
72        Ok(id)
73    }
74    fn get_by_name(&mut self, name: &str) -> Result<Option<T>> {
75        Ok(EngineTransaction::get_by_name(self, name))
76    }
77    fn remove_name(&mut self, id: T) -> Result<()> {
78        Ok(EngineTransaction::set_name(self, id, None)?)
79    }
80    fn save(&mut self) -> Result<()> {
81        // The protocol saves after individual writes. A session must keep
82        // them private until its caller explicitly commits the whole batch.
83        if self.closed {
84            anyhow::bail!("transaction is closed");
85        }
86        Ok(())
87    }
88}
89
90impl<T: QueryAddress> NamedTypeLinks<T> for super::cluster::ShardSession<T> {
91    fn reserve_query_allocations(
92        &mut self,
93        processor: &crate::GenericQueryProcessor<T>,
94        restriction: &crate::lino_link::LinoLink,
95        substitution: &crate::lino_link::LinoLink,
96    ) -> Result<Option<std::collections::HashSet<T>>> {
97        self.session
98            .reserve_query_allocations(processor, restriction, substitution)
99    }
100    fn create(&mut self, source: T, target: T) -> T {
101        NamedTypeLinks::create(&mut self.session, source, target)
102    }
103    fn try_create(&mut self, source: T, target: T) -> Result<T> {
104        NamedTypeLinks::try_create(&mut self.session, source, target)
105    }
106    fn ensure_created(&mut self, id: T) -> T {
107        NamedTypeLinks::ensure_created(&mut self.session, id)
108    }
109    fn try_ensure_created(&mut self, id: T) -> Result<T> {
110        NamedTypeLinks::try_ensure_created(&mut self.session, id)
111    }
112    fn get_link(&mut self, id: T) -> Option<Link<T>> {
113        NamedTypeLinks::get_link(&mut self.session, id)
114    }
115    fn exists(&mut self, id: T) -> bool {
116        NamedTypeLinks::exists(&mut self.session, id)
117    }
118    fn update_observed(
119        &mut self,
120        id: T,
121        source: T,
122        target: T,
123        observer: crate::link_storage::ChangeObserver<'_, T>,
124    ) -> Result<Link<T>> {
125        NamedTypeLinks::update_observed(&mut self.session, id, source, target, observer)
126    }
127    fn delete_observed(
128        &mut self,
129        id: T,
130        observer: crate::link_storage::ChangeObserver<'_, T>,
131    ) -> Result<Link<T>> {
132        NamedTypeLinks::delete_observed(&mut self.session, id, observer)
133    }
134    fn all_links(&mut self) -> Vec<Link<T>> {
135        NamedTypeLinks::all_links(&mut self.session)
136    }
137    fn search(&mut self, source: T, target: T) -> Option<T> {
138        NamedTypeLinks::search(&mut self.session, source, target)
139    }
140    fn get_or_create(&mut self, source: T, target: T) -> T {
141        NamedTypeLinks::get_or_create(&mut self.session, source, target)
142    }
143    fn get_name(&mut self, id: T) -> Result<Option<String>> {
144        NamedTypeLinks::get_name(&mut self.session, id)
145    }
146    fn set_name(&mut self, id: T, name: &str) -> Result<T> {
147        NamedTypeLinks::set_name(&mut self.session, id, name)
148    }
149    fn get_by_name(&mut self, name: &str) -> Result<Option<T>> {
150        NamedTypeLinks::get_by_name(&mut self.session, name)
151    }
152    fn remove_name(&mut self, id: T) -> Result<()> {
153        NamedTypeLinks::remove_name(&mut self.session, id)
154    }
155    fn save(&mut self) -> Result<()> {
156        NamedTypeLinks::save(&mut self.session)
157    }
158}