indexingdatomic

What is the use case that makes EAVT index preferable to EATV?


From what I understand, EATV (which Datomic does not have) would be great fit for as-of queries. On the other hand, I see no use-case for EAVT.


Solution

  • You are not looking for a single value at a specific point in time. You are looking for a set of values up to a specific point in time T. History is on a per value basis (not attribute basis).

    For example, assert X, retract X then assert X again. These are 3 distinct facts over 3 distinct transactions. You need to compute that X was added, then removed and then possibly added again at some point.

    You can do this with SQL:

    create table Datoms (
      E bigint not null,
      A bigint not null,
      V varbinary(1536) not null,
      T bigint not null,
      Op bit not null --assert/retract
    )
    
    select E, A, V
    from Datoms
    where E = 1 and T <= 42
    group by E, A, V
    having 0 < sum(case Op when 1 then +1 else -1 end)
    

    The fifth component Op of the datom tells you whether the value is asserted (1) or retracted (0). By summing over this value (as +1/-1) we arrive at either 1 or 0.

    Asserting the same value twice does nothing, and you always retract the old value before you assert a new value. The last part is a prerequisite for the algorithm to work out this nicely.

    With an EAVT index, this is a very efficient query and it's quite elegant. You can build a basic Datomic-like system in just 150 lines of SQL like this. It is the same pattern repeated for any permutation of EAVT index that you want.