use super::{AllocationRetirementError, LedgerCompatibilityError, LedgerIntegrityError};
use crate::{
declaration::AllocationDeclaration, key::StableKey, schema::SchemaMetadata,
slot::AllocationSlotDescriptor,
};
use serde::{Deserialize, Serialize};
pub const CURRENT_LEDGER_SCHEMA_VERSION: u32 = 1;
pub const CURRENT_PHYSICAL_FORMAT_ID: u32 = 1;
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct AllocationLedger {
pub(crate) ledger_schema_version: u32,
pub(crate) physical_format_id: u32,
pub(crate) current_generation: u64,
pub(crate) allocation_history: AllocationHistory,
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct AllocationHistory {
pub records: Vec<AllocationRecord>,
pub generations: Vec<GenerationRecord>,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct AllocationRecord {
pub(crate) stable_key: StableKey,
pub(crate) slot: AllocationSlotDescriptor,
pub(crate) state: AllocationState,
pub(crate) first_generation: u64,
pub(crate) last_seen_generation: u64,
pub(crate) retired_generation: Option<u64>,
pub(crate) schema_history: Vec<SchemaMetadataRecord>,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct AllocationRetirement {
pub stable_key: StableKey,
pub slot: AllocationSlotDescriptor,
}
impl AllocationRetirement {
pub fn new(
stable_key: impl AsRef<str>,
slot: AllocationSlotDescriptor,
) -> Result<Self, AllocationRetirementError> {
Ok(Self {
stable_key: StableKey::parse(stable_key).map_err(AllocationRetirementError::Key)?,
slot,
})
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum AllocationState {
Reserved,
Active,
Retired,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct SchemaMetadataRecord {
pub generation: u64,
pub schema: SchemaMetadata,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct GenerationRecord {
pub generation: u64,
pub parent_generation: Option<u64>,
pub runtime_fingerprint: Option<String>,
pub declaration_count: u32,
pub committed_at: Option<u64>,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct LedgerCompatibility {
pub min_ledger_schema_version: u32,
pub max_ledger_schema_version: u32,
pub physical_format_id: u32,
}
impl LedgerCompatibility {
#[must_use]
pub const fn current() -> Self {
Self {
min_ledger_schema_version: CURRENT_LEDGER_SCHEMA_VERSION,
max_ledger_schema_version: CURRENT_LEDGER_SCHEMA_VERSION,
physical_format_id: CURRENT_PHYSICAL_FORMAT_ID,
}
}
pub const fn validate(
&self,
ledger: &AllocationLedger,
) -> Result<(), LedgerCompatibilityError> {
if ledger.ledger_schema_version < self.min_ledger_schema_version {
return Err(LedgerCompatibilityError::UnsupportedLedgerSchemaVersion {
found: ledger.ledger_schema_version,
min_supported: self.min_ledger_schema_version,
max_supported: self.max_ledger_schema_version,
});
}
if ledger.ledger_schema_version > self.max_ledger_schema_version {
return Err(LedgerCompatibilityError::UnsupportedLedgerSchemaVersion {
found: ledger.ledger_schema_version,
min_supported: self.min_ledger_schema_version,
max_supported: self.max_ledger_schema_version,
});
}
if ledger.physical_format_id != self.physical_format_id {
return Err(LedgerCompatibilityError::UnsupportedPhysicalFormat {
found: ledger.physical_format_id,
supported: self.physical_format_id,
});
}
Ok(())
}
}
impl Default for LedgerCompatibility {
fn default() -> Self {
Self::current()
}
}
impl AllocationRecord {
#[must_use]
pub(crate) fn from_declaration(
generation: u64,
declaration: AllocationDeclaration,
state: AllocationState,
) -> Self {
Self {
stable_key: declaration.stable_key,
slot: declaration.slot,
state,
first_generation: generation,
last_seen_generation: generation,
retired_generation: None,
schema_history: vec![SchemaMetadataRecord {
generation,
schema: declaration.schema,
}],
}
}
#[must_use]
pub(crate) fn reserved(generation: u64, declaration: AllocationDeclaration) -> Self {
Self::from_declaration(generation, declaration, AllocationState::Reserved)
}
#[must_use]
pub const fn stable_key(&self) -> &StableKey {
&self.stable_key
}
#[must_use]
pub const fn slot(&self) -> &AllocationSlotDescriptor {
&self.slot
}
#[must_use]
pub const fn state(&self) -> AllocationState {
self.state
}
#[must_use]
pub const fn first_generation(&self) -> u64 {
self.first_generation
}
#[must_use]
pub const fn last_seen_generation(&self) -> u64 {
self.last_seen_generation
}
#[must_use]
pub const fn retired_generation(&self) -> Option<u64> {
self.retired_generation
}
#[must_use]
pub fn schema_history(&self) -> &[SchemaMetadataRecord] {
&self.schema_history
}
pub(crate) fn observe_declaration(
&mut self,
generation: u64,
declaration: &AllocationDeclaration,
) {
self.last_seen_generation = generation;
if self.state == AllocationState::Reserved {
self.state = AllocationState::Active;
}
let latest_schema = self.schema_history.last().map(|record| &record.schema);
if latest_schema != Some(&declaration.schema) {
self.schema_history.push(SchemaMetadataRecord {
generation,
schema: declaration.schema.clone(),
});
}
}
pub(crate) fn observe_reservation(
&mut self,
generation: u64,
reservation: &AllocationDeclaration,
) {
self.last_seen_generation = generation;
let latest_schema = self.schema_history.last().map(|record| &record.schema);
if latest_schema != Some(&reservation.schema) {
self.schema_history.push(SchemaMetadataRecord {
generation,
schema: reservation.schema.clone(),
});
}
}
}
impl AllocationLedger {
pub fn new(
ledger_schema_version: u32,
physical_format_id: u32,
current_generation: u64,
allocation_history: AllocationHistory,
) -> Result<Self, LedgerIntegrityError> {
let ledger = Self {
ledger_schema_version,
physical_format_id,
current_generation,
allocation_history,
};
ledger.validate_integrity()?;
Ok(ledger)
}
#[must_use]
pub const fn ledger_schema_version(&self) -> u32 {
self.ledger_schema_version
}
#[must_use]
pub const fn physical_format_id(&self) -> u32 {
self.physical_format_id
}
#[must_use]
pub const fn current_generation(&self) -> u64 {
self.current_generation
}
#[must_use]
pub const fn allocation_history(&self) -> &AllocationHistory {
&self.allocation_history
}
}