harmos-domain 0.1.5

Harmos domain model: identifiers, contracts, catalogs and storage identities.
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
430
431
432
433
434
435
436
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

/* === Modules === */

/* === Re-Exports === */

/* === Imports === */

// Standard Library Imports
use std::collections::BTreeMap;
// Third Party Imports
use serde::Deserialize;
// Local Imports
use super::{StreamDeclaration, digits, quoted, quoted_len, write};
use crate::config::SchemaSettings;
use crate::schema::declaration::{Definition, Named};
use crate::schema::graph::Schema;

/* === Constants === */

const HEAD: &[u8] = b"\0HARMOS:ARTIFACT:V6:{\"kind\":\"publications\",\"facility\":";
const OWNER: &[u8] = b",\"owner\":";
const STREAMS: &[u8] = b",\"streams\":[";
const ID: &[u8] = b"{\"id\":";
const SCHEMA: &[u8] = b",\"schema\":";
const WINDOW: &[u8] = b",\"window\":";
const METADATA: &[u8] = b",\"metadata\":";
const ROW_METADATA: &[u8] = b",\"row_metadata\":";
const ROW: &[u8] = b",\"row\":";
const TAIL: &[u8] = b"]}\0";
const METADATA_ID: &[u8] = b"{\"identifier\":";
const METADATA_LABEL: &[u8] = b",\"label\":";

/* === Macros === */

/* === Type Aliases === */

/* === Structs === */

#[derive(Clone, Copy)]
pub struct Entry {
    pub id: &'static str,
    pub metadata_json: &'static str,
    pub row_metadata_json: &'static str,
    pub schema: u32,
    pub window: usize,
    pub row: Definition,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Manifest {
    pub facility: String,
    pub owner: String,
    pub streams: Vec<StreamDeclaration>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NamedStream {
    pub metadata: crate::Metadata,

    #[serde(default)]
    pub row_metadata: Vec<crate::StreamField>,

    pub id: String,
    pub schema: u32,
    pub window: usize,
    pub row: String,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NamedManifest {
    pub facility: String,
    pub owner: String,
    pub streams: Vec<NamedStream>,
}

/* === Enums === */

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("publication inventory differs from authored streams")]
    Mismatch,

    #[error("the artifact carries no complete {facility} publication inventory for `{owner}`")]
    Missing { facility: String, owner: String },

    #[error("the artifact carries multiple complete {facility} publication inventories for `{owner}`")]
    Duplicate { facility: String, owner: String },

    #[error("`{id}` is not a portable Harmos stream id")]
    InvalidId { id: String },

    #[error("the stream `{id}` has a zero schema ordinal")]
    ZeroSchema { id: String },

    #[error("the stream `{id}` has a zero window")]
    ZeroWindow { id: String },

    #[error(
        "the stream `{id}` is declared more than once in its publication inventory; each stream must appear exactly once in the artifact publication grant"
    )]
    DuplicateStream { id: String },

    #[error("stream `{id}` has invalid presentation metadata: {reason}")]
    Metadata { id: String, reason: String },

    #[error("stream `{id}` has an invalid field schema: {source}")]
    Fields {
        id: String,

        #[source]
        source: crate::schema::Error,
    },
}

/* === Traits === */

pub trait StreamSpec {
    type Row;
    const ID: &'static str;
    const VERSION: u32;
    const WINDOW: usize;
    const METADATA_JSON: &'static str = "";
    const ROW_METADATA_JSON: &'static str = "[]";

    fn metadata() -> crate::Metadata {
        if Self::METADATA_JSON.is_empty() {
            crate::Metadata::new(crate::Identifier::declared(Self::ID, "stream"), Self::ID, None)
        } else {
            serde_json::from_str(Self::METADATA_JSON).expect("macro-generated stream metadata is valid")
        }
    }

    fn row_metadata() -> Vec<crate::StreamField> {
        serde_json::from_str(Self::ROW_METADATA_JSON).expect("macro-generated row metadata is valid")
    }
}

/* === Implementations === */

impl NamedManifest {
    pub fn resolve(self, declarations: &[Named], settings: &mut SchemaSettings) -> Result<Manifest, Error> {
        let streams = self
            .streams
            .into_iter()
            .map(|stream| {
                let row = Schema::from_named(&stream.row, declarations, settings)
                    .map_err(|source| Error::Fields { id: stream.id.clone(), source: source.into() })?;
                let row =
                    super::retain_schema(row, settings).map_err(|source| Error::Fields { id: stream.id.clone(), source: source.into() })?;
                let stream = StreamDeclaration {
                    metadata: stream.metadata,
                    row_metadata: stream.row_metadata,
                    id: stream.id,
                    schema: stream.schema,
                    window: stream.window,
                    row,
                };
                validate(&stream)?;
                Ok(stream)
            })
            .collect::<Result<_, Error>>()?;
        Ok(Manifest { facility: self.facility, owner: self.owner, streams })
    }
}

