Skip to content
Bill La Forge edited this page Dec 6, 2015 · 13 revisions

API

With so many different implementations of map, set and vector, things could get messy. But the function (aatree.core.transcribe val opts) can be used to convert trees using one implementation to trees that use a different implementation. Even nicer, transcription is invoked transparently when adding structures to an aa structure with a different implementation.

(ns aatree.transcribe-examples
  (:require [aatree.core :refer :all]))

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

; The recognized classes
(println (class []))
; -> clojure.lang.PersistentVector
(println (class (new-vector (standard-opts))))
; -> clojure.lang.PersistentVector
(println (class (new-vector (basic-opts))))
; -> aatree.AAVector
(println (class (sorted-map)))
; -> clojure.lang.PersistentTreeMap
(println (class (new-sorted-map (standard-opts))))
; -> clojure.lang.PersistentTreeMap
(println (class (new-sorted-map (basic-opts))))
; -> clojure.lang.aatree.AAMap
(println (class (sorted-set)))
; -> clojure.lang.PersistentTreeSet
(println (class (new-sorted-set (standard-opts))))
; -> clojure.lang.PersistentTreeSet
(println (class (new-sorted-set (basic-opts))))
; -> clojure.lang.aatree.AASet
(println)

; Top-level conversion
(println (class (transcribe [] (basic-opts))))
; -> aatree.AAVector
(println (class (transcribe (transcribe [] (basic-opts))
                            (standard-opts))))
; -> clojure.lang.PersistentVector
(println (class (transcribe (sorted-map) (basic-opts))))
; -> clojure.lang.aatree.AAMap
(println (class (transcribe (transcribe (sorted-map) (basic-opts))
                            (standard-opts))))
; -> clojure.lang.PersistentTreeMap
(println (class (transcribe (sorted-set) (basic-opts))))
; -> clojure.lang.aatree.AASet
(println (class (transcribe (transcribe (sorted-set) (basic-opts))
                            (standard-opts))))
; -> clojure.lang.PersistentTreeSet
(println)

; No conversion of unrecognized structures
(println (class (list)))
; -> clojure.lang.PersistentList$EmptyList
(println (class (transcribe (list) (basic-opts))))
; -> clojure.lang.PersistentList$EmptyList
(println)

; recursive conversion of recognized structures
(def std-vec [[]])
(def basic-vec (transcribe std-vec (basic-opts)))
(println (class (basic-vec 0)))
; -> aatree.AAVector
(println (class (transcribe basic-vec (standard-opts))))
; -> clojure.lang.PersistentVector
(def std-map (conj (sorted-map) [:m {}]))
(def basic-map (transcribe std-map (basic-opts)))
(println (class (basic-map :m)))
; -> aatree.AAMap
(println (class (transcribe basic-map (standard-opts))))
; -> clojure.lang.PersistentTreeMap

Moving Data between Databases

Another potential complication occurs when moving data between aatree databases, as virtual aa structures are tied to the database that contains them. But this is handled transparently through transcription when an aa database is updated with data from a different database.

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

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

(deftest transparent-transcription
  (let [file-a (File. "transcription-a.yearling")
        file-b (File. "transcription-b.yearling")
        _ (.delete file-a)
        _ (.delete file-b)
        yearling-a (yearling-open file-a)
        yearling-b (yearling-open file-b)
        _ (db-update yearling-a
                     (fn [db]
                       (update-assoc-in! db [:app-map :v] [1 2 3])))
        va (db-get-in yearling-a [:app-map :v])
        _ (is (= "aatree.AAVector" (.getName (class va))))
        va-opts (aa-opts va)
        ^File va-file (:db-file va-opts)
        va-file-name (.toString va-file)
        _ (is (= va-file-name (.toString file-a)))
        _ (db-update yearling-b
                     (fn [db]
                       (update-assoc-in! db [:app-map :v] va)))
        vb (db-get-in yearling-b [:app-map :v])
        _ (is (= "aatree.AAVector" (.getName (class vb))))
        vb-opts (aa-opts vb)
        ^File vb-file (:db-file vb-opts)
        vb-file-name (.toString vb-file)
        _ (is (= vb-file-name (.toString file-b)))
        _ (close-components yearling-a)
        _ (close-components yearling-b)
        ])

  (Thread/sleep 200))
Clone this wiki locally