New optional "strict" mode for databases.

This commit is contained in:
Jim Menard 2008-12-16 17:08:15 -05:00
parent bc61eacf03
commit 103d7e5c62
3 changed files with 74 additions and 4 deletions

View File

@ -39,6 +39,8 @@ http://github.com/geir/mongo-java-driver/tree/master.
== Release Notes == Release Notes
Added "strict" db attribute.
I plan to remove the auto-generation of _id primary keys. I plan to remove the auto-generation of _id primary keys.
If you ran tests using code before release If you ran tests using code before release
@ -52,6 +54,8 @@ type
= To Do = To Do
* Add way to ask db if it is master or slave.
* Tests that prove that this driver's ObjectID and Geir's Java version do the * Tests that prove that this driver's ObjectID and Geir's Java version do the
same thing. (I've done so manually.) same thing. (I've done so manually.)

View File

@ -27,12 +27,20 @@ module XGen
SYSTEM_INDEX_COLLECTION = "system.indexes" SYSTEM_INDEX_COLLECTION = "system.indexes"
SYSTEM_COMMAND_COLLECTION = "$cmd" 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 attr_reader :name, :socket
def initialize(db_name, host, port) def initialize(db_name, host, port)
raise "Invalid DB name" if !db_name || (db_name && db_name.length > 0 && db_name.include?(".")) raise "Invalid DB name" if !db_name || (db_name && db_name.length > 0 && db_name.include?("."))
@name, @host, @port = db_name, host, port @name, @host, @port = db_name, host, port
@socket = TCPSocket.new(@host, @port) @socket = TCPSocket.new(@host, @port)
@strict = false
end end
def collection_names def collection_names
@ -47,9 +55,18 @@ module XGen
query(SYSTEM_NAMESPACE_COLLECTION, Query.new(selector)) query(SYSTEM_NAMESPACE_COLLECTION, Query.new(selector))
end 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={}) def create_collection(name, options={})
# First check existence # 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 # Create new collection
oh = OrderedHash.new oh = OrderedHash.new
@ -66,11 +83,17 @@ module XGen
Admin.new(self) Admin.new(self)
end 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) def collection(name)
# We do not implement the Java driver's optional strict mode, which return Collection.new(self, name) if collection_names.include?(full_coll_name(name))
# throws an exception if the collection does not exist. if strict?
raise "Collection #{name} doesn't exist. Currently in strict mode."
else
create_collection(name) create_collection(name)
end end
end
def drop_collection(name) def drop_collection(name)
coll = collection(name) coll = collection(name)

View File

@ -203,4 +203,47 @@ class DBAPITest < Test::Unit::TestCase
assert_equal 1, rows.length assert_equal 1, rows.length
assert_equal regex, rows[0]['b'] assert_equal regex, rows[0]['b']
end 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
end end