From 02cd38fce0d0e80854860bc19a7158930389b03b Mon Sep 17 00:00:00 2001 From: Jim Menard Date: Tue, 16 Dec 2008 17:35:31 -0500 Subject: [PATCH] Added XGen::Mongo::Driver::DB.master? --- README.rdoc | 7 ++----- lib/mongo/db.rb | 27 ++++++++++++++++++--------- tests/test_db_api.rb | 4 ++++ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/README.rdoc b/README.rdoc index 16fbfad..64b6c70 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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. diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 0971b34..5b8a637 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -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. diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 6a0da23..93f423b 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -246,4 +246,8 @@ class DBAPITest < Test::Unit::TestCase end end + def test_ismaster + assert @db.master? + end + end