Skip to content

Commit

Permalink
Use mongo Collection#find_first where appropriate. Close cursor in fi…
Browse files Browse the repository at this point in the history
…nd_froom_ids.
  • Loading branch information
Jim Menard committed Feb 9, 2009
1 parent 50d5a6f commit 4bd5f91
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.rdoc
Expand Up @@ -13,7 +13,8 @@ database.yml file. See below for instructions.

== Installing

As noted above, this plugin requires the "mongo" Ruby Gem.
As noted above, this plugin requires the "mongo" Ruby Gem, version 0.5.4 or
higher.

To add this plugin to your Rails app, move (or link) this directory into your
Rails app's vendor/plugins directory and name it mongo_record. In other words,
Expand Down
6 changes: 5 additions & 1 deletion init.rb
Expand Up @@ -6,7 +6,11 @@
db_config = db_config[RAILS_ENV]

if db_config['adapter'] == 'mongo'
require 'mongo'
begin
require 'mongo >= 0.5.4'
rescue
require 'mongo'
end
require 'mongo_record/pk_factory'
require 'mongo_record'
$db = XGen::Mongo::Driver::Mongo.new(db_config['host'], db_config['port']).db(db_config['database'], :pk => MongoRecord::PKFactory.new)
Expand Down
14 changes: 10 additions & 4 deletions lib/mongo_record/active_record/base.rb
Expand Up @@ -176,7 +176,7 @@ def count_by_sql(sql)
# that needs to list both the number of posts and comments.
def increment_counter(counter_name, id)
sel = {:_id => id}
rec = collection.find(sel, :limit => 1).next_object
rec = collection.find_first(sel, :limit => 1)
raise "counter named \"#{counter_name}\" was not found" unless rec
rec[counter_name.to_s] += 1
collection.replace(sel, rec)
Expand All @@ -185,7 +185,7 @@ def increment_counter(counter_name, id)
# Works like increment_counter, but decrements instead.
def decrement_counter(counter_name, id)
sel = {:_id => id}
rec = collection.find(sel, :limit => 1).next_object
rec = collection.find_first(sel, :limit => 1)
raise "counter named \"#{counter_name}\" was not found" unless rec
rec[counter_name.to_s] -= 1
collection.replace(sel, rec)
Expand Down Expand Up @@ -230,7 +230,7 @@ def sanitize(object) #:nodoc:
def find_initial(options)
criteria = criteria_from(options[:conditions]).merge(where_func(options[:where]))
fields = fields_from(options[:select])
row = collection.find(criteria, :fields => fields, :limit => 1).next_object
row = collection.find_first(criteria, :fields => fields, :limit => 1)
(row.nil? || row['_id'] == nil) ? nil : self.send(:instantiate, row)
end

Expand All @@ -247,7 +247,13 @@ def find_from_ids(ids, options)
criteria[:_id] = ids_clause(ids)
options = rails_to_mongo_find_options(options)
db_cursor = collection.find(criteria, options)
ids.length == 1 ? instantiate(db_cursor.next_object) : MongoRecord::Cursor.new(db_cursor, self)
if ids.length == 1
row = db_cursor.next_object
db_cursor.close
instantiate(row)
else
MongoRecord::Cursor.new(db_cursor, self)
end
end

def ids_clause(ids)
Expand Down

0 comments on commit 4bd5f91

Please sign in to comment.