This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-03-08
Channels
- # announcements (11)
- # babashka (13)
- # beginners (11)
- # biff (2)
- # calva (17)
- # cider (19)
- # clojure (60)
- # clojure-berlin (1)
- # clojure-dev (20)
- # clojure-europe (48)
- # clojure-nl (1)
- # clojure-norway (98)
- # clojure-spec (7)
- # clojure-uk (5)
- # core-typed (32)
- # cursive (13)
- # datomic (12)
- # dev-tooling (5)
- # emacs (7)
- # figwheel-main (2)
- # graalvm (4)
- # hyperfiddle (4)
- # introduce-yourself (1)
- # malli (14)
- # missionary (32)
- # off-topic (7)
- # overtone (4)
- # pedestal (10)
- # proletarian (4)
- # re-frame (8)
- # releases (11)
- # tools-build (1)
- # tools-deps (4)
- # xtdb (38)
Hi guys, I have a question regarding dumping datomic data into file using history.
(defn dump-props-history
"Given a datomic `db`, query all the prop transactions between `t1` and `t2`.
Return a coll of prop transactions following the [[prop-pull-pattern]]."
[db t1 t2]
(->> (d/q '[:find ?prop ?tx-time
:in $ ?t1 ?t2
:where
[?t :db/txInstant ?tx-time]
[(<= ?t1 ?tx-time)]
[(<= ?tx-time ?t2)]
[?prop :prop/id ?id]]
(d/history db) t1 t2)
(mapv (fn [[e t]]
(d/pull (d/as-of db t) prop-pull-pattern e)))))
The prop entity has 6 attributes and :prop/id
is the :db.unique/identity
attribute.
When I transact the new version of a prop, a few attributes change (prop/status, prop/op-at etc) so d/history
returns all the txn for each attribute of a prop.
However, I just want to dump the prop before and after new version (all the attributes are always updated at once).
Therefore I use d/as-of
to get the db snapshot at the time ?tx-time
of the prop update.
It seems to work but it is extremely slow and very demanding in terms of heap.
Is there a better way to do it? I could not make it work using d/history only.(defn dump-props-history
[db t1 t2]
(let [lookup-db (-> (d/history db)
(d/as-of t2))
change-db (-> (d/history db)
(d/since (java.util.Date. (dec (inst-ms t1))))
(d/as-of t2))]
(->> (d/q '[:find ?prop ?tx
:in $ldb $cdb
:where
[$ldb ?prop :prop/id]
[$cdb ?prop _ _ ?tx]]
lookup-db change-db)
(mapv (fn [[e t]]
(d/pull (d/as-of db t) prop-pull-pattern e))))))
Your query is the cross-product of all transactions in your range and all props that ever existed
This narrows the dbs to only props whose ids were asserted before the end of your range, then narrows it again to only prop+tx pairs where something happened to the prop
that should reduce the number of duplicate pulls significantly to only those where some change happened to the prop
Thank you very much for the help @U09R86PA4!
It is indeed really fast now and work as expected.
I am wondering why the need of lookup-db
?
I am planning to dump the props history every 24h so I think I just need change-db
no?