Skip to content

Instantly share code, notes, and snippets.

@dustingetz
Last active November 29, 2017 19:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustingetz/39f28f148942728c13edef1c7d8baebf to your computer and use it in GitHub Desktop.
Save dustingetz/39f28f148942728c13edef1c7d8baebf to your computer and use it in GitHub Desktop.
(def conn (let [conn-uri "datomic:mem://asdf"]
(d/delete-database conn-uri)
(d/create-database conn-uri)
(d/connect conn-uri)))
; empty db and basis of empty db
(def $ (d/db conn))
(d/basis-t $) ; => 63 (basis of empty database)
; transact something, this timeline is "left branch"
(def $-left (:db-after @(d/transact conn [[:db/add "-1" :db/ident :foo/one]])))
(d/basis-t $-left) ; => 1000
; In another peer process, sync with a user-provided basis
(def $ (d/as-of @(d/sync conn) 63))
; Basis is the most recent sync basis, which has been "rewound" by setting an as-of property
; This is the source of confusion as the semantics are not quite values i think
(d/basis-t $) ; => 1000 !
(d/as-of-t $) ; => 63
; Now we will branch from 63 to a different timeline, "right branch"
(def $-right (:db-after (d/with $ [[:db/add "-2" :db/ident :foo/two]])))
; One is not found; Two should be found, but is not, which is pretty weird
(d/q '[:find ?e :where [?e :db/ident :foo/one]] $-right) ; #{}
(d/q '[:find ?e :where [?e :db/ident :foo/two]] $-right) ; #{}
; why not? because d/with returned a dbval with the as-of still attached
; And, the time is 1002, not 1001, so knowledge of Left timeline has leaked into Right.
(d/basis-t $-right) ; => 1002
(d/as-of-t $-right) ; => 63
; Gymnastics to fast-forward $-right to include :foo/two
(def $-right' (d/as-of $-right (d/basis-t $-right)))
(d/basis-t $-right') ; => 1002
(d/as-of-t $-right') ; => 1002
; BROKEN: the right branch sees the transaction 1001 from the left branch
(d/q '[:find ?e :where [?e :db/ident :foo/one]] $-right') ; => #{[17592186045417]}
(d/q '[:find ?e :where [?e :db/ident :foo/two]] $-right') ; => #{[17592186045419]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment