Skip to content

Commit 0de5053

Browse files
committed
hash with type name and value
1 parent 2ad2690 commit 0de5053

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/xitdb/util/conversion.clj

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,33 @@
5656
Uses the MessageDigest from the database."
5757
^bytes [^Database jdb v]
5858
(if (nil? v)
59-
(byte-array (-> jdb .-header .hashSize))
60-
(let [hash-code (hash v)
61-
buffer (ByteBuffer/allocate Integer/BYTES)
62-
_ (.putInt buffer hash-code)
63-
bytes (.array buffer)]
64-
(.digest (.md jdb) bytes))))
59+
(byte-array (-> jdb .md .getDigestLength))
60+
(do
61+
;; add type name
62+
(.update (.md jdb) (-> v .getClass .getCanonicalName (.getBytes "UTF-8")))
63+
;; add null byte as separator
64+
(.update (.md jdb) (byte-array 1))
65+
;; add the value
66+
(cond
67+
(validation/lazy-seq? v)
68+
(throw (IllegalArgumentException. "Lazy sequences can be infinite and not allowed!"))
69+
70+
(bytes? v)
71+
(.update (.md jdb) v)
72+
73+
(instance? Database$Bytes v)
74+
(.update (.md jdb) (.value v))
75+
76+
(coll? v)
77+
(with-open [os (java.security.DigestOutputStream. (java.io.OutputStream/nullOutputStream) (.md jdb))]
78+
(with-open [writer (java.io.OutputStreamWriter. os)]
79+
(binding [*out* writer]
80+
(pr v))))
81+
82+
:else
83+
(.update (.md jdb) (.getBytes (str v) "UTF-8")))
84+
;; finish hash
85+
(.digest (.md jdb)))))
6586

6687
(defn ^Slot primitive-for
6788
"Converts a Clojure primitive value to its corresponding XitDB representation.

0 commit comments

Comments
 (0)