From 964526193bc56c68deba87745b71ed43e29317ae Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Thu, 16 Jul 2009 20:38:15 -0700 Subject: [PATCH] Optimized Model.count to run about 3x faster --- history.txt | 3 ++- lib/couchrest/mixins/design_doc.rb | 3 --- lib/couchrest/mixins/document_queries.rb | 4 +--- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/history.txt b/history.txt index 0035981..b434056 100644 --- a/history.txt +++ b/history.txt @@ -5,7 +5,8 @@ * Created an abstraction HTTP layer to support different http adapters (Matt Aimonetti) * Minor enhancements - + + * Optimized Model.count to run about 3x faster (Matt Aimonetti) * Added Float casting (Ryan Felton & Matt Aimonetti) == 0.30 diff --git a/lib/couchrest/mixins/design_doc.rb b/lib/couchrest/mixins/design_doc.rb index 4ed6fdf..661b6ef 100644 --- a/lib/couchrest/mixins/design_doc.rb +++ b/lib/couchrest/mixins/design_doc.rb @@ -37,9 +37,6 @@ module CouchRest if (doc['couchrest-type'] == '#{self.to_s}') { emit(null,1); } - }", - 'reduce' => "function(keys, values) { - return sum(values); }" } } diff --git a/lib/couchrest/mixins/document_queries.rb b/lib/couchrest/mixins/document_queries.rb index 3ea516c..defff3d 100644 --- a/lib/couchrest/mixins/document_queries.rb +++ b/lib/couchrest/mixins/document_queries.rb @@ -19,9 +19,7 @@ module CouchRest # equal to the name of the current class. Takes the standard set of # CouchRest::Database#view options def count(opts = {}, &block) - result = all({:reduce => true}.merge(opts), &block)['rows'] - return 0 if result.empty? - result.first['value'] + all({:raw => true, :limit => 0}.merge(opts), &block)['total_rows'] end # Load the first document that have the "couchrest-type" field equal to