Skip to content

improve hashing #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/xitdb/array_list.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions src/xitdb/linked_list.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 27 additions & 6 deletions src/xitdb/util/conversion.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down