-
Notifications
You must be signed in to change notification settings - Fork 11
/
tools.clj
65 lines (52 loc) · 1.52 KB
/
tools.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(ns cbass.tools
(:require [taoensso.nippy :as n]
[æsahættr :refer [hash-object hash-bytes murmur3-32 murmur3-128]])
(:import [org.apache.hadoop.hbase.util Bytes]
[java.time Instant ZoneId ZonedDateTime ZoneOffset Duration]
[java.time.format DateTimeFormatter]
(com.google.common.hash HashCode)))
(defmacro bytes? [s]
`(= (Class/forName "[B")
(.getClass ~s)))
(defmacro to-bytes [s]
`(if-not (bytes? ~s)
(Bytes/toBytes ~s)
~s))
(defmacro from-bytes [s]
`(Bytes/fromBytes ~s))
(defn bytes->num
"Convert byte array to a number"
[data]
(BigInteger. data))
(defn bytes->str
"Convert byte array to a string"
[data]
(apply str (map char data)))
(defn thaw [data]
(when (seq data)
(n/thaw data)))
(defonce no-values
(byte-array 0))
(defn hash-it [obj]
(->> obj
(hash-object (murmur3-128))
str))
(defn hash-key [#^bytes k-bytes]
(.asBytes ^HashCode (hash-bytes (murmur3-32)
k-bytes)))
(defn current-utc-millis []
(-> (ZoneOffset/UTC)
(ZonedDateTime/now)
(.toInstant)
(.toEpochMilli)))
(defn parse-long [ts]
(try
(Long/valueOf ^String ts)
(catch Throwable t
(prn "could not parse " ts " to a Long due to " (class t) ": " (.getMessage t)))))
(defn str-now [ts]
(if-let [timestamp (parse-long ts)]
(-> (ZonedDateTime/ofInstant
(Instant/ofEpochMilli timestamp)
(ZoneId/of "UTC"))
(.format (DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss.SSS")))))