link-cli 3.3.2

A CLI tool and reusable library for links manipulation backed by a LiNo-notation doublet storage engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Writes kept to be undone (issue #149): [`LinksUndoLog`] and the
//! [`UndoableLinksDecorator`] that fills it, so a request that fails can be
//! undone whole. The C# counterparts are `LinksUndoLog<TLinkAddress>` and
//! `UndoableLinksDecorator<TLinkAddress>`.

use std::collections::HashSet;

use anyhow::Result;

use crate::link::Link;
use crate::link_storage::ChangeObserver;
use crate::named_type_links::NamedTypeLinks;

/// A change as a store reports it, `None` standing for no link: `(None, link)`
/// is a creation, `(link, None)` a deletion.
pub type LinkChange = (Option<Link>, Option<Link>);

fn present(link: Link) -> Option<Link> {
    (!link.is_null()).then_some(link)
}

/// The writes made to a store, kept to undo them: every change a write
/// reports, and the names the links had before.
///
/// Undoing goes back through the changes: it deletes what was created,
/// updates back what was updated and creates again, at the same address,
/// what was deleted. Names are not changes: a deleted link loses its name
/// before anyone above the names store hears of the deletion, so the names a
/// write may touch are kept before it, with [`keep_name`](Self::keep_name)
/// and [`keep_names_of_usages`](Self::keep_names_of_usages).
#[derive(Debug, Default, Clone)]
pub struct LinksUndoLog {
    changes: Vec<LinkChange>,
    names: Vec<(u32, Option<String>)>,
    named: HashSet<u32>,
}

impl LinksUndoLog {
    pub fn new() -> Self {
        Self::default()
    }

    /// Whether nothing is kept.
    pub fn is_empty(&self) -> bool {
        self.changes.is_empty() && self.names.is_empty()
    }

    /// The changes kept, in the order they were made.
    pub fn changes(&self) -> &[LinkChange] {
        &self.changes
    }

    /// Keeps one change; a change that changes nothing is not kept.
    pub fn record(&mut self, before: Option<Link>, after: Option<Link>) {
        if before.is_some() && before == after {
            return;
        }
        self.changes.push((before, after));
    }

    /// Keeps one change as a [`ChangeObserver`] reports it: the null link is
    /// no link.
    pub fn record_observed(&mut self, before: Link, after: Link) {
        self.record(present(before), present(after));
    }

    /// Keeps the name `link` has now, unless one of it is kept already.
    pub fn keep_name<L: NamedTypeLinks + ?Sized>(
        &mut self,
        names: &mut L,
        link: u32,
    ) -> Result<()> {
        if self.named.insert(link) {
            let name = names.get_name(link)?;
            self.names.push((link, name));
        }
        Ok(())
    }

    /// Keeps the name of the link called `name` now, if any.
    pub fn keep_holder_of<L: NamedTypeLinks + ?Sized>(
        &mut self,
        links: &mut L,
        name: &str,
    ) -> Result<()> {
        match links.get_by_name(name)? {
            Some(holder) if holder != 0 => self.keep_name(links, holder),
            _ => Ok(()),
        }
    }

    /// Keeps the names of `link` and of every link that uses it, directly or
    /// not: the links deleting it deletes, or merging it repoints.
    pub fn keep_names_of_usages<L: NamedTypeLinks + ?Sized>(
        &mut self,
        links: &mut L,
        link: u32,
    ) -> Result<()> {
        for usage in usages_of(links, link) {
            self.keep_name(links, usage)?;
        }
        Ok(())
    }

    /// Forgets everything kept: the writes stay.
    pub fn clear(&mut self) {
        self.changes.clear();
        self.names.clear();
        self.named.clear();
    }

    /// Undoes every write kept, on `links` (the store the writes were made
    /// on, not a view over it), and forgets them.
    ///
    /// Returns the addresses of the links the writes created, now deleted again.
    pub fn undo<L: NamedTypeLinks + ?Sized>(&mut self, links: &mut L) -> Result<Vec<u32>> {
        let mut created = Vec::new();
        for &(before, after) in self.changes.iter().rev() {
            match (before, after) {
                (None, Some(made)) => {
                    if links.exists(made.index) {
                        // Whatever used it is undone already.
                        links.delete(made.index)?;
                    }
                    created.push(made.index);
                }
                (Some(was), None) => {
                    if !links.exists(was.index) {
                        links.try_ensure_created(was.index)?;
                    }
                    if was.source != 0 || was.target != 0 {
                        links.update(was.index, was.source, was.target)?;
                    }
                }
                (Some(old), Some(_)) => {
                    if links.exists(old.index) {
                        links.update(old.index, old.source, old.target)?;
                    }
                }
                (None, None) => {}
            }
        }
        // A name is the link's again; first take every name a write gave.
        for (link, name) in &self.names {
            if *name != links.get_name(*link)? && (name.is_none() || !links.exists(*link)) {
                links.remove_name(*link)?;
            }
        }
        for (link, name) in &self.names {
            if let Some(name) = name {
                if links.exists(*link) && Some(name) != links.get_name(*link)?.as_ref() {
                    links.set_name(*link, name)?;
                }
            }
        }
        self.clear();
        Ok(created)
    }
}

/// `link` and every link that uses it, directly or not, `link` first.
pub fn usages_of<L: NamedTypeLinks + ?Sized>(links: &mut L, link: u32) -> Vec<u32> {
    let mut found = vec![link];
    let mut seen = HashSet::from([link]);
    let mut i = 0;
    while i < found.len() {
        let at = found[i];
        let usages = links
            .query_links(None, Some(at), None)
            .into_iter()
            .chain(links.query_links(None, None, Some(at)));
        for usage in usages {
            if seen.insert(usage.index) {
                found.push(usage.index);
            }
        }
        i += 1;
    }
    found
}

/// Keeps the names an update of `index` to `(source target)` may take: of
/// the link, and of its usages too when it merges into an equal link and so
/// is deleted.
pub fn keep_names_before_update<L: NamedTypeLinks + ?Sized>(
    log: &mut LinksUndoLog,
    links: &mut L,
    index: u32,
    source: u32,
    target: u32,
) -> Result<()> {
    match links.search(source, target) {
        Some(equal) if equal != 0 && equal != index => log.keep_names_of_usages(links, index),
        _ => log.keep_name(links, index),
    }
}

/// A store that keeps every write in a [`LinksUndoLog`] while
/// [`recording`](Self::recording), so a request that fails can be undone
/// whole. The links server puts it under every session with permissions.
pub struct UndoableLinksDecorator<L: NamedTypeLinks> {
    links: L,
    log: LinksUndoLog,
    recording: bool,
}

impl<L: NamedTypeLinks> UndoableLinksDecorator<L> {
    pub fn new(links: L) -> Self {
        Self {
            links,
            log: LinksUndoLog::new(),
            recording: false,
        }
    }

    /// The writes kept since [`begin`](Self::begin).
    pub fn log(&self) -> &LinksUndoLog {
        &self.log
    }

    /// Whether writes are kept.
    pub fn recording(&self) -> bool {
        self.recording
    }

    /// Starts keeping writes, forgetting the ones kept before.
    pub fn begin(&mut self) {
        self.log.clear();
        self.recording = true;
    }

    /// Stops keeping writes; they stay.
    pub fn commit(&mut self) {
        self.recording = false;
        self.log.clear();
    }

    /// Undoes every write kept since [`begin`](Self::begin).
    ///
    /// Returns the addresses of the links the writes created, now deleted again.
    pub fn rollback(&mut self) -> Result<Vec<u32>> {
        self.recording = false;
        self.log.undo(&mut self.links)
    }

    pub fn inner(&self) -> &L {
        &self.links
    }

    pub fn inner_mut(&mut self) -> &mut L {
        &mut self.links
    }

    pub fn into_inner(self) -> L {
        self.links
    }

    fn created(&mut self, id: u32) -> u32 {
        if self.recording {
            let link = self.links.get_link(id).unwrap_or(Link::new(id, 0, 0));
            self.log.record(None, Some(link));
        }
        id
    }
}

impl<L: NamedTypeLinks> NamedTypeLinks for UndoableLinksDecorator<L> {
    fn query_limits(&self) -> Option<crate::query_options::QueryLimits> {
        self.links.query_limits()
    }

    fn reserve_query_allocations(
        &mut self,
        processor: &crate::query_processor::QueryProcessor,
        restriction: &crate::lino_link::LinoLink,
        substitution: &crate::lino_link::LinoLink,
    ) -> Result<Option<std::collections::HashSet<u32>>> {
        self.links
            .reserve_query_allocations(processor, restriction, substitution)
    }

    fn create(&mut self, source: u32, target: u32) -> u32 {
        let id = self.links.create(source, target);
        self.created(id)
    }

    fn ensure_created(&mut self, id: u32) -> u32 {
        if id == 0 || self.links.exists(id) {
            return id;
        }
        let id = self.links.ensure_created(id);
        self.created(id)
    }

    fn try_ensure_created(&mut self, id: u32) -> Result<u32> {
        if id != 0 && self.links.exists(id) {
            return Ok(id);
        }
        let id = self.links.try_ensure_created(id)?;
        Ok(self.created(id))
    }

    fn get_link(&mut self, id: u32) -> Option<Link> {
        self.links.get_link(id)
    }

    fn exists(&mut self, id: u32) -> bool {
        self.links.exists(id)
    }

    fn try_create(&mut self, source: u32, target: u32) -> Result<u32> {
        let id = self.links.try_create(source, target)?;
        Ok(self.created(id))
    }

    fn reference_exists(&mut self, id: u32) -> bool {
        self.links.reference_exists(id)
    }

    fn next_created_addresses(&mut self, count: usize) -> Result<Vec<u32>> {
        // Created and deleted again: nothing to undo.
        self.links.next_created_addresses(count)
    }

    fn update_observed(
        &mut self,
        id: u32,
        source: u32,
        target: u32,
        observer: ChangeObserver<'_>,
    ) -> Result<Link> {
        if !self.recording {
            return self.links.update_observed(id, source, target, observer);
        }
        keep_names_before_update(&mut self.log, &mut self.links, id, source, target)?;
        let log = &mut self.log;
        self.links
            .update_observed(id, source, target, &mut |before, after| {
                log.record_observed(before, after);
                observer(before, after);
            })
    }

    fn delete_observed(&mut self, id: u32, observer: ChangeObserver<'_>) -> Result<Link> {
        if !self.recording {
            return self.links.delete_observed(id, observer);
        }
        self.log.keep_names_of_usages(&mut self.links, id)?;
        let log = &mut self.log;
        self.links.delete_observed(id, &mut |before, after| {
            log.record_observed(before, after);
            observer(before, after);
        })
    }

    fn all_links(&mut self) -> Vec<Link> {
        self.links.all_links()
    }

    fn query_links(
        &mut self,
        index: Option<u32>,
        source: Option<u32>,
        target: Option<u32>,
    ) -> Vec<Link> {
        self.links.query_links(index, source, target)
    }

    fn search(&mut self, source: u32, target: u32) -> Option<u32> {
        self.links.search(source, target)
    }

    fn get_or_create(&mut self, source: u32, target: u32) -> u32 {
        match self.links.search(source, target) {
            Some(found) => found,
            None => self.create(source, target),
        }
    }

    fn get_name(&mut self, id: u32) -> Result<Option<String>> {
        self.links.get_name(id)
    }

    fn set_name(&mut self, id: u32, name: &str) -> Result<u32> {
        if self.recording {
            self.log.keep_name(&mut self.links, id)?;
            self.log.keep_holder_of(&mut self.links, name)?;
        }
        self.links.set_name(id, name)
    }

    fn get_by_name(&mut self, name: &str) -> Result<Option<u32>> {
        self.links.get_by_name(name)
    }

    fn remove_name(&mut self, id: u32) -> Result<()> {
        if self.recording {
            self.log.keep_name(&mut self.links, id)?;
        }
        self.links.remove_name(id)
    }

    fn save(&mut self) -> Result<()> {
        self.links.save()
    }
}