use super::interner::InternId;
use crate::coordinate::Coordinate;
use crate::event::{EventKind, HashChain};
use crate::store::{EncodedBytes, ExtensionKey};
use std::collections::BTreeMap;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ClockKey {
pub(crate) wall_ms: u64,
pub(crate) clock: u32,
pub(crate) uuid: u128,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct IndexEntry {
pub(crate) event_id: u128,
pub(crate) correlation_id: u128,
pub(crate) causation_id: Option<u128>,
pub(crate) coord: Coordinate,
pub(crate) entity_id: InternId,
pub(crate) scope_id: InternId,
pub(crate) kind: EventKind,
pub(crate) wall_ms: u64,
pub(crate) clock: u32,
pub(crate) dag_lane: u32,
pub(crate) dag_depth: u32,
pub(crate) hash_chain: HashChain,
pub(crate) disk_pos: DiskPos,
pub(crate) global_sequence: u64,
pub(crate) receipt_extensions: BTreeMap<ExtensionKey, EncodedBytes>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DiskPos {
pub(crate) segment_id: u64,
pub(crate) offset: u64,
pub(crate) length: u32,
}
impl DiskPos {
pub const fn new(segment_id: u64, offset: u64, length: u32) -> Self {
Self {
segment_id,
offset,
length,
}
}
pub const fn segment_id(self) -> u64 {
self.segment_id
}
pub const fn offset(self) -> u64 {
self.offset
}
pub const fn length(self) -> u32 {
self.length
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct QueryHit {
pub(crate) event_id: u128,
pub(crate) global_sequence: u64,
pub(crate) disk_pos: DiskPos,
pub(crate) kind: EventKind,
pub(crate) clock: u32,
}
impl QueryHit {
pub(crate) fn from_entry(entry: &IndexEntry) -> Self {
Self {
event_id: entry.event_id,
global_sequence: entry.global_sequence,
disk_pos: entry.disk_pos,
kind: entry.kind,
clock: entry.clock,
}
}
}
impl Ord for ClockKey {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.wall_ms
.cmp(&other.wall_ms)
.then(self.clock.cmp(&other.clock))
.then(self.uuid.cmp(&other.uuid))
}
}
impl PartialOrd for ClockKey {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl IndexEntry {
#[must_use]
pub const fn event_id(&self) -> u128 {
self.event_id
}
#[must_use]
pub const fn correlation_id(&self) -> u128 {
self.correlation_id
}
#[must_use]
pub const fn causation_id(&self) -> Option<u128> {
self.causation_id
}
#[must_use]
pub const fn coord(&self) -> &Coordinate {
&self.coord
}
#[must_use]
pub const fn event_kind(&self) -> EventKind {
self.kind
}
#[must_use]
pub const fn wall_ms(&self) -> u64 {
self.wall_ms
}
#[must_use]
pub const fn clock(&self) -> u32 {
self.clock
}
#[must_use]
pub const fn dag_lane(&self) -> u32 {
self.dag_lane
}
#[must_use]
pub const fn dag_depth(&self) -> u32 {
self.dag_depth
}
#[must_use]
pub const fn hash_chain(&self) -> &HashChain {
&self.hash_chain
}
#[must_use]
pub const fn disk_pos(&self) -> DiskPos {
self.disk_pos
}
#[must_use]
pub const fn global_sequence(&self) -> u64 {
self.global_sequence
}
#[must_use]
pub const fn receipt_extensions(&self) -> &BTreeMap<ExtensionKey, EncodedBytes> {
&self.receipt_extensions
}
pub fn is_correlated(&self) -> bool {
self.event_id != self.correlation_id
}
pub fn is_caused_by(&self, event_id: crate::id::EventId) -> bool {
use crate::id::EntityIdType;
self.causation_id == Some(event_id.as_u128())
}
pub fn is_root_cause(&self) -> bool {
self.causation_id.is_none()
}
}