Hints are now BSON objects (probably ordered hashes) instead of arrays. Also make sure index exists for hint field in test.

This commit is contained in:
Jim Menard 2009-01-30 14:35:22 -05:00
parent 26738f30d4
commit 6532dd7d9b
5 changed files with 21 additions and 21 deletions

View File

@ -30,9 +30,9 @@ module XGen
@name = name @name = name
end end
# Set hint fields to use and return +self+. hint may be a # Set hint fields to use and return +self+. hint may be a single field
# single field name, array of field names, or a hash whose keys will # name, array of field names, or a hash (preferably an OrderedHash).
# become the hint field names. May be +nil+. # May be +nil+.
def hint=(hint) def hint=(hint)
@hint = normalize_hint_fields(hint) @hint = normalize_hint_fields(hint)
self self
@ -59,7 +59,7 @@ module XGen
if hint if hint
hint = normalize_hint_fields(hint) hint = normalize_hint_fields(hint)
else else
hint = @hint hint = @hint # assumed to be normalized already
end end
raise RuntimeError, "Unknown options [#{options.inspect}]" unless options.empty? raise RuntimeError, "Unknown options [#{options.inspect}]" unless options.empty?
@db.query(self, Query.new(selector, fields, offset, limit, sort, hint)) @db.query(self, Query.new(selector, fields, offset, limit, sort, hint))
@ -157,13 +157,15 @@ module XGen
def normalize_hint_fields(hint) def normalize_hint_fields(hint)
case hint case hint
when String when String
[hint] {hint => 1}
when Hash when Hash
hint.keys hint
when nil when nil
nil nil
else else
hint.to_a h = OrderedHash.new
hint.to_a.each { |k| h[k] = 1 }
h
end end
end end
end end

View File

@ -44,14 +44,8 @@ module XGen
raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash" raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash"
end end
end end
if query.hint && query.hint.length > 0 sel['$hint'] = query.hint if query.hint && query.hint.length > 0
hints = OrderedHash.new sel['$explain'] = true if query.explain
query.hint.each { |hf| hints[hf] = 1 }
sel['$hint'] = hints
end
if query.explain
sel['$explain'] = true
end
end end
write_doc(sel) write_doc(sel)

View File

@ -28,7 +28,7 @@ module XGen
attr_accessor :number_to_skip, :number_to_return, :order_by attr_accessor :number_to_skip, :number_to_return, :order_by
# If true, $explain will be set in QueryMessage that uses this query. # If true, $explain will be set in QueryMessage that uses this query.
attr_accessor :explain attr_accessor :explain
# Either +nil+ or an array of hint field names. # Either +nil+ or a hash (preferably an OrderedHash).
attr_accessor :hint attr_accessor :hint
attr_reader :selector # writer defined below attr_reader :selector # writer defined below
@ -59,7 +59,8 @@ module XGen
# is not preserved. (order_by is called :sort in calls to # is not preserved. (order_by is called :sort in calls to
# Collection#find.) # 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. # Collection#hint.
def initialize(sel={}, return_fields=nil, number_to_skip=0, number_to_return=0, order_by=nil, hint=nil) 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 = @number_to_skip, @number_to_return, @order_by, @hint =

View File

@ -1,6 +1,6 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = 'mongo' s.name = 'mongo'
s.version = '0.4.0' s.version = '0.4.1'
s.platform = Gem::Platform::RUBY s.platform = Gem::Platform::RUBY
s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB' 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.' s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'

View File

@ -373,6 +373,7 @@ class DBAPITest < Test::Unit::TestCase
end end
def test_hint def test_hint
@coll.create_index('test_a_index', 'a')
begin begin
assert_nil @coll.hint assert_nil @coll.hint
assert_equal 1, @coll.find({'a' => 1}, :hint => 'a').to_a.size 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 assert_equal 1, @coll.find({'a' => 1}, :hint => {'a' => 1}).to_a.size
@coll.hint = 'a' @coll.hint = 'a'
assert_equal ['a'], @coll.hint assert_equal({'a' => 1}, @coll.hint)
assert_equal 1, @coll.find('a' => 1).to_a.size assert_equal 1, @coll.find('a' => 1).to_a.size
@coll.hint = ['a'] @coll.hint = ['a']
assert_equal ['a'], @coll.hint assert_equal({'a' => 1}, @coll.hint)
assert_equal 1, @coll.find('a' => 1).to_a.size assert_equal 1, @coll.find('a' => 1).to_a.size
@coll.hint = {'a' => 1} @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 assert_equal 1, @coll.find('a' => 1).to_a.size
@coll.hint = nil @coll.hint = nil
@ -396,6 +397,8 @@ class DBAPITest < Test::Unit::TestCase
assert_equal 1, @coll.find('a' => 1).to_a.size assert_equal 1, @coll.find('a' => 1).to_a.size
rescue => ex rescue => ex
fail ex.to_s fail ex.to_s
ensure
@coll.drop_index('test_a_index')
end end
end end