Merge branch 'master' of git://github.com/jimm/mongo-ruby-driver
This commit is contained in:
commit
7b086fe4d1
|
@ -39,6 +39,8 @@ http://github.com/geir/mongo-java-driver/tree/master.
|
|||
|
||||
== Release Notes
|
||||
|
||||
Added "strict" db attribute.
|
||||
|
||||
I plan to remove the auto-generation of _id primary keys.
|
||||
|
||||
If you ran tests using code before release
|
||||
|
@ -52,6 +54,11 @@ type
|
|||
|
||||
= To Do
|
||||
|
||||
* 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.
|
||||
|
||||
* Tests that prove that this driver's ObjectID and Geir's Java version do the
|
||||
same thing. (I've done so manually.)
|
||||
|
||||
|
|
|
@ -27,12 +27,20 @@ module XGen
|
|||
SYSTEM_INDEX_COLLECTION = "system.indexes"
|
||||
SYSTEM_COMMAND_COLLECTION = "$cmd"
|
||||
|
||||
# Strict mode means that trying to access a collection that does not
|
||||
# exist will raise an error. Strict mode is off (false) by default.
|
||||
attr_writer :strict
|
||||
|
||||
# Returns the value of the +strict+ flag.
|
||||
def strict?; @strict; end
|
||||
|
||||
attr_reader :name, :socket
|
||||
|
||||
def initialize(db_name, host, port)
|
||||
raise "Invalid DB name" if !db_name || (db_name && db_name.length > 0 && db_name.include?("."))
|
||||
@name, @host, @port = db_name, host, port
|
||||
@socket = TCPSocket.new(@host, @port)
|
||||
@strict = false
|
||||
end
|
||||
|
||||
def collection_names
|
||||
|
@ -47,16 +55,25 @@ module XGen
|
|||
query(SYSTEM_NAMESPACE_COLLECTION, Query.new(selector))
|
||||
end
|
||||
|
||||
# Create a collection. If +strict+ is false, will return existing or
|
||||
# new collection. If +strict+ is true, will raise an error if
|
||||
# collection +name+ does not already exist.
|
||||
def create_collection(name, options={})
|
||||
# First check existence
|
||||
return Collection.new(self, name) if collection_names.include?(name)
|
||||
if collection_names.include?(full_coll_name(name))
|
||||
if strict?
|
||||
raise "Collection #{name} already exists. Currently in strict mode."
|
||||
else
|
||||
return Collection.new(self, name)
|
||||
end
|
||||
end
|
||||
|
||||
# Create new collection
|
||||
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
|
||||
|
||||
|
@ -66,10 +83,16 @@ module XGen
|
|||
Admin.new(self)
|
||||
end
|
||||
|
||||
# Return a collection. If +strict+ is false, will return existing or
|
||||
# new collection. If +strict+ is true, will raise an error if
|
||||
# collection +name+ already exists.
|
||||
def collection(name)
|
||||
# We do not implement the Java driver's optional strict mode, which
|
||||
# throws an exception if the collection does not exist.
|
||||
create_collection(name)
|
||||
return Collection.new(self, name) if collection_names.include?(full_coll_name(name))
|
||||
if strict?
|
||||
raise "Collection #{name} doesn't exist. Currently in strict mode."
|
||||
else
|
||||
create_collection(name)
|
||||
end
|
||||
end
|
||||
|
||||
def drop_collection(name)
|
||||
|
@ -77,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
|
||||
|
@ -119,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
|
||||
|
||||
|
@ -129,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)
|
||||
|
@ -176,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.
|
||||
|
|
|
@ -222,6 +222,52 @@ class DBAPITest < Test::Unit::TestCase
|
|||
assert_equal regex, rows[0]['b']
|
||||
end
|
||||
|
||||
def test_strict
|
||||
assert !@db.strict?
|
||||
@db.strict = true
|
||||
assert @db.strict?
|
||||
end
|
||||
|
||||
def test_strict_access_collection
|
||||
@db.strict = true
|
||||
begin
|
||||
@db.collection('does-not-exist')
|
||||
fail "expected exception"
|
||||
rescue => ex
|
||||
assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
|
||||
ensure
|
||||
@db.strict = false
|
||||
@db.drop_collection('does-not-exist')
|
||||
end
|
||||
end
|
||||
|
||||
def test_strict_create_collection
|
||||
@db.drop_collection('foobar')
|
||||
@db.strict = true
|
||||
|
||||
begin
|
||||
@db.create_collection('foobar')
|
||||
assert true
|
||||
rescue => ex
|
||||
fail "did not expect exception \"#{ex}\""
|
||||
end
|
||||
|
||||
# Now the collection exists. This time we should see an exception.
|
||||
begin
|
||||
@db.create_collection('foobar')
|
||||
fail "expected exception"
|
||||
rescue => ex
|
||||
assert_equal "Collection foobar already exists. Currently in strict mode.", ex.to_s
|
||||
ensure
|
||||
@db.strict = false
|
||||
@db.drop_collection('foobar')
|
||||
end
|
||||
end
|
||||
|
||||
def test_ismaster
|
||||
assert @db.master?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def new_oid
|
||||
|
|
Loading…
Reference in New Issue