Skip to main content

corium_core/
datom.rs

1//! Datoms and covering index key composition.
2
3use crate::{AttrId, EntityId, TxId, Value, encoding::Encodable};
4
5/// A single immutable fact assertion or retraction.
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct Datom {
8    /// Entity id.
9    pub e: EntityId,
10    /// Attribute id.
11    pub a: AttrId,
12    /// Value.
13    pub v: Value,
14    /// Transaction id.
15    pub tx: TxId,
16    /// Assertion (`true`) or retraction (`false`).
17    pub added: bool,
18}
19
20/// Covering index orders.
21#[derive(Clone, Copy, Debug, Eq, PartialEq)]
22pub enum IndexOrder {
23    /// Entity, attribute, value, transaction.
24    Eavt,
25    /// Attribute, entity, value, transaction.
26    Aevt,
27    /// Attribute, value, entity, transaction.
28    Avet,
29    /// Value, attribute, entity, transaction.
30    Vaet,
31}
32
33impl Datom {
34    /// Returns this datom's byte key for the requested index order.
35    #[must_use]
36    pub fn key(&self, order: IndexOrder) -> Vec<u8> {
37        let mut out = Vec::new();
38        match order {
39            IndexOrder::Eavt => {
40                self.e.encode_into(&mut out);
41                self.a.encode_into(&mut out);
42                self.v.encode_into(&mut out);
43            }
44            IndexOrder::Aevt => {
45                self.a.encode_into(&mut out);
46                self.e.encode_into(&mut out);
47                self.v.encode_into(&mut out);
48            }
49            IndexOrder::Avet => {
50                self.a.encode_into(&mut out);
51                self.v.encode_into(&mut out);
52                self.e.encode_into(&mut out);
53            }
54            IndexOrder::Vaet => {
55                self.v.encode_into(&mut out);
56                self.a.encode_into(&mut out);
57                self.e.encode_into(&mut out);
58            }
59        }
60        encode_tx_added(self.tx, self.added, &mut out);
61        out
62    }
63}
64
65fn encode_tx_added(tx: TxId, added: bool, out: &mut Vec<u8>) {
66    ((tx.raw() << 1) | u64::from(added)).encode_into(out);
67}