Skip to content

Commit

Permalink
Updating for multi-index support.
Browse files Browse the repository at this point in the history
  • Loading branch information
pat committed Nov 20, 2009
1 parent 1bb46c0 commit 8c20a5f
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 27 deletions.
4 changes: 3 additions & 1 deletion features/support/env.rb
Expand Up @@ -11,7 +11,9 @@
world = Cucumber::ThinkingSphinx::InternalWorld.new
world.configure_database

require 'thinking_sphinx'
SphinxVersion = ENV['VERSION'] || '0.9.8'

require "thinking_sphinx/#{SphinxVersion}"
require 'thinking_sphinx/deltas/delayed_delta'

world.setup
4 changes: 2 additions & 2 deletions lib/thinking_sphinx/deltas/delayed_delta.rb
Expand Up @@ -34,13 +34,13 @@ def index(model, instance = nil)
return true if skip? instance

ThinkingSphinx::Deltas::Job.enqueue(
ThinkingSphinx::Deltas::DeltaJob.new(delta_index_name(model)),
ThinkingSphinx::Deltas::DeltaJob.new(model.delta_index_names),
ThinkingSphinx::Configuration.instance.delayed_job_priority
)

Delayed::Job.enqueue(
ThinkingSphinx::Deltas::FlagAsDeletedJob.new(
core_index_name(model), instance.sphinx_document_id
model.core_index_names, instance.sphinx_document_id
),
ThinkingSphinx::Configuration.instance.delayed_job_priority
) if instance
Expand Down
8 changes: 4 additions & 4 deletions lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb
@@ -1,14 +1,14 @@
# A simple job class that processes a given index.
#
class ThinkingSphinx::Deltas::DeltaJob
attr_accessor :index
attr_accessor :indexes

# Initialises the object with an index name.
#
# @param [String] index the name of the Sphinx index
#
def initialize(index)
@index = index
def initialize(indexes)
@indexes = indexes
end

# Runs Sphinx's indexer tool to process the index. Currently assumes Sphinx is
Expand All @@ -19,7 +19,7 @@ def initialize(index)
def perform
config = ThinkingSphinx::Configuration.instance

output = `#{config.bin_path}#{config.indexer_binary_name} --config #{config.config_file} --rotate #{index}`
output = `#{config.bin_path}#{config.indexer_binary_name} --config #{config.config_file} --rotate #{indexes.join(' ')}`
puts output unless ThinkingSphinx.suppress_delta_output?

true
Expand Down
20 changes: 11 additions & 9 deletions lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb
Expand Up @@ -2,7 +2,7 @@
# 'deleted'.
#
class ThinkingSphinx::Deltas::FlagAsDeletedJob
attr_accessor :index, :document_id
attr_accessor :indexes, :document_id

# Initialises the object with an index name and document id. Please note that
# the document id is Sphinx's unique identifier, and will almost certainly not
Expand All @@ -11,8 +11,8 @@ class ThinkingSphinx::Deltas::FlagAsDeletedJob
# @param [String] index The index name
# @param [Integer] document_id The document id
#
def initialize(index, document_id)
@index, @document_id = index, document_id
def initialize(indexes, document_id)
@indexes, @document_id = indexes, document_id
end

# Updates the sphinx_deleted attribute for the given document, setting the
Expand All @@ -26,12 +26,14 @@ def initialize(index, document_id)
def perform
config = ThinkingSphinx::Configuration.instance

config.client.update(
@index,
['sphinx_deleted'],
{@document_id => [1]}
) if ThinkingSphinx.sphinx_running? &&
ThinkingSphinx::Search.search_for_id(@document_id, @index)
indexes.each do |index|
config.client.update(
index,
['sphinx_deleted'],
{@document_id => [1]}
) if ThinkingSphinx.sphinx_running? &&
ThinkingSphinx::Search.search_for_id(@document_id, index)
end

true
end
Expand Down
2 changes: 1 addition & 1 deletion lib/thinking_sphinx/deltas/delayed_delta/job.rb
Expand Up @@ -16,7 +16,7 @@ class ThinkingSphinx::Deltas::Job < Delayed::Job
# @param [Integer] priority (0)
#
def self.enqueue(object, priority = 0)
Delayed::Job.enqueue(object, priority) unless duplicates_exist(object)
::Delayed::Job.enqueue(object, priority) unless duplicates_exist(object)
end

# Remove all Thinking Sphinx/Delayed Delta jobs from the queue. If the
Expand Down
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -4,7 +4,9 @@
require 'spec'
require 'spec/autorun'

require 'thinking_sphinx'
SphinxVersion = ENV['VERSION'] || '0.9.8'

