gmeow-gts 0.9.5

GTS (Graph Transport Substrate) format engine: CBOR-sequence append-only RDF 1.2 log reader, folder, and verifier
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// SPDX-FileCopyrightText: 2026 Blackcat Informatics® Inc. <paudley@blackcatinformatics.ca>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Relational exports for the folded graph (§14).
//!
//! The schema mirrors the Python `gts.db` module: five dictionary-encoded
//! tables (`terms`, `quads`, `reifiers`, `annotations`, `blobs`) whose rows keep
//! GTS integer term ids intact so query engines can resolve labels lazily.
//!
//! To avoid pulling database engines into the core Rust crate, these helpers use
//! command-line database tools at runtime: `sqlite3` for SQLite, plus `duckdb`
//! for DuckDB/Parquet when the optional `duckdb` feature is enabled.

use std::fmt;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{SystemTime, UNIX_EPOCH};

use crate::model::{Graph, TermKind};

const SCHEMA: &[&str] = &[
    "CREATE TABLE terms (id INTEGER PRIMARY KEY, kind INTEGER, lex TEXT, datatype INTEGER, lang TEXT, direction TEXT, reifier INTEGER)",
    "CREATE TABLE quads (s INTEGER, p INTEGER, o INTEGER, g INTEGER)",
    "CREATE TABLE reifiers (reifier INTEGER, s INTEGER, p INTEGER, o INTEGER)",
    "CREATE TABLE annotations (reifier INTEGER, predicate INTEGER, value INTEGER)",
    "CREATE TABLE blobs (digest TEXT PRIMARY KEY, bytes BLOB)",
];

const INDEXES: &[&str] = &[
    "CREATE INDEX quads_s ON quads (s)",
    "CREATE INDEX quads_p ON quads (p)",
    "CREATE INDEX quads_o ON quads (o)",
    "CREATE INDEX annot_reifier ON annotations (reifier)",
];

#[cfg(feature = "duckdb")]
const TABLES: &[&str] = &["terms", "quads", "reifiers", "annotations", "blobs"];

/// Export failure with a displayable message suitable for CLI output.
#[derive(Debug)]
pub struct DbExportError {
    detail: String,
}

impl DbExportError {
    fn new(detail: impl Into<String>) -> Self {
        Self {
            detail: detail.into(),
        }
    }
}

impl fmt::Display for DbExportError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.detail)
    }
}

impl std::error::Error for DbExportError {}

#[derive(Clone, Copy)]
enum SqlDialect {
    Sqlite,
    #[cfg(feature = "duckdb")]
    Duckdb,
}

fn term_kind_int(kind: TermKind) -> u8 {
    match kind {
        TermKind::Iri => 0,
        TermKind::Literal => 1,
        TermKind::Bnode => 2,
        TermKind::Triple => 3,
    }
}

fn sql_io(err: io::Error) -> DbExportError {
    DbExportError::new(format!("cannot write SQL script: {err}"))
}

fn write_sql(writer: &mut dyn Write, bytes: &[u8]) -> Result<(), DbExportError> {
    writer.write_all(bytes).map_err(sql_io)
}

fn write_sql_fmt(writer: &mut dyn Write, args: fmt::Arguments<'_>) -> Result<(), DbExportError> {
    writer.write_fmt(args).map_err(sql_io)
}

fn write_sql_text(writer: &mut dyn Write, value: Option<&str>) -> Result<(), DbExportError> {
    let Some(text) = value else {
        return write_sql(writer, b"NULL");
    };
    write_sql(writer, b"'")?;
    let bytes = text.as_bytes();
    let mut start = 0;
    for (idx, _) in text.match_indices('\'') {
        write_sql(writer, &bytes[start..idx])?;
        write_sql(writer, b"''")?;
        start = idx + 1;
    }
    write_sql(writer, &bytes[start..])?;
    write_sql(writer, b"'")
}

fn write_sql_usize(writer: &mut dyn Write, value: Option<usize>) -> Result<(), DbExportError> {
    match value {
        Some(v) => write_sql_fmt(writer, format_args!("{v}")),
        None => write_sql(writer, b"NULL"),
    }
}

