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
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

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"
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)

View File

@ -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 =

View File

@ -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.'

View File

@ -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