use super::DomainError;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum EntityKind {
Node,
Relationship,
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum EntityId {
Node(NodeId),
Relationship(RelId),
}
impl EntityId {
#[must_use]
pub const fn kind(self) -> EntityKind {
match self {
Self::Node(_) => EntityKind::Node,
Self::Relationship(_) => EntityKind::Relationship,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct GraphGeneration(u64);
impl GraphGeneration {
#[must_use]
pub const fn new(value: u64) -> Self {
Self(value)
}
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
}
macro_rules! identity {
($name:ident, $doc:literal) => {
#[doc = $doc]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct $name(u128);
impl $name {
pub const fn new(value: u128) -> Result<Self, DomainError> {
if value == 0 {
Err(DomainError::ZeroIdentity)
} else {
Ok(Self(value))
}
}
#[must_use]
pub const fn get(self) -> u128 {
self.0
}
}
};
}
identity!(
NodeId,
"Stable store-local node identity, unrelated to a physical row.\n\n```compile_fail\nuse zeppelin_embed::property_graph::{NodeId, RelId};\nlet node: NodeId = RelId::new(1).unwrap();\n```\n\n```\nuse zeppelin_embed::{ingest::DocId, property_graph::NodeId};\nlet node: NodeId = DocId::new(0).into();\nassert_eq!(node.get(), 0);\n```"
);
identity!(
RelId,
"Stable store-local relationship identity, distinct from node identity."
);
identity!(
StoreInstanceId,
"Persisted store identity, distinct from either kind of entity identity."
);
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct GraphRevision(u64);
impl GraphRevision {
pub const fn new(value: u64) -> Result<Self, DomainError> {
if value == 0 {
Err(DomainError::ZeroRevision)
} else {
Ok(Self(value))
}
}
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
pub const fn checked_next(self) -> Result<Self, DomainError> {
match self.0.checked_add(1) {
Some(value) => Ok(Self(value)),
None => Err(DomainError::RevisionOverflow),
}
}
}
impl From<crate::ingest::DocId> for NodeId {
fn from(document: crate::ingest::DocId) -> Self {
Self(document.get())
}
}
impl From<NodeId> for crate::ingest::DocId {
fn from(node: NodeId) -> Self {
Self::new(node.get())
}
}