Skip to content

Commit

Permalink
Move index stuff in mongo_driver
Browse files Browse the repository at this point in the history
  • Loading branch information
aymerick committed Jan 29, 2014
1 parent 624f014 commit 77f2b7a
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 142 deletions.
80 changes: 1 addition & 79 deletions lib/activr/storage.rb
Expand Up @@ -296,89 +296,11 @@ def delete_timeline_entries_for_entity_model(model)
# Indexes
#

# Add index for activities
#
# @param index [String,Array<String>] Field or array of fields
# @param options [Hash] Options hash
# @option options (see Activr::Storage::MongoDriver#add_index)
# @return [String] Index created
def add_activity_index(index, options = { })
self.driver.add_activity_index(index, options)
end

# Add index for timeline entries
#
# @param timeline_kind [String] Timeline kind
# @param index [String,Array<String>] Field or array of fields
# @param options [Hash] Options hash
# @option options (see Activr::Storage::MongoDriver#add_index)
# @return [String] Index created
def add_timeline_index(timeline_kind, index, options = { })
self.driver.add_timeline_index(timeline_kind, index, options)
end

# Ensure all necessary indexes
#
# @yield [String] Created index name
def create_indexes
# Create indexes on 'activities' collection for models that includes Activr::Entity::ModelMixin
#
# eg: activities
# [['actor', Mongo::ASCENDING], ['at', Mongo::ASCENDING]]
# [['album', Mongo::ASCENDING], ['at', Mongo::ASCENDING]]
# [['picture', Mongo::ASCENDING], ['at', Mongo::ASCENDING]]
Activr.registry.models.each do |model_class|
if !model_class.activr_entity_settings[:feed_index]
# @todo Output a warning to remove the index if it exists
else
fields = [ model_class.activr_entity_feed_actual_name.to_s, 'at' ]

index_name = Activr.storage.add_activity_index(fields)
yield("activity / #{index_name}") if block_given?
end
end

# Create indexes on '*_timelines' collections for defined timeline classes
#
# eg: user_news_feed_timelines
# [['rcpt', Mongo::ASCENDING], ['activity.at', Mongo::ASCENDING]]
Activr.registry.timelines.each do |timeline_kind, timeline_class|
fields = [ 'rcpt', 'activity.at' ]

index_name = Activr.storage.add_timeline_index(timeline_kind, fields)
yield("#{timeline_kind} timeline / #{index_name}") if block_given?
end

# Create sparse indexes to remove activities and timeline entries when entity is deleted
#
# eg: activities
# [['actor', Mongo::ASCENDING]], :sparse => true
#
# eg: user_news_feed_timelines
# [['activity.actor', Mongo::ASCENDING]], :sparse => true
# [['activity.album', Mongo::ASCENDING]], :sparse => true
# [['activity.picture', Mongo::ASCENDING]], :sparse => true
Activr.registry.models.each do |model_class|
if model_class.activr_entity_settings[:deletable]
# create sparse index on `activities`
Activr.registry.activity_entities_for_model(model_class).each do |entity_name|
# if entity activity feed is enabled and this is the entity name used to fetch that feed then we can use the existing index...
if !model_class.activr_entity_settings[:feed_index] || (entity_name != model_class.activr_entity_feed_actual_name)
# ... else we create an index
index_name = Activr.storage.add_activity_index(entity_name.to_s, :sparse => true)
yield("activity / #{index_name}") if block_given?
end
end

# create sparse index on timeline classes where that entity can be present
Activr.registry.timeline_entities_for_model(model_class).each do |timeline_class, entities|
entities.each do |entity_name|
index_name = Activr.storage.add_timeline_index(timeline_class.kind, "activity.#{entity_name}", :sparse => true)
yield("#{timeline_class.kind} timeline / #{index_name}") if block_given?
end
end
end
end
self.driver.create_indexes
end


Expand Down
84 changes: 82 additions & 2 deletions lib/activr/storage/mongo_driver.rb
Expand Up @@ -438,9 +438,14 @@ def delete_activities(options = { })
self.delete(self.activity_collection, selector)
end

