Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use defer_as instead of to_ for Deferrables. #30

Merged
merged 1 commit into from
Sep 13, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ For operations that require IO, em-mongo always returns an EventMachine deferrab
#most cursor methods return an EM::Mongo::RequestResponse,
#which is an EventMachine::Deferrable

resp = cursor.to_a
resp = cursor.defer_as_a

#when em-mongo IO methods succeed, they
#will always call back with the return
Expand Down Expand Up @@ -85,11 +85,11 @@ em-mongo will present errors in two different ways. First, em-mongo will raise e

Errors returned by the database, or errors communicating with the database, will be delivered via standard EM::Deferrable errbacks. While it is tempting to subscribe just to a callback

my_colletion.find.to_a.callback {|docs| ... }
my_colletion.find.defer_as_a.callback {|docs| ... }

in the case of an error you will never receive a response. If you are waiting for a response before your program continues, you will be waiting a very long time. A better approach would be to store the deferrable into a variable and subscribe to its callback and errback

resp = my_collection.find.to_a
resp = my_collection.find.defer_as_a
resp.callback { |docs| ... }
resp.errback { |err| raise *err }

Expand Down Expand Up @@ -136,13 +136,13 @@ to this

my_collection.first().callback { |doc| p doc }

EM::Mongo::Collection#find now returns a cursor, not an array, to maintain compatibility with the mongo-ruby-driver. This provides a great deal more flexibility, but requires you to select a specific cursor method to actually fetch data from the server, such as #to_a or #next
EM::Mongo::Collection#find now returns a cursor, not an array, to maintain compatibility with the mongo-ruby-driver. This provides a great deal more flexibility, but requires you to select a specific cursor method to actually fetch data from the server, such as #defer_as_a or #next

my_collection.find() { |docs| ... }

becomes

my_collection.find.to_a.callback { |docs| ... }
my_collection.find.defer_as_a.callback { |docs| ... }

If for some reason you aren't ready to upgrade your project but you want to be able to use the newer gem, you can require a compatibility file that will revert the new API to the API found in 0.3.6

Expand Down
7 changes: 5 additions & 2 deletions lib/em-mongo/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def each(&blk)
# efficient to retrieve documents as you need them by iterating over the cursor.
#
# @return [EM::Mongo::RequestResponse] Calls back with an array of documents.
def to_a
def defer_as_a
response = RequestResponse.new
items = []
self.each do |doc,err|
Expand All @@ -314,6 +314,9 @@ def to_a
response
end

# XXX to_a is confusing but we will leave it for now
alias to_a defer_as_a

# Get the explain plan for this cursor.
#
# @return [EM::Mongo::RequestResponse] Calls back with a document containing the explain plan for this cursor.
Expand Down Expand Up @@ -534,4 +537,4 @@ def check_modifiable
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/em-mongo/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def name
# @return [EM::Mongo::RequestResponse]
def collection_names
response = RequestResponse.new
name_resp = collections_info.to_a
name_resp = collections_info.defer_as_a
name_resp.callback do |docs|
names = docs.collect{ |doc| doc['name'] || '' }
names = names.delete_if {|name| name.index(self.name).nil? || name.index('$')}
Expand Down Expand Up @@ -200,7 +200,7 @@ def drop_index(collection_name, index_name)
def index_information(collection_name)
response = RequestResponse.new
sel = {:ns => full_collection_name(collection_name)}
idx_resp = Cursor.new(self.collection(SYSTEM_INDEX_COLLECTION), :selector => sel).to_a
idx_resp = Cursor.new(self.collection(SYSTEM_INDEX_COLLECTION), :selector => sel).defer_as_a
idx_resp.callback do |indexes|
info = indexes.inject({}) do |info, index|
info[index['name']] = index
Expand Down
6 changes: 3 additions & 3 deletions lib/em-mongo/prev.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Collection
def find(selector={}, opts={}, &blk)
raise "find requires a block" if not block_given?

new_find(selector, opts).to_a.callback do |docs|
new_find(selector, opts).defer_as_a.callback do |docs|
blk.call(docs)
end
end
Expand Down Expand Up @@ -38,7 +38,7 @@ def delete(collection_name, selector)

