quoll / asami

A flexible graph store, written in Clojure
Eclipse Public License 1.0
328 stars 10 forks source link

Immutable API/db-wth support #20

Open bahulneel opened 8 months ago

bahulneel commented 8 months ago

Description

Asami is great for producing data-driven apps with arbitrary data sources, and it pairs well with other triple-based tools. I'm working on a library that uses Asami as its state atom. Currently, it uses the DB name to separate sessions/states. However, I would like to start being able to reuse earlier states and exploit Clojure's cheap persistent data structures for features such as incremental dataflow or pre-computing some expensive initial data set and then using the db value at this point as the initial db value.

Datomic and Datascript have the db-with function for this in-memory/speculative work.

Problem

Looking at the implementation of transactions in the repo, it's not too clear if such a thing exists or how one might roll their own. I've tried to bake a connection value based on the shape of the "atoms in an atom" connection, but it was a bit flakey and felt like a code smell. This also makes subscribing to updates non-trivial.

Suggested Approach

A separate functional API for in-memory graph values that leverages a db-with approach to transactions would provide a good balance between keeping the existing implementation and supporting this functionality. Such a thing may already exist if you know where to look, which, right now, I don't

I'm happy to be shown in the right direction to be able to implement this myself.

bahulneel commented 8 months ago

I found this solution as a stopgap:

(defn copy-connection! [conn new-url]
  (let [[_ _ db-name] (re-find #"asami:([^:]+)://(.+)" new-url)
        new-conn (-> conn
                     (assoc :name db-name)
                     (update :state #(-> % deref atom)))]
    (swap! d/connections assoc new-url new-conn)
    (d/connect new-url)))