/
collection.clj
318 lines (284 loc) · 11.2 KB
/
collection.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
;; The MIT License
;;
;; Copyright (c) 2010 Wilkes Joiner
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
(ns karras.collection
(:use [karras.core :only [*mongo-db* to-dbo to-clj build-dbo]]
[karras.def :only [defvar]])
(:import [com.mongodb Mongo DB DBCollection DBObject]
[org.bson.types ObjectId]))
(defn- keyword-str [kw]
(if (keyword? kw)
(name kw)
(str kw)))
(defn- has-option? [options k]
(boolean (some #{k} options)))
(defn collection-db
"Get the DB that this collection is associated with."
[#^DBCollection collection]
(.getDB collection))
(defn collection-db-name
"Get the name of the DB that this collection is associated with."
[#^DBCollection collection]
(.getName (collection-db collection)))
(defn collection-name
"Get the name of the collection."
[collection]
(.getName collection))
(defn collection
"Returns a DBCollection."
([collection-name]
(collection *mongo-db* collection-name))
([#^DB db collection-name]
(.getCollection db (keyword-str collection-name)))
([#^Mongo mongo db-name collection-name]
(collection (.getDB mongo (keyword-str db-name))
collection-name)))
(defn drop-collection
""
[#^DBCollection coll]
(.drop coll))
(defn save
"Saves a document to a colection, does an upsert behind the scenes.
Returns the object with :_id if it was inserted.."
[#^DBCollection coll obj]
(let [dbo (to-dbo obj)]
(.save coll dbo)
(-> dbo to-clj (with-meta (meta obj)))))
(defn insert
"Inserts one or more documents into a collection.
Returns the inserted object with :_id"
[#^DBCollection coll & objs]
(let [inserted (doall (map #(save coll %) objs))]
(if (= 1 (count objs))
(first inserted)
inserted)))
(defn update
"Updates one or more documents in a collection that match the criteria with the document
provided.
:upsert, performs an insert if the document doesn't have :_id
:multi, update all documents that match the criteria"
[#^DBCollection coll criteria obj & options]
(let [o? #(has-option? options %)]
(.update coll
(to-dbo criteria)
(to-dbo obj)
(o? :upsert)
(o? :multi))))
(defn upsert
"Shortcut for (update collection criteria obj :upsert)"
([coll obj]
(upsert coll obj obj))
([coll criteria obj]
(update coll criteria obj :upsert)))
(defn update-all
"Shortcut for (update collection criteria obj :multi)"
([coll obj]
(update-all coll {} obj))
([coll criteria obj]
(update coll criteria obj :multi)))
(defn build-fields-subset [include exclude]
(let [make-map #(apply merge
(zipmap (remove map? %1) (repeat %2))
(filter map? %1))]
(if include
(make-map include 1)
(make-map exclude 0))))
(defn fetch
"Fetch a seq of documents that match a given criteria.
Accepts the following keywords:
:limit, maximum number of documents to return
:skip, where in the result set the seq will begin, i.e. paging
:include, which keys to include in the result set, can not be combined with :exclude
:exclude, which keys to exclude from the result set, can not be combined with :include
:sort, which keys to order by
:count, if true return the count of the result set, defaults to false"
[#^DBCollection coll criteria
& {:keys [limit skip include exclude sort count]
:or {count false}}]
(let [cursor (if criteria
(if (or include exclude)
(let [keys (build-fields-subset include exclude)]
(.find coll
#^DBObject (to-dbo criteria)
#^DBObject (to-dbo keys)))
(.find coll
#^DBObject (to-dbo criteria)))
(.find coll ))
cursor (if limit
(.limit cursor limit)
cursor)
cursor (if skip
(.skip cursor skip)
cursor)
cursor (if sort
(.sort cursor (to-dbo
(apply merge (reverse sort))))
cursor)]
(if count
(.count cursor)
(map to-clj cursor))))
(defn fetch-all
"Fetch all the documents of a collection. Same options as fetch."
[coll & options]
(apply fetch coll nil options))
(defn fetch-one
"Fetch one document of a collection. Supports same options as fetch except :limit and :count"
[coll criteria & options]
(first (apply fetch coll criteria options)))
(defn count-docs
"Returns the count of documents, optionally, matching a criteria"
([#^DBCollection coll]
(count-docs coll {}))
([#^DBCollection coll criteria]
(fetch coll criteria :count true)))
(defn fetch-by-id
"Fetch a document by :_id"
[#^DBCollection coll id]
(let [id (if (= ObjectId (class id)) id (ObjectId. id))]
(to-clj (fetch-one coll {:_id id}))))
(defn distinct-values
"Fetch a seq of the distinct values of a given collection for a key."
[#^DBCollection coll kw]
(set (.distinct coll (name kw))))
(defn group
"Fetch a seq of grouped items.
Example:
SQL: select a,b,sum(c) csum from coll where active=1 group by a,b
Karras: (group coll
[:a :b]
{:active 1}
{:csum 0}
\"function(obj,prev) { prev.csum += obj.c; }\")"
([#^DBCollection coll keys]
(group coll keys nil {:values []}
"function(obj,prev) {prev.values.push(obj)}"))
([#^DBCollection coll keys cond initial reduce]
(group coll keys cond initial reduce nil))
([#^DBCollection coll keys cond initial reduce finalize]
(let [cmd [:ns (.getName coll)
:key (zipmap (map name keys) (repeat true))
:cond cond
:initial initial
:$reduce reduce]
cmd (apply build-dbo (if finalize
(conj cmd :finalize finalize)
cmd))
response (.command (collection-db coll)
(to-dbo {:group cmd}))]
(.throwOnError response)
(to-clj (.get response "retval")))))
(defn- find-and-modify* [#^DBCollection coll criteria modifier remove sort return-new fields upsert]
(let [cmd [:findandmodify (.getName coll)
:query criteria
:sort (apply build-dbo (flatten (apply concat sort)))
:new return-new
:fields (build-fields-subset fields nil)
:upsert (boolean upsert)]
cmd (apply build-dbo (concat cmd
(if remove
[:remove true]
[:update modifier])
))
db (collection-db coll)
response (.command db cmd)]
(.throwOnError response)
(to-clj (.get response "value"))))
(defn find-and-modify
"See http://www.mongodb.org/display/DOCS/findandmodify+Command"
[#^DBCollection coll criteria modifier
& {:keys [sort return-new fields upsert]
:or {sort [] return-new true fields [] upsert false}}]
(find-and-modify* coll criteria modifier false sort return-new fields upsert))
(defn find-and-remove
"See http://www.mongodb.org/display/DOCS/findandmodify+Command"
[#^DBCollection coll criteria
& {:keys [sort return-new fields]
:or {sort [] return-new true fields []}}]
(find-and-modify* coll criteria nil true sort return-new fields false))
(defn map-reduce
"See http://www.mongodb.org/display/DOCS/MapReduce"
[#^DBCollection coll mapfn reducefn
& {:keys [query sort limit out keeptemp? finalize scope verbose?]
:or {sort [] keeptemp? false verbose? true}}]
(let [db (collection-db coll)
cmd (apply build-dbo
(concat [:mapreduce (.getName coll)]
[:map mapfn]
[:reduce reducefn]
[:keeptemp keeptemp?]
[:verbose verbose?]
(if query [:query query])
(if sort [:sort (apply build-dbo (flatten (apply concat sort)))])
(if limit [:limit limit])
(if out [:out out])
(if finalize [:finalize finalize])
(if scope [:scope scope])))
response (.command db (to-dbo cmd))
clj-response (to-clj response)
results-coll (collection db (:result clj-response))]
(.throwOnError response)
(assoc clj-response :fetch-fn (fn [& [criteria & options]]
(apply fetch results-coll criteria options)))))
(defn fetch-map-reduce-values
"Takes the result of map-reduce and fetches the values. Takes the same options as fetch."
[map-reduce-result & fetch-options]
(let [fetch-fn (:fetch-fn map-reduce-result)]
(apply fetch-fn fetch-options)))
(defvar map-reduce-fetch-all (comp fetch-map-reduce-values map-reduce)
"Composes map-reduce and fetch-map-reduce-values and returns all the results.
If you need to filter the results use:
(fetch-map-reduce-values (map-reduce ...) ...your fetch options...")
(defn delete
"Removes documents matching the supplied queries from a collection."
[#^DBCollection coll & queries]
(doseq [q queries]
(.remove coll (to-dbo q))))
(defn ensure-index
"Ensure an index exist on a collection."
([#^DBCollection coll fields]
(.ensureIndex coll (to-dbo fields)))
([#^DBCollection coll fields options]
(.ensureIndex coll (to-dbo fields) (to-dbo options))))
(defn ensure-unique-index
"Ensure a unique index exist on a collection."
[#^DBCollection coll #^String name fields]
(.ensureIndex coll
#^DBObject (to-dbo fields)
name
true))
(defn ensure-named-index
"Ensure an index exist on a collection with the given name."
[#^DBCollection coll #^String name fields]
(.ensureIndex coll
#^DBObject (to-dbo fields)
name))
(defn drop-index
""
[#^DBCollection coll o]
(.dropIndex coll #^DBObject (to-dbo o)))
(defn drop-index-named
""
[#^DBCollection coll kw]
(.dropIndex coll (name kw)))
(defn list-indexes
""
[#^DBCollection coll]
(map to-clj (.getIndexInfo coll)))