From f36528b592514d3208cefa78ad5d33c12f1250e4 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Tue, 21 Feb 2012 13:02:01 +1100 Subject: [PATCH] command namespace and basic commands --- src/monger/command.clj | 47 ++++++++++++++++++++++++++++++++++++ test/monger/test/command.clj | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 src/monger/command.clj create mode 100644 test/monger/test/command.clj diff --git a/src/monger/command.clj b/src/monger/command.clj new file mode 100644 index 0000000..7b0fcf5 --- /dev/null +++ b/src/monger/command.clj @@ -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 })) diff --git a/test/monger/test/command.clj b/test/monger/test/command.clj new file mode 100644 index 0000000..5043828 --- /dev/null +++ b/test/monger/test/command.clj @@ -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-re-index-collection + (let [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"))))