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