Skip to content

Virtual Structures, easy to use but almost too simple to explain

Bill La Forge edited this page Nov 14, 2015 · 5 revisions

Virtual persistent data structures are very easy to use, as all the database stuff is pretty much transparent to the application developer. You can query and update them the same way you query and update any Clojure persistent data structure.

First, you do need to open and close the database:

(let [opts (yearling-open (File. "my-database"))]
  .
  .
  .
  (db-close opts)

Between the open and the close, you can do queries and updates, like this:

  (println (db-get-sorted-map opts)); -> {}
  (db-update (fn [aamap opts]
               (-> 
                 (assoc :me "Hello world!")
                 (assoc :v (reduce conj [] (range 3))))
             opts)
  (println (db-get-sorted-map opts)); -> {:me Hello world!, :v 0 1 2}

Note that [1 2 3] started life as an ordinary Clojure vector, but is converted to a virtual vector when it is added to the database. This transparent transcription into virtual forms occurs recursively so long as the structure is a sorted map, sorted set or vector, and its container is (or is converted to) a virtual sorted map or virtual vector.