impl Entry {
    pub fn metadata(&self) -> crate::Metadata {
        if self.metadata_json.is_empty() {
            crate::Metadata::new(crate::Identifier::declared(self.id, "stream"), self.id, None)
        } else {
            serde_json::from_str(self.metadata_json).expect("declared stream metadata is valid")
        }
    }

    pub fn row_metadata(&self) -> Vec<crate::StreamField> {
        serde_json::from_str(self.row_metadata_json).expect("declared row metadata is valid")
    }

    pub const fn of<S: StreamSpec>() -> Self
    where
        S::Row: crate::schema::Described,
    {
        Self {
            id: S::ID,
            metadata_json: S::METADATA_JSON,
            row_metadata_json: S::ROW_METADATA_JSON,
            schema: S::VERSION,
            window: S::WINDOW,
            row: <S::Row as crate::schema::Described>::DEFINITION,
        }
    }
}

/* === Public Functions === */

pub const fn merge<const N: usize>(named: &[Entry], sampled: &[Entry]) -> [Entry; N] {
    let mut merged = [Entry { id: "", metadata_json: "", row_metadata_json: "[]", schema: 0, window: 0, row: Definition::of::<()>() }; N];
    let mut index = 0;
    while index < named.len() {
        merged[index] = named[index];
        index += 1;
    }
    let mut offset = 0;
    while offset < sampled.len() {
        merged[index + offset] = sampled[offset];
        offset += 1;
    }

    merged
}

pub const fn require_tree_subset(observers: &[crate::catalog::Node<super::observers::Entry>], grants: &[Entry]) {
    let mut index = 0;
    while index < observers.len() {
        match &observers[index] {
            crate::catalog::Node::Leaf(entry) => require_subset(&[entry.streams], grants),
            crate::catalog::Node::Nested(nested) => require_tree_subset(nested, grants),
        }
        index += 1;
    }
}

pub const fn require_subset(outputs: &[&[Entry]], grants: &[Entry]) {
    let mut observer = 0;
    while observer < outputs.len() {
        let mut output = 0;
        while output < outputs[observer].len() {
            let required = &outputs[observer][output];
            let mut grant = 0;
            let mut found = false;
            while grant < grants.len() {
                let declared = &grants[grant];
                if same_bytes(required.id, declared.id) && required.schema == declared.schema && required.window == declared.window {
                    found = true;
                    break;
                }
                grant += 1;
            }
            assert!(found, "observer outputs must be a subset of the artifact publication grant");
            output += 1;
        }
        observer += 1;
    }
}

pub fn select(manifests: Vec<Manifest>, facility: &str, owner: &str) -> Result<Vec<StreamDeclaration>, Error> {
    let mut matching = manifests.into_iter().filter(|m| m.facility == facility && m.owner == owner);
    let manifest = matching.next().ok_or_else(|| Error::Missing { facility: facility.into(), owner: owner.into() })?;
    if matching.next().is_some() {
        return Err(Error::Duplicate { facility: facility.into(), owner: owner.into() });
    }
    let mut streams = BTreeMap::new();
    for stream in manifest.streams {
        validate(&stream)?;
        let id = stream.id.clone();
        if streams.insert(id.clone(), stream).is_some() {
            return Err(Error::DuplicateStream { id });
        }
    }
    Ok(streams.into_values().collect())
}

pub fn validate_match(expected: &[Entry], actual: &[StreamDeclaration]) -> Result<(), Error> {
    let mut authored = BTreeMap::new();
    for entry in expected {
        let metadata = if entry.metadata_json.is_empty() {
            let id = crate::Identifier::new(entry.id).map_err(|_| Error::InvalidId { id: entry.id.to_owned() })?;
            crate::Metadata::new(id, entry.id, None)
        } else {
            serde_json::from_str(entry.metadata_json)
                .map_err(|source| Error::Metadata { id: entry.id.to_owned(), reason: source.to_string() })?
        };
        let row_metadata = serde_json::from_str(entry.row_metadata_json)
            .map_err(|source| Error::Metadata { id: entry.id.to_owned(), reason: source.to_string() })?;
        let stream = StreamDeclaration {
            metadata,
            row_metadata,
            id: entry.id.to_owned(),
            schema: entry.schema,
            window: entry.window,
            row: Schema::from_declaration(entry.row, &SchemaSettings::default())
                .map_err(|source| Error::Fields { id: entry.id.to_owned(), source: source.into() })?,
        };
        validate(&stream)?;
        if authored.insert(entry.id, stream).is_some() {
            return Err(Error::DuplicateStream { id: entry.id.to_owned() });
        }
    }
    let mut admitted = BTreeMap::new();
    for stream in actual {
        validate(stream)?;
        if admitted.insert(stream.id.as_str(), stream).is_some() {
            return Err(Error::DuplicateStream { id: stream.id.clone() });
        }
    }
    if authored.len() != admitted.len() || authored.iter().any(|(id, stream)| admitted.get(id).is_none_or(|actual| *actual != stream)) {
        return Err(Error::Mismatch);
    }
    Ok(())
}

