Skip to content

Commit

Permalink
Merge pull request #53 from den1k/den1k/compare-bytes-clj
Browse files Browse the repository at this point in the history
Compare bytes in clj
  • Loading branch information
huahaiy committed Apr 29, 2021
2 parents 1aff4ec + 3c5c99b commit bcd1377
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/datalevin/datom.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
[datalevin.constants :refer [tx0]]
[datalevin.util :refer [combine-hashes]])
#?(:cljs
(:require-macros [datalevin.util :refer [combine-cmp]])))
(:require-macros [datalevin.util :refer [combine-cmp]]))
#?(:clj
(:import (java.util Arrays))))

(declare hash-datom equiv-datom seq-datom nth-datom assoc-datom val-at-datom)

Expand Down Expand Up @@ -191,11 +193,12 @@
;; are not of the same type, so we use `=`.
;; since `a` and `b` are of identical type
;; `coll?` check only one.
(if (coll? a)
(if (= a b)
0
1)
(compare a b))
(cond
(coll? a) (if (= a b)
0
1)
#?@(:clj [(bytes? a) (Arrays/compare ^bytes a ^bytes b)])
:else (compare a b))
-1))

(def nil-cmp (nil-check-cmp-fn compare))
Expand Down
14 changes: 14 additions & 0 deletions test/datalevin/test/transact.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,17 @@
dts2 (d/datoms db :eavt)]
(is (= dts1 dts2))
(d/close-db db)))

#?(:clj
(deftest test-transact-bytes
"requires comparing byte-arrays"
(let [schema {:bytes {:db/valueType :db.type/bytes}}
byte-arrays (mapv #(.getBytes ^String %) ["foo" "bar" "foo"])]
(testing "equal bytes"
(let [db (d/empty-db nil schema)
ents (mapv (fn [ba] {:bytes ba}) byte-arrays)]
(is (every? true?
(map #(zero? (java.util.Arrays/compare ^bytes %1 ^bytes %2))
byte-arrays
(map :v (:tx-data (d/with db ents)))))))))))

0 comments on commit bcd1377

Please sign in to comment.