Capped collections support
This commit is contained in:
parent
d203f27479
commit
2c9cce8466
3 changed files with 52 additions and 1 deletions
|
|
@ -546,3 +546,14 @@
|
||||||
([^DB db ^String collection ^String key ^Map query]
|
([^DB db ^String collection ^String key ^Map query]
|
||||||
(let [^DBCollection coll (.getCollection db collection)]
|
(let [^DBCollection coll (.getCollection db collection)]
|
||||||
(.distinct coll ^String (to-db-object key) ^DBObject (to-db-object query)))))
|
(.distinct coll ^String (to-db-object key) ^DBObject (to-db-object query)))))
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; create/capped collections
|
||||||
|
;;
|
||||||
|
|
||||||
|
(defn create
|
||||||
|
"Creates a collection. Options are: :capped (pass true to create a capped collection), :max (number of documents)
|
||||||
|
and :size (max allowed size of the collection, in bytes)."
|
||||||
|
[^String name options]
|
||||||
|
(.createCollection ^DB monger.core/*mongodb-database* name (to-db-object options)))
|
||||||
|
|
|
||||||
38
test/monger/test/capped_collections_test.clj
Normal file
38
test/monger/test/capped_collections_test.clj
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
(set! *warn-on-reflection* true)
|
||||||
|
|
||||||
|
(ns monger.test.capped-collections-test
|
||||||
|
(:require [monger core util]
|
||||||
|
[monger.collection :as mc]
|
||||||
|
[monger.result :as mres]
|
||||||
|
[monger.test.helper :as helper])
|
||||||
|
(:use clojure.test
|
||||||
|
monger.operators
|
||||||
|
monger.test.fixtures))
|
||||||
|
|
||||||
|
|
||||||
|
(helper/connect!)
|
||||||
|
|
||||||
|
(use-fixtures :each purge-cached)
|
||||||
|
|
||||||
|
(defn- megabytes
|
||||||
|
[^long n]
|
||||||
|
(* n 1024 1024))
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; Tests
|
||||||
|
;;
|
||||||
|
|
||||||
|
(deftest test-inserting-into-capped-collection
|
||||||
|
(let [n 1000
|
||||||
|
cname "cached"
|
||||||
|
_ (mc/drop cname)
|
||||||
|
coll (mc/create cname {:capped true :size (-> 16 megabytes) :max n})]
|
||||||
|
(is (= cname (.getName coll)))
|
||||||
|
(mc/insert-batch cname (for [i (range 0 (+ n 100))] {:i i}))
|
||||||
|
(is (= n (mc/count cname)))
|
||||||
|
;; older elements get replaced by newer ones
|
||||||
|
(is (not (mc/any? cname {:i 1})))
|
||||||
|
(is (not (mc/any? cname {:i 5})))
|
||||||
|
(is (not (mc/any? cname {:i 9})))
|
||||||
|
(is (mc/any? cname {:i (+ n 80)}))))
|
||||||
|
|
@ -15,3 +15,5 @@
|
||||||
(defcleaner domains "domains")
|
(defcleaner domains "domains")
|
||||||
(defcleaner pages "pages")
|
(defcleaner pages "pages")
|
||||||
|
|
||||||
|
(defcleaner cached "cached")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue