use std::collections::BTreeSet;
use serde::{Deserialize, Serialize};
use crate::{
ElementId, IncidenceId, IndexId, LabelId, ProjectionId, PropertyKeyId, RelationId,
RelationTypeId, RoleId, backing::DbHeader, catalog::PropertyFamily,
};
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ElementRecord {
pub id: ElementId,
pub labels: BTreeSet<LabelId>,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct RelationRecord {
pub id: RelationId,
pub relation_type: Option<RelationTypeId>,
pub labels: BTreeSet<LabelId>,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct IncidenceRecord {
pub id: IncidenceId,
pub relation: RelationId,
pub element: ElementId,
pub role: RoleId,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum PropertySubject {
Element(ElementId),
Relation(RelationId),
Incidence(IncidenceId),
}
impl PropertySubject {
#[must_use]
pub const fn family(self) -> PropertyFamily {
match self {
Self::Element(_id) => PropertyFamily::Element,
Self::Relation(_id) => PropertyFamily::Relation,
Self::Incidence(_id) => PropertyFamily::Incidence,
}
}
}
impl From<ElementId> for PropertySubject {
fn from(id: ElementId) -> Self {
Self::Element(id)
}
}
impl From<RelationId> for PropertySubject {
fn from(id: RelationId) -> Self {
Self::Relation(id)
}
}
impl From<IncidenceId> for PropertySubject {
fn from(id: IncidenceId) -> Self {
Self::Incidence(id)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct NextIds {
pub(crate) element: ElementId,
pub(crate) relation: RelationId,
pub(crate) incidence: IncidenceId,
pub(crate) role: RoleId,
pub(crate) label: LabelId,
pub(crate) relation_type: RelationTypeId,
pub(crate) property_key: PropertyKeyId,
pub(crate) projection: ProjectionId,
pub(crate) index: IndexId,
}
impl NextIds {
pub(crate) const INITIAL: Self = Self {
element: ElementId::new(1),
relation: RelationId::new(1),
incidence: IncidenceId::new(1),
role: RoleId::new(1),
label: LabelId::new(1),
relation_type: RelationTypeId::new(1),
property_key: PropertyKeyId::new(1),
projection: ProjectionId::new(1),
index: IndexId::new(1),
};
pub(crate) const fn from_header(header: &DbHeader) -> Self {
Self {
element: ElementId::new(header.next_element),
relation: RelationId::new(header.next_relation),
incidence: IncidenceId::new(header.next_incidence),
role: RoleId::new(header.next_role),
label: LabelId::new(header.next_label),
relation_type: RelationTypeId::new(header.next_relation_type),
property_key: PropertyKeyId::new(header.next_property_key),
projection: ProjectionId::new(header.next_projection),
index: IndexId::new(header.next_index),
}
}
pub(crate) fn elementwise_max(self, other: Self) -> Self {
Self {
element: self.element.max(other.element),
relation: self.relation.max(other.relation),
incidence: self.incidence.max(other.incidence),
role: self.role.max(other.role),
label: self.label.max(other.label),
relation_type: self.relation_type.max(other.relation_type),
property_key: self.property_key.max(other.property_key),
projection: self.projection.max(other.projection),
index: self.index.max(other.index),
}
}
}