Merge branch 'toby-commands'

This commit is contained in:
Michael S. Klishin 2012-02-21 15:06:29 +04:00
commit 60688e7482
3 changed files with 96 additions and 3 deletions

47
src/monger/command.clj Normal file
View file

@ -0,0 +1,47 @@
;; Copyright (c) 2011-2012 Michael S. Klishin
;; Copyright (c) 2012 Toby Hede
;;
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns monger.command
(:use [monger.conversion])
(:refer-clojure :exclude [find remove count drop distinct empty?])
(:import [com.mongodb Mongo DB DBObject CommandResult]
[java.util Map])
(:require [monger core]))
(defn collection-stats
([collection]
(collection-stats monger.core/*mongodb-database* collection))
([^DB database collection]
(monger.core/command database { :collstats collection })))
(defn db-stats
([]
(db-stats monger.core/*mongodb-database*))
([^DB database]
(monger.core/command database {:dbStats 1 })))
(defn reindex-collection
([collection]
(reindex-collection monger.core/*mongodb-database* collection))
([^DB database collection]
(monger.core/command database { :reIndex collection })))
(defn server-status
([]
(server-status monger.core/*mongodb-database*))
([^DB database]
(monger.core/command database {:serverStatus 1 })))
(defn top []
(monger.core/command (monger.core/get-db "admin") {:top 1 }))

View file

@ -176,9 +176,11 @@
For :distinct, :count, :drop, :dropIndexes, :mapReduce we suggest to use monger/collection #distinct, #count, #drop, #dropIndexes, :mapReduce respectively.
"
[^Map cmd]
(.command ^DB *mongodb-database* ^DBObject (to-db-object cmd)))
([^Map cmd]
(.command ^DB *mongodb-database* ^DBObject (to-db-object cmd)))
([^DB database ^Map cmd]
(.command ^DB database ^DBObject (to-db-object cmd)))
)
(defprotocol Countable
(count [this] "Returns size of the object"))

View file

@ -0,0 +1,44 @@
(ns monger.test.command
(:require [monger core command]
[monger.test.helper :as helper]
[monger.collection :as mgcol])
(:import (com.mongodb Mongo DB CommandResult))
(:use [clojure.test]))
(helper/connect!)
(deftest test-db-stats
(let [stats (monger.command/db-stats)]
(is (monger.result/ok? stats))
(is (= "monger-test" (get stats "db")))))
(deftest test-collection-stats
(let [collection "stat_test"
_ (mgcol/insert collection { :name "Clojure" })
check (mgcol/count collection)
stats (monger.command/collection-stats collection)]
(is (monger.result/ok? stats))
(is (= "monger-test.stat_test" (get stats "ns")))
(is (= check (get stats "count")))))
(deftest test-reindex-collection
(let [_ (mgcol/insert "test" { :name "Clojure" })
result (monger.command/reindex-collection "test")]
(is (monger.result/ok? result))
(is (get result "indexes"))))
(deftest test-server-status
(let [status (monger.command/server-status)]
(is (monger.result/ok? status))
(is (not-empty status))
(is (get status "serverUsed"))))
(deftest test-top
(let [result (monger.command/top)]
(is (monger.result/ok? result))
(is (not-empty result))
(is (get result "serverUsed"))))