diff --git a/src/xitdb/array_list.clj b/src/xitdb/array_list.clj index 6d65d48..2a7e67d 100644 --- a/src/xitdb/array_list.clj +++ b/src/xitdb/array_list.clj @@ -44,12 +44,9 @@ (common/-read-from-cursor cursor))) (nth [_ i not-found] - (try - (let [cursor (.getCursor ral (long i))] - (if cursor - (common/-read-from-cursor cursor) - not-found)) - (catch Exception _ + (let [cursor (.getCursor ral (long i))] + (if cursor + (common/-read-from-cursor cursor) not-found))) clojure.lang.ILookup diff --git a/src/xitdb/linked_list.clj b/src/xitdb/linked_list.clj index 15dcfa1..7e1d8e5 100644 --- a/src/xitdb/linked_list.clj +++ b/src/xitdb/linked_list.clj @@ -38,12 +38,9 @@ (common/-read-from-cursor cursor))) (nth [_ i not-found] - (try - (let [cursor (.getCursor rlal (long i))] - (if cursor - (common/-read-from-cursor cursor) - not-found)) - (catch Exception _ + (let [cursor (.getCursor rlal (long i))] + (if cursor + (common/-read-from-cursor cursor) not-found))) clojure.lang.ILookup diff --git a/src/xitdb/util/conversion.clj b/src/xitdb/util/conversion.clj index 96973e7..781bf4f 100644 --- a/src/xitdb/util/conversion.clj +++ b/src/xitdb/util/conversion.clj @@ -56,12 +56,33 @@ Uses the MessageDigest from the database." ^bytes [^Database jdb v] (if (nil? v) - (byte-array (-> jdb .-header .hashSize)) - (let [hash-code (hash v) - buffer (ByteBuffer/allocate Integer/BYTES) - _ (.putInt buffer hash-code) - bytes (.array buffer)] - (.digest (.md jdb) bytes)))) + (byte-array (-> jdb .md .getDigestLength)) + (do + ;; add type name + (.update (.md jdb) (-> v .getClass .getCanonicalName (.getBytes "UTF-8"))) + ;; add null byte as separator + (.update (.md jdb) (byte-array 1)) + ;; add the value + (cond + (validation/lazy-seq? v) + (throw (IllegalArgumentException. "Lazy sequences can be infinite and not allowed!")) + + (bytes? v) + (.update (.md jdb) v) + + (instance? Database$Bytes v) + (.update (.md jdb) (.value v)) + + (coll? v) + (with-open [os (java.security.DigestOutputStream. (java.io.OutputStream/nullOutputStream) (.md jdb))] + (with-open [writer (java.io.OutputStreamWriter. os)] + (binding [*out* writer] + (pr v)))) + + :else + (.update (.md jdb) (.getBytes (str v) "UTF-8"))) + ;; finish hash + (.digest (.md jdb))))) (defn ^Slot primitive-for "Converts a Clojure primitive value to its corresponding XitDB representation.