Skip to main content

sbol_db_postgres/repo/
graph.rs

1//! Repository for `sbol3`-kind graphs: the named graphs created by importing
2//! an SBOL document. Each is dedicated to one import and carries that import's
3//! metadata (name, serialization format, source, content hash). `GraphId` is
4//! the graph's surrogate id; its IRI is `graph:document:{id}`. Standalone
5//! `verbatim` graphs (written through the Graph Store / SPARQL Update
6//! endpoints) are not managed here.
7
8use sbol_db_core::{DomainError, GraphId, GraphRecord, IriString, NewGraph, SerializationFormat};
9use sbol_db_rdf::GRAPH_IRI_PREFIX;
10use sqlx::Row;
11
12use crate::repo::db_err;
13use crate::PgPool;
14
15use sbol_db_storage::ListGraphsFilter;
16
17#[derive(Clone)]
18pub struct GraphRepository {
19    pool: PgPool,
20}
21
22impl GraphRepository {
23    pub fn new(pool: PgPool) -> Self {
24        Self { pool }
25    }
26
27    /// Create the `sbol3`-kind graph that backs a document. The caller mints
28    /// the `GraphId` so the document's triples can be attributed to its
29    /// `graph:document:{id}` IRI before this write.
30    pub async fn insert(
31        &self,
32        conn: &mut sqlx::PgConnection,
33        id: GraphId,
34        input: NewGraph,
35    ) -> Result<GraphId, DomainError> {
36        let id = id.0;
37        let iri = format!("{GRAPH_IRI_PREFIX}{id}");
38        sqlx::query(
39            r#"
40            INSERT INTO sbol_graphs (
41                iri, id, kind, document_iri, name, description,
42                serialization_format, source_uri, content_hash, created_by
43            ) VALUES ($1, $2, 'sbol3', $3, $4, $5, $6, $7, $8, $9)
44            "#,
45        )
46        .bind(&iri)
47        .bind(id)
48        .bind(input.document_iri.as_ref().map(|i| i.as_str()))
49        .bind(input.name)
50        .bind(input.description)
51        .bind(input.serialization_format.as_db_str())
52        .bind(input.source_uri)
53        .bind(input.content_hash)
54        .bind(input.created_by)
55        .execute(&mut *conn)
56        .await
57        .map_err(db_err)?;
58
59        Ok(GraphId(id))
60    }
61
62    /// Returns true when any document graph already carries this content hash.
63    /// Used by bulk import to dedup re-uploaded files without re-parsing.
64    pub async fn exists_by_hash(&self, hash: &[u8]) -> Result<bool, DomainError> {
65        let row = sqlx::query(
66            "SELECT EXISTS(SELECT 1 FROM sbol_graphs \
67             WHERE kind = 'sbol3' AND content_hash = $1) AS hit",
68        )
69        .bind(hash)
70        .fetch_one(&self.pool)
71        .await
72        .map_err(db_err)?;
73        row.try_get::<bool, _>("hit").map_err(db_err)
74    }
75
76    pub async fn get(&self, id: GraphId) -> Result<Option<GraphRecord>, DomainError> {
77        let row = sqlx::query(
78            r#"
79            SELECT id, document_iri, name, description, serialization_format,
80                   source_uri, content_hash, created_at, updated_at
81            FROM sbol_graphs
82            WHERE id = $1 AND kind = 'sbol3'
83            "#,
84        )
85        .bind(id.0)
86        .fetch_optional(&self.pool)
87        .await
88        .map_err(db_err)?;
89
90        row.map(row_to_record).transpose()
91    }
92
93    /// Paginated newest-first listing for the CLI / lab UI. Filters are
94    /// optional and ANDed; cardinality is small so we don't expose a
95    /// keyset cursor here.
96    pub async fn list(&self, filter: &ListGraphsFilter) -> Result<Vec<GraphRecord>, DomainError> {
97        let format_value = filter.format.map(|f| f.as_db_str().to_owned());
98        let rows = sqlx::query(
99            r#"
100            SELECT id, document_iri, name, description, serialization_format,
101                   source_uri, content_hash, created_at, updated_at
102            FROM sbol_graphs
103            WHERE kind = 'sbol3'
104              AND ($1::text IS NULL OR name ILIKE '%' || $1 || '%')
105              AND ($2::text IS NULL OR serialization_format = $2)
106            ORDER BY created_at DESC
107            LIMIT $3
108            "#,
109        )
110        .bind(filter.name.as_deref())
111        .bind(format_value)
112        .bind(filter.limit as i64)
113        .fetch_all(&self.pool)
114        .await
115        .map_err(db_err)?;
116        rows.into_iter().map(row_to_record).collect()
117    }
118
119    /// Delete one document by dropping its graph, which cascades to the graph's
120    /// triples. (Derived-view objects have an `ON DELETE SET NULL` FK and survive;
121    /// cleaning them is the derived-view refresh's job.) Returns `true` if a row
122    /// was removed.
123    pub async fn delete(&self, id: GraphId) -> Result<bool, DomainError> {
124        let affected = sqlx::query("DELETE FROM sbol_graphs WHERE id = $1 AND kind = 'sbol3'")
125            .bind(id.0)
126            .execute(&self.pool)
127            .await
128            .map_err(db_err)?
129            .rows_affected();
130        Ok(affected > 0)
131    }
132}
133
134fn row_to_record(row: sqlx::postgres::PgRow) -> Result<GraphRecord, DomainError> {
135    let id: uuid::Uuid = row.try_get("id").map_err(db_err)?;
136    let document_iri: Option<String> = row.try_get("document_iri").map_err(db_err)?;
137    let name: Option<String> = row.try_get("name").map_err(db_err)?;
138    let description: Option<String> = row.try_get("description").map_err(db_err)?;
139    let serialization_format: Option<String> =
140        row.try_get("serialization_format").map_err(db_err)?;
141    let source_uri: Option<String> = row.try_get("source_uri").map_err(db_err)?;
142    let content_hash: Option<Vec<u8>> = row.try_get("content_hash").map_err(db_err)?;
143    let created_at: chrono::DateTime<chrono::Utc> = row.try_get("created_at").map_err(db_err)?;
144    let updated_at: chrono::DateTime<chrono::Utc> = row.try_get("updated_at").map_err(db_err)?;
145    let serialization_format = serialization_format
146        .as_deref()
147        .map(parse_serialization_format)
148        .transpose()?
149        .unwrap_or(SerializationFormat::Json);
150    Ok(GraphRecord {
151        id: GraphId(id),
152        document_iri: document_iri.map(IriString::unchecked),
153        name,
154        description,
155        serialization_format,
156        source_uri,
157        content_hash: content_hash.unwrap_or_default(),
158        created_at,
159        updated_at,
160    })
161}
162
163fn parse_serialization_format(value: &str) -> Result<SerializationFormat, DomainError> {
164    Ok(match value {
165        "json" => SerializationFormat::Json,
166        "jsonld" => SerializationFormat::JsonLd,
167        "rdfxml" => SerializationFormat::RdfXml,
168        "turtle" => SerializationFormat::Turtle,
169        "trig" => SerializationFormat::TriG,
170        "ntriples" => SerializationFormat::NTriples,
171        "nquads" => SerializationFormat::NQuads,
172        "genbank" => SerializationFormat::GenBank,
173        "fasta" => SerializationFormat::Fasta,
174        other => {
175            return Err(DomainError::Database(format!(
176                "unknown serialization_format: {other}"
177            )))
178        }
179    })
180}