From b4c104d64e27f012e7457f1c224fdbe07c60bb8a Mon Sep 17 00:00:00 2001 From: Nikita Prokopov Date: Mon, 1 Jun 2015 17:05:06 +0600 Subject: [PATCH] Updated deps, added squuid test --- README.md | 6 ++++-- project.clj | 35 +++------------------------------- src/datascript.cljc | 21 +++++++++++--------- src/datascript/js.cljs | 4 ++-- test/datascript/test/core.cljc | 15 +++++++++++++++ 5 files changed, 36 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index c17cddaf..7e4fbd35 100644 --- a/README.md +++ b/README.md @@ -47,11 +47,13 @@ For more examples, see [our acceptance test suite](test/datascript/test/). ```clj :dependencies [ [org.clojure/clojure "1.7.0-beta2"] - [org.clojure/clojurescript "0.0-3211"] - [datascript "0.11.2"] + [org.clojure/clojurescript "0.0-3291"] + [datascript "0.11.3"] ] ``` +_Note:_ DataScript 0.11.3 and later requires CLJS 0.0-3291 or later + _Note:_ DataScript 0.11.2 and later requires Clojure 1.7.0-beta2 or later _Note:_ DataScript 0.11.0 and later will only work with CLJS 0.0-3196 and later diff --git a/project.clj b/project.clj index c366a76b..5ba152bd 100644 --- a/project.clj +++ b/project.clj @@ -5,8 +5,8 @@ :url "https://github.com/tonsky/datascript" :dependencies [ - [org.clojure/clojure "1.7.0-beta3" :scope "provided"] - [org.clojure/clojurescript "0.0-3211" :scope "provided"] + [org.clojure/clojure "1.7.0-RC1" :scope "provided"] + [org.clojure/clojurescript "0.0-3297" :scope "provided"] ] :global-vars { @@ -16,36 +16,7 @@ :jvm-opts ["-Xmx2g"] - :aliases {"test-clj" - ["test" - "datascript.test.core" - "datascript.test.btset" - "datascript.test.components" - "datascript.test.entity" - "datascript.test.explode" - "datascript.test.filter" - "datascript.test.index" - "datascript.test.listen" - "datascript.test.lookup-refs" - "datascript.test.parser" - "datascript.test.parser-find" - "datascript.test.parser-rules" - "datascript.test.parser-query" - "datascript.test.parser-where" - "datascript.test.perf" - "datascript.test.pull-api" - "datascript.test.pull-parser" - "datascript.test.query" - "datascript.test.query-aggregates" - "datascript.test.query-find-specs" - "datascript.test.query-fns" - "datascript.test.query-pull" - "datascript.test.query-rules" - "datascript.test.query-v3" - "datascript.test.serialization" - "datascript.test.transact" - "datascript.test.validation" - "datascript.test.upsert"]} + :aliases {"test-clj" ["run" "-m" "datascript.test/test-all"]} :cljsbuild { :builds [ diff --git a/src/datascript.cljc b/src/datascript.cljc index 151d823a..3253d59f 100644 --- a/src/datascript.cljc +++ b/src/datascript.cljc @@ -6,7 +6,7 @@ [datascript.query :as dq] [datascript.impl.entity :as de] [datascript.btset :as btset]) - #?(:clj (:import [datascript.core FilteredDB]))) + #?(:clj (:import [datascript.core FilteredDB] [java.util UUID]))) ;; SUMMING UP @@ -173,17 +173,18 @@ (defn squuid [] #?(:clj - (let [uuid (java.util.UUID/randomUUID) + (let [uuid (UUID/randomUUID) time (int (/ (System/currentTimeMillis) 1000)) high (.getMostSignificantBits uuid) low (.getLeastSignificantBits uuid) new-high (bit-or (bit-and high 0x00000000FFFFFFFF) (bit-shift-left time 32)) ] - (java.util.UUID. new-high low)) + (UUID. new-high low)) :cljs - (UUID. + (uuid (str - (-> (js/Date.) (.getTime) (/ 1000) (Math/round) (to-hex-string 8)) + (-> (int (/ (.getTime (js/Date.)) 1000)) + (to-hex-string 8)) "-" (-> (rand-bits 16) (to-hex-string 4)) "-" (-> (rand-bits 16) (bit-and 0x0FFF) (bit-or 0x4000) (to-hex-string 4)) "-" (-> (rand-bits 16) (bit-and 0x3FFF) (bit-or 0x8000) (to-hex-string 4)) @@ -192,7 +193,9 @@ (-> (rand-bits 16) (to-hex-string 4)))))) (defn squuid-time-millis [uuid] - (-> (subs (:uuid uuid) 0 8) - #?(:clj (Integer/parseInt 16) - :cljs (js/parseInt 16)) - (* 1000))) + #?(:clj (-> (.getMostSignificantBits ^UUID uuid) + (bit-shift-right 32) + (* 1000)) + :cljs (-> (subs (str uuid) 0 8) + (js/parseInt 16) + (* 1000)))) diff --git a/src/datascript/js.cljs b/src/datascript/js.cljs index a57681dc..4b26177d 100644 --- a/src/datascript/js.cljs +++ b/src/datascript/js.cljs @@ -124,7 +124,7 @@ (into-array (d/index-range db attr start end))) (defn ^:export squuid [] - (.-uuid (d/squuid))) + (str (d/squuid))) (defn ^:export squuid_time_millis [uuid] - (d/squuid-time-millis (UUID. uuid))) + (d/squuid-time-millis (cljs.core/uuid uuid))) diff --git a/test/datascript/test/core.cljc b/test/datascript/test/core.cljc index 71ee2b26..3effbc73 100644 --- a/test/datascript/test/core.cljc +++ b/test/datascript/test/core.cljc @@ -78,3 +78,18 @@ (is (= nil (-> (.-__hash db) #?(:clj (deref))))) (let [h (hash db)] (is (= h (-> (.-__hash db) #?(:clj (deref)))))))) + +(defn- now [] + #?(:clj (System/currentTimeMillis) + :cljs (.getTime (js/Date.)))) + +(deftest test-uuid + (loop [] + (when (> (mod (now) 1000) 990) ;; sleeping over end of a second + (recur))) + (let [now-ms (now) + now (int (/ now-ms 1000))] + (is (= (* 1000 now) (d/squuid-time-millis (d/squuid)))) + (is (not= (d/squuid) (d/squuid))) + (is (= (subs (str (d/squuid)) 0 8) + (subs (str (d/squuid)) 0 8)))))