fn write_hex(writer: &mut dyn Write, bytes: &[u8]) -> Result<(), DbExportError> {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut out = [0u8; 4096];
    for chunk in bytes.chunks(out.len() / 2) {
        for (i, byte) in chunk.iter().enumerate() {
            out[i * 2] = HEX[(byte >> 4) as usize];
            out[i * 2 + 1] = HEX[(byte & 0x0f) as usize];
        }
        write_sql(writer, &out[..chunk.len() * 2])?;
    }
    Ok(())
}

fn write_sql_blob(
    writer: &mut dyn Write,
    bytes: &[u8],
    dialect: SqlDialect,
) -> Result<(), DbExportError> {
    match dialect {
        SqlDialect::Sqlite => {
            write_sql(writer, b"X'")?;
            write_hex(writer, bytes)?;
            write_sql(writer, b"'")
        }
        #[cfg(feature = "duckdb")]
        SqlDialect::Duckdb => {
            write_sql(writer, b"from_hex('")?;
            write_hex(writer, bytes)?;
            write_sql(writer, b"')")
        }
    }
}

fn write_insert_rows(
    graph: &Graph,
    dialect: SqlDialect,
    writer: &mut dyn Write,
) -> Result<(), DbExportError> {
    for (id, term) in graph.terms.iter().enumerate() {
        write_sql_fmt(
            writer,
            format_args!(
                "INSERT INTO terms VALUES ({id},{}",
                term_kind_int(term.kind)
            ),
        )?;
        write_sql(writer, b",")?;
        write_sql_text(writer, term.value.as_deref())?;
        write_sql(writer, b",")?;
        write_sql_usize(writer, term.datatype)?;
        write_sql(writer, b",")?;
        write_sql_text(writer, term.lang.as_deref())?;
        write_sql(writer, b",")?;
        write_sql_text(writer, term.direction.as_deref())?;
        write_sql(writer, b",")?;
        write_sql_usize(writer, term.reifier)?;
        write_sql(writer, b");\n")?;
    }
    for (s, p, o, g) in &graph.quads {
        write_sql_fmt(
            writer,
            format_args!("INSERT INTO quads VALUES ({s},{p},{o},"),
        )?;
        write_sql_usize(writer, *g)?;
        write_sql(writer, b");\n")?;
    }
    for (r, (s, p, o)) in &graph.reifiers {
        write_sql_fmt(
            writer,
            format_args!("INSERT INTO reifiers VALUES ({r},{s},{p},{o});\n"),
        )?;
    }
    for (r, p, v) in &graph.annotations {
        write_sql_fmt(
            writer,
            format_args!("INSERT INTO annotations VALUES ({r},{p},{v});\n"),
        )?;
    }
    for (digest, entry) in &graph.blobs {
        let bytes = entry
            .decoded_bytes()
            .map_err(|err| DbExportError::new(format!("cannot decode blob {digest}: {err:?}")))?;
        write_sql(writer, b"INSERT INTO blobs VALUES (")?;
        write_sql_text(writer, Some(digest.as_str()))?;
        write_sql(writer, b",")?;
        write_sql_blob(writer, bytes.as_ref(), dialect)?;
        write_sql(writer, b");\n")?;
    }
    Ok(())
}

fn write_load_script(
    graph: &Graph,
    dialect: SqlDialect,
    writer: &mut dyn Write,
) -> Result<(), DbExportError> {
    for ddl in SCHEMA {
        write_sql_fmt(writer, format_args!("{ddl};\n"))?;
    }
    write_sql(writer, b"BEGIN TRANSACTION;\n")?;
    write_insert_rows(graph, dialect, writer)?;
    write_sql(writer, b"COMMIT;\n")?;
    for ddl in INDEXES {
        write_sql_fmt(writer, format_args!("{ddl};\n"))?;
    }
    Ok(())
}

fn run_sql_tool<F>(program: &str, args: &[&str], write_script: F) -> Result<(), DbExportError>
where
    F: FnOnce(&mut dyn Write) -> Result<(), DbExportError>,
{
    let mut child = Command::new(program)
        .args(args)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .map_err(|e| {
            DbExportError::new(format!(
                "{program} is required for this export and could not be started: {e}"
            ))
        })?;
    let mut stdin = child
        .stdin
        .take()
        .ok_or_else(|| DbExportError::new(format!("{program}: stdin unavailable")))?;
    if let Err(err) = write_script(&mut stdin) {
        drop(stdin);
        let _ = child.kill();
        let _ = child.wait();
        return Err(err);
    }
    drop(stdin);
    let output = child
        .wait_with_output()
        .map_err(|e| DbExportError::new(format!("{program}: could not collect status: {e}")))?;
    if output.status.success() {
        return Ok(());
    }
    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);
    Err(DbExportError::new(format!(
        "{program} failed with status {}: {}{}{}",
        output.status,
        stderr.trim(),
        if stderr.trim().is_empty() || stdout.trim().is_empty() {
            ""
        } else {
            "\n"
        },
        stdout.trim()
    )))
}

