Skip to content

Commit

Permalink
Clojure reader pr/read support for DB and Datom
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jul 7, 2014
1 parent 091fb9d commit 718dbb1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.1.6

- Clojure reader support (pr/read) for DB and Datom

# 0.1.5

- `datoms` and `seek-datoms` API calls
Expand Down
2 changes: 2 additions & 0 deletions src/data_readers.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{datascript/Datom datascript/datom-from-reader
datascript/DB datascript/db-from-reader}
39 changes: 38 additions & 1 deletion src/datascript.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@
(cmp (.-tx d1) (.-tx d2))))

(defrecord DB [schema eavt aevt avet max-eid max-tx]
Object
(toString [this]
(pr-str* this))

ISearch
(-search [db [e a v tx]]
(-search [_ [e a v tx]]
(case-tree [e a (some? v) tx] [
(slice eavt (Datom. e a v tx nil)) ;; e a v tx
(slice eavt (Datom. e a v nil nil)) ;; e a v _
Expand Down Expand Up @@ -529,3 +533,36 @@

(defn seek-datoms [db index & cs]
(slice (get db index) (components->pattern index cs) (Datom. nil nil nil nil nil)))

;; printing and reading
;; #datomic/DB {:schema <map>, :datoms <vector of [e a v tx]>}

(extend-type Datom
IPrintWithWriter
(-pr-writer [d w opts]
(pr-sequential-writer w pr-writer "#datascript/Datom [" " " "]" opts [(.-e d) (.-a d) (.-v d) (.-tx d) (.-added d)])))

(defn datom-from-reader [[e a v tx added]]
(Datom. e a v tx added))

(extend-type DB
IPrintWithWriter
(-pr-writer [db w opts]
(-write w "#datascript/DB {")
(-write w ":schema ")
(pr-writer (.-schema db) w opts)
(-write w ", :datoms ")
(pr-sequential-writer w
(fn [d w opts]
(pr-sequential-writer w pr-writer "[" " " "]" opts [(.-e d) (.-a d) (.-v d) (.-tx d)]))
"[" " " "]" opts (.-eavt db))
(-write w "}")))

(defn db-from-reader [{:keys [schema datoms]}]
(let [datoms (map (fn [[e a v tx]] (Datom. e a v tx true)) datoms)]
(DB. schema
(apply btset-by cmp-datoms-eavt datoms)
(apply btset-by cmp-datoms-aevt datoms)
(apply btset-by cmp-datoms-avet datoms)
(reduce max 0 (map :e datoms))
(reduce max tx0 (map :tx datoms)))))
17 changes: 17 additions & 0 deletions test/test/datascript.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -583,5 +583,22 @@
[ [1 :name "Petr"]
[3 :name "Sergey"] ])))))

(deftest test-pr-read
(binding [cljs.reader/*tag-table* (atom {"datascript/Datom" d/datom-from-reader})]
(let [d (d/Datom. 1 :name 3 17 true)]
(is (= d (cljs.reader/read-string (pr-str d)))))
(let [d (d/Datom. 1 :name 3 nil nil)]
(is (= d (cljs.reader/read-string (pr-str d))))))

(let [db (-> (d/empty-db)
(d/with [[:db/add 1 :name "Petr"]
[:db/add 1 :age 44]
[:db/add 2 :name "Ivan"]
[:db/add 2 :age 25]
[:db/add 3 :name "Sergey"]
[:db/add 3 :age 11]]))]
(binding [cljs.reader/*tag-table* (atom {"datascript/DB" d/db-from-reader})]
(is (= db (cljs.reader/read-string (pr-str db)))))))

;; (t/test-ns 'test.datascript)

0 comments on commit 718dbb1

Please sign in to comment.