replikativ / datahike

A fast, immutable, distributed & compositional Datalog engine for everyone.
https://datahike.io
Eclipse Public License 1.0
1.62k stars 95 forks source link

Questions about the behavior of multiple db transacting #123

Open wandersoncferreira opened 4 years ago

wandersoncferreira commented 4 years ago

Hi, I met the project because of my simple impl of a similar idea. Congrats by the complete solution!! I was studying a particular situation to implement in my scenario as well. The question is more like, what is the correct answer for the sequence of transactions below? What happened with the transaction I did with the conn2 value? It looks like there's only a single writer and whoever commits last takes the durable state of the database to become equal his db. Would be like an update in-place in the durable storage.

(def uri "datahike:file:///tmp/file_example")
(def schema-tx [{:db/ident :name
                 :db/valueType :db.type/string
                 :db/unique :db.unique/identity
                 :db/index true
                 :db/cardinality :db.cardinality/one}
                {:db/ident :age
                 :db/valueType :db.type/long
                 :db/cardinality :db.cardinality/one}])

(d/delete-database uri)
(d/create-database uri :initial-tx schema-tx)
(def query '[:find ?n ?a :where [?e :name ?n] [?e :age ?a]])

;;; connection one
(def conn (d/connect uri))
(d/transact conn [{:name "Alice" :age 25} {:name "Bob" :age 30}])
(d/q query @conn)
;; => #{["Alice" 25] ["Bob" 30]}

;;; new connection with the new database state
(def conn2 (d/connect uri))
(d/transact conn2 [{:name "Alice Mother" :age 60} {:name "Bob Father" :age 52}])
(d/q query @conn2)
;; => #{["Alice" 25] ["Alice Mother" 60] ["Bob Father" 52] ["Bob" 30]}

;;; transact again with connection one
(d/transact conn [{:name "Alice Grandmother" :age 90}])
(d/q query @conn)
;; => #{["Alice" 25] ["Alice Grandmother" 90] ["Bob" 30]}

;;; new connection 3... what should be the database state?
(def conn3 (d/connect uri))
(d/q query @conn3)
;; => #{["Alice" 25] ["Alice Grandmother" 90] ["Bob" 30]}
(d/q query (d/history @conn3))          ;even in the history db
;; => #{["Alice" 25] ["Alice Grandmother" 90] ["Bob" 30]}

Related to https://github.com/wandersoncferreira/mamulengo/issues/49

Thanks !

whilo commented 4 years ago

Thanks for reporting! We plan to address this issue with our new transactor and maybe an interim bug fix. Please use a single connection for now.