From 6532dd7d9bfbb92f5dce82c5eda50d7a3ff36685 Mon Sep 17 00:00:00 2001 From: Jim Menard Date: Fri, 30 Jan 2009 14:35:22 -0500 Subject: [PATCH] Hints are now BSON objects (probably ordered hashes) instead of arrays. Also make sure index exists for hint field in test. --- lib/mongo/collection.rb | 16 +++++++++------- lib/mongo/message/query_message.rb | 10 ++-------- lib/mongo/query.rb | 5 +++-- mongo-ruby-driver.gemspec | 2 +- tests/test_db_api.rb | 9 ++++++--- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 194b293..d7b277a 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -30,9 +30,9 @@ module XGen @name = name end - # Set hint fields to use and return +self+. hint may be a - # single field name, array of field names, or a hash whose keys will - # become the hint field names. May be +nil+. + # Set hint fields to use and return +self+. hint may be a single field + # name, array of field names, or a hash (preferably an OrderedHash). + # May be +nil+. def hint=(hint) @hint = normalize_hint_fields(hint) self @@ -59,7 +59,7 @@ module XGen if hint hint = normalize_hint_fields(hint) else - hint = @hint + hint = @hint # assumed to be normalized already end raise RuntimeError, "Unknown options [#{options.inspect}]" unless options.empty? @db.query(self, Query.new(selector, fields, offset, limit, sort, hint)) @@ -157,13 +157,15 @@ module XGen def normalize_hint_fields(hint) case hint when String - [hint] + {hint => 1} when Hash - hint.keys + hint when nil nil else - hint.to_a + h = OrderedHash.new + hint.to_a.each { |k| h[k] = 1 } + h end end end diff --git a/lib/mongo/message/query_message.rb b/lib/mongo/message/query_message.rb index d070327..2e8bb1e 100644 --- a/lib/mongo/message/query_message.rb +++ b/lib/mongo/message/query_message.rb @@ -44,14 +44,8 @@ module XGen raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash" end end - if query.hint && query.hint.length > 0 - hints = OrderedHash.new - query.hint.each { |hf| hints[hf] = 1 } - sel['$hint'] = hints - end - if query.explain - sel['$explain'] = true - end + sel['$hint'] = query.hint if query.hint && query.hint.length > 0 + sel['$explain'] = true if query.explain end write_doc(sel) diff --git a/lib/mongo/query.rb b/lib/mongo/query.rb index ed8ceb7..ee84e83 100644 --- a/lib/mongo/query.rb +++ b/lib/mongo/query.rb @@ -28,7 +28,7 @@ module XGen attr_accessor :number_to_skip, :number_to_return, :order_by # If true, $explain will be set in QueryMessage that uses this query. attr_accessor :explain - # Either +nil+ or an array of hint field names. + # Either +nil+ or a hash (preferably an OrderedHash). attr_accessor :hint attr_reader :selector # writer defined below @@ -59,7 +59,8 @@ module XGen # is not preserved. (order_by is called :sort in calls to # Collection#find.) # - # hint :: If not +nil+, specifies query hint fields. See + # hint :: If not +nil+, specifies query hint fields. Must be either + # +nil+ or a hash (preferably an OrderedHash). See # Collection#hint. def initialize(sel={}, return_fields=nil, number_to_skip=0, number_to_return=0, order_by=nil, hint=nil) @number_to_skip, @number_to_return, @order_by, @hint = diff --git a/mongo-ruby-driver.gemspec b/mongo-ruby-driver.gemspec index 5b4dc34..22e2ce7 100644 --- a/mongo-ruby-driver.gemspec +++ b/mongo-ruby-driver.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'mongo' - s.version = '0.4.0' + s.version = '0.4.1' s.platform = Gem::Platform::RUBY s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB' s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.' diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 307270f..b0bbc87 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -373,6 +373,7 @@ class DBAPITest < Test::Unit::TestCase end def test_hint + @coll.create_index('test_a_index', 'a') begin assert_nil @coll.hint assert_equal 1, @coll.find({'a' => 1}, :hint => 'a').to_a.size @@ -380,15 +381,15 @@ class DBAPITest < Test::Unit::TestCase assert_equal 1, @coll.find({'a' => 1}, :hint => {'a' => 1}).to_a.size @coll.hint = 'a' - assert_equal ['a'], @coll.hint + assert_equal({'a' => 1}, @coll.hint) assert_equal 1, @coll.find('a' => 1).to_a.size @coll.hint = ['a'] - assert_equal ['a'], @coll.hint + assert_equal({'a' => 1}, @coll.hint) assert_equal 1, @coll.find('a' => 1).to_a.size @coll.hint = {'a' => 1} - assert_equal ['a'], @coll.hint + assert_equal({'a' => 1}, @coll.hint) assert_equal 1, @coll.find('a' => 1).to_a.size @coll.hint = nil @@ -396,6 +397,8 @@ class DBAPITest < Test::Unit::TestCase assert_equal 1, @coll.find('a' => 1).to_a.size rescue => ex fail ex.to_s + ensure + @coll.drop_index('test_a_index') end end