-
Notifications
You must be signed in to change notification settings - Fork 11
/
keymaker.clj
71 lines (56 loc) · 1.75 KB
/
keymaker.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
66
67
68
69
70
71
(ns cbass.keymaker
(:require [cbass.tools :refer [to-bytes hash-key current-utc-millis str-now]]
[clojure.string :as s]))
(defonce | (.getBytes "|"))
(defonce |+ (.getBytes "}"))
(defonce hash-and-pipe 5) ;; murmur32 is 4 bytes + 1 for a pipe
(defonce splitter
(re-pattern (str "\\" (String. |))))
(defn split-key [k]
(s/split k splitter))
(defn seq->byte-array [xs]
(byte-array (mapcat seq xs)))
(defn without-hash [k]
(->> k
(drop hash-and-pipe)
byte-array
(String.)))
(defn key-nth [k n]
(-> k
without-hash
split-key
(nth n)))
(defn with-hash-pipe [^String k to]
(let [#^bytes k-bytes (.getBytes (s/trim k))
#^bytes prefix (hash-key k-bytes)]
(seq->byte-array [prefix | k-bytes to])))
(defn hash-pipe [^String k]
(with-hash-pipe k |))
(defn hash-pipe+ [^String k]
(with-hash-pipe k |+))
(defn with-hash
([^String k]
(let [#^bytes k-bytes (.getBytes (s/trim k))
#^bytes prefix (hash-key k-bytes)]
(seq->byte-array [prefix | k-bytes])))
([^String from ^String to]
(let [#^bytes f-bytes (.getBytes from)
#^bytes t-bytes (.getBytes to)
#^bytes prefix (hash-key f-bytes)]
(seq->byte-array [prefix | f-bytes | t-bytes]))))
(defn with-hash-and-ts
([^String k]
(with-hash-and-ts k (current-utc-millis)))
([^String k ts]
(let [#^bytes k-bytes (.getBytes (s/trim k))
#^bytes prefix (hash-key k-bytes)
#^bytes ts (to-bytes (str ts))] ;; TODO: should be no "str" if timestamp is just long
(seq->byte-array [prefix | k-bytes | ts]))))
(defn key->ts [k]
(-> k split-key last))
(defn key->date [k]
(-> k key->ts str-now))
(defn keys->dates [m]
(into []
(for [[k v] m]
[(key->date k) v])))