def find(collection_name, skip, limit, order, query, fields, &blk)
db_name, col_name = db_and_col_name(collection_name)
db(db_name).collection(col_name).find(query, :skip=>skip,:limit=>limit,:order=>order,:fields=>fields).to_a.callback do |docs|
db(db_name).collection(col_name).find(query, :skip=>skip,:limit=>limit,:order=>order,:fields=>fields).defer_as_a.callback do |docs|
yield docs if block_given?
end
end
Expand All @@ -50,4 +50,4 @@ def db_and_col_name(full_name)

end
end
end
end
32 changes: 16 additions & 16 deletions spec/integration/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@conn, @coll = connection_and_collection

@coll.insert("hello" => 'world')
@coll.find({"hello" => "world"},{}).to_a.callback do |res|
@coll.find({"hello" => "world"},{}).defer_as_a.callback do |res|
res.size.should >= 1
res[0]["hello"].should == "world"
done
Expand All @@ -54,7 +54,7 @@
@conn, @coll = connection_and_collection

@coll.insert('hello' => 'world')
@coll.find({:hello => "world"},{}).to_a.callback do |res|
@coll.find({:hello => "world"},{}).defer_as_a.callback do |res|
res.size.should >= 1
res[0]["hello"].should == "world"
done
Expand All @@ -65,7 +65,7 @@
@conn, @coll = connection_and_collection

id = @coll.insert('hello' => 'world')
@coll.find({:_id => id},{}).to_a.callback do |res|
@coll.find({:_id => id},{}).defer_as_a.callback do |res|
res.size.should >= 1
res[0]['hello'].should == "world"
done
Expand All @@ -77,7 +77,7 @@

@coll.insert('one' => 'one')
@coll.insert('two' => 'two')
@coll.find.to_a.callback do |res|
@coll.find.defer_as_a.callback do |res|
res.size.should >= 2
done
end
Expand All @@ -90,14 +90,14 @@
@coll.insert(:name => 'three', :position => 2)
@coll.insert(:name => 'two', :position => 1)

@coll.find({}, {:order => 'position'}).to_a.callback do |res|
@coll.find({}, {:order => 'position'}).defer_as_a.callback do |res|
res[0]["name"].should == 'one'
res[1]["name"].should == 'two'
res[2]["name"].should == 'three'
done
end

@coll.find({}, {:order => [:position, :desc]}).to_a.callback do |res|
@coll.find({}, {:order => [:position, :desc]}).defer_as_a.callback do |res|
res[0]["name"].should == 'three'
res[1]["name"].should == 'two'
res[2]["name"].should == 'one'
Expand Down Expand Up @@ -141,7 +141,7 @@
@coll.insert({'num' => num, 'word' => word})
end

@coll.find({'num' => {'$in' => [1,3,5]}}).to_a.callback do |res|
@coll.find({'num' => {'$in' => [1,3,5]}}).defer_as_a.callback do |res|
res.size.should == 3
res.map{|r| r['num'] }.sort.should == [1,3,5]
done
Expand All @@ -155,7 +155,7 @@
@coll.insert('num' => num, 'word' => word)
end

@coll.find({'num' => {'$gt' => 3}}).to_a.callback do |res|
@coll.find({'num' => {'$gt' => 3}}).defer_as_a.callback do |res|
res.size.should == 6
res.map{|r| r['num'] }.sort.should == [4,5,6,7,8,9]
done
Expand Down Expand Up @@ -202,7 +202,7 @@

t = Time.now.utc.freeze
@coll.insert('date' => t)
@coll.find.to_a.callback do |res|
@coll.find.defer_as_a.callback do |res|
res[0]['date'].to_s.should == t.to_s
done
end
Expand All @@ -222,7 +222,7 @@
'regex' => /abc$/ix
}
retobj = @coll.insert(obj)
@coll.find({:_id => obj[:_id]}).to_a.callback do |ret|
@coll.find({:_id => obj[:_id]}).defer_as_a.callback do |ret|
ret.size.should == 1
ret[0].each_key do |key|
next if key == '_id'
Expand Down Expand Up @@ -262,7 +262,7 @@

