From 75bb240af96e3acd95cdcaa228b7f2d7125d752b Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 10 Jun 2012 12:35:52 +0400 Subject: [PATCH] Add monger.collection/system-collection?, a convenience function We don't add it to the change log because the majority of apps won't use it and it is not an important change between RC1 and RC2 (or what may even be 1.0). --- src/monger/collection.clj | 14 ++++++++++++++ test/monger/test/collection_test.clj | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 592720d..0eaa4fc 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -612,3 +612,17 @@ ;; this is what DBCollection#distinct does. Turning a blind eye! (.throwOnError res) (map #(from-db-object % true) (.get res "result")))) + + +;; +;; Misc +;; + +(def ^{:const true} + system-collection-pattern #"^(system|fs)") + +(defn- system-collection? + "Evaluates to true if the given collection name refers to a system collection. System collections + are prefixed with system. or fs. (default GridFS collection prefix)" + [^String coll-name] + (re-find system-collection-pattern coll-name)) diff --git a/test/monger/test/collection_test.clj b/test/monger/test/collection_test.clj index 27349b2..5644cc5 100644 --- a/test/monger/test/collection_test.clj +++ b/test/monger/test/collection_test.clj @@ -132,3 +132,22 @@ (mc/insert-batch collection batch) (is (= ["CA" "IL" "NY"] (sort (mc/distinct mg/*mongodb-database* collection :state {})))) (is (= ["CA" "NY"] (sort (mc/distinct collection :state {:price {$gt 100.00}})))))) + + +;; +;; miscellenous +;; + +(deftest test-system-collection-predicate + (are [name] (is (mc/system-collection? name)) + "system.indexes" + "system" + ;; we treat default GridFS collections as system ones, + ;; possibly this is a bad idea, time will tell. MK. + "fs.chunks" + "fs.files") + (are [name] (is (not (mc/system-collection? name))) + "events" + "accounts" + "megacorp_account" + "myapp_development"))