semiont 0.7.2

Semiont's Rust SDK: the protocol's types, the transport contract and a client of the bus over it, and what a client needs to name its principals and hold its roles, as protocol/specs/ states it.
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! What keeps a client's queries true: protocol/specs/src/client/refresh.json
//! (`crate::refresh`), applied to the caches of the namespaces that hold
//! queries. Each event on the bus, and the reopening of a dropped stream,
//! asks again for the queries its row names, writes the ones whose new value
//! the event carries, and ends the ones whose entity is gone.
//!
//! A client has one refresher. What each event names (its resource, its
//! annotation, the value it carries) it reads where the table's row says it
//! is. It states for itself only which namespace's cache answers each query
//! of the table: `gather` what refers to a resource, `match_` the searches,
//! `browse` the rest; a match over the table's own enum, so a query added to
//! the table does not compile until it is answered here.
//!
//! An event acts only on a key its cache holds, and the refetches one key is
//! asked for inside a window are one refetch
//! (protocol/contract/CACHE-SEMANTICS.md B12–B13b, B19, B20).

use super::browse::{self, BrowseNamespace, WHOLE, invalidate_engines};
use super::gather::{GatherNamespace, ReferencedBy};
use super::match_::{MatchNamespace, ResourceSearch};
use crate::bus::held_to;
use crate::cache::{Cache, CacheKey, CacheValue};
use crate::client::Links;
use crate::locked;
use crate::refresh::{CacheQuery, CacheRefresh, Reach, RefreshTrigger, RefreshWhen};
use crate::transport::{ConnectionState, Frame, FrameRefusal, Lagged};
use crate::types::{Annotation, AnnotationId, MatchResourcesResponse, ResourceId};
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::watch;
use tokio::task::{AbortHandle, JoinSet};

// ── Windows ─────────────────────────────────────────────────────────────

/// What a window still owes: the refetch asked for while it was open.
type Owed = Box<dyn FnOnce() + Send>;

struct Window {
    owed: Option<Owed>,
    closing: AbortHandle,
}

/// B19: per key, the first refetch an event asks for runs at once and opens
/// a window; any more inside it are owed, and run as one when it closes,
/// which opens the next. So a storm of events costs a refetch per key per
/// window, and the last event is always reflected.
struct Windows {
    lasting: Duration,
    /// `None` once disposed.
    open: Mutex<Option<HashMap<String, Window>>>,
}

impl Windows {
    fn run(self: &Arc<Self>, key: String, refetch: Owed) {
        let mut open = locked(&self.open);
        let Some(windows) = open.as_mut() else {
            return;
        };
        if let Some(window) = windows.get_mut(&key) {
            window.owed = Some(refetch);
            return;
        }
        let these = Arc::downgrade(self);
        let lasting = self.lasting;
        let of = key.clone();
        let closing = tokio::spawn(async move {
            tokio::time::sleep(lasting).await;
            let Some(these) = these.upgrade() else {
                return;
            };
            let owed = locked(&these.open)
                .as_mut()
                .and_then(|windows| windows.remove(&of))
                .and_then(|window| window.owed);
            if let Some(owed) = owed {
                these.run(of, owed);
            }
        })
        .abort_handle();
        windows.insert(
            key,
            Window {
                owed: None,
                closing,
            },
        );
        drop(open);
        refetch();
    }

    /// Every window closes, and what each owed is dropped.
    fn dispose(&self) {
        if let Some(windows) = locked(&self.open).take() {
            for window in windows.into_values() {
                window.closing.abort();
            }
        }
    }
}

// ── What an event names ─────────────────────────────────────────────────

/// What an event names: which of a split channel's rows it is, the keys a
/// row that reaches its `Subject` acts on, and the value an enriched event
/// carries.
#[derive(Default)]
pub(super) struct Subject {
    pub(super) when: Option<RefreshWhen>,
    pub(super) resource: Option<ResourceId>,
    pub(super) annotation: Option<AnnotationId>,
    pub(super) written: Option<Annotation>,
}

/// An event that cannot be read: missed, refused, or one a member of which
/// its row names is present and is not what the row names.
struct Unreadable;

/// The member `way` ends at in `event`: none where the row names no way, or
/// where the member, or one it passes through, is absent or null. One it
/// passes through that is present and is no object makes the event
/// unreadable.
fn member_at<'a>(event: &'a Value, way: Option<&[&str]>) -> Result<Option<&'a Value>, Unreadable> {
    let Some(way) = way else {
        return Ok(None);
    };
    let mut member = event;
    for name in way {
        member = match member {
            Value::Null => return Ok(None),
            Value::Object(members) => match members.get(*name) {
                Some(within) => within,
                None => return Ok(None),
            },
            _ => return Err(Unreadable),
        };
    }
    Ok(match member {
        Value::Null => None,
        present => Some(present),
    })
}

