From d982d27caaa46596527420c951e4eb5f73d441df Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Tue, 21 Feb 2012 13:01:42 +1100 Subject: [PATCH 1/4] core/command accepts db as explicit parameter option --- src/monger/core.clj | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/monger/core.clj b/src/monger/core.clj index 7ee65b0..b248411 100644 --- a/src/monger/core.clj +++ b/src/monger/core.clj @@ -167,9 +167,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")) From f36528b592514d3208cefa78ad5d33c12f1250e4 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Tue, 21 Feb 2012 13:02:01 +1100 Subject: [PATCH 2/4] 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")))) From 219c9fa41c122077bb2d0075fd48154d949447c7 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Tue, 21 Feb 2012 13:07:25 +1100 Subject: [PATCH 3/4] temporarily remove reindex collection due to strange CI error --- test/monger/test/command.clj | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/monger/test/command.clj b/test/monger/test/command.clj index 5043828..ea96e80 100644 --- a/test/monger/test/command.clj +++ b/test/monger/test/command.clj @@ -23,11 +23,10 @@ (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-reindex-collection +; (let [result (monger.command/reindex-collection "test")] +; (is (monger.result/ok? result)) +; (is (get result "indexes")))) (deftest test-server-status From 9cc2d6dd198610b4f376f8456a566d6f15ff6f95 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Tue, 21 Feb 2012 13:13:20 +1100 Subject: [PATCH 4/4] insert into collection before re-index just to see if that makes a diff --- test/monger/test/command.clj | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/monger/test/command.clj b/test/monger/test/command.clj index ea96e80..749ec76 100644 --- a/test/monger/test/command.clj +++ b/test/monger/test/command.clj @@ -23,10 +23,11 @@ (is (= check (get stats "count"))))) -; (deftest test-reindex-collection -; (let [result (monger.command/reindex-collection "test")] -; (is (monger.result/ok? result)) -; (is (get result "indexes")))) +(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