gen-models 0.5.1

Models for the gen sequence graph and version control system.
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
use gen_core::Sha256Hash;
use rusqlite::{Row, params, types::Type};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use thiserror::Error;

use crate::{ModelSelect, db::GraphConnection};

/// A typed sample metadata value.
///
/// Postcard persists enum variant indices. Preserve this order and append new variants.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum MetadataValue {
    /// A text value.
    Text(String),
    /// A signed 64-bit integer.
    Integer(i64),
    /// A finite double-precision value.
    Float(f64),
    /// A boolean value.
    Boolean(bool),
}

/// One metadata value associated with a sample and key.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ModelSelect)]
#[model_select(
    table = "sample_metadata",
    select = "hash, sample_name, key, value",
    from_row = SampleMetadata::from_row
)]
pub struct SampleMetadata {
    /// Content hash including the sample, key, value type, and value.
    #[model_select(primary_key)]
    pub hash: Sha256Hash,
    /// The sample's primary key in the samples table.
    pub sample_name: String,
    /// The metadata key, unique within the sample.
    pub key: String,
    /// Exactly one typed metadata value.
    #[model_select(skip)]
    pub value: MetadataValue,
}

/// Errors storing sample metadata.
#[derive(Debug, Error, PartialEq)]
pub enum SampleMetadataError {
    /// The database rejected the operation.
    #[error(transparent)]
    Database(#[from] rusqlite::Error),
    /// The metadata value could not be serialized.
    #[error(transparent)]
    Serialization(#[from] postcard::Error),
    /// Metadata floats must remain valid in all supported export formats.
    #[error("sample metadata float values must be finite")]
    NonFiniteFloat,
}

impl SampleMetadata {
    /// Store a new value; an existing key for this sample causes a uniqueness error.
    pub fn create(
        conn: &GraphConnection,
        sample_name: &str,
        key: &str,
        value: &MetadataValue,
    ) -> Result<Self, SampleMetadataError> {
        Self::store(conn, sample_name, key, value, false)
    }

    /// Store a value, replacing the value and content hash for an existing sample key.
    pub fn upsert(
        conn: &GraphConnection,
        sample_name: &str,
        key: &str,
        value: &MetadataValue,
    ) -> Result<Self, SampleMetadataError> {
        Self::store(conn, sample_name, key, value, true)
    }

    fn store(
        conn: &GraphConnection,
        sample_name: &str,
        key: &str,
        value: &MetadataValue,
        replace: bool,
    ) -> Result<Self, SampleMetadataError> {
        let value = match value {
            MetadataValue::Float(float) if !float.is_finite() => {
                return Err(SampleMetadataError::NonFiniteFloat);
            }
            MetadataValue::Float(float) if *float == 0.0 => MetadataValue::Float(0.0),
            value => value.clone(),
        };
        let encoded_value = postcard::to_stdvec(&value)?;

        // Length prefixes and a type tag keep field boundaries and numeric types distinct.
        let mut hasher = Sha256::new();
        for field in [sample_name, key] {
            hasher.update((field.len() as u64).to_be_bytes());
            hasher.update(field.as_bytes());
        }
        match &value {
            MetadataValue::Text(text) => {
                hasher.update([0]);
                hasher.update(text.as_bytes());
            }
            MetadataValue::Integer(integer) => {
                hasher.update([1]);
                hasher.update(integer.to_be_bytes());
            }
            MetadataValue::Float(float) => {
                hasher.update([2]);
                hasher.update(float.to_bits().to_be_bytes());
            }
            MetadataValue::Boolean(boolean) => {
                hasher.update([3]);
                hasher.update([u8::from(*boolean)]);
            }
        }
        let hash = Sha256Hash(hasher.finalize().into());
        let mut sql = String::from(
            "INSERT INTO sample_metadata
             (hash, sample_name, key, value)
             VALUES (?1, ?2, ?3, ?4)",
        );
        if replace {
            sql.push_str(
                " ON CONFLICT(sample_name, key) DO UPDATE SET
                  hash = excluded.hash, value = excluded.value",
            );
        }
        conn.execute(&sql, params![hash, sample_name, key, encoded_value])?;
        Ok(Self {
            hash,
            sample_name: sample_name.to_string(),
            key: key.to_string(),
            value,
        })
    }

    fn from_row(row: &Row<'_>) -> rusqlite::Result<Self> {
        let encoded_value: Vec<u8> = row.get(3)?;
        let (value, remaining) = postcard::take_from_bytes::<MetadataValue>(&encoded_value)
            .map_err(|error| {
                rusqlite::Error::FromSqlConversionFailure(3, Type::Blob, Box::new(error))
            })?;
        if !remaining.is_empty() {
            return Err(rusqlite::Error::InvalidQuery);
        }
        if matches!(&value, MetadataValue::Float(float) if !float.is_finite()) {
            return Err(rusqlite::Error::FromSqlConversionFailure(
                3,
                Type::Blob,
                Box::new(SampleMetadataError::NonFiniteFloat),
            ));
        }
        Ok(Self {
            hash: row.get(0)?,
            sample_name: row.get(1)?,
            key: row.get(2)?,
            value,
        })
    }
}

#[cfg(test)]
mod tests {
    use rusqlite::params;

    use crate::{
        sample::{NewSample, Sample},
        sample_metadata::{MetadataValue, SampleMetadata, SampleMetadataError},
        test_helpers::get_connection,
    };

    #[test]
    fn test_sample_metadata_upsert_replaces_type_and_hash() {
        let conn = get_connection(None).expect("should open database");
        Sample::create(
            &conn,
            NewSample {
                name: "sample",
                ..Default::default()
            },
        )
        .expect("should create sample");
        let original = SampleMetadata::upsert(
            &conn,
            "sample",
            "Score",
            &MetadataValue::Text("pending".to_string()),
        )
        .expect("should insert metadata");
        let updated =
            SampleMetadata::upsert(&conn, "sample", "Score", &MetadataValue::Float(90.68))
                .expect("should update metadata");
        assert_ne!(original.hash, updated.hash);
        assert_eq!(
            SampleMetadata::select(&conn)
                .load()
                .expect("should load metadata"),
            vec![updated.clone()],
        );
        let repeated =
            SampleMetadata::upsert(&conn, "sample", "Score", &MetadataValue::Float(90.68))
                .expect("should repeat update");
        assert_eq!(updated, repeated);
    }

    #[test]
    fn test_sample_metadata_boolean_hash_and_type_changes() {
        let conn = get_connection(None).expect("should open database");
        Sample::create(
            &conn,
            NewSample {
                name: "sample",
                ..Default::default()
            },
        )
        .expect("should create sample");
        let mut hashes = Vec::new();
        for value in [
            MetadataValue::Integer(1),
            MetadataValue::Boolean(true),
            MetadataValue::Boolean(false),
            MetadataValue::Text("false".to_string()),
            MetadataValue::Float(0.0),
        ] {
            let metadata = SampleMetadata::upsert(&conn, "sample", "flag", &value)
                .expect("should replace metadata type");
            assert!(
                !hashes.contains(&metadata.hash),
                "typed values should have distinct hashes"
            );
            hashes.push(metadata.hash);
            assert_eq!(
                SampleMetadata::select(&conn)
                    .load()
                    .expect("should load metadata"),
                vec![metadata]
            );
        }
    }

    #[test]
    fn test_sample_metadata_round_trip_and_uniqueness() {
        let conn = get_connection(None).expect("should open database");
        Sample::create(
            &conn,
            NewSample {
                name: "sample",
                ..Default::default()
            },
        )
        .expect("should create sample");
        for (key, value) in [
            ("text", MetadataValue::Text("1".to_string())),
            ("integer", MetadataValue::Integer(i64::MAX)),
            ("float", MetadataValue::Float(1.25)),
            ("true", MetadataValue::Boolean(true)),
            ("false", MetadataValue::Boolean(false)),
        ] {
            let metadata = SampleMetadata::create(&conn, "sample", key, &value)
                .expect("should create metadata");
            let loaded = SampleMetadata::select(&conn)
                .sample_name("sample")
                .key(key)
                .load()
                .expect("should load metadata");
            assert_eq!(loaded, vec![metadata]);
            assert!(
                SampleMetadata::create(&conn, "sample", key, &MetadataValue::Integer(2)).is_err()
            );
        }
        assert!(
            SampleMetadata::create(&conn, "missing", "key", &MetadataValue::Integer(1)).is_err()
        );
        for float in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            assert!(matches!(
                SampleMetadata::create(&conn, "sample", "invalid", &MetadataValue::Float(float)),
                Err(SampleMetadataError::NonFiniteFloat)
            ));
        }
    }

    #[test]
    fn test_sample_metadata_database_value_constraints() {
        let conn = get_connection(None).expect("should open database");
        Sample::create(
            &conn,
            NewSample {
                name: "sample",
                ..Default::default()
            },
        )
        .expect("should create sample");
        for value in ["NULL", "'invalid'", "1", "1.5"] {
            let sql = format!(
                "INSERT INTO sample_metadata (hash, sample_name, key, value)
                 VALUES (?1, 'sample', 'key', {value})"
            );
            assert!(conn.execute(&sql, params![vec![0u8; 32]]).is_err());
        }
    }

    #[test]
    fn test_sample_metadata_rejects_invalid_encoded_values() {
        let conn = get_connection(None).expect("should open database");
        Sample::create(
            &conn,
            NewSample {
                name: "sample",
                ..Default::default()
            },
        )
        .expect("should create sample");
        let mut trailing_bytes =
            postcard::to_stdvec(&MetadataValue::Boolean(true)).expect("should encode boolean");
        trailing_bytes.push(0);
        let mut invalid_values = vec![vec![], vec![255], vec![3, 2], trailing_bytes];
        for float in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            invalid_values.push(
                postcard::to_stdvec(&MetadataValue::Float(float))
                    .expect("should encode non-finite float for corruption test"),
            );
        }
        for encoded_value in invalid_values {
            conn.execute(
                "INSERT INTO sample_metadata (hash, sample_name, key, value)
                 VALUES (?1, 'sample', 'key', ?2)",
                params![vec![0u8; 32], encoded_value],
            )
            .expect("should insert corrupt metadata");
            assert!(SampleMetadata::select(&conn).load().is_err());
            conn.execute("DELETE FROM sample_metadata", [])
                .expect("should remove corrupt metadata");
        }
    }

