Skip to content

Commit

Permalink
Updated deps, added squuid test
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jun 1, 2015
1 parent 7fc9e75 commit b4c104d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 45 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 3 additions & 32 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 [
Expand Down
21 changes: 12 additions & 9 deletions src/datascript.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand 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))
Expand All @@ -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))))
4 changes: 2 additions & 2 deletions src/datascript/js.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
15 changes: 15 additions & 0 deletions test/datascript/test/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))

0 comments on commit b4c104d

Please sign in to comment.