Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reindexing of works and bookmarks post-wrangling moved into a redis-b…
…ased queue Made sure all works and bookmarks get reindexed when appropriate Cleaned up no-longer-used methods in work/work_test Signed-off-by: shalott <shalott@gmail.com>
- Loading branch information
Showing
9 changed files
with
189 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Queue the ids of works and bookmarks to be reindexed in redis, and reindex them only at intervals | ||
# Usage: | ||
# RedisSearchIndexQueue.queue_work(work) or queue_bookmark(bookmark) | ||
# RedisSearchIndexQueue.queue_works(work_ids) | ||
# RedisSearchIndexQueue.queue_bookmarks(bookmark_ids) | ||
class RedisSearchIndexQueue | ||
|
||
# Reindex an object | ||
def self.reindex(item) | ||
if item.is_a?(Work) | ||
queue_work(item) | ||
elsif item.is_a?(Bookmark) | ||
queue_bookmark(item) | ||
end | ||
end | ||
|
||
|
||
#### WORKS | ||
|
||
WORKS_INDEX_KEY = "search_index_work" | ||
|
||
def self.queue_works(work_ids) | ||
work_ids.each {|id| $redis.sadd(WORKS_INDEX_KEY, id)} | ||
# queue their bookmarks also | ||
queue_bookmarks(Bookmark.where(:bookmarkable_type => "Work", :bookmarkable_id => work_ids).value_of(:id)) | ||
end | ||
|
||
# queue a work to have its search index updated | ||
def self.queue_work(work) | ||
$redis.sadd(WORKS_INDEX_KEY, work.id) | ||
queue_bookmarks(Bookmark.where(:bookmarkable_type => "Work", :bookmarkable_id => work.id).value_of(:id)) | ||
end | ||
|
||
# update the work and its bookmarks | ||
def self.reindex_works | ||
work_ids, resp = $redis.multi do | ||
$redis.smembers(WORKS_INDEX_KEY) | ||
$redis.del(WORKS_INDEX_KEY) | ||
end | ||
|
||
works.where(:id => work_ids).find_each do |w| | ||
w.update_index | ||
end | ||
end | ||
|
||
|
||
#### BOOKMARKS | ||
|
||
BOOKMARKS_INDEX_KEY = "search_index_bookmarks" | ||
|
||
def self.queue_bookmark(bookmark) | ||
$redis.sadd(BOOKMARKS_INDEX_KEY, bookmark.id) | ||
end | ||
|
||
def self.queue_bookmarks(bookmark_ids) | ||
bookmark_ids.each {|id| $redis.sadd(BOOKMARKS_INDEX_KEY, id)} | ||
end | ||
|
||
# reindex the bookmarks | ||
def self.reindex_bookmarks | ||
bookmark_ids, resp = $redis.multi do | ||
$redis.smembers(BOOKMARKS_INDEX_KEY) | ||
$redis.del(BOOKMARKS_INDEX_KEY) | ||
end | ||
|
||
bookmarks.where(:id => bookmark_ids).find_each do |b| | ||
b.update_index | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace :search do | ||
|
||
desc "Reindex works" | ||
task(:reindex_works => :environment) do | ||
RedisSearchIndexQueue.reindex_works | ||
end | ||
|
||
desc "Reindex bookmarks" | ||
task(:reindex_bookmarks => :environment) do | ||
RedisSearchIndexQueue.reindex_bookmarks | ||
end | ||
|
||
end |
Oops, something went wrong.