# (see Storage#add_activity_index)
# Add index for activities
#
# @api private
#
# @param index [String,Array<String>] Field or array of fields
# @param options [Hash] Options hash
# @option options (see Activr::Storage::MongoDriver#add_index)
# @return [String] Index created
def add_activity_index(index, options = { })
index = index.is_a?(Array) ? index : [ index ]
index_spec = index.map{ |field| [ field, 1 ] }
Expand Down Expand Up @@ -552,14 +557,89 @@ def delete_timeline_entries(timeline_kind, recipient_id, options = { })
self.delete(self.timeline_collection(timeline_kind), selector)
end

# (see Storage#add_timeline_index)
# Add index for timeline entries
#
# @api private
#
# @param timeline_kind [String] Timeline kind
# @param index [String,Array<String>] Field or array of fields
# @param options [Hash] Options hash
# @option options (see Activr::Storage::MongoDriver#add_index)
# @return [String] Index created
def add_timeline_index(timeline_kind, index, options = { })
index = index.is_a?(Array) ? index : [ index ]
index_spec = index.map{ |field| [ field, 1 ] }

self.add_index(self.timeline_collection(timeline_kind), index_spec, options)
end


#
# Indexes
#

# (see Storage#create_indexes)
#
# @api private
def create_indexes
# Create indexes on 'activities' collection for models that includes Activr::Entity::ModelMixin
#
# eg: activities
# [['actor', Mongo::ASCENDING], ['at', Mongo::ASCENDING]]
# [['album', Mongo::ASCENDING], ['at', Mongo::ASCENDING]]
# [['picture', Mongo::ASCENDING], ['at', Mongo::ASCENDING]]
Activr.registry.models.each do |model_class|
if !model_class.activr_entity_settings[:feed_index]
# @todo Output a warning to remove the index if it exists
else
fields = [ model_class.activr_entity_feed_actual_name.to_s, 'at' ]

index_name = self.add_activity_index(fields)
yield("activity / #{index_name}") if block_given?
end
end

# Create indexes on '*_timelines' collections for defined timeline classes
#
# eg: user_news_feed_timelines
# [['rcpt', Mongo::ASCENDING], ['activity.at', Mongo::ASCENDING]]
Activr.registry.timelines.each do |timeline_kind, timeline_class|
fields = [ 'rcpt', 'activity.at' ]

index_name = self.add_timeline_index(timeline_kind, fields)
yield("#{timeline_kind} timeline / #{index_name}") if block_given?
end

# Create sparse indexes to remove activities and timeline entries when entity is deleted
#
# eg: activities
# [['actor', Mongo::ASCENDING]], :sparse => true
#
# eg: user_news_feed_timelines
# [['activity.actor', Mongo::ASCENDING]], :sparse => true
# [['activity.album', Mongo::ASCENDING]], :sparse => true
# [['activity.picture', Mongo::ASCENDING]], :sparse => true
Activr.registry.models.each do |model_class|
if model_class.activr_entity_settings[:deletable]
# create sparse index on `activities`
Activr.registry.activity_entities_for_model(model_class).each do |entity_name|
# if entity activity feed is enabled and this is the entity name used to fetch that feed then we can use the existing index...
if !model_class.activr_entity_settings[:feed_index] || (entity_name != model_class.activr_entity_feed_actual_name)
# ... else we create an index
index_name = self.add_activity_index(entity_name.to_s, :sparse => true)
yield("activity / #{index_name}") if block_given?
end
end

# create sparse index on timeline classes where that entity can be present
Activr.registry.timeline_entities_for_model(model_class).each do |timeline_class, entities|
entities.each do |entity_name|
index_name = self.add_timeline_index(timeline_class.kind, "activity.#{entity_name}", :sparse => true)
yield("#{timeline_class.kind} timeline / #{index_name}") if block_given?
end
end
end
end
end

end # class Storage::MongoDriver
65 changes: 65 additions & 0 deletions spec/storage/mongo_driver_spec.rb
@@ -0,0 +1,65 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe Activr::Storage::MongoDriver do

#
# Indexes
#

it "adds an activity index" do
col = Activr.storage.driver.activity_collection

