Added XGen::Mongo::Driver::DB.master?

This commit is contained in:
Jim Menard 2008-12-16 17:35:31 -05:00
parent 9ee07cb74f
commit 02cd38fce0
3 changed files with 24 additions and 14 deletions

View File

@ -54,11 +54,8 @@ type
= To Do
* Add way to ask db if it is master or slave. Mongo needs to add this, first.
* Once Mongo can tell us if an instance is master or slave, add a way to
specify a collection of databases on startup (a simple array of IP
address/port numbers, perhaps, or a hash or something). The driver would
* Add a way to specify a collection of databases on startup (a simple array of
IP address/port numbers, perhaps, or a hash or something). The driver would
then find the master and, on each subsequent command, ask that machine if it
is the master before proceeding.

View File

@ -72,8 +72,8 @@ module XGen
oh = OrderedHash.new
oh[:create] = name
doc = db_command(oh.merge(options))
o = doc['ok']
return Collection.new(self, name) if o.kind_of?(Numeric) && (o.to_i == 1 || o.to_i == 0)
ok = doc['ok']
return Collection.new(self, name) if ok.kind_of?(Numeric) && (ok.to_i == 1 || ok.to_i == 0)
raise "Error creating collection: #{doc.inspect}"
end
@ -100,9 +100,15 @@ module XGen
return true if coll == nil
coll.drop_indexes # Mongo requires that we drop indexes manually
doc = db_command(:drop => name)
o = doc['ok']
return o.kind_of?(Numeric) && o.to_i == 1
ok?(db_command(:drop => name))
end
# Returns true if this database is a master (or is not paired with any
# other database), false if it is a slave.
def master?
doc = db_command(:ismaster => 1)
is_master = doc['ismaster']
ok?(doc) && is_master.kind_of?(Numeric) && is_master.to_i == 1
end
def close
@ -142,8 +148,7 @@ module XGen
oh[:count] = collection_name
oh[:query] = selector
doc = db_command(oh)
o = doc['ok']
return doc['n'].to_i if o.to_i == 1
return doc['n'].to_i if ok?(doc)
raise "Error with count command: #{doc.inspect}"
end
@ -152,8 +157,7 @@ module XGen
oh[:deleteIndexes] = collection_name
oh[:index] = name
doc = db_command(oh)
o = doc['ok']
raise "Error with drop_index command: #{doc.inspect}" unless o.kind_of?(Numeric) && o.to_i == 1
raise "Error with drop_index command: #{doc.inspect}" unless ok?(doc)
end
def index_information(collection_name)
@ -199,6 +203,11 @@ module XGen
protected
def ok?(doc)
ok = doc['ok']
ok.kind_of?(Numeric) && ok.to_i == 1
end
# DB commands need to be ordered, so selector must be an OrderedHash
# (or a Hash with only one element). What DB commands really need is
# that the "command" key be first.

View File

@ -246,4 +246,8 @@ class DBAPITest < Test::Unit::TestCase
end
end
def test_ismaster
assert @db.master?
end
end