Browse files

Client#close and Client#connected?

  • Loading branch information...
1 parent 0618081 commit 24d74dbdf77d85e78bc057c133ee21b711ecadf9 @tmm1 committed Aug 24, 2008
Showing with 28 additions and 221 deletions.
  1. +6 −214 lib/mongo.rb
  2. +19 −7 lib/mongo/client.rb
  3. +3 −0 lib/mongo/collection.rb
View
220 lib/mongo.rb
@@ -165,220 +165,12 @@ def Mongo namespace, client = nil
done
end
end
-
-end
-
-__END__
-
-EM.run{
- # connect to mongo
- mongo = Mongo::Client.connect :port => 27017
-
- # remove all objects in the database
- mongo.remove({})
-
- # insert a simple object with a string
- mongo.insert :_id => '000000000000000000000001',
- :hello => 'world'
-
- # find all objects
- mongo.find({}) do |results|
-
- end
-
- # find specific object
- mongo.find(:_id => '000000000000000000000001') do |results|
-
- end
-
- # insert complex object
- mongo.insert :_id => '000000000000000000000002',
- :array => [1,2,3],
- :float => 123.456,
- :hash => {:boolean => true},
- :nil => nil,
- :symbol => :name,
- :string => 'hello world',
- :time => Time.now,
- :regex => /abc$/ix
-
- # find all objects
- mongo.find({}) do |results|
-
- end
-
- # query nested properties
- mongo.find(:'hash.boolean' => true) do |results|
-
- end
- # insert test data
- mongo.insert(:_id => '000000000000000000000010', :n => 1, :array => [1,2,3])
- mongo.insert(:_id => '000000000000000000000011', :n => 2, :string => 'ruby and js')
- mongo.insert(:_id => '000000000000000000000012', :n => 3, :number => 112233.445566)
- mongo.insert(:_id => '000000000000000000000013', :n => 4, :null => nil)
- mongo.insert(:_id => '000000000000000000000014', :n => 5, :object => { :boolean => true })
- mongo.insert(:_id => '000000000000000000000015', :n => 6, :symbol => :Constant)
-
- # add index on n
- mongo.namespace('default.system.indexes') do
- mongo.insert(:name => 'n', :ns => 'default.test', :key => { :n => true })
- end
-
- # simple searches
- mongo.find(:n > 1) do |results|
-
- end
-
- # in queries
- mongo.find :n.in([ 1,3,5 ]) do |results|
-
- end
-
- # sorting (sorting is faster with an index)
- mongo.find({}, :n.desc) do |results|
-
+ should 'close the connection' do
+ @mongo.client.close{
+ @mongo.client.should.not.be.connected?
+ done
+ }
end
-
- # switch to editors namespace
- mongo.namespace = 'default.editors'
-
- # delete all rows
- mongo.remove({})
-
- # insert editors with platforms supported tags
- mongo.insert(:_id => '000000000000000000000101', :name => :textmate, :platform => [:osx])
- mongo.insert(:_id => '000000000000000000000102', :name => :vim, :platform => [:osx, :linux])
- mongo.insert(:_id => '000000000000000000000103', :name => :eclipse, :platform => [:osx, :linux, :windows])
- mongo.insert(:_id => '000000000000000000000104', :name => :notepad, :platform => [:windows])
- # find all editors
- mongo.find({}) do |results|
-
- end
-
- # add multikey index on platforms property
- mongo.namespace('default.system.indexes') do
- mongo.insert(:name => 'platforms', :ns => 'default.editors', :key => { :platform => true })
- end
-
- mongo.find(:platform.in [:linux, :osx]) do |results|
-
- end
-
- # find objects with linux tag # XXX how can i find an object with two tags?
- mongo.find(:platform => :linux) do |results|
-
- end
-
- # close the connection and stop the reactor
- mongo.close{ EM.stop_event_loop }
-}
-
-__END__
-
-["connected"]
-
---------------------------------------------------------------------------------
-
-FIND {} =>
-
-[{:_id=>"000000000000000000000001", :hello=>"world"}]
-
---------------------------------------------------------------------------------
-
-FIND {:_id=>"000000000000000000000001"} =>
-
-[{:_id=>"000000000000000000000001", :hello=>"world"}]
-
---------------------------------------------------------------------------------
-
-FIND {} =>
-
-[{:_id=>"000000000000000000000001", :hello=>"world"},
- {:_id=>"000000000000000000000002",
- :hash=>{:boolean=>true},
- :regex=>/abc$/ix,
- :float=>123.456,
- :symbol=>:name,
- :array=>[1.0, 2.0, 3.0],
- :nil=>nil,
- :string=>"hello world",
- :time=>Sat Aug 02 02:08:27 -0700 2008}]
-
---------------------------------------------------------------------------------
-
-FIND {:"hash.boolean"=>true} =>
-
-[{:_id=>"000000000000000000000002",
- :hash=>{:boolean=>true},
- :regex=>/abc$/ix,
- :float=>123.456,
- :symbol=>:name,
- :array=>[1.0, 2.0, 3.0],
- :nil=>nil,
- :string=>"hello world",
- :time=>Sat Aug 02 02:08:27 -0700 2008}]
-
---------------------------------------------------------------------------------
-
-FIND {:n=>{:$gt=>1}} =>
-
-[{:_id=>"000000000000000000000011", :n=>2.0, :string=>"ruby and js"},
- {:_id=>"000000000000000000000012", :number=>112233.445566, :n=>3.0},
- {:_id=>"000000000000000000000013", :n=>4.0, :null=>nil},
- {:_id=>"000000000000000000000014", :n=>5.0, :object=>{:boolean=>true}},
- {:_id=>"000000000000000000000015", :adf=>"123", :n=>6.0}]
-
---------------------------------------------------------------------------------
-
-FIND {:n=>{:$in=>[1, 3, 5]}} =>
-
-[{:_id=>"000000000000000000000001", :hello=>"world"},
- {:_id=>"000000000000000000000002",
- :hash=>{:boolean=>true},
- :regex=>/abc$/ix,
- :float=>123.456,
- :symbol=>:name,
- :array=>[1.0, 2.0, 3.0],
- :nil=>nil,
- :string=>"hello world",
- :time=>Sat Aug 02 02:08:27 -0700 2008},
- {:_id=>"000000000000000000000010", :array=>[1.0, 2.0, 3.0], :n=>1.0},
- {:_id=>"000000000000000000000012", :number=>112233.445566, :n=>3.0},
- {:_id=>"000000000000000000000014", :n=>5.0, :object=>{:boolean=>true}}]
-
---------------------------------------------------------------------------------
-
-FIND {:orderby=>{:n=>-1}, :query=>{}} =>
-
-[{:symbol=>:Constant, :n=>6.0, :_id=>"000000000000000000000015"},
- {:object=>{:boolean=>true}, :n=>5.0, :_id=>"000000000000000000000014"},
- {:null=>nil, :n=>4.0, :_id=>"000000000000000000000013"},
- {:number=>112233.445566, :n=>3.0, :_id=>"000000000000000000000012"},
- {:n=>2.0, :_id=>"000000000000000000000011", :string=>"ruby and js"},
- {:array=>[1.0, 2.0, 3.0], :n=>1.0, :_id=>"000000000000000000000010"}]
-
---------------------------------------------------------------------------------
-
-FIND {} =>
-
-[{:name=>:textmate, :_id=>"000000000000000000000101", :platform=>[:osx]},
- {:name=>:vim, :_id=>"000000000000000000000102", :platform=>[:osx, :linux]},
- {:name=>:eclipse,
- :_id=>"000000000000000000000103",
- :platform=>[:osx, :linux, :windows]},
- {:name=>:notepad, :_id=>"000000000000000000000104", :platform=>[:windows]}]
-
---------------------------------------------------------------------------------
-
-FIND {:platform=>:linux} =>
-
-[{:_id=>"000000000000000000000102", :name=>:vim, :platform=>[:osx, :linux]},
- {:_id=>"000000000000000000000103",
- :name=>:eclipse,
- :platform=>[:osx, :linux, :windows]}]
-
---------------------------------------------------------------------------------
-
-["disconnected"]
+end
View
26 lib/mongo/client.rb
@@ -6,18 +6,27 @@ def initialize opts = {}
@settings = opts
@id = 0
@responses = {}
+ @connected = false
- timeout opts[:timeout] || 0.5
- errback{
+ @on_close = proc{
raise Error, 'could not connect to server'
}
+
+ timeout opts[:timeout] || 0.5
+ errback{ @on_close.call }
end
+ def connected?() @connected end
+
# EM hooks
def connection_completed
log 'connected'
@buf = Buffer.new
+ @connected = true
+ @on_close = proc{
+ raise Error, 'disconnected from server'
+ }
succeed
end
@@ -48,7 +57,7 @@ def receive_data data
end
# close if no more responses pending
- @on_close.succeed if @on_close and @responses.size == 0
+ close_connection if @close_pending and @responses.size == 0
end
end
@@ -59,16 +68,19 @@ def receive_data data
def unbind
log 'disconnected'
+ @connected = false
+ @on_close.call
end
# connection
def close
- @on_close = EM::DefaultDeferrable.new
- @on_close.callback{
+ @on_close = proc{ yield if block_given? }
+ if @responses.empty?
close_connection
- yield if block_given?
- }
+ else
+ @close_pending = true
+ end
end
def send command_id, *args, &blk
View
3 lib/mongo/collection.rb
@@ -12,6 +12,8 @@ def initialize ns, client = nil
@ns = ns.to_s.freeze
end
+ attr_reader :client
+
# find {:a => 1}, :b < 2, :a.asc, :b.desc, :limit => 10, :skip => 5
def find *args, &blk
opts = Hash.new(0)
@@ -91,6 +93,7 @@ def indexes obj = {}, &blk
end
def method_missing meth
+ raise ArgumentError, 'collection cannot take block' if block_given?
(@subns ||= {})[meth] ||= self.class.new("#{@ns}.#{meth}", @client)
end

0 comments on commit 24d74db

Please sign in to comment.