/// What `way` names in `event`, read as a `T` by its own decoder (an id's
/// decoder holds its pattern): one present and not a `T` makes the event
/// unreadable.
fn named<'a, T: Deserialize<'a>>(
    event: &'a Value,
    way: Option<&[&str]>,
) -> Result<Option<T>, Unreadable> {
    match member_at(event, way)? {
        None => Ok(None),
        Some(member) => T::deserialize(member).map(Some).map_err(|_| Unreadable),
    }
}

/// `trigger`'s row for events `when` says they are. The table's own reader
/// refuses a trigger that has not exactly one.
fn row(trigger: RefreshTrigger, when: Option<RefreshWhen>) -> &'static CacheRefresh {
    match trigger.rows().iter().find(|row| row.when == when) {
        Some(row) => row,
        None => panic!("the refresh table has no row for {trigger:?} when {when:?}"),
    }
}

/// What a frame on `trigger`'s `channel` names, by the ways of its row. A
/// split channel's event is enriched when the member its enriched row
/// carries is present.
fn subject(
    trigger: RefreshTrigger,
    channel: &str,
    frame: Result<Result<Frame, FrameRefusal>, Lagged>,
) -> Result<Subject, Unreadable> {
    let Ok(Ok(frame)) = frame else {
        return Err(Unreadable);
    };
    let event = held_to(channel, frame.payload).map_err(|_| Unreadable)?;
    let enriched = trigger
        .rows()
        .iter()
        .find(|row| row.when == Some(RefreshWhen::Enriched));
    let when = match enriched {
        None => None,
        Some(enriched) => match member_at(&event, enriched.carries)? {
            Some(_) => Some(RefreshWhen::Enriched),
            None => Some(RefreshWhen::Unenriched),
        },
    };
    let row = row(trigger, when);
    Ok(Subject {
        when,
        resource: named(&event, row.names.resource)?,
        annotation: named(&event, row.names.annotation)?,
        written: named(&event, row.carries)?,
    })
}

// ── The refresher ───────────────────────────────────────────────────────

/// See the module's documentation.
pub(crate) struct Refresher {
    browse: Arc<browse::Live>,
    referenced_by: Cache<ResourceId, Vec<ReferencedBy>>,
    searches: Cache<ResourceSearch, MatchResourcesResponse>,
    windows: Arc<Windows>,
    /// What listens for each trigger, until the refresher is disposed.
    listening: Mutex<JoinSet<()>>,
}

impl Refresher {
    /// A refresher of the queries the three namespaces hold, listening from
    /// now on.
    pub(crate) fn new(
        links: &Links,
        browse: &BrowseNamespace,
        gather: &GatherNamespace,
        match_: &MatchNamespace,
    ) -> Arc<Refresher> {
        let refresher = Arc::new(Refresher {
            browse: browse.live.clone(),
            referenced_by: gather.referenced_by.clone(),
            searches: match_.searches.clone(),
            windows: Arc::new(Windows {
                lasting: links.timing.invalidation_window,
                open: Mutex::new(Some(HashMap::new())),
            }),
            listening: Mutex::new(JoinSet::new()),
        });
        refresher.listen(links);
        refresher
    }

    /// Listen for every trigger of the refresh table: the stream's reopening,
    /// and the events of each channel the table names.
    fn listen(self: &Arc<Self>, links: &Links) {
        self.on_reopening(links.transport.state());
        for trigger in RefreshTrigger::ALL {
            if let Some(channel) = trigger.channel() {
                self.on(links, *trigger, channel);
            }
        }
    }

    /// Apply `trigger`'s row to what each event on its `channel` names; and,
    /// to one that cannot be read, the row of an unreadable event. A missed
    /// event is one: something changed and nothing says what.
    fn on(self: &Arc<Self>, links: &Links, trigger: RefreshTrigger, channel: &'static str) {
        let mut frames = links.own.frames(channel);
        let refresher = Arc::downgrade(self);
        locked(&self.listening).spawn(async move {
            while let Some(frame) = frames.next().await {
                let Some(refresher) = refresher.upgrade() else {
                    return;
                };
                match subject(trigger, channel, frame) {
                    Ok(subject) => refresher.refresh(trigger, &subject),
                    Err(Unreadable) => {
                        refresher.refresh(RefreshTrigger::Unreadable, &Subject::default());
                    }
                }
            }
        });
    }

    /// B13: the stream is open again having left `Open`, which only a drop
    /// does. A changed subscription is handed over with the state still
    /// `Open`, and misses nothing.
    fn on_reopening(self: &Arc<Self>, mut state: watch::Receiver<ConnectionState>) {
        let refresher = Arc::downgrade(self);
        locked(&self.listening).spawn(async move {
            let (mut opened, mut left) = (false, false);
            loop {
                // A reopening is seen as one because a transport waits on its
                // connection between leaving `Open` and reaching it again,
                // and this task runs in that wait.
                if *state.borrow_and_update() == ConnectionState::Open {
                    if left && let Some(refresher) = refresher.upgrade() {
                        refresher.refresh(RefreshTrigger::Reopened, &Subject::default());
                    }
                    opened = true;
                    left = false;
                } else {
                    left = opened;
                }
                if state.changed().await.is_err() {
                    return;
                }
            }
        });
    }

