From 4d7f06a6beacea3463ab190b6ba0c68d031a199a Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Sun, 31 May 2009 22:16:01 -0400 Subject: [PATCH 1/3] Implemented that index fields can also be specified with symbols. --- lib/mongo/db.rb | 6 +++--- tests/test_db_api.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 8550dcf..d8a5519 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -460,10 +460,10 @@ module XGen # enforce a uniqueness constraint. def create_index(collection_name, field_or_spec, unique=false) field_h = OrderedHash.new - if field_or_spec.is_a? String - field_h[field_or_spec] = 1 + if field_or_spec.is_a?(String) || field_or_spec.is_a?(Symbol) + field_h[field_or_spec.to_s] = 1 else - field_or_spec.each { |f| field_h[f[0]] = f[1] } + field_or_spec.each { |f| field_h[f[0].to_s] = f[1] } end name = gen_index_name(field_h) sel = { diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 3507401..55cafd4 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -294,6 +294,20 @@ class DBAPITest < Test::Unit::TestCase ensure @@db.drop_index(@@coll.name, name) end + + def test_index_create_with_symbol + name = @@db.create_index(@@coll.name, :a) + list = @@db.index_information(@@coll.name) + assert_equal @@coll.index_information, list + assert_equal 2, list.length + + info = list[1] + assert_equal name, 'a_1' + assert_equal name, info[:name] + assert_equal 1, info[:keys]['a'] + ensure + @@db.drop_index(@@coll.name, name) + end def test_multiple_index_cols name = @@db.create_index(@@coll.name, [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]]) @@ -308,6 +322,20 @@ class DBAPITest < Test::Unit::TestCase ensure @@db.drop_index(@@coll.name, name) end + + def test_multiple_index_cols_with_symbols + name = @@db.create_index(@@coll.name, [[:a, DESCENDING], [:b, ASCENDING], [:c, DESCENDING]]) + list = @@db.index_information(@@coll.name) + assert_equal 2, list.length + + info = list[1] + assert_equal name, 'a_-1_b_1_c_-1' + assert_equal name, info[:name] + keys = info[:keys].keys + assert_equal ['a', 'b', 'c'], keys.sort + ensure + @@db.drop_index(@@coll.name, name) + end def test_unique_index @@db.drop_collection("blah") From 4cfa9cbcf3fc7a76e78ccfc39bf42a7d034da937 Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Sun, 31 May 2009 22:17:20 -0400 Subject: [PATCH 2/3] Fixed lack of parenthesis ruby warning that I was receiving on 1.8.6. --- lib/mongo/util/bson.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo/util/bson.rb b/lib/mongo/util/bson.rb index a4acd43..23267d0 100644 --- a/lib/mongo/util/bson.rb +++ b/lib/mongo/util/bson.rb @@ -391,7 +391,7 @@ class BSON buf.put_double(val) else if val > 2**32 / 2 - 1 or val < -2**32 / 2 - raise RangeError.new "MongoDB can only handle 4-byte ints - try converting to a double before saving" + raise RangeError.new("MongoDB can only handle 4-byte ints - try converting to a double before saving") end buf.put_int(val) end From f95102c2bbcba82080f774e18e5151e835df9ac7 Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Sun, 31 May 2009 22:45:05 -0400 Subject: [PATCH 3/3] Updated save to actually check if id exists in the collection. --- lib/mongo/collection.rb | 9 +++++---- tests/test_db_api.rb | 8 ++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index d54d7f0..865bb62 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -72,13 +72,14 @@ module XGen cursor = find(selector, h) cursor.next_object # don't need to explicitly close b/c of limit end - + # Save an updated +object+ to the collection, or insert it if it doesn't exist already. def save(object) - if id = object[:_id] || object['_id'] - modify({:_id => id}, object) - else + id = object[:_id] || object['_id'] + if id.nil? || find_first({'_id' => id}).nil? insert(object) + else + modify({:_id => id}, object) end end diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 55cafd4..7e0da18 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -612,6 +612,14 @@ class DBAPITest < Test::Unit::TestCase @@coll.save(a) assert_equal 2, @@coll.count end + + def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection + @@coll.clear + + a = {'_id' => '1', 'hello' => 'world'} + @@coll.save(a) + assert_equal(1, @@coll.count) + end # TODO this test fails with error message "Undefed Before end of object" # That is a database error. The undefined type may go away.