Skip to content

Commit

Permalink
Compatibility with 1.9 defrecords (closes tonsky#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Sep 14, 2016
1 parent 7e77545 commit e9a6b67
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 70 deletions.
2 changes: 1 addition & 1 deletion bench/bench
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#^:shebang '[
exec java -cp "$HOME/.m2/repository/org/clojure/clojure/1.7.0-RC2/clojure-1.7.0-RC2.jar" clojure.main "$0" "$@"
exec java -cp "$HOME/.m2/repository/org/clojure/clojure/1.7.0/clojure-1.7.0.jar" clojure.main "$0" "$@"
]

"USAGE: ./bench [-c | -f] (jvm|js) (<bench-name>|all) (<tag>|<revision>)"
Expand Down
72 changes: 40 additions & 32 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
[org.clojure/clojurescript "1.7.228" :scope "provided"]
]

:plugins [
[lein-cljsbuild "1.1.4"]
]

:global-vars {
*warn-on-reflection* true
;; *unchecked-math* :warn-on-boxed
Expand All @@ -19,7 +23,13 @@
:aliases {"test-clj" ["run" "-m" "datascript.test/test-most"]
"test-clj-all" ["run" "-m" "datascript.test/test-all"]
"node-repl" ["run" "-m" "user/node-repl"]
"browser-repl" ["run" "-m" "user/browser-repl"]}
"browser-repl" ["run" "-m" "user/browser-repl"]
"test-all" ["do" ["clean"]
["test-clj-all"]
["cljsbuild" "once" "release" "advanced"]
["run" "-m" "datascript.test/test-node" "--all"]]
"test-1.8" ["with-profile" "dev,1.8" "test-all"]
"test-1.9" ["with-profile" "dev,1.9" "test-all"]}

:cljsbuild {
:builds [
Expand All @@ -35,40 +45,38 @@
:parallel-build true
}
:notify-command ["release-js/wrap_bare.sh"]}

{ :id "advanced"
:source-paths ["src" "bench/src" "test"]
:compiler {
:output-to "target/datascript.js"
:optimizations :advanced
:source-map "target/datascript.js.map"
:pretty-print true
:recompile-dependents false
:parallel-build true
}}

{ :id "none"
:source-paths ["src" "bench/src" "test" "dev"]
:compiler {
:main datascript.test
:output-to "target/datascript.js"
:output-dir "target/none"
:optimizations :none
:source-map true
:recompile-dependents false
:parallel-build true
}}
]}

:profiles {
:dev {
:source-paths ["bench/src" "test" "dev"]
:plugins [
[lein-cljsbuild "1.1.2"]
]
:cljsbuild {
:builds [
{ :id "advanced"
:source-paths ["src" "bench/src" "test"]
:compiler {
:output-to "target/datascript.js"
:optimizations :advanced
:source-map "target/datascript.js.map"
:pretty-print true
:recompile-dependents false
:parallel-build true
}}
{ :id "none"
:source-paths ["src" "bench/src" "test" "dev"]
:compiler {
:main datascript.test
:output-to "target/datascript.js"
:output-dir "target/none"
:optimizations :none
:source-map true
:recompile-dependents false
:parallel-build true
}}
]
}
}
:1.8 { :dependencies [[org.clojure/clojure "1.8.0" :scope "provided"]
[org.clojure/clojurescript "1.8.51" :scope "provided"]] }
:1.9 { :dependencies [[org.clojure/clojure "1.9.0-alpha12" :scope "provided"]
[org.clojure/clojurescript "1.9.229" :scope "provided"]]
:global-vars { *print-namespace-maps* false } }
:dev { :source-paths ["bench/src" "test" "dev"] }
}

