Skip to main content

corium_core/
ids.rs

1//! Entity, transaction, attribute, keyword, and partition identifiers.
2
3/// Number of low bits reserved for the sequence within a partition.
4pub const SEQUENCE_BITS: u32 = 42;
5const SEQUENCE_MASK: u64 = (1_u64 << SEQUENCE_BITS) - 1;
6
7/// A raw partition id stored in the high bits of an entity id.
8pub type PartitionId = u32;
9/// Interned keyword id.
10pub type KwId = u64;
11/// Attribute ids are entity ids in the database partition.
12pub type AttrId = EntityId;
13/// Transaction ids are entity ids in the transaction partition.
14pub type TxId = EntityId;
15
16/// Built-in partitions.
17#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
18#[repr(u32)]
19pub enum Partition {
20    /// Schema and database metadata entities.
21    Db = 0,
22    /// Transaction entities.
23    Tx = 1,
24    /// Default user data partition.
25    User = 2,
26}
27
28/// A Datomic-style entity id: 22-bit partition plus 42-bit sequence.
29#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
30pub struct EntityId(u64);
31
32impl EntityId {
33    /// Constructs an entity id from a partition and sequence.
34    #[must_use]
35    pub const fn new(partition: PartitionId, sequence: u64) -> Self {
36        Self(((partition as u64) << SEQUENCE_BITS) | (sequence & SEQUENCE_MASK))
37    }
38
39    /// Constructs an entity id from raw bits.
40    #[must_use]
41    pub const fn from_raw(raw: u64) -> Self {
42        Self(raw)
43    }
44
45    /// Returns the raw integer representation.
46    #[must_use]
47    pub const fn raw(self) -> u64 {
48        self.0
49    }
50
51    /// Returns the partition component.
52    #[must_use]
53    pub const fn partition(self) -> PartitionId {
54        (self.0 >> SEQUENCE_BITS) as PartitionId
55    }
56
57    /// Returns the sequence component.
58    #[must_use]
59    pub const fn sequence(self) -> u64 {
60        self.0 & SEQUENCE_MASK
61    }
62}