From 90daa7c9671f1030c5703e4d166bdd4d2baf0902 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 14 Aug 2011 22:36:54 +0400 Subject: [PATCH] Implement partial document fetching for monger.collection/find-by-id --- src/monger/collection.clj | 64 +++++++++++++++++---------------- test/monger/test/collection.clj | 14 ++++---- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index e7d9ae4..ee4120e 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -1,26 +1,16 @@ ;; Copyright (c) 2011 Michael S. Klishin ;; -;; Permission is hereby granted, free of charge, to any person obtaining a copy -;; of this software and associated documentation files (the "Software"), to deal -;; in the Software without restriction, including without limitation the rights -;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -;; copies of the Software, and to permit persons to whom the Software is -;; furnished to do so, subject to the following conditions: -;; -;; The above copyright notice and this permission notice shall be included in -;; all copies or substantial portions of the Software. -;; -;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -;; THE SOFTWARE. +;; 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.collection - (:import (com.mongodb Mongo DB WriteResult DBObject WriteConcern DBCursor) (java.util List Map)) - (:require [monger core convertion errors])) + (:import (com.mongodb Mongo DB DBCollection WriteResult DBObject WriteConcern DBCursor) (java.util List Map)) + (:require [monger core errors]) + (:use [monger.convertion])) ;; ;; API @@ -30,30 +20,41 @@ (defn ^WriteResult insert ([^String collection, ^DBObject doc] - (.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object doc) monger.core/*mongodb-write-concern*)) + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.insert coll (to-db-object doc) monger.core/*mongodb-write-concern*))) ([^String collection, ^DBObject doc, ^WriteConcern concern] - (.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object doc) concern))) + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.insert coll (to-db-object doc) concern)))) (defn ^WriteResult insert-batch ([^String collection, ^List docs] - (.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object docs) WriteConcern/NORMAL)) + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.insert coll (to-db-object docs) WriteConcern/NORMAL))) ([^String collection, ^List docs, ^WriteConcern concern] - (.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object docs) concern))) + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.insert coll (to-db-object docs) concern)))) ;; monger.collection/find (defn ^DBCursor find ([^String collection] - (.find (.getCollection monger.core/*mongodb-database* collection))) + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.find coll))) ([^String collection, ^Map ref] - (.find (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object ref))) + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.find coll (to-db-object ref)))) ) (defn ^DBObject find-by-id ([^String collection, ^String id] - (.findOne (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object { :_id id }))) + (.findOne (.getCollection monger.core/*mongodb-database* collection) (to-db-object { :_id id }))) + ([^String collection, ^String id, ^List fields] + (let [n (count fields) + ones (replicate 10 1) + map-of-fields (zipmap fields ones)] + (.findOne (.getCollection monger.core/*mongodb-database* collection) (to-db-object { :_id id }) (to-db-object map-of-fields)))) ) @@ -63,7 +64,8 @@ ;; monger.collection/count (defn ^long count [^String collection] - (.count (.getCollection monger.core/*mongodb-database* collection))) + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.count coll))) ;; monger.collection/update ;; monger.collection/update-multi @@ -71,9 +73,11 @@ (defn ^WriteResult remove ([^String collection] - (.remove (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object {}))) - ([^String collection, ^DBObject conditions] - (.remove (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object conditions))) + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.remove coll (to-db-object {})))) + ([^String collection, ^Map conditions] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.remove coll (to-db-object conditions)))) ) ;; monger.collection/ensure-index diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index c9d3162..2dcfe6f 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -93,10 +93,10 @@ (monger.collection/insert collection doc) (is (= (doc (monger.collection/find-by-id collection doc-id)))))) -;; (deftest find-partial-document-by-id-when-document-exists -;; (let [collection "libraries" -;; doc-id (monger.util/random-str 140 16) -;; doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }] -;; (monger.collection/remove collection) -;; (monger.collection/insert collection doc) -;; (is (= (doc (monger.collection/find-by-id collection doc-id [])))))) +(deftest find-partial-document-by-id-when-document-exists + (let [collection "libraries" + doc-id (monger.util/random-str 140 16) + doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }] + (monger.collection/remove collection) + (monger.collection/insert collection doc) + (is (= ({ :language "Clojure" } (monger.collection/find-by-id collection doc-id [ :language ]))))))