Reduce monger.result to two functions: acknowledged? and
`updated-existing?` `ok?` and other variations are now all superceded by `acknowledged?`, `WriteConcern/ACKNOWLEDGED` is the recommended way of ensuring safe writes.
This commit is contained in:
parent
d9e6be671a
commit
8511d3714d
1 changed files with 8 additions and 53 deletions
|
|
@ -17,67 +17,22 @@
|
||||||
* http://clojuremongodb.info/articles/updating.html
|
* http://clojuremongodb.info/articles/updating.html
|
||||||
* http://clojuremongodb.info/articles/commands.html
|
* http://clojuremongodb.info/articles/commands.html
|
||||||
* http://clojuremongodb.info/articles/mapreduce.html"
|
* http://clojuremongodb.info/articles/mapreduce.html"
|
||||||
(:import [com.mongodb DBObject WriteResult MapReduceOutput]
|
(:import [com.mongodb WriteResult])
|
||||||
clojure.lang.IPersistentMap)
|
|
||||||
(:require monger.conversion))
|
(:require monger.conversion))
|
||||||
|
|
||||||
|
|
||||||
;;
|
|
||||||
;; Implementation
|
|
||||||
;;
|
|
||||||
|
|
||||||
(defn- okayish?
|
|
||||||
[value]
|
|
||||||
(contains? #{true "true" 1 1.0} value))
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; API
|
;; API
|
||||||
;;
|
;;
|
||||||
|
|
||||||
(defprotocol MongoCommandResult
|
(defprotocol WriteResultPredicates
|
||||||
(ok? [input] "Returns true if command result is a success")
|
(acknowledged? [input] "Returns true if write result is a success")
|
||||||
(has-error? [input] "Returns true if command result indicates an error")
|
(updated-existing? [input] "Returns true if write result has updated an existing document"))
|
||||||
(updated-existing? [input] "Returns true if command result has `updatedExisting` field set to true"))
|
|
||||||
|
|
||||||
(extend-protocol MongoCommandResult
|
|
||||||
DBObject
|
|
||||||
(ok?
|
|
||||||
[^DBObject result]
|
|
||||||
(okayish? (.get result "ok")))
|
|
||||||
(has-error?
|
|
||||||
[^DBObject result]
|
|
||||||
;; yes, this is exactly the logic MongoDB Java driver uses.
|
|
||||||
(> (count (str (.get result "err"))) 0))
|
|
||||||
(updated-existing?
|
|
||||||
[^DBObject result]
|
|
||||||
(let [v ^Boolean (.get result "updatedExisting")]
|
|
||||||
(and v (Boolean/valueOf v))))
|
|
||||||
|
|
||||||
|
|
||||||
|
(extend-protocol WriteResultPredicates
|
||||||
WriteResult
|
WriteResult
|
||||||
(ok?
|
(acknowledged?
|
||||||
[^WriteResult result]
|
[^WriteResult result]
|
||||||
(and (not (nil? result)) (ok? (.getLastError result))))
|
(.wasAcknowledged result))
|
||||||
(has-error?
|
|
||||||
[^WriteResult result]
|
|
||||||
(has-error? (.getLastError result)))
|
|
||||||
(updated-existing?
|
(updated-existing?
|
||||||
[^WriteResult result]
|
[^WriteResult result]
|
||||||
(updated-existing? (.getLastError result)))
|
(.isUpdateOfExisting result)))
|
||||||
|
|
||||||
MapReduceOutput
|
|
||||||
(ok?
|
|
||||||
[^MapReduceOutput result]
|
|
||||||
(ok? ^DBObject (.getRaw result)))
|
|
||||||
|
|
||||||
Boolean
|
|
||||||
(ok?
|
|
||||||
[^Boolean b]
|
|
||||||
(= Boolean/TRUE b))
|
|
||||||
|
|
||||||
IPersistentMap
|
|
||||||
(ok?
|
|
||||||
[^IPersistentMap m]
|
|
||||||
(okayish? (or (get m :ok)
|
|
||||||
(get m "ok")))))
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue