Skip to content

Commit 3f1052c

Browse files
committed
Add docstrings
1 parent c424c77 commit 3f1052c

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

src/xitdb/util/conversion.clj

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
(integer? v) :key-integer
3636
(instance? java.time.Instant v) :inst
3737
(instance? java.util.Date v) :date
38-
(coll? v) :coll
3938
(string? v) :string))
4039

4140
;; map of logical tag -> string used as formatTag in the Bytes record.
@@ -46,7 +45,6 @@
4645
:nil "nl" ;; TODO: Could use Tag/NONE instead
4746
:inst "in"
4847
:date "da"
49-
:coll "co"
5048
:string "st"})
5149

5250
(def true-str "#t")
@@ -204,44 +202,6 @@
204202
:else
205203
(primitive-for v)))
206204

207-
(defn ^Database$Bytes db-key
208-
"Converts k from a Clojure type to a Database$Bytes representation to be used in
209-
cursor functions."
210-
[k]
211-
(cond
212-
(integer? k)
213-
(database-bytes (str k) "ki") ;integer keys are stored as strings with 'ki' format tag
214-
:else
215-
(primitive-for k)))
216-
217-
(defn read-bytes-with-format-tag [^ReadCursor cursor]
218-
(let [bytes-obj (.readBytesObject cursor nil)
219-
str (String. (.value bytes-obj))
220-
fmt-tag (some-> bytes-obj .formatTag String.)]
221-
(cond
222-
223-
(= fmt-tag (fmt-tag-value :keyword))
224-
(keyword str)
225-
226-
(= fmt-tag (fmt-tag-value :boolean))
227-
(= str true-str)
228-
229-
(= fmt-tag (fmt-tag-value :key-integer))
230-
(Integer/parseInt str)
231-
232-
(= fmt-tag (fmt-tag-value :inst))
233-
(java.time.Instant/parse str)
234-
235-
(= fmt-tag (fmt-tag-value :date))
236-
(java.util.Date/from
237-
(java.time.Instant/parse str))
238-
239-
(= fmt-tag (fmt-tag-value :nil))
240-
nil
241-
242-
:else
243-
str)))
244-
245205
(def ^:dynamic *debug?* false)
246206

247207
(defn ^WriteCursor coll->ArrayListCursor!
@@ -320,4 +280,38 @@
320280
(let [hash-code (db-key-hash db v)
321281
cursor (.putCursor whm hash-code)]
322282
(.writeIfEmpty cursor (v->slot! cursor v))))
323-
(.-cursor whm)))
283+
(.-cursor whm)))
284+
285+
(defn read-bytes-with-format-tag
286+
"Reads a `BYTES` value (as a string) at `cursor` and converts it to a Clojure type.
287+
Checks the `formatTag` at cursor to determine the type encoded in the bytes object and
288+
converts the value to the respective Clojure type (eg. keyword, boolean, instant, date).
289+
Supported types are in the global constant `fmt-tag-value`.
290+
If there is no format tag (or it is unknown), returns the value as a string."
291+
[^ReadCursor cursor]
292+
(let [bytes-obj (.readBytesObject cursor nil)
293+
str (String. (.value bytes-obj))
294+
fmt-tag (some-> bytes-obj .formatTag String.)]
295+
(cond
296+
297+
(= fmt-tag (fmt-tag-value :keyword))
298+
(keyword str)
299+
300+
(= fmt-tag (fmt-tag-value :boolean))
301+
(= str true-str)
302+
303+
(= fmt-tag (fmt-tag-value :key-integer))
304+
(Integer/parseInt str)
305+
306+
(= fmt-tag (fmt-tag-value :inst))
307+
(java.time.Instant/parse str)
308+
309+
(= fmt-tag (fmt-tag-value :date))
310+
(java.util.Date/from
311+
(java.time.Instant/parse str))
312+
313+
(= fmt-tag (fmt-tag-value :nil))
314+
nil
315+
316+
:else
317+
str)))

src/xitdb/xitdb_types.clj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
(:import
1010
(io.github.radarroark.xitdb ReadCountedHashMap ReadCursor ReadHashMap Slot Tag WriteCursor WriteHashMap)))
1111

12-
(defn read-from-cursor [^ReadCursor cursor for-writing?]
12+
(defn read-from-cursor
13+
"Reads the value at cursor and converts it to a Clojure type.
14+
Values tagged Hash map, Array List, Linked Array List and Set will be converted to
15+
the respective XITDB* type.
16+
If `for-writing?` is true, the function will return XITDBWrite* types, so `cursor` must be a WriteCursor.
17+
`BYTES` values will be pre-processed (see `conversion/read-bytes-with-format-tag`)."
18+
[^ReadCursor cursor for-writing?]
1319
(let [value-tag (some-> cursor .slot .tag)]
1420
#_(println "read-from-cursor" value-tag "for-writing?" for-writing?)
1521
(cond

0 commit comments

Comments
 (0)