#[cfg(feature = "duckdb")]
fn table_count(graph: &Graph, table: &str) -> usize {
    match table {
        "terms" => graph.terms.len(),
        "quads" => graph.quads.len(),
        "reifiers" => graph.reifiers.len(),
        "annotations" => graph.annotations.len(),
        "blobs" => graph.blobs.len(),
        _ => 0,
    }
}

#[cfg(feature = "duckdb")]
fn write_sql_path(writer: &mut dyn Write, path: &Path) -> Result<(), DbExportError> {
    let path = path.to_string_lossy();
    let bytes = path.as_bytes();
    let mut start = 0;
    for (idx, _) in path.match_indices('\'') {
        write_sql(writer, &bytes[start..idx])?;
        write_sql(writer, b"''")?;
        start = idx + 1;
    }
    write_sql(writer, &bytes[start..])
}

#[cfg(feature = "duckdb")]
fn write_copy_stmt(writer: &mut dyn Write, table: &str, path: &Path) -> Result<(), DbExportError> {
    write_sql_fmt(writer, format_args!("COPY {table} TO '"))?;
    write_sql_path(writer, path)?;
    write_sql(writer, b"' (FORMAT parquet);\n")
}

fn temp_sibling_path(path: &Path, suffix: &str) -> PathBuf {
    let parent = path
        .parent()
        .filter(|p| !p.as_os_str().is_empty())
        .unwrap_or_else(|| Path::new("."));
    let name = path
        .file_name()
        .map(|n| n.to_string_lossy())
        .unwrap_or_else(|| "export".into());
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    parent.join(format!(
        ".{name}.{}.{}.{}",
        std::process::id(),
        nanos,
        suffix
    ))
}

fn replace_file(staged: &Path, out: &Path) -> Result<(), DbExportError> {
    if out.is_dir() {
        return Err(DbExportError::new(format!(
            "cannot replace directory {} with exported file",
            out.display()
        )));
    }
    if out.exists() {
        let backup = temp_sibling_path(out, "bak");
        let _ = std::fs::remove_file(&backup);
        std::fs::rename(out, &backup).map_err(|e| {
            DbExportError::new(format!(
                "cannot stage replacement for {}: {e}",
                out.display()
            ))
        })?;
        if let Err(err) = std::fs::rename(staged, out) {
            let _ = std::fs::rename(&backup, out);
            return Err(DbExportError::new(format!(
                "cannot replace {}: {err}",
                out.display()
            )));
        }
        let _ = std::fs::remove_file(&backup);
        return Ok(());
    }
    std::fs::rename(staged, out)
        .map_err(|e| DbExportError::new(format!("cannot write {}: {e}", out.display())))
}

fn export_database_file<F>(program: &str, out: &Path, write_script: F) -> Result<(), DbExportError>
where
    F: FnOnce(&mut dyn Write) -> Result<(), DbExportError>,
{
    let staged = temp_sibling_path(out, "tmp");
    let _ = std::fs::remove_file(&staged);
    let staged_arg = staged.to_string_lossy().into_owned();
    if let Err(err) = run_sql_tool(program, &[staged_arg.as_str()], write_script) {
        let _ = std::fs::remove_file(&staged);
        return Err(err);
    }
    replace_file(&staged, out)
}

/// Write a folded graph to a SQLite database.
pub fn to_sqlite(graph: &Graph, path: impl AsRef<Path>) -> Result<PathBuf, DbExportError> {
    let out = path.as_ref();
    export_database_file("sqlite3", out, |writer| {
        write_load_script(graph, SqlDialect::Sqlite, writer)
    })?;
    Ok(out.to_path_buf())
}

