add support for unique index creation
This commit is contained in:
parent
a2898effb4
commit
1312c70677
|
@ -117,8 +117,10 @@ module XGen
|
||||||
# should be either a single field name or a Array of [field name,
|
# should be either a single field name or a Array of [field name,
|
||||||
# direction] pairs. Directions should be specified as
|
# direction] pairs. Directions should be specified as
|
||||||
# XGen::Mongo::ASCENDING or XGen::Mongo::DESCENDING.
|
# XGen::Mongo::ASCENDING or XGen::Mongo::DESCENDING.
|
||||||
def create_index(field_or_spec)
|
# +unique+ is an optional boolean indicating whether this index
|
||||||
@db.create_index(@name, field_or_spec)
|
# should enforce a uniqueness constraint.
|
||||||
|
def create_index(field_or_spec, unique=false)
|
||||||
|
@db.create_index(@name, field_or_spec, unique)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Drop index +name+.
|
# Drop index +name+.
|
||||||
|
|
|
@ -430,8 +430,9 @@ module XGen
|
||||||
# should be either a single field name or a Array of [field name,
|
# should be either a single field name or a Array of [field name,
|
||||||
# direction] pairs. Directions should be specified as
|
# direction] pairs. Directions should be specified as
|
||||||
# XGen::Mongo::ASCENDING or XGen::Mongo::DESCENDING. Normally called
|
# XGen::Mongo::ASCENDING or XGen::Mongo::DESCENDING. Normally called
|
||||||
# by Collection#create_index.
|
# by Collection#create_index. If +unique+ is true the index will
|
||||||
def create_index(collection_name, field_or_spec)
|
# enforce a uniqueness constraint.
|
||||||
|
def create_index(collection_name, field_or_spec, unique=false)
|
||||||
field_h = OrderedHash.new
|
field_h = OrderedHash.new
|
||||||
if field_or_spec.is_a? String
|
if field_or_spec.is_a? String
|
||||||
field_h[field_or_spec] = 1
|
field_h[field_or_spec] = 1
|
||||||
|
@ -442,7 +443,8 @@ module XGen
|
||||||
sel = {
|
sel = {
|
||||||
:name => name,
|
:name => name,
|
||||||
:ns => full_coll_name(collection_name),
|
:ns => full_coll_name(collection_name),
|
||||||
:key => field_h
|
:key => field_h,
|
||||||
|
:unique => unique
|
||||||
}
|
}
|
||||||
@semaphore.synchronize {
|
@semaphore.synchronize {
|
||||||
send_to_db(InsertMessage.new(@name, SYSTEM_INDEX_COLLECTION, sel))
|
send_to_db(InsertMessage.new(@name, SYSTEM_INDEX_COLLECTION, sel))
|
||||||
|
|
|
@ -309,6 +309,28 @@ class DBAPITest < Test::Unit::TestCase
|
||||||
@@db.drop_index(@@coll.name, name)
|
@@db.drop_index(@@coll.name, name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_unique_index
|
||||||
|
@@db.drop_collection("blah")
|
||||||
|
test = @@db.collection("blah")
|
||||||
|
test.create_index("hello")
|
||||||
|
|
||||||
|
test.insert("hello" => "world")
|
||||||
|
test.insert("hello" => "mike")
|
||||||
|
test.insert("hello" => "world")
|
||||||
|
assert !@@db.error?
|
||||||
|
|
||||||
|
@@db.drop_collection("blah")
|
||||||
|
test = @@db.collection("blah")
|
||||||
|
test.create_index("hello", unique=true)
|
||||||
|
|
||||||
|
test.insert("hello" => "world")
|
||||||
|
test.insert("hello" => "mike")
|
||||||
|
test.insert("hello" => "world")
|
||||||
|
assert @@db.error?
|
||||||
|
|
||||||
|
@@db.drop_collection("blah")
|
||||||
|
end
|
||||||
|
|
||||||
def test_array
|
def test_array
|
||||||
@@coll << {'b' => [1, 2, 3]}
|
@@coll << {'b' => [1, 2, 3]}
|
||||||
rows = @@coll.find({}, {:fields => ['b']}).to_a
|
rows = @@coll.find({}, {:fields => ['b']}).to_a
|
||||||
|
|
Loading…
Reference in New Issue