use serde::{Deserialize, Serialize};
pub(super) const FORMAT_NAME: &str = "hawdb-logical-dump";
pub(super) const MANIFEST_FILE: &str = "manifest.json";
pub(super) const SCHEMA_FILE: &str = "schema.json";
pub(super) const DATA_DIR: &str = "data";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct Manifest {
pub format: String,
pub dump_format_version: u16,
pub created_by: CreatedBy,
pub source: Source,
pub schema: FileEntry,
pub files: Vec<FileEntry>,
pub counts: Counts,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct CreatedBy {
pub hawdb_version: String,
pub persistent_format_version: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct Source {
pub branch: Option<String>,
pub commit_epoch: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct FileEntry {
pub path: String,
pub kind: FileKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub table: Option<String>,
pub records: u64,
pub bytes: u64,
pub sha256: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum FileKind {
Schema,
TableRows,
Nodes,
Relationships,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct Counts {
pub tables: u64,
pub table_rows: u64,
pub nodes: u64,
pub relationships: u64,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub(super) struct Schema {
pub tables: Vec<Table>,
pub graph: GraphSchema,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(super) struct Table {
pub name: String,
pub columns: Vec<Column>,
pub primary_key: Vec<String>,
#[serde(default)]
pub unique: Vec<Vec<String>>,
#[serde(default)]
pub foreign_keys: Vec<ForeignKey>,
#[serde(default)]
pub indexes: Vec<Index>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub search_indexes: Vec<SearchIndex>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(super) struct Column {
pub name: String,
#[serde(rename = "type")]
pub column_type: String,
pub nullable: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub default: Option<ColumnDefault>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum ColumnDefault {
Literal(serde_json::Value),
Uuidv7,
CurrentTimestamp {
zoned: bool,
precision: u8,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct ForeignKey {
pub columns: Vec<String>,
pub references_table: String,
pub references_columns: Vec<String>,
pub on_delete: ReferentialAction,
pub on_update: ReferentialAction,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum ReferentialAction {
NoAction,
Restrict,
Cascade,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct Index {
pub name: String,
pub columns: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub included_columns: Vec<String>,
pub unique: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub predicate: Option<IndexPredicate>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub(super) enum IndexPredicate {
And {
left: Box<IndexPredicate>,
right: Box<IndexPredicate>,
},
Or {
left: Box<IndexPredicate>,
right: Box<IndexPredicate>,
},
Not {
predicate: Box<IndexPredicate>,
},
Compare {
column: String,
operator: IndexComparison,
value: serde_json::Value,
},
IsNull {
column: String,
negated: bool,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum IndexComparison {
Eq,
NotEq,
Lt,
Lte,
Gt,
Gte,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct SearchIndex {
pub name: String,
pub column: String,
pub analyzer: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct GraphSchema {
#[serde(default)]
pub node_labels: Vec<String>,
#[serde(default)]
pub relationship_types: Vec<String>,
#[serde(default)]
pub tables: Vec<GraphTable>,
#[serde(default)]
pub properties: Vec<GraphProperty>,
#[serde(default)]
pub indexes: Vec<GraphIndex>,
#[serde(default)]
pub constraints: Vec<GraphConstraint>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum GraphTableKind {
Node,
Relationship,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum SchemaState {
DeleteOnly,
WriteOnly,
Backfill,
Validating,
Public,
Gc,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct GraphTable {
pub kind: GraphTableKind,
pub name: String,
pub state: SchemaState,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct GraphProperty {
pub table_kind: GraphTableKind,
pub table: String,
pub name: String,
#[serde(rename = "type")]
pub property_type: String,
pub nullable: bool,
pub state: SchemaState,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub default: Option<GraphPropertyDefault>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum GraphPropertyDefault {
Literal(serde_json::Value),
CurrentTimestamp,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum GraphIndexKind {
Equality,
Range,
FullText,
Composite,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct GraphIndex {
pub label: String,
pub properties: Vec<String>,
pub kind: GraphIndexKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum GraphConstraintKind {
NodeUnique,
NodeExists,
RelationshipUnique,
RelationshipExists,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct GraphConstraint {
pub kind: GraphConstraintKind,
pub subject: String,
pub property: String,
}