Calf - laforge49/aatree GitHub Wiki

API

Calf is a small database written using lazy aatree structures, Closer, Db File and either Db Chan or Db Agent. On disk there are two blocks which are written alternately, which makes it a C.O.W (Copy-On-Write) database. When opened, both blocks are read and the newest valid block is used. (Each block contains a transaction count and a checksum.)

Calf uses the following key vectors to organize its contents:

  • [:app-map] Location of application data.

The API is quite short:

  • (aatree.calf.calf-open file block-size) Opens the database. The top-level data structure held by the database is a lazy sorted map. The updated opts map is returned.
  • (aatree.calf.calf-open this file block-size) Opens the database. The updated options map (this) is returned.
  • (aatree.core.db-block-size this) Returns the block size.
  • (aatree.core.get-transaction-count this) Returns the transaction count.

See also Calf Database Options.

Calf Test

(ns aatree.calf-test
  (:require [clojure.test :refer :all]
            [aatree.core :refer :all]
            [aatree.calf :refer :all]
            [aatree.closer-trait :refer :all])
  (:import (java.io File)))

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

(deftest calf
  (.delete (File. "calf-test.calf"))

  (let [calf (calf-open (File. "calf-test.calf") 10000)
        app-map (db-get-in calf [:app-map])
        _ (is (= (get-transaction-count calf) 2))
        _ (is (= app-map nil))
        _ (db-update calf
                     (fn [db]
                       (update-assoc-in!
                         db
                         [:app-map :fun]
                         "Clojure")))
        fun (db-get-in calf [:app-map :fun])
        _ (is (= fun "Clojure"))
        _ (is (= (get-transaction-count calf) 3))
        _ (close-components calf)])

  (let [calf (calf-open (File. "calf-test.calf") 10000)
        fun (db-get-in calf [:app-map :fun])
        _ (is (= (get-transaction-count calf) 3))
        _ (is (= fun "Clojure"))
        _ (close-components calf)]))