Activr.storage.driver.drop_indexes(col)
Activr.storage.driver.indexes(col).should == [ "_id_" ]

# test
Activr.storage.driver.add_activity_index("foo")

# check
Activr.storage.driver.indexes(col).should == [ "_id_", "foo_1" ]

# test
Activr.storage.driver.add_activity_index([ "bar", "baz" ])

# check
Activr.storage.driver.indexes(col).should == ["_id_", "foo_1", "bar_1_baz_1"]
end

it "adds a timeline index" do
col = Activr.storage.driver.timeline_collection('user_news_feed')

Activr.storage.driver.drop_indexes(col)
Activr.storage.driver.indexes(col).should == [ "_id_" ]

# test
Activr.storage.driver.add_timeline_index('user_news_feed', "foo")

# check
Activr.storage.driver.indexes(col).should == [ "_id_", "foo_1" ]

# test
Activr.storage.driver.add_timeline_index('user_news_feed', [ "bar", "baz" ])

# check
Activr.storage.driver.indexes(col).should == ["_id_", "foo_1", "bar_1_baz_1"]
end

it "create all necessary indexes" do
activities_col = Activr.storage.driver.activity_collection
timelines_col = Activr.storage.driver.timeline_collection('user_news_feed')

Activr.storage.driver.drop_indexes(activities_col)
Activr.storage.driver.indexes(activities_col).should == [ "_id_" ]

Activr.storage.driver.drop_indexes(timelines_col)
Activr.storage.driver.indexes(timelines_col).should == [ "_id_" ]

# test
Activr.storage.driver.create_indexes

# check
Activr.storage.driver.indexes(activities_col).should == [ "_id_", "actor_1_at_1", "picture_1_at_1", "album_1_at_1" ]
Activr.storage.driver.indexes(timelines_col).should == [ "_id_", "rcpt_1_activity.at_1", "activity.picture_1", "activity.album_1" ]
end

end
61 changes: 0 additions & 61 deletions spec/storage_spec.rb
Expand Up @@ -337,67 +337,6 @@
end


#
# Indexes
#

it "adds an activity index" do
col = Activr.storage.driver.activity_collection

Activr.storage.driver.drop_indexes(col)
Activr.storage.driver.indexes(col).should == [ "_id_" ]

# test
Activr.storage.add_activity_index("foo")

# check
Activr.storage.driver.indexes(col).should == [ "_id_", "foo_1" ]

# test
Activr.storage.add_activity_index([ "bar", "baz" ])

# check
Activr.storage.driver.indexes(col).should == ["_id_", "foo_1", "bar_1_baz_1"]
end

it "adds a timeline index" do
col = Activr.storage.driver.timeline_collection('user_news_feed')

Activr.storage.driver.drop_indexes(col)
Activr.storage.driver.indexes(col).should == [ "_id_" ]

# test
Activr.storage.add_timeline_index('user_news_feed', "foo")

# check
Activr.storage.driver.indexes(col).should == [ "_id_", "foo_1" ]

# test
Activr.storage.add_timeline_index('user_news_feed', [ "bar", "baz" ])

# check
Activr.storage.driver.indexes(col).should == ["_id_", "foo_1", "bar_1_baz_1"]
end

it "create all necessary indexes" do
activities_col = Activr.storage.driver.activity_collection
timelines_col = Activr.storage.driver.timeline_collection('user_news_feed')

Activr.storage.driver.drop_indexes(activities_col)
Activr.storage.driver.indexes(activities_col).should == [ "_id_" ]

Activr.storage.driver.drop_indexes(timelines_col)
Activr.storage.driver.indexes(timelines_col).should == [ "_id_" ]

# test
Activr.storage.create_indexes

# check
Activr.storage.driver.indexes(activities_col).should == [ "_id_", "actor_1_at_1", "picture_1_at_1", "album_1_at_1" ]
Activr.storage.driver.indexes(timelines_col).should == [ "_id_", "rcpt_1_activity.at_1", "activity.picture_1", "activity.album_1" ]
end


#
# Hooks
#
Expand Down

0 comments on commit 77f2b7a

Please sign in to comment.