Skip to main content

oxgraph_postgres/artifact/
mod.rs

1//! OXGTOPO artifact storage helpers for Postgres-owned snapshot sections.
2
3mod metadata;
4mod sections;
5
6pub use metadata::{
7    PostgresMetadata, PostgresSectionError, SNAPSHOT_KIND_PG_CATALOG,
8    SNAPSHOT_KIND_PG_INBOUND_OFFSETS_BASE, SNAPSHOT_KIND_PG_INBOUND_OFFSETS_U32,
9    SNAPSHOT_KIND_PG_INBOUND_TARGETS_BASE, SNAPSHOT_KIND_PG_INBOUND_TARGETS_U32,
10    SNAPSHOT_KIND_PG_METADATA,
11};
12use oxgraph_snapshot::Snapshot;
13pub use sections::{attach_metadata, attach_postgres_sections};
14
15use crate::error::{BuildError, PostgresGraphError};
16
17/// Validates and borrows snapshot bytes without copying the payload region.
18///
19/// # Errors
20///
21/// Returns [`PostgresGraphError::Snapshot`] when header or section table validation fails.
22///
23/// # Performance
24///
25/// This function is `O(s)` where `s` is the section count.
26pub fn load_snapshot_bytes(bytes: &[u8]) -> Result<Snapshot<'_>, PostgresGraphError> {
27    Snapshot::open(bytes).map_err(PostgresGraphError::from)
28}
29
30/// Returns owned snapshot bytes unchanged after validation.
31///
32/// # Errors
33///
34/// Returns [`PostgresGraphError::Snapshot`] when validation fails.
35///
36/// # Performance
37///
38/// This function is `O(s + b)` where `s` is section count and `b` is byte length.
39pub fn validate_snapshot_bytes(bytes: &[u8]) -> Result<Vec<u8>, PostgresGraphError> {
40    let _ = Snapshot::open(bytes)?;
41    Ok(bytes.to_vec())
42}
43
44/// Reads Postgres-owned metadata from a validated snapshot.
45///
46/// # Errors
47///
48/// Returns [`PostgresGraphError::Snapshot`] or [`PostgresGraphError::Build`] when metadata
49/// is missing or malformed.
50///
51/// # Performance
52///
53/// This function is `O(s)`.
54pub fn read_metadata(snapshot: &Snapshot<'_>) -> Result<PostgresMetadata, PostgresGraphError> {
55    metadata::read_postgres_metadata(snapshot).map_err(|error| match error {
56        PostgresSectionError::Snapshot(snapshot_error) => {
57            PostgresGraphError::Snapshot(snapshot_error)
58        }
59        PostgresSectionError::MissingSection => {
60            PostgresGraphError::Build(BuildError::MissingMetadataSection)
61        }
62        PostgresSectionError::Malformed(message) => {
63            PostgresGraphError::Build(BuildError::MalformedMetadata(message))
64        }
65    })
66}
67
68/// Writes validated snapshot bytes suitable for BYTEA/LO persistence.
69///
70/// # Errors
71///
72/// Returns [`PostgresGraphError::Snapshot`] when validation fails.
73///
74/// # Performance
75///
76/// This function is `O(b)`.
77pub fn write_snapshot_bytes(bytes: &[u8]) -> Result<Vec<u8>, PostgresGraphError> {
78    validate_snapshot_bytes(bytes)
79}