    fn refresh(&self, trigger: RefreshTrigger, subject: &Subject) {
        let row = row(trigger, subject.when);
        // What an event carries, and what it says is gone, is only ever an
        // annotation: the table's own build refuses a row that says more.
        for query in row.writes {
            self.browse.write(*query, subject);
        }
        for query in row.removes {
            self.browse.remove(*query, subject);
        }
        for query in row.refetches {
            self.refetch(*query, subject, row.reach);
        }
    }

    /// Ask again for one key, when the cache holds it (B20), through the
    /// key's window (B19).
    fn again<K, V>(&self, cache: &Cache<K, V>, key: K, window: String)
    where
        K: CacheKey,
        V: CacheValue,
    {
        if cache.known(&key) {
            let cache = cache.clone();
            self.windows
                .run(window, Box::new(move || cache.invalidate(&key)));
        }
    }

    /// Ask again for every key the cache holds, as one, through one window:
    /// what an event does to a query's answers when it cannot say which of
    /// them it changes.
    fn all_again<K, V>(&self, cache: &Cache<K, V>, window: &str)
    where
        K: CacheKey,
        V: CacheValue,
    {
        let cache = cache.clone();
        self.windows
            .run(window.to_owned(), Box::new(move || cache.invalidate_all()));
    }

    /// B7: ask again, for the keys the row reaches, showing what there is
    /// meanwhile. Each arm names the cache that answers its query, and a
    /// query with no arm does not compile.
    fn refetch(&self, query: CacheQuery, subject: &Subject, reach: Reach) {
        let browse = &self.browse;
        let reached = |held: Vec<ResourceId>| match reach {
            Reach::Held => held,
            Reach::Subject => subject.resource.iter().cloned().collect(),
        };
        match query {
            CacheQuery::Resource => {
                for id in reached(browse.resource.keys()) {
                    let window = format!("resource/{id}");
                    self.again(&browse.resource, id, window);
                }
            }
            CacheQuery::Annotations => {
                for id in reached(browse.annotations.keys()) {
                    let window = format!("annotations/{id}");
                    self.again(&browse.annotations, id, window);
                }
            }
            CacheQuery::Events => {
                for id in reached(browse.events.keys()) {
                    let window = format!("events/{id}");
                    self.again(&browse.events, id, window);
                }
            }
            CacheQuery::ReferencedBy => {
                for id in reached(self.referenced_by.keys()) {
                    let window = format!("referenced-by/{id}");
                    self.again(&self.referenced_by, id, window);
                }
            }
            CacheQuery::Annotation => {
                // The annotation the event names; when it names none, each
                // one held of the resource it names.
                let annotations = match (reach, &subject.annotation) {
                    (Reach::Held, _) => browse.annotation.keys(),
                    (Reach::Subject, Some(annotation)) => vec![annotation.clone()],
                    (Reach::Subject, None) => locked(&browse.annotation_of)
                        .iter()
                        .filter(|(_, of)| Some(*of) == subject.resource.as_ref())
                        .map(|(annotation, _)| annotation.clone())
                        .collect(),
                };
                for id in annotations {
                    let window = format!("annotation/{id}");
                    self.again(&browse.annotation, id, window);
                }
            }
            // An event does not say which lists it changes, nor which
            // searches would now find its resource.
            CacheQuery::Resources => self.all_again(&browse.lists, "resource-lists"),
            CacheQuery::MatchedResources => self.all_again(&self.searches, "matched-resources"),
            CacheQuery::EntityTypes => {
                self.again(
                    &browse.entity_types,
                    WHOLE.to_owned(),
                    "entity-types".to_owned(),
                );
            }
            CacheQuery::TagSchemas => {
                self.again(
                    &browse.tag_schemas,
                    WHOLE.to_owned(),
                    "tag-schemas".to_owned(),
                );
            }
            CacheQuery::Engines => {
                if browse.engines.known(&WHOLE.to_owned()) {
                    let (engines, limits) = (browse.engines.clone(), browse.limits.clone());
                    self.windows.run(
                        "engines".to_owned(),
                        Box::new(move || invalidate_engines(&engines, &limits)),
                    );
                }
            }
        }
    }

    /// Stop: no event refreshes anything after, and what a window owed is
    /// dropped.
    pub(crate) fn dispose(&self) {
        locked(&self.listening).abort_all();
        self.windows.dispose();
    }
}

impl Drop for Refresher {
    fn drop(&mut self) {
        self.dispose();
    }
}