Skip to content

Commit 9a1e8b4

Browse files
committed
store: Add Metadata::from_file() for dump deserialization
Add a method to read and validate metadata.json from a dump directory. Make fields of Metadata, Manifest, BlockPtr, Health, and Error structs pub(crate) so they are accessible from the restore module.
1 parent 6ebbe73 commit 9a1e8b4

1 file changed

Lines changed: 51 additions & 33 deletions

File tree

  • store/postgres/src/relational

store/postgres/src/relational/dump.rs

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ use crate::AsyncPgConnection;
3030
use super::Layout;
3131

3232
#[derive(Serialize, Deserialize)]
33-
struct Manifest {
34-
spec_version: String,
35-
description: Option<String>,
36-
repository: Option<String>,
37-
features: Vec<String>,
38-
entities_with_causality_region: Vec<String>,
39-
history_blocks: i32,
33+
pub(crate) struct Manifest {
34+
pub spec_version: String,
35+
pub description: Option<String>,
36+
pub repository: Option<String>,
37+
pub features: Vec<String>,
38+
pub entities_with_causality_region: Vec<String>,
39+
pub history_blocks: i32,
4040
}
4141

4242
impl Manifest {
@@ -67,9 +67,9 @@ impl Manifest {
6767
}
6868

6969
#[derive(Serialize, Deserialize)]
70-
struct BlockPtr {
71-
number: i32,
72-
hash: String,
70+
pub(crate) struct BlockPtr {
71+
pub number: i32,
72+
pub hash: String,
7373
}
7474

7575
impl BlockPtr {
@@ -81,11 +81,11 @@ impl BlockPtr {
8181
}
8282

8383
#[derive(Serialize, Deserialize)]
84-
struct Error {
85-
message: String,
86-
block_ptr: Option<BlockPtr>,
87-
handler: Option<String>,
88-
deterministic: bool,
84+
pub(crate) struct Error {
85+
pub message: String,
86+
pub block_ptr: Option<BlockPtr>,
87+
pub handler: Option<String>,
88+
pub deterministic: bool,
8989
}
9090

9191
impl Error {
@@ -108,11 +108,11 @@ impl Error {
108108
}
109109

110110
#[derive(Serialize, Deserialize)]
111-
struct Health {
112-
failed: bool,
113-
health: String,
114-
fatal_error: Option<Error>,
115-
non_fatal_errors: Vec<Error>,
111+
pub(crate) struct Health {
112+
pub failed: bool,
113+
pub health: String,
114+
pub fatal_error: Option<Error>,
115+
pub non_fatal_errors: Vec<Error>,
116116
}
117117

118118
impl Health {
@@ -149,23 +149,41 @@ pub(crate) struct TableInfo {
149149
/// as entity tables are written to Parquet files.
150150
#[derive(Serialize, Deserialize)]
151151
pub(crate) struct Metadata {
152-
version: u32,
153-
deployment: DeploymentHash,
154-
network: String,
155-
manifest: Manifest,
156-
earliest_block_number: i32,
157-
start_block: Option<BlockPtr>,
158-
head_block: Option<BlockPtr>,
159-
entity_count: usize,
160-
graft_base: Option<DeploymentHash>,
161-
graft_block: Option<BlockPtr>,
162-
debug_fork: Option<DeploymentHash>,
163-
health: Health,
164-
indexes: HashMap<String, Vec<String>>,
152+
pub version: u32,
153+
pub deployment: DeploymentHash,
154+
pub network: String,
155+
pub manifest: Manifest,
156+
pub earliest_block_number: i32,
157+
pub start_block: Option<BlockPtr>,
158+
pub head_block: Option<BlockPtr>,
159+
pub entity_count: usize,
160+
pub graft_base: Option<DeploymentHash>,
161+
pub graft_block: Option<BlockPtr>,
162+
pub debug_fork: Option<DeploymentHash>,
163+
pub health: Health,
164+
pub indexes: HashMap<String, Vec<String>>,
165165
pub tables: BTreeMap<String, TableInfo>,
166166
}
167167

168168
impl Metadata {
169+
/// Read and validate a dump's `metadata.json`.
170+
#[allow(dead_code)]
171+
pub fn from_file(path: &Path) -> Result<Self, StoreError> {
172+
let content = fs::read_to_string(path).map_err(|e| {
173+
StoreError::InternalError(format!("failed to read {}: {e}", path.display()))
174+
})?;
175+
let metadata: Self = serde_json::from_str(&content).map_err(|e| {
176+
StoreError::InternalError(format!("failed to parse {}: {e}", path.display()))
177+
})?;
178+
if metadata.version != 1 {
179+
return Err(StoreError::InternalError(format!(
180+
"unsupported dump version {} (expected 1)",
181+
metadata.version
182+
)));
183+
}
184+
Ok(metadata)
185+
}
186+
169187
fn new(
170188
deployment: DeploymentHash,
171189
network: String,

0 commit comments

Comments
 (0)