10 July 2016
The most recent Datomic Release provides access to the Datomic Log API for memory databases. I would like to take this opportunity to describe some of the features and uses of the Datomic Log API.
The transactional log is a fundamental component of ACID database systems, a durable record of the transactions performed by the database. In addition to its critical function in ensuring ACID semantics, the Datomic log, as a sequential (in database time, t) record of all transactions, also functions as a time-ordered index to the data stored in a Datomic database.
Datomic provides access to the transaction log directly via the tx-range function and from within query using the tx-ids and tx-data functions.
An example using the log in query is now available in the Day of Datomic repo. Our example database records the streets on which our three protagonists, John, Mary, and Joe, live.
Let’s find out when Joe moved to Broadway:
(d/q '[:find ?tx
:in $ ?name ?street
:where
[?e :person/name ?name]
[?e :person/street ?street ?tx true]]
(d/history (d/db conn)) "Joe" "Broadway")
(d/pull (d/db conn) '[:db/txInstant] 13194139534317)
(d/q '[:find ?e ?a ?v ?tx ?op
:in ?log ?tx
:where [(tx-data ?log ?tx)[[?e ?a ?v _ ?op]]]]
(d/log conn) 13194139534317)
;; result:
#{[17592186045420 64 "2nd" 13194139534317 true]
[17592186045419 64 "Broadway" 13194139534317 true]
[13194139534317 50 #inst"1983-01-01T00:00:00.000-00:00" 13194139534317 true]
[17592186045420 64 "Elm" 13194139534317 false]
[17592186045419 64 "1st" 13194139534317 false]}
(d/pull (d/db conn) '[*] 17592186045420)
;; result:
{:db/id 17592186045420, :person/name "Mary", :person/street "2nd"}