:clean-targets ^{:protect false} [
Expand Down
4 changes: 2 additions & 2 deletions src/datascript/btset.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@
(every? #(contains? this %) other)))

IHash
(-hash [this] (caching-hash this hash-iset __hash))
(-hash [this] (caching-hash this hash-unordered-coll __hash))

ICollection
(-conj [this key] (btset-conj this key comparator))
Expand Down Expand Up @@ -530,7 +530,7 @@
clojure.lang.ILookup
(valAt [this key] (.get this key))
(valAt [this key not-found] (or (.get this key) not-found))

clojure.lang.IHashEq
(hasheq [this]
(when (== -1 _hasheq)
Expand Down
4 changes: 2 additions & 2 deletions src/datascript/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
(if (is-filtered db)
(let [^FilteredDB fdb db
u (.-unfiltered-db fdb)]
(FilteredDB. u #(and (pred u %) ((.-pred fdb) %)) #?(:clj (atom nil))))
(FilteredDB. db #(pred db %) #?(:clj (atom nil)))))
(FilteredDB. u #(and (pred u %) ((.-pred fdb) %)) (atom 0)))
(FilteredDB. db #(pred db %) (atom 0))))

(defn ^:export with
([db tx-data] (with db tx-data nil))
Expand Down
59 changes: 32 additions & 27 deletions src/datascript/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
[then else]
(if (cljs-env? &env) then else)))

(defn combine-hashes [x y]
#?(:clj (clojure.lang.Util/hashCombine x y)
:cljs (hash-combine x y)))

#?(:clj
(defn- get-sig [method]
;; expects something like '(method-symbol [arg arg arg] ...)
Expand Down Expand Up @@ -180,8 +184,8 @@

(defn- hash-datom [^Datom d]
(-> (hash (.-e d))
(hash-combine (hash (.-a d)))
(hash-combine (hash (.-v d)))))
(combine-hashes (hash (.-a d)))
(combine-hashes (hash (.-v d)))))

(defn- equiv-datom [^Datom d ^Datom o]
(and (= (.-e d) (.-e o))
Expand Down Expand Up @@ -366,7 +370,7 @@

(declare hash-db hash-fdb equiv-db empty-db pr-db resolve-datom validate-attr components->pattern indexing?)

(defrecord-updatable DB [schema eavt aevt avet max-eid max-tx rschema #?(:clj __hash)]
(defrecord-updatable DB [schema eavt aevt avet max-eid max-tx rschema hash]
#?@(:cljs
[IHash (-hash [db] (hash-db db))
IEquiv (-equiv [db other] (equiv-db db other))
Expand Down Expand Up @@ -448,7 +452,7 @@
(satisfies? IDB x)))

;; ----------------------------------------------------------------------------
(defrecord-updatable FilteredDB [unfiltered-db pred #?(:clj __hash)]
(defrecord-updatable FilteredDB [unfiltered-db pred hash]
#?@(:cljs
[IHash (-hash [db] (hash-fdb db))
IEquiv (-equiv [db other] (equiv-db db other))
Expand Down Expand Up @@ -566,7 +570,7 @@
:max-eid 0
:max-tx tx0
:rschema (rschema schema)
#?@(:clj [:__hash (atom nil)])})))
:hash (atom 0)})))

(defn- init-max-eid [eavt]
(if-let [slice (btset/slice
Expand Down Expand Up @@ -611,32 +615,31 @@
:max-eid max-eid
:max-tx max-tx
:rschema rschema
#?@(:clj [:__hash (atom nil)])})))))
:hash (atom 0)})))))

(defn- equiv-db-index [x y]
(and (= (count x) (count y))
(loop [xs (seq x)
ys (seq y)]
(cond
(nil? xs) true
(= (first xs) (first ys)) (recur (next xs) (next ys))
:else false))))
(loop [xs (seq x)
ys (seq y)]
(cond
(nil? xs) (nil? ys)
(= (first xs) (first ys)) (recur (next xs) (next ys))
:else false)))

(defn- hash-db [^DB db]
#?(:cljs
(or (.-__hash db)
(set! (.-__hash db) (hash-ordered-coll (-datoms db :eavt []))))
:clj
(or @(.-__hash db)
(reset! (.-__hash db) (hash-ordered-coll (or (-datoms db :eavt []) []))))))
(let [h @(.-hash db)]
(if (zero? h)
(reset! (.-hash db) (combine-hashes (hash (.-schema db))
(hash (.-eavt db))))
h)))

(defn- hash-fdb [^FilteredDB db]
#?(:cljs
(or (.-__hash db)
(set! (.-__hash db) (hash-ordered-coll (-datoms db :eavt []))))
:clj
(or @(.-__hash db)
(reset! (.-__hash db) (hash-ordered-coll (or (-datoms db :eavt []) []))))))
(let [h @(.-hash db)
datoms (or (-datoms db :eavt []) #{})]
(if (zero? h)
(let [datoms (or (-datoms db :eavt []) #{})]
(reset! (.-hash db) (combine-hashes (hash (-schema db))
(hash-unordered-coll datoms))))
h)))

(defn- equiv-db [db other]
(and (or (instance? DB other) (instance? FilteredDB other))
Expand Down Expand Up @@ -819,12 +822,14 @@
true (update-in [:eavt] btset/btset-conj datom cmp-datoms-eavt-quick)
true (update-in [:aevt] btset/btset-conj datom cmp-datoms-aevt-quick)
indexing? (update-in [:avet] btset/btset-conj datom cmp-datoms-avet-quick)
true (advance-max-eid (.-e datom)))
true (advance-max-eid (.-e datom))
true (assoc :hash (atom 0)))
(if-let [removing (first (-search db [(.-e datom) (.-a datom) (.-v datom)]))]
(cond-> db
true (update-in [:eavt] btset/btset-disj removing cmp-datoms-eavt-quick)
true (update-in [:aevt] btset/btset-disj removing cmp-datoms-aevt-quick)
indexing? (update-in [:avet] btset/btset-disj removing cmp-datoms-avet-quick))
indexing? (update-in [:avet] btset/btset-disj removing cmp-datoms-avet-quick)
true (assoc :hash (atom 0)))
db))))

(defn- transact-report [report datom]
Expand Down
10 changes: 9 additions & 1 deletion test/datascript/test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@
(datascript.test.core/wrap-res #(t/run-all-tests #"datascript\.test\.btset")))

(defn ^:export test-all []
(datascript.test.core/wrap-res #(t/run-all-tests)))
(datascript.test.core/wrap-res #(t/run-all-tests #"datascript\..*")))

#?(:clj
(defn test-node [& args]
(let [res (apply clojure.java.shell/sh "node" "test_node.js" args)]
(println (:out res))
(binding [*out* *err*]
(println (:err res)))
(System/exit (:exit res)))))
4 changes: 2 additions & 2 deletions test/datascript/test/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
;; whitebox test to confirm that hash cache caches
(deftest test-db-hash-cache
(let [db (db/empty-db)]
(is (= nil (-> (.-__hash db) #?(:clj (deref)))))
(is (= 0 @(.-hash db)))
(let [h (hash db)]
(is (= h (-> (.-__hash db) #?(:clj (deref))))))))
(is (= h @(.-hash db))))))

(defn- now []
#?(:clj (System/currentTimeMillis)
Expand Down
15 changes: 12 additions & 3 deletions test/datascript/test/filter.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[datascript.test.core :as tdc]))

(deftest test-filter-db
(let [db (-> (d/empty-db {:aka { :db/cardinality :db.cardinality/many }})
(let [empty-db (d/empty-db {:aka { :db/cardinality :db.cardinality/many }})
db (-> empty-db
(d/db-with [{:db/id 1
:name "Petr"
:email "petya@spb.ru"
Expand Down Expand Up @@ -56,8 +57,16 @@
(is (= (map :v (d/datoms (d/filter db remove-pass) :aevt :password))
[])))

(testing "equiv and hash"
(testing "equiv"
(is (= (d/db-with db [[:db.fn/retractEntity 2]])
(d/filter db remove-ivan)))
(is (= empty-db
(d/filter empty-db (constantly true))
(d/filter db (constantly false)))))

(testing "hash"
(is (= (hash (d/db-with db [[:db.fn/retractEntity 2]]))
(hash (d/filter db remove-ivan)))))))
(hash (d/filter db remove-ivan))))
(is (= (hash empty-db)
(hash (d/filter empty-db (constantly true)))
(hash (d/filter db (constantly false))))))))

0 comments on commit e9a6b67

Please sign in to comment.