Skip to content

hash with format tag instead of class name #3

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 1 commit into from
May 28, 2025
Merged
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
24 changes: 18 additions & 6 deletions src/xitdb/util/conversion.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,26 @@
(= tag Tag/FLOAT) :float
:else :unknown))

(defn fmt-tag-keyword [v]
(cond
(keyword? v) :keyword
(boolean? v) :boolean
(integer? v) :key-integer
(instance? java.time.Instant v) :inst
(instance? java.util.Date v) :date
(coll? v) :coll
(string? v) :string))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No :else, will cause exception, is this the intent ?


;; map of logical tag -> string used as formatTag in the Bytes record.
(def fmt-tag-value
{:keyword "kw"
:boolean "bl"
:key-integer "ki"
:nil "nl" ;; TODO: Could use Tag/NONE instead
:inst "in"
:date "da"})
:date "da"
:coll "co"
:string "st"})

(def true-str "#t")
(def false-str "#f")
Expand All @@ -59,11 +71,11 @@
^bytes [^Database jdb v]
(if (nil? v)
(byte-array (-> jdb .md .getDigestLength))
(let [digest (.md jdb)]
;; add type name
(.update digest (-> v .getClass .getCanonicalName (.getBytes "UTF-8")))
;; add null byte as separator
(.update digest (byte-array 1))
(let [digest (.md jdb)
fmt-tag (or (some-> v fmt-tag-keyword fmt-tag-value)
(throw (ex-info (str "Format tag not found for type: " (type v)) {})))]
;; add format tag
(.update digest (.getBytes fmt-tag "UTF-8"))
;; add the value
(cond
(validation/lazy-seq? v)
Expand Down