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