-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
238 lines (210 loc) · 8.3 KB
/
core.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
; Copyright 2018 Amobee Inc.
; This file is part of meeseeks-db.
;
; meeseeks-db is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; meeseeks-db is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with meeseeks-db. If not, see <https://www.gnu.org/licenses/>.
(ns meeseeks-db.core
(:require [taoensso.carmine :as car :refer [wcar]]
[taoensso.carmine.locks :refer [with-lock]]
[taoensso.nippy :refer [freeze lzma2-compressor]]
[clojure.set :refer [difference]]
[clojure.string :refer [starts-with? replace-first]]
[meeseeks-db.query :as q]
[meeseeks-db.cursor :as c]
[schema.core :as s]
[meeseeks-db.utils :refer [fetch-object QueryExpression Key Value deref-of]])
(:import [clojure.lang Murmur3]))
;; ===========================================================================
;; utils
(defn attr [key value]
(meeseeks-db.utils/attr key value))
(defn- freeze-hashmap [m]
(->> m
(map (fn [[k v]]
[k (if (coll? v)
(freeze v {:compressor lzma2-compressor})
v)]))
(into {})))
(defn- save-object! [id obj]
(let [k (str "p:" id)]
(if obj
(car/hmset* k (freeze-hashmap obj))
(car/del k))))
(defn- update-attr! [id prefix old new]
(let [s1 (set old)
s2 (set new)
to-del (difference s1 s2)
to-add (difference s2 s1)
k (partial attr prefix)]
(doseq [v to-del] (car/srem (k v) id))
(doseq [v to-add] (car/sadd (k v) id))))
(defn- walk-keys [conn cursor pattern]
(let [[cursor ks] (wcar conn
(car/scan cursor
:match pattern
:count 1000))]
[(Long/parseLong cursor) ks]))
(defn- scan-indices* [conn pattern f]
(loop [[cursor ks] (walk-keys conn 0 pattern)]
(f ks)
(when-not (zero? cursor)
(recur (walk-keys conn cursor pattern)))))
(defn- indexify [f-index obj]
(letfn [(normalize [vs]
(remove nil? (if (coll? vs) vs [vs])))]
(map (fn [[k vs]]
[k (normalize vs)])
(if (empty? obj) [] (f-index obj)))))
;; ===========================================================================
;; API
;; default id<->iid mappers
(defn default-id->iid
([conn id]
(if-let [iid (wcar conn (car/get (str "id:" id)))]
iid
(let [iid (wcar conn (car/incr "next-iid"))]
(when iid (car/atomic conn 10
(car/multi)
(car/setnx (str "id:" id) iid)
(car/setnx (str "iid:" iid) id)))
(wcar conn (car/get (str "id:" id))))))
([conn id delete?]
(wcar conn (car/get (str "id:" id)))))
(defn default-iid->id [iid]
(car/get (str "iid:" iid)))
(defn default-id->conn [db id]
(if (> (count db) 1)
(let [hc (Murmur3/hashUnencodedChars (str id))]
(nth db (mod hc (count db))))
(first db)))
;; API
(s/defschema Connection
"Carmine connection spec"
{(s/optional-key :pool) s/Any
:spec (s/pred map?)})
(s/defschema ClientConfig
{:f-index (s/pred fn?)
(s/optional-key :data-db) (deref-of [Connection])
(s/optional-key :f-id->iid) (s/pred fn?)
(s/optional-key :f-iid->id) (s/pred fn?)
(s/optional-key :f-id->conn) (s/pred fn?)})
(s/defn init [dbs :- (deref-of [Connection]) {:keys [f-index data-db f-id->iid f-iid->id f-id->conn]
:or {f-id->iid default-id->iid
f-iid->id default-iid->id
f-id->conn default-id->conn}} :- ClientConfig]
"Initialize meeseeks client
Options:
:f-index - Function from domain object maps to indices (required)
:data-db - Optionally use different Redis connections for storing domain object maps
:f-id->iid - Function from connection and ID to IID for that connection
:f-iid->id - Function from IID to the original ID. Run in the context of `wcar`
:f-id->conn - Function from list of DBs (either dbs or data-db) and ID to the appropriate connection
"
(assert (ifn? f-index) "f-index function is mandatory")
(let [mdb {:db dbs
:data-db (or data-db dbs)
:f-id->iid f-id->iid
:f-id->conn f-id->conn
:f-iid->id f-iid->id
:f-index f-index}]
mdb))
(defn index!
"Index and store an object."
[{:keys [db data-db f-id->iid f-index f-id->conn]} {:keys [id] :as obj}]
(let [db (deref db)
data-db (deref data-db)
conn (f-id->conn db id)
iid (f-id->iid conn id)
data-conn (f-id->conn data-db id)
old (wcar data-conn (fetch-object id))
index-pairs (merge-with conj
(->> (indexify f-index old)
(map (fn [[k vs]] [k {:old vs}]))
(into {}))
(->> (indexify f-index obj)
(map (fn [[k vs]] [k {:new vs}]))
(into {})))]
(when (and iid (not-empty index-pairs))
(wcar conn
(do
(when (empty? (car/get (str "iid:" iid)))
(car/set (str "iid:" iid) id))
(doseq [[k {:keys [old new]}] index-pairs]
(update-attr! iid k old new)))
(car/sadd "total" iid)))
(wcar data-conn
(save-object! id obj))
[id iid data-conn]))
(defn unindex!
"Remove the object and its indices."
[{:keys [db data-db f-id->iid f-index f-id->conn]} id]
(let [db @db
data-db @data-db
conn (f-id->conn db id)
data-conn (f-id->conn data-db id)
iid (f-id->iid conn id "delete")
obj (wcar data-conn (fetch-object id))
indices (indexify f-index obj)]
(wcar conn
(doseq [[k vs] indices]
(update-attr! iid k vs nil))
(car/srem "total" iid))
(wcar data-conn
(save-object! id nil))))
(defn fetch
"Fetch object by ID"
[{:keys [data-db f-id->conn]} id & [fields]]
(let [data-db (deref data-db)
conn (f-id->conn data-db id)]
(wcar conn (fetch-object id fields))))
(s/defn query :- {:size s/Int :sample [{Key s/Any}]}
[client &
[query :- (s/maybe QueryExpression)
sample-size :- (s/maybe s/Int)
fields :- [Key]]]
(with-open [cursor (c/create-cursor! client (q/compile-query query))]
{:size (c/cursor-size cursor)
:sample (if (pos-int? sample-size)
(c/sample-cursor cursor sample-size fields)
[])}))
(defn scan-indices [{:keys [db]} pattern f]
(dorun (pmap #(scan-indices* % pattern f) db)))
(defn remove-all! [{:keys [db data-db]}]
(let [db (deref db)
data-db (deref data-db)]
(dorun
(pmap #(wcar % (car/flushdb)) db))
(dorun
(pmap (fn [conn]
(scan-indices* conn "p:*"
(fn [ks]
(when (seq ks)
(wcar conn
(apply car/del ks))))))
data-db))))
(defn memory-status [{:keys [db data-db]}]
(letfn [(status [conn]
(let [[[_ max-memory] {:keys [used-memory]}]
(wcar conn
(car/config-get :maxmemory)
(car/info* true))
max-memory (Long/parseLong max-memory)
used-memory (Long/parseLong used-memory)]
{:capacity max-memory
:used used-memory}))
(aggregate [statuses]
{:raw statuses
:capacity (reduce + (map :capacity statuses))
:used (reduce + (map :used statuses))})]
{:db (aggregate (pmap status db))
:data-db (aggregate (pmap status data-db))}))