Skip to content

Instantly share code, notes, and snippets.

@refset
Forked from comnik/lww-datalog.clj
Created April 26, 2021 18:19
Show Gist options
  • Save refset/9c4af79b359c1d51d83ba46f930fbedd to your computer and use it in GitHub Desktop.
Save refset/9c4af79b359c1d51d83ba46f930fbedd to your computer and use it in GitHub Desktop.
;; LWW Register
;; https://speakerdeck.com/ept/data-structures-as-queries-expressing-crdts-using-datalog?slide=15
(def schema
{:assign/time {:db/valueType :Number}
:assign/key {:db/valueType :Number}
:assign/value {:db/valueType :Number}})
(def rules
'[[(older ?t1 ?key)
[?op :assign/key ?key] [?op :assign/time ?t1]
[?op2 :assign/key ?key] [?op2 :assign/time ?t2]
[(< ?t1 ?t2)]]
[(lww ?key ?val)
[?op :assign/time ?t]
[?op :assign/key ?key]
[?op :assign/value ?val]
(not [(older ?t ?key)])]])
;; possible ergonomics improvement
(def rules
'[[(older ?t1 ?key)
#:assign{:key ?key :time ?t1}
#:assign{:key ?key :time ?t2}
[(< ?t1 ?t2)]]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment