1use std::path::{Path, PathBuf};
8
9use anyhow::Result;
10
11use crate::link::Link;
12use crate::link_storage::{ChangeObserver, LinkStorage};
13use crate::named_links::NamedLinks;
14use crate::pinned_types::{PinnedTypesAccess, PinnedTypesDecorator};
15
16pub trait NamedTypes {
17 fn get_name(&mut self, link: u32) -> Result<Option<String>>;
18 fn set_name(&mut self, link: u32, name: &str) -> Result<u32>;
19 fn get_by_name(&mut self, name: &str) -> Result<Option<u32>>;
20 fn remove_name(&mut self, link: u32) -> Result<()>;
21}
22
23pub struct NamedTypesDecorator {
24 pinned_types_decorator: PinnedTypesDecorator,
25 names_links: LinkStorage,
26 trace: bool,
27}
28
29impl NamedTypesDecorator {
30 pub fn new<P>(database_filename: P, trace: bool) -> Result<Self>
31 where
32 P: AsRef<Path>,
33 {
34 let names_database_filename =
35 Self::make_names_database_filename(database_filename.as_ref());
36 Self::with_names_database_path(database_filename, names_database_filename, trace)
37 }
38
39 pub fn with_names_database_path<P, N>(
40 database_filename: P,
41 names_database_filename: N,
42 trace: bool,
43 ) -> Result<Self>
44 where
45 P: AsRef<Path>,
46 N: AsRef<Path>,
47 {
48 let database_path = path_to_string(database_filename.as_ref());
49 let names_database_path = path_to_string(names_database_filename.as_ref());
50 let links = LinkStorage::new(&database_path, trace)?;
51 let names_links = LinkStorage::new(&names_database_path, trace)?;
52 Ok(Self::from_link_storages_with_trace(
53 links,
54 names_links,
55 trace,
56 ))
57 }
58
59 pub fn from_link_storages(links: LinkStorage, names_links: LinkStorage) -> Self {
60 Self::from_link_storages_with_trace(links, names_links, false)
61 }
62
63 pub fn from_pinned_types_decorator(
64 pinned_types_decorator: PinnedTypesDecorator,
65 names_links: LinkStorage,
66 ) -> Self {
67 Self::from_decorators_with_trace(pinned_types_decorator, names_links, false)
68 }
69
70 pub fn with_indexed_lookups(mut self) -> Self {
73 self.links_mut().enable_indexed_lookups();
74 self.names_links.enable_indexed_lookups();
75 self
76 }
77
78 pub fn make_names_database_filename<P>(database_filename: P) -> PathBuf
79 where
80 P: AsRef<Path>,
81 {
82 let path = database_filename.as_ref();
83 let filename_without_extension = path
84 .file_stem()
85 .and_then(|value| value.to_str())
86 .unwrap_or_default();
87 let names_filename = format!("{filename_without_extension}.names.links");
88
89 path.parent()
90 .filter(|parent| !parent.as_os_str().is_empty())
91 .map(|parent| parent.join(&names_filename))
92 .unwrap_or_else(|| PathBuf::from(names_filename))
93 }
94
95 pub fn links(&self) -> &LinkStorage {
96 self.pinned_types_decorator.links()
97 }
98
99 pub fn links_mut(&mut self) -> &mut LinkStorage {
100 self.pinned_types_decorator.links_mut()
101 }
102
103 pub fn pinned_types_decorator(&self) -> &PinnedTypesDecorator {
104 &self.pinned_types_decorator
105 }
106
107 pub fn pinned_types_decorator_mut(&mut self) -> &mut PinnedTypesDecorator {
108 &mut self.pinned_types_decorator
109 }
110
111 pub fn names_links(&self) -> &LinkStorage {
112 &self.names_links
113 }
114
115 pub fn names_links_mut(&mut self) -> &mut LinkStorage {
116 &mut self.names_links
117 }
118
119 pub fn into_link_storages(self) -> (LinkStorage, LinkStorage) {
120 (
121 self.pinned_types_decorator.into_link_storage(),
122 self.names_links,
123 )
124 }
125
126 pub fn save(&self) -> Result<()> {
127 self.pinned_types_decorator.save()?;
128 self.names_links.save()?;
129 Ok(())
130 }
131
132 pub fn create(&mut self, source: u32, target: u32) -> u32 {
133 self.pinned_types_decorator.create(source, target)
134 }
135
136 pub fn ensure_created(&mut self, id: u32) -> u32 {
137 self.pinned_types_decorator.ensure_created(id)
138 }
139
140 pub fn get(&self, id: u32) -> Option<&Link> {
141 self.pinned_types_decorator.get(id)
142 }
143
144 pub fn exists(&self, id: u32) -> bool {
145 self.pinned_types_decorator.exists(id)
146 }
147
148 pub fn update(&mut self, id: u32, source: u32, target: u32) -> Result<Link> {
149 self.update_observed(id, source, target, &mut |_, _| {})
150 }
151
152 pub fn update_observed(
166 &mut self,
167 id: u32,
168 source: u32,
169 target: u32,
170 observer: ChangeObserver<'_>,
171 ) -> Result<Link> {
172 let mut deleted = Vec::new();
173 let updated = self.pinned_types_decorator.update_observed(
174 id,
175 source,
176 target,
177 &mut |before, after| {
178 if after.is_null() && !before.is_null() {
179 deleted.push(before.index);
180 }
181 observer(before, after);
182 },
183 )?;
184 self.remove_names(&deleted)?;
185 Ok(updated)
186 }
187
188 pub fn delete(&mut self, id: u32) -> Result<Link> {
189 self.delete_observed(id, &mut |_, _| {})
190 }
191
192 pub fn delete_observed(&mut self, id: u32, observer: ChangeObserver<'_>) -> Result<Link> {
197 let mut deleted = Vec::new();
198 let removed = self
199 .pinned_types_decorator
200 .delete_observed(id, &mut |before, after| {
201 if after.is_null() && !before.is_null() {
202 deleted.push(before.index);
203 }
204 observer(before, after);
205 })?;
206 if !deleted.contains(&id) {
207 deleted.push(id);
208 }
209 self.remove_names(&deleted)?;
210 Ok(removed)
211 }
212
213 fn remove_names(&mut self, indexes: &[u32]) -> Result<()> {
214 for index in indexes {
215 self.remove_name(*index)?;
216 }
217 Ok(())
218 }
219
220 pub fn all(&self) -> Vec<&Link> {
221 self.pinned_types_decorator.all()
222 }
223
224 pub fn query(
225 &self,
226 index: Option<u32>,
227 source: Option<u32>,
228 target: Option<u32>,
229 ) -> Vec<&Link> {
230 self.pinned_types_decorator.query(index, source, target)
231 }
232
233 pub fn search(&self, source: u32, target: u32) -> Option<u32> {
234 self.pinned_types_decorator.search(source, target)
235 }
236
237 pub fn get_or_create(&mut self, source: u32, target: u32) -> u32 {
238 self.pinned_types_decorator.get_or_create(source, target)
239 }
240
241 fn from_link_storages_with_trace(
242 links: LinkStorage,
243 names_links: LinkStorage,
244 trace: bool,
245 ) -> Self {
246 Self::from_decorators_with_trace(
247 PinnedTypesDecorator::from_link_storage(links),
248 names_links,
249 trace,
250 )
251 }
252
253 fn from_decorators_with_trace(
254 pinned_types_decorator: PinnedTypesDecorator,
255 names_links: LinkStorage,
256 trace: bool,
257 ) -> Self {
258 Self {
259 pinned_types_decorator,
260 names_links,
261 trace,
262 }
263 }
264
265 fn with_named_links<T>(
266 &mut self,
267 action: impl FnOnce(&mut NamedLinks<'_>) -> Result<T>,
268 ) -> Result<T> {
269 let mut named_links = NamedLinks::new(&mut self.names_links)?;
270 action(&mut named_links)
271 }
272}
273
274impl NamedTypes for NamedTypesDecorator {
275 fn get_name(&mut self, link: u32) -> Result<Option<String>> {
276 if self.trace {
277 eprintln!("[TRACE] NamedTypesDecorator get_name for link {link}");
278 }
279 self.with_named_links(|named_links| named_links.get_name_by_external_reference(link))
280 }
281
282 fn set_name(&mut self, link: u32, name: &str) -> Result<u32> {
283 if self.trace {
284 eprintln!("[TRACE] NamedTypesDecorator set_name for link {link}: {name}");
285 }
286 if let Some(existing_link_with_name) = self.get_by_name(name)? {
287 if existing_link_with_name != link {
288 self.remove_name(existing_link_with_name)?;
289 }
290 }
291 self.remove_name(link)?;
292 self.with_named_links(|named_links| named_links.set_name_for_external_reference(link, name))
293 }
294
295 fn get_by_name(&mut self, name: &str) -> Result<Option<u32>> {
296 if self.trace {
297 eprintln!("[TRACE] NamedTypesDecorator get_by_name for name {name}");
298 }
299 self.with_named_links(|named_links| named_links.get_external_reference_by_name(name))
300 }
301
302 fn remove_name(&mut self, link: u32) -> Result<()> {
303 if self.trace {
304 eprintln!("[TRACE] NamedTypesDecorator remove_name for link {link}");
305 }
306 self.with_named_links(|named_links| named_links.remove_name_by_external_reference(link))
307 }
308}
309
310impl PinnedTypesAccess for NamedTypesDecorator {
311 fn pinned_types(&mut self, count: usize) -> Result<Vec<u32>> {
312 self.pinned_types_decorator.pinned_types(count)
313 }
314}
315
316fn path_to_string(path: &Path) -> String {
317 path.to_string_lossy().into_owned()
318}