id = @coll.insert('hello' => 'world')
@coll.update({'hello' => 'world'}, {'hello' => 'newworld'})
@coll.find({:_id => id},{}).to_a.callback do |res|
@coll.find({:_id => id},{}).defer_as_a.callback do |res|
res[0]['hello'].should == 'newworld'
done
end
Expand All @@ -273,7 +273,7 @@

id = @coll.insert('hello' => 'world')
@coll.update({'hello' => 'world'}, {'$inc' => {'count' => 1}})
@coll.find({:_id => id},{}).to_a.callback do |res|
@coll.find({:_id => id},{}).defer_as_a.callback do |res|
res.first['hello'].should == 'world'
res.first['count'].should == 1
done
Expand Down Expand Up @@ -301,7 +301,7 @@
it "should insert a record when no id is present" do
@conn, @coll = connection_and_collection
id = @coll.save("x" => 1)
@coll.find("x" => 1).to_a.callback do |result|
@coll.find("x" => 1).defer_as_a.callback do |result|
result[0]["_id"].should == id
done
end
Expand All @@ -313,7 +313,7 @@
id = @coll.save(doc)
doc["x"] = 2
@coll.save(doc).should be_true
@coll.find().to_a.callback do |result|
@coll.find().defer_as_a.callback do |result|
result.count.should == 1
result[0]["x"].should == 2
done
Expand Down Expand Up @@ -342,7 +342,7 @@

id = @coll.insert('hello' => 'world')
@coll.remove(:_id => id)
@coll.find({'hello' => "world"}).to_a.callback do |res|
@coll.find({'hello' => "world"}).defer_as_a.callback do |res|
res.size.should == 0
done
end
Expand All @@ -354,7 +354,7 @@
@coll.insert('one' => 'one')
@coll.insert('two' => 'two')
@coll.remove
@coll.find.to_a.callback do |res|
@coll.find.defer_as_a.callback do |res|
res.size.should == 0
done
end
Expand Down
16 changes: 8 additions & 8 deletions spec/integration/cursor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@coll.save(all[-1])
end

cursor.limit(5).skip(3).sort("x",1).to_a.callback do |results|
cursor.limit(5).skip(3).sort("x",1).defer_as_a.callback do |results|
all.slice(3...8).each_with_index do |item,idx|
results[idx]["x"].should == item["x"]
end
Expand All @@ -49,7 +49,7 @@
1501.times do |i|
@coll.insert(i.to_s => i.to_s)
end
cursor.limit(1500).to_a.callback do |docs|
cursor.limit(1500).defer_as_a.callback do |docs|
docs.length.should == 1500
done
end
Expand Down Expand Up @@ -80,12 +80,12 @@
100.times do |i|
@coll.save("x" => 1)
end
cursor.to_a.callback do |r1|
cursor.defer_as_a.callback do |r1|
r1.length.should == 100
cursor.to_a.callback do |r2|
cursor.defer_as_a.callback do |r2|
r2.length.should == 0
cursor.rewind!
cursor.to_a.callback do |r3|
cursor.defer_as_a.callback do |r3|
r3.length.should == 100
done
end
Expand All @@ -101,7 +101,7 @@
1000.times do |i|
@coll.save("x" => 1)
end
cursor.to_a.callback do |results|
cursor.defer_as_a.callback do |results|
results.length.should == 1000
done
end
Expand Down Expand Up @@ -290,14 +290,14 @@
end
end

describe "to_a" do
describe "defer_as_a" do
it "should return an array of all documents in a query" do
@conn, @coll = connection_and_collection
5.times do |i|
@coll.insert("x" => i)
end
cursor = EM::Mongo::Cursor.new(@coll).sort("x",1)
cursor.to_a.callback do |docs|
cursor.defer_as_a.callback do |docs|
docs.length.should == 5
5.times do |i|
docs[i]["x"].should == i
Expand Down