pub fn validate(stream: &StreamDeclaration) -> Result<(), Error> {
    if !crate::stream_subset(&stream.id) {
        return Err(Error::InvalidId { id: stream.id.clone() });
    }
    if stream.schema == 0 {
        return Err(Error::ZeroSchema { id: stream.id.clone() });
    }
    if stream.window == 0 {
        return Err(Error::ZeroWindow { id: stream.id.clone() });
    }
    if stream.metadata.identifier.as_str() != stream.id {
        return Err(Error::Metadata { id: stream.id.clone(), reason: "identifier differs from stream identity".into() });
    }
    stream.row.prepare(&SchemaSettings::default()).map_err(|source| Error::Fields { id: stream.id.clone(), source: source.into() })?;
    let mut described = std::collections::BTreeSet::new();
    for field in &stream.row_metadata {
        let name = field.metadata.identifier.as_str();
        if !stream.row.definitions[stream.row.root].iter().any(|wire| wire.name == name) || !described.insert(name) {
            return Err(Error::Metadata {
                id: stream.id.clone(),
                reason: format!("field `{name}` is unknown or described more than once"),
            });
        }
    }
    Ok(())
}

pub const fn encoded_len(facility: &str, owner: &str, streams: &[Entry]) -> usize {
    let mut len = HEAD.len() + quoted_len(facility) + OWNER.len() + quoted_len(owner) + STREAMS.len() + TAIL.len();
    let mut index = 0;
    while index < streams.len() {
        len += ID.len()
            + quoted_len(streams[index].id)
            + SCHEMA.len()
            + digits(streams[index].schema as u64)
            + WINDOW.len()
            + digits(streams[index].window as u64)
            + ROW.len()
            + quoted_len(streams[index].row.name)
            + METADATA.len()
            + metadata_len(&streams[index])
            + ROW_METADATA.len()
            + streams[index].row_metadata_json.len()
            + 1;
        if index > 0 {
            len += 1;
        }
        index += 1;
    }
    len
}

pub const fn encode<const N: usize>(facility: &str, owner: &str, streams: &[Entry]) -> [u8; N] {
    assert!(N == encoded_len(facility, owner, streams));
    let mut output = [0; N];
    let mut cursor = 0;
    write(&mut output, &mut cursor, HEAD);
    quoted(&mut output, &mut cursor, facility);
    write(&mut output, &mut cursor, OWNER);
    quoted(&mut output, &mut cursor, owner);
    write(&mut output, &mut cursor, STREAMS);
    let mut index = 0;
    while index < streams.len() {
        if index > 0 {
            write(&mut output, &mut cursor, b",");
        }
        write(&mut output, &mut cursor, ID);
        quoted(&mut output, &mut cursor, streams[index].id);
        write(&mut output, &mut cursor, SCHEMA);
        super::number(&mut output, &mut cursor, streams[index].schema as u64);
        write(&mut output, &mut cursor, WINDOW);
        super::number(&mut output, &mut cursor, streams[index].window as u64);
        write(&mut output, &mut cursor, ROW);
        quoted(&mut output, &mut cursor, streams[index].row.name);
        write(&mut output, &mut cursor, METADATA);
        metadata_into(&mut output, &mut cursor, &streams[index]);
        write(&mut output, &mut cursor, ROW_METADATA);
        write(&mut output, &mut cursor, streams[index].row_metadata_json.as_bytes());
        write(&mut output, &mut cursor, b"}");
        index += 1;
    }
    write(&mut output, &mut cursor, TAIL);
    output
}

pub(crate) const fn same_bytes(left: &str, right: &str) -> bool {
    let left = left.as_bytes();
    let right = right.as_bytes();
    if left.len() != right.len() {
        return false;
    }
    let mut i = 0;
    while i < left.len() {
        if left[i] != right[i] {
            return false;
        }
        i += 1;
    }
    true
}

/* === Private Functions === */

const fn metadata_len(entry: &Entry) -> usize {
    if entry.metadata_json.is_empty() {
        METADATA_ID.len() + quoted_len(entry.id) + METADATA_LABEL.len() + quoted_len(entry.id) + 1
    } else {
        entry.metadata_json.len()
    }
}

const fn metadata_into<const N: usize>(output: &mut [u8; N], cursor: &mut usize, entry: &Entry) {
    if entry.metadata_json.is_empty() {
        write(output, cursor, METADATA_ID);
        quoted(output, cursor, entry.id);
        write(output, cursor, METADATA_LABEL);
        quoted(output, cursor, entry.id);
        write(output, cursor, b"}");
    } else {
        write(output, cursor, entry.metadata_json.as_bytes());
    }
}

/* === Tests === */