mongodb-mongo 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,27 +23,18 @@ module XGen
23
23
  # A named collection of records in a database.
24
24
  class Collection
25
25
 
26
- attr_reader :db, :name, :hint_fields
26
+ attr_reader :db, :name, :hint
27
27
 
28
28
  def initialize(db, name)
29
29
  @db = db
30
30
  @name = name
31
31
  end
32
32
 
33
- # Set hint fields to use and return +self+. hint_fields may be a
33
+ # Set hint fields to use and return +self+. hint may be a
34
34
  # single field name, array of field names, or a hash whose keys will
35
35
  # become the hint field names. May be +nil+.
36
- def hint(hint_fields)
37
- @hint_fields = case hint_fields
38
- when String
39
- [hint_fields]
40
- when Hash
41
- hint_fields.keys
42
- when nil
43
- nil
44
- else
45
- hint_fields.to_a
46
- end
36
+ def hint=(hint)
37
+ @hint = normalize_hint_fields(hint)
47
38
  self
48
39
  end
49
40
 
@@ -57,14 +48,21 @@ module XGen
57
48
  # :sort :: Either hash of field names as keys and 1/-1 as values; 1 ==
58
49
  # ascending, -1 == descending, or array of field names (all
59
50
  # assumed to be sorted in ascending order).
51
+ # :hint :: See #hint. This option overrides the collection-wide value.
60
52
  def find(selector={}, options={})
61
53
  fields = options.delete(:fields)
62
54
  fields = nil if fields && fields.empty?
63
55
  offset = options.delete(:offset) || 0
64
56
  limit = options.delete(:limit) || 0
65
57
  sort = options.delete(:sort)
58
+ hint = options.delete(:hint)
59
+ if hint
60
+ hint = normalize_hint_fields(hint)
61
+ else
62
+ hint = @hint
63
+ end
66
64
  raise RuntimeError, "Unknown options [#{options.inspect}]" unless options.empty?
67
- @db.query(self, Query.new(selector, fields, offset, limit, sort))
65
+ @db.query(self, Query.new(selector, fields, offset, limit, sort, hint))
68
66
  end
69
67
 
70
68
  # Insert +objects+, which are hashes. "<<" is aliased to this method.
@@ -154,6 +152,20 @@ module XGen
154
152
  @db.count(@name, selector || {})
155
153
  end
156
154
 
155
+ protected
156
+
157
+ def normalize_hint_fields(hint)
158
+ case hint
159
+ when String
160
+ [hint]
161
+ when Hash
162
+ hint.keys
163
+ when nil
164
+ nil
165
+ else
166
+ hint.to_a
167
+ end
168
+ end
157
169
  end
158
170
  end
159
171
  end
data/lib/mongo/cursor.rb CHANGED
@@ -42,24 +42,6 @@ module XGen
42
42
 
43
43
  def closed?; @closed; end
44
44
 
45
- # Set hint fields to use and return +self+. hint_fields may be a
46
- # single field name, array of field names, or a hash whose keys will
47
- # become the hint field names. May be +nil+. If no hint fields are
48
- # specified, the ones in the collection are used if they exist.
49
- def hint(hint_fields)
50
- @hint_fields = case hint_fields
51
- when String
52
- [hint_fields]
53
- when Hash
54
- hint_fields.keys
55
- when nil
56
- nil
57
- else
58
- hint_fields.to_a
59
- end
60
- self
61
- end
62
-
63
45
  # Return +true+ if there are more records to retrieve. We do not check
64
46
  # @num_to_return; #each is responsible for doing that.
65
47
  def more?
@@ -223,12 +205,8 @@ module XGen
223
205
  def send_query_if_needed
224
206
  # Run query first time we request an object from the wire
225
207
  unless @query_run
226
- hints = @hint_fields || @collection.hint_fields
227
- old_hints = @query.hint_fields
228
- @query.hint_fields = hints
229
208
  @db.send_query_message(QueryMessage.new(@db.name, @collection.name, @query))
230
209
  @query_run = true
231
- @query.hint_fields = old_hints
232
210
  read_all
233
211
  end
234
212
  end
@@ -44,9 +44,9 @@ module XGen
44
44
  raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash"
45
45
  end
46
46
  end
47
- if query.hint_fields && query.hint_fields.length > 0
47
+ if query.hint && query.hint.length > 0
48
48
  hints = OrderedHash.new
49
- query.hint_fields.each { |hf| hints[hf] = 1 }
49
+ query.hint.each { |hf| hints[hf] = 1 }
50
50
  sel['$hint'] = hints
51
51
  end
52
52
  if query.explain
data/lib/mongo/query.rb CHANGED
@@ -29,7 +29,7 @@ module XGen
29
29
  # If true, $explain will be set in QueryMessage that uses this query.
