Skip to content

Commit

Permalink
added indexing options: background, geo, dropDups, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed Mar 16, 2010
1 parent 9dd1a5c commit 367f9b3
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 22 deletions.
3 changes: 2 additions & 1 deletion lib/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ module Mongo
end

module Mongo
ASCENDING = 1
ASCENDING = 1
DESCENDING = -1
GEO2D = '2d'

module Constants
OP_REPLY = 1
Expand Down
46 changes: 40 additions & 6 deletions lib/mongo/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,30 +318,64 @@ def update(selector, document, options={})
#
# @param [String, Array] field_or_spec
# should be either a single field name or an array of
# [field name, direction] pairs. Directions should be specified as Mongo::ASCENDING or Mongo::DESCENDING.
# [field name, direction] pairs. Directions should be specified
# as Mongo::ASCENDING, Mongo::DESCENDING, or Mongo::GEO2D.
#
# @param [Boolean] unique if true, this index will enforce a uniqueness constraint.
# Note that geospatial indexing only work in versions of MongoDB >= 1.3.3+. Keep in mind, too,
# that in order to index a given field, that field must reference either an array or a sub-object
# where the first two values represent x- and y-coordinates. Examples can be seen below.
#
# @param [Boolean] unique if true, this index will enforce a uniqueness constraint. DEPRECATED. Future
# versions of this driver will specify the uniqueness constraint using a hash param.
#
# @option opts [Boolean] :unique (false) if true, this index will enforce a uniqueness constraint.
# @option opts [Boolean] :background (false) indicate that the index should be built in the background. This
# feature is only available in MongoDB >= 1.3.2.
# @option opts [Boolean] :dropDups If creating a unique index on a collection with pre-existing records,
# this option will keep the first document the database indexes and drop all subsequent with duplicate values.
# @option opts [Integer] :min specify the minimum longitude and latitude for a geo index.
# @option opts [Integer] :max specify the maximum longitude and latitude for a geo index.
#
# @example Creating a compound index:
# @posts.create_index([['subject', Mongo::ASCENDING], ['created_at', Mongo::DESCENDING]])
#
# @example Creating a geospatial index:
# @restaurants.create_index(['location', Mongo::GEO2D])
#
# # Note that this will work only if 'location' represents x,y coordinates:
# {'location': [0, 50]}
# {'location': {'x' => 0, 'y' => 50}}
# {'location': {'latitude' => 0, 'longitude' => 50}}
#
# @example A geospatial index with alternate longitude and latitude:
# @restaurants.create_index(['location', Mongo::GEO2D], :min => 500, :max => 500)
#
# @return [String] the name of the index created.
#
# @core indexes create_index-instance_method
def create_index(field_or_spec, unique=false)
def create_index(field_or_spec, opts={})
opts.assert_valid_keys(:min, :max, :background, :unique, :dropDups) if opts.is_a?(Hash)
field_h = OrderedHash.new
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].to_s] = f[1] }
end
name = generate_index_names(field_h)
if opts == true || opts == false
warn "If you're using Collection#create_index, the method for specifying a unique index has changed." +
"Please pass :unique => true to the method instead."
end
sel = {
:name => name,
:ns => "#{@db.name}.#{@name}",
:key => field_h,
:unique => unique }
:unique => (opts == true ? true : false) }
sel.merge!(opts) if opts.is_a?(Hash)
begin
insert_documents([sel], Mongo::DB::SYSTEM_INDEX_COLLECTION, false, true)
response = insert_documents([sel], Mongo::DB::SYSTEM_INDEX_COLLECTION, false, true)
rescue Mongo::OperationFailure
raise Mongo::OperationFailure, "Failed to create index #{sel.inspect}."
raise Mongo::OperationFailure, "Failed to create index #{sel.inspect}. Errors: #{response}"
end
name
end
Expand Down
8 changes: 4 additions & 4 deletions lib/mongo/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,11 @@ def drop_index(collection_name, index_name)
# @return [Hash] keys are index names and the values are lists of [key, direction] pairs
# defining the index.
def index_information(collection_name)
sel = {:ns => full_collection_name(collection_name)}
sel = {:ns => full_collection_name(collection_name)}
info = {}
Cursor.new(Collection.new(self, SYSTEM_INDEX_COLLECTION), :selector => sel).each { |index|
info[index['name']] = index['key'].map {|k| k}
}
Cursor.new(Collection.new(self, SYSTEM_INDEX_COLLECTION), :selector => sel).each do |index|
info[index['name']] = index
end
info
end

Expand Down
11 changes: 11 additions & 0 deletions lib/mongo/util/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ def returning(value)
end

end

#:nodoc:
class Hash

#:nodoc:
def assert_valid_keys(*valid_keys)
unknown_keys = keys - [valid_keys].flatten
raise(Mongo::MongoArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
end

end
25 changes: 24 additions & 1 deletion test/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,30 @@ def test_small_limit

context "Creating indexes " do
setup do
@@db.drop_collection('geo')
@@db.drop_collection('test-collection')
@collection = @@db.collection('test-collection')
@geo = @@db.collection('geo')
end

should "create a geospatial index" do
@geo.save({'loc' => [-100, 100]})
@geo.create_index([['loc', Mongo::GEO2D]])
assert @geo.index_information['loc_2d']
end

should "create a unique index" do
@collection.create_index([['a', Mongo::ASCENDING]], true)
assert @collection.index_information['a_1']['unique'] == true
end

should "create an index in the background" do
if @@version > '1.3.1'
@collection.create_index([['b', Mongo::ASCENDING]], :background => true)
assert @collection.index_information['b_1']['background'] == true
else
assert true
end
end

should "generate indexes in the proper order" do
Expand All @@ -508,7 +531,7 @@ def test_small_limit
end

should "return properly ordered index information" do
assert_equal [['b', 1], ['a', 1]], @collection.index_information["b_1_a_1"]
assert @collection.index_information['b_1_a_1']
end
end
end
Expand Down
14 changes: 4 additions & 10 deletions test/db_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ def test_find_advanced
assert_equal 2, docs.size
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }

# Find by advanced query (regexp)
docs = @@coll.find('a' => /[1|2]/).to_a
assert_equal 2, docs.size
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }
end

def test_find_sorting
Expand Down Expand Up @@ -287,7 +281,7 @@ def test_index_information
assert_equal 2, info.length

assert info.has_key?(name)
assert_equal info[name], [["a", ASCENDING]]
assert_equal info[name]["key"], {"a" => 1}
ensure
@@db.drop_index(@@coll.name, name)
end
Expand All @@ -302,7 +296,7 @@ def test_index_create_with_symbol
assert_equal 2, info.length

assert info.has_key?(name)
assert_equal info[name], [["a", ASCENDING]]
assert_equal info[name]['key'], {"a" => 1}
ensure
@@db.drop_index(@@coll.name, name)
end
Expand All @@ -314,7 +308,7 @@ def test_multiple_index_cols

assert_equal name, 'a_-1_b_1_c_-1'
assert info.has_key?(name)
assert_equal [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]], info[name]
assert_equal info[name]['key'], {"a" => -1, "b" => 1, "c" => -1}
ensure
@@db.drop_index(@@coll.name, name)
end
Expand All @@ -326,7 +320,7 @@ def test_multiple_index_cols_with_symbols

assert_equal name, 'a_-1_b_1_c_-1'
assert info.has_key?(name)
assert_equal [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]], info[name]
assert_equal info[name]['key'], {"a" => -1, "b" => 1, "c" => -1}
ensure
@@db.drop_index(@@coll.name, name)
end
Expand Down

0 comments on commit 367f9b3

Please sign in to comment.