    #[test]
    fn test_sample_metadata_persisted_encoding() {
        // These bytes define the stored format independently of Rust enum refactors.
        for (value, encoded_value) in [
            (MetadataValue::Text("x".to_string()), vec![0, 1, b'x']),
            (MetadataValue::Integer(-1), vec![1, 1]),
            (
                MetadataValue::Float(1.0),
                vec![2, 0, 0, 0, 0, 0, 0, 240, 63],
            ),
            (MetadataValue::Boolean(false), vec![3, 0]),
            (MetadataValue::Boolean(true), vec![3, 1]),
        ] {
            assert_eq!(
                postcard::to_stdvec(&value).expect("should encode value"),
                encoded_value
            );
            assert_eq!(
                postcard::from_bytes::<MetadataValue>(&encoded_value)
                    .expect("should decode stored value"),
                value
            );
        }
    }

    #[test]
    fn test_sample_metadata_hash_encoding() {
        let conn = get_connection(None).expect("should open database");
        for name in ["a", "ab"] {
            Sample::create(
                &conn,
                NewSample {
                    name,
                    ..Default::default()
                },
            )
            .expect("should create sample");
        }
        let text = SampleMetadata::create(&conn, "a", "bc", &MetadataValue::Text("1".to_string()))
            .expect("should create text");
        SampleMetadata::select(&conn)
            .delete_by_ids([text.hash])
            .expect("should delete metadata");
        let integer = SampleMetadata::create(&conn, "a", "bc", &MetadataValue::Integer(1))
            .expect("should create integer");
        SampleMetadata::select(&conn)
            .delete_by_ids([integer.hash])
            .expect("should delete metadata");
        let float = SampleMetadata::create(&conn, "a", "bc", &MetadataValue::Float(1.0))
            .expect("should create float");
        assert_ne!(text.hash, integer.hash);
        assert_ne!(integer.hash, float.hash);
        let other = SampleMetadata::create(&conn, "ab", "c", &MetadataValue::Float(1.0))
            .expect("should create metadata for sample 'ab' with key 'c'");
        assert_ne!(
            float.hash, other.hash,
            "sample/key pairs ('a', 'bc') and ('ab', 'c') should hash differently even though both concatenate to 'abc'"
        );
        let zero = SampleMetadata::create(&conn, "a", "zero", &MetadataValue::Float(-0.0))
            .expect("should create zero");
        SampleMetadata::select(&conn)
            .delete_by_ids([zero.hash])
            .expect("should delete zero");
        let positive_zero = SampleMetadata::create(&conn, "a", "zero", &MetadataValue::Float(0.0))
            .expect("should recreate zero");
        assert_eq!(zero.hash, positive_zero.hash);
    }
}