30
30
  attr_accessor :explain
31
31
  # Either +nil+ or an array of hint field names.
32
- attr_accessor :hint_fields
32
+ attr_accessor :hint
33
33
  attr_reader :selector # writer defined below
34
34
 
35
35
  # sel :: A hash describing the query. See the Mongo docs for details.
@@ -58,8 +58,12 @@ module XGen
58
58
  # probably will not be what you intend because key order
59
59
  # is not preserved. (order_by is called :sort in calls to
60
60
  # Collection#find.)
61
- def initialize(sel={}, return_fields=nil, number_to_skip=0, number_to_return=0, order_by=nil)
62
- @number_to_skip, @number_to_return, @order_by = number_to_skip, number_to_return, order_by
61
+ #
62
+ # hint :: If not +nil+, specifies query hint fields. See
63
+ # Collection#hint.
64
+ def initialize(sel={}, return_fields=nil, number_to_skip=0, number_to_return=0, order_by=nil, hint=nil)
65
+ @number_to_skip, @number_to_return, @order_by, @hint =
66
+ number_to_skip, number_to_return, order_by, hint
63
67
  self.selector = sel
64
68
  self.fields = return_fields
65
69
  end
@@ -102,7 +106,7 @@ module XGen
102
106
  end
103
107
 
104
108
  def contains_special_fields
105
- (@order_by != nil && @order_by.length > 0) || @explain || @hint_fields
109
+ (@order_by != nil && @order_by.length > 0) || @explain || @hint
106
110
  end
107
111
  end
108
112
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mongo'
3
- s.version = '0.3.1'
3
+ s.version = '0.3.2'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB'
6
6
  s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
data/tests/test_cursor.rb CHANGED
@@ -54,13 +54,4 @@ class CursorTest < Test::Unit::TestCase
54
54
  end
55
55
  end
56
56
 
57
- def test_hint
58
- begin
59
- cursor = @coll.find('a' => 1).hint('a')
60
- assert_equal 1, cursor.to_a.size
61
- rescue => ex
62
- fail ex.to_s
63
- end
64
- end
65
-
66
57
  end
data/tests/test_db.rb CHANGED
@@ -62,16 +62,17 @@ class DBTest < Test::Unit::TestCase
62
62
  coll = db.collection('test')
63
63
  coll.clear
64
64
 
65
+ # new id gets added to returned object
65
66
  obj = coll.insert('name' => 'Fred', 'age' => 42)
66
67
  row = coll.find({'name' => 'Fred'}, :limit => 1).next_object
67
- oid = row.delete('_id')
68
+ oid = row['_id']
68
69
  assert_not_nil oid
69
70
  assert_equal obj, row
70
71
 
71
72
  oid = XGen::Mongo::Driver::ObjectID.new
72
73
  obj = coll.insert('_id' => oid, 'name' => 'Barney', 'age' => 41)
73
74
  row = coll.find({'name' => 'Barney'}, :limit => 1).next_object
74
- db_oid = row.delete('_id')
75
+ db_oid = row['_id']
75
76
  assert_equal oid, db_oid
76
77
  assert_equal obj, row
77
78
 
data/tests/test_db_api.rb CHANGED
@@ -372,4 +372,31 @@ class DBAPITest < Test::Unit::TestCase
372
372
  assert_equal "#{@db.host}:#{@db.port}", @db.master
373
373
  end
374
374
 
375
+ def test_hint
376
+ begin
377
+ assert_nil @coll.hint
378
+ assert_equal 1, @coll.find({'a' => 1}, :hint => 'a').to_a.size
379
+ assert_equal 1, @coll.find({'a' => 1}, :hint => ['a']).to_a.size
380
+ assert_equal 1, @coll.find({'a' => 1}, :hint => {'a' => 1}).to_a.size
381
+
382
+ @coll.hint = 'a'
383
+ assert_equal ['a'], @coll.hint
384
+ assert_equal 1, @coll.find('a' => 1).to_a.size
385
+
386
+ @coll.hint = ['a']
387
+ assert_equal ['a'], @coll.hint
388
+ assert_equal 1, @coll.find('a' => 1).to_a.size
389
+
390
+ @coll.hint = {'a' => 1}
391
+ assert_equal ['a'], @coll.hint
392
+ assert_equal 1, @coll.find('a' => 1).to_a.size
393
+
394
+ @coll.hint = nil
395
+ assert_nil @coll.hint
396
+ assert_equal 1, @coll.find('a' => 1).to_a.size
397
+ rescue => ex
398
+ fail ex.to_s
399
+ end
400
+ end
401
+
375
402
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb-mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Menard