require "thinking_sphinx/#{SphinxVersion}"
require 'thinking_sphinx/deltas/delayed_delta'

Spec::Runner.configure do |config|
Expand Down
15 changes: 13 additions & 2 deletions spec/thinking_sphinx/deltas/delayed_delta/delta_job_spec.rb
Expand Up @@ -5,7 +5,7 @@
before :each do
ThinkingSphinx.suppress_delta_output = false

@delta_job = ThinkingSphinx::Deltas::DeltaJob.new('foo_core')
@delta_job = ThinkingSphinx::Deltas::DeltaJob.new(['foo_core'])
@delta_job.stub! :` => true
@delta_job.stub! :puts => nil
end
Expand All @@ -23,13 +23,24 @@
@delta_job.perform
end

it "should process just the requested index" do
it "should process just the requested indexes" do
@delta_job.should_receive(:`) do |command|
command.should match(/foo_core/)
command.should_not match(/--all/)
end

@delta_job.perform
end

context 'multiple indexes' do
it "should process all requested indexes" do
@delta_job.indexes = ['foo_core', 'bar_core']
@delta_job.should_receive(:`) do |command|
command.should match(/foo_core bar_core/)
end

@delta_job.perform
end
end
end
end
Expand Up @@ -10,7 +10,7 @@
ThinkingSphinx::Search.stub!(:search_for_id => true)
ThinkingSphinx.stub!(:sphinx_running? => true)

@job = ThinkingSphinx::Deltas::FlagAsDeletedJob.new('foo_core', 12)
@job = ThinkingSphinx::Deltas::FlagAsDeletedJob.new(['foo_core'], 12)
end

it "should not update if Sphinx isn't running" do
Expand All @@ -35,6 +35,14 @@
@job.perform
end

it "should update all specified indexes" do
@job.indexes = ['foo_core', 'bar_core']
@client.should_receive(:update).with('foo_core', anything, anything)
@client.should_receive(:update).with('bar_core', anything, anything)

@job.perform
end

it "should update the sphinx_deleted attribute" do
@client.should_receive(:update) do |index, attributes, values|
attributes.should == ['sphinx_deleted']
Expand Down
12 changes: 7 additions & 5 deletions spec/thinking_sphinx/deltas/delayed_delta_spec.rb
Expand Up @@ -8,7 +8,7 @@
ThinkingSphinx::Configuration.instance.delayed_job_priority = 2

ThinkingSphinx::Deltas::Job.stub!(:enqueue => true)
Delayed::Job.stub!(:enqueue => true)
Delayed::Job.stub!(:enqueue => true, :inspect => "Delayed::Job")

@delayed_delta = ThinkingSphinx::Deltas::DelayedDelta.new(
stub('instance'), {}
Expand All @@ -18,6 +18,8 @@
@model = stub('foo')
@model.stub!(:name => 'foo')
@model.stub!(:source_of_sphinx_index => @model)
@model.stub!(:core_index_names => ['foo_core'])
@model.stub!(:delta_index_names => ['foo_delta'])

@instance = stub('instance')
@instance.stub!(:sphinx_document_id => 42)
Expand Down Expand Up @@ -77,9 +79,9 @@
end
end

it "should enqueue a delta job for the appropriate index" do
it "should enqueue a delta job for the appropriate indexes" do
ThinkingSphinx::Deltas::Job.should_receive(:enqueue) do |job, priority|
job.index.should == 'foo_delta'
job.indexes.should == ['foo_delta']
end

@delayed_delta.index(@model)
Expand All @@ -93,9 +95,9 @@
@delayed_delta.index(@model)
end

it "should enqueue a flag-as-deleted job for the appropriate index" do
it "should enqueue a flag-as-deleted job for the appropriate indexes" do
Delayed::Job.should_receive(:enqueue) do |job, priority|
job.index.should == 'foo_core'
job.indexes.should == ['foo_core']
end

@delayed_delta.index(@model, @instance)
Expand Down
3 changes: 2 additions & 1 deletion tasks/distribution.rb
Expand Up @@ -11,7 +11,8 @@
gem.homepage = "http://github.com/freelancing-god/ts-delayed-delta"
gem.authors = ["Pat Allan"]

gem.add_dependency 'delayed_job', '>= 1.8.4'
gem.add_dependency 'thinking-sphinx', '>= 1.3.6'
gem.add_dependency 'delayed_job', '>= 1.8.4'

gem.add_development_dependency "rspec", ">= 1.2.9"
gem.add_development_dependency "yard", ">= 0"
Expand Down

0 comments on commit 8c20a5f

Please sign in to comment.