Skip to content
Bill La Forge edited this page Dec 9, 2015 · 2 revisions

API

Db chan is a component that provides access to a database via an asynchronous channel. It supports the following API:

  • (aatree.db-chan-trait db-chan this) Returns an updated options map (this), which supports the rest of the API.
  • (aatree.core.create-db-chan this initial-state) Creates and initializes the db channel.
  • (aatree.core.db-get this) Returns the committed contents of the database. Used for queries.
  • (aatree.core.db-get-in this [keys]) Uses get-in to return some selected content of the committed state of the database. Used for queries.
  • (aatree.core.db-send this f) Uses send to call (f this) in the context of the database for processing. If an exception is thrown by f, the stack trace is logged and the database is closed.
  • (aatree.core.db-update this f) Sends the function using db-send and then waits for completion. If :send-update-timeout is specified in opts, then a timeout will occur after the given number of milliseconds.
  • (aatree.core.update-get this) Returns the contents of the database. Used while updating the database in place of db-get. Should only be called in the context of an update operation.
  • (aatree.core.update-get-in this [keys]) Returns some selected content of the database. Used while updating the database in place of db-get. Should only be called in the context of an update operation.
  • (aatree.core.update-assoc-in! this [keys] value) Used to add content to the database. Should only be called in the context of an update operation.
  • (aatree.core.update-dissoc-in! this [keys]) Used to remove content from the database. Should only be called in the context of an update operation.

See also Db-chan Options.

(ns aatree.db-chan-test
  (:require [clojure.test :refer :all]
            [aatree.core :refer :all]
            [aatree.db-chan-trait :refer :all]
            [aatree.closer-trait :refer :all]))

(set! *warn-on-reflection* true)

(deftest chan-trait
  (try
    (println "#######################")
    (let [db (-> {:db-updater
                  (fn [this app-updater]
                    (app-updater this))}
                 (db-chan)
                 (create-db-chan {}))]
      (db-send db (fn [_] (println ":-)")))
      (db-send db (fn [_] (println ":D")))
      (println (db-update db (fn [_] (println ";-}"))))
      (let [db (assoc db :send-update-timeout 300)]
        (println (db-update db (fn [_] (Thread/sleep 1000))))
        (println (db-update db (fn [_] (Thread/sleep 1000))))
        (println (db-update db (fn [_] (throw (Exception. "fun")))))
        (Thread/sleep 200)
        (close-components db)))
    (finally
      (println "done"))))
Clone this wiki locally