Capped collections support

This commit is contained in:
Michael S. Klishin 2012-04-12 06:10:08 +04:00
parent d203f27479
commit 2c9cce8466
3 changed files with 52 additions and 1 deletions

View file

@ -204,7 +204,7 @@
(check-not-nil! id "id must not be nil")
(from-db-object ^DBObject (find-one-as-map collection { :_id id }) true))
([^String collection id fields]
(check-not-nil! id "id must not be nil")
(check-not-nil! id "id must not be nil")
(from-db-object ^DBObject (find-one-as-map collection { :_id id } fields) true))
([^String collection id fields keywordize]
(check-not-nil! id "id must not be nil")
@ -546,3 +546,14 @@
([^DB db ^String collection ^String key ^Map query]
(let [^DBCollection coll (.getCollection db collection)]
(.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)))

View 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)}))))

View file

@ -15,3 +15,5 @@
(defcleaner domains "domains")
(defcleaner pages "pages")
(defcleaner cached "cached")