/// Write a folded graph to a DuckDB database.
#[cfg(feature = "duckdb")]
pub fn to_duckdb(graph: &Graph, path: impl AsRef<Path>) -> Result<PathBuf, DbExportError> {
    let out = path.as_ref();
    export_database_file("duckdb", out, |writer| {
        write_load_script(graph, SqlDialect::Duckdb, writer)
    })?;
    Ok(out.to_path_buf())
}

/// Write one Parquet file per non-empty relational table.
#[cfg(feature = "duckdb")]
pub fn to_parquet(graph: &Graph, out_dir: impl AsRef<Path>) -> Result<Vec<PathBuf>, DbExportError> {
    let target = out_dir.as_ref();
    std::fs::create_dir_all(target)
        .map_err(|e| DbExportError::new(format!("cannot create {}: {e}", target.display())))?;
    let staged_dir = temp_sibling_path(target, "tmpdir");
    let _ = std::fs::remove_dir_all(&staged_dir);
    std::fs::create_dir_all(&staged_dir).map_err(|e| {
        DbExportError::new(format!(
            "cannot create staging directory {}: {e}",
            staged_dir.display()
        ))
    })?;
    let mut written = Vec::new();
    for table in TABLES {
        if table_count(graph, table) == 0 {
            continue;
        }
        let staged = staged_dir.join(format!("{table}.parquet"));
        let out = target.join(format!("{table}.parquet"));
        written.push((*table, staged, out));
    }
    if let Err(err) = run_sql_tool("duckdb", &[], |writer| {
        write_load_script(graph, SqlDialect::Duckdb, writer)?;
        for (table, staged, _) in &written {
            write_copy_stmt(writer, table, staged)?;
        }
        Ok(())
    }) {
        let _ = std::fs::remove_dir_all(&staged_dir);
        return Err(err);
    }
    let mut final_paths = Vec::new();
    let mut replace_err = None;
    for (_, staged, out) in &written {
        if let Err(err) = replace_file(staged, out) {
            replace_err = Some(err);
            break;
        }
        final_paths.push(out.clone());
    }
    if replace_err.is_none() {
        for table in TABLES {
            if table_count(graph, table) == 0 {
                let _ = std::fs::remove_file(target.join(format!("{table}.parquet")));
            }
        }
    }
    let _ = std::fs::remove_dir_all(&staged_dir);
    if let Some(err) = replace_err {
        return Err(err);
    }
    Ok(final_paths)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::codec::Codec;
    use crate::wire::{digest_str, hex};

    fn zstd_bytes(data: &[u8]) -> Vec<u8> {
        ruzstd::encoding::compress_to_vec(data, ruzstd::encoding::CompressionLevel::Uncompressed)
    }

    fn zstd_codec() -> Vec<Codec> {
        vec![Codec {
            name: "zstd".into(),
            cls: "compress".into(),
        }]
    }

    #[test]
    fn load_script_decodes_lazy_blob_without_caching_it() {
        let payload = b"blob-heavy relational fixture".repeat(128);
        let digest = digest_str(&payload);
        let mut graph = Graph::default();
        graph.set_lazy_blob(digest.clone(), zstd_bytes(&payload), zstd_codec());

        let mut script = Vec::new();
        write_load_script(&graph, SqlDialect::Sqlite, &mut script).unwrap();

        assert!(graph
            .blob_entry(&digest)
            .is_some_and(|entry| entry.is_lazy()));
        let script = String::from_utf8(script).unwrap();
        assert!(script.contains(&format!(
            "INSERT INTO blobs VALUES ('{}',X'{}');",
            digest,
            hex(&payload)
        )));
    }

    #[test]
    fn load_script_decode_failure_stops_before_commit() {
        let digest = format!("blake3:{}", "00".repeat(32));
        let mut graph = Graph::default();
        graph.set_lazy_blob(digest.clone(), b"not zstd".to_vec(), zstd_codec());

        let mut script = Vec::new();
        let err = write_load_script(&graph, SqlDialect::Sqlite, &mut script).unwrap_err();

        assert!(
            err.to_string().contains("cannot decode blob"),
            "unexpected error: {err}"
        );
        assert!(graph
            .blob_entry(&digest)
            .is_some_and(|entry| entry.is_lazy()));
        let script = String::from_utf8_lossy(&script);
        assert!(script.contains("BEGIN TRANSACTION;\n"));
        assert!(!script.contains("COMMIT;\n"));
    }
}