Sha256: d5c5faedd78c77feafa26091679c2de4db040c38ae6f50320f4d7646f0b1c3b6

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

class ThinkingSphinx::Deletion
  delegate :name, :to => :index

  def self.perform(index, ids)
    return if index.distributed?

    {
      'plain' => PlainDeletion,
      'rt'    => RealtimeDeletion
    }[index.type].new(index, ids).perform
  rescue ThinkingSphinx::ConnectionError => error
    # This isn't vital, so don't raise the error.
  end

  def initialize(index, ids)
    @index, @ids = index, Array(ids)
  end

  private

  attr_reader :index, :ids

  def document_ids_for_keys
    ids.collect { |id| index.document_id_for_key id }
  end

  def execute(statement)
    ThinkingSphinx::Connection.take do |connection|
      connection.execute statement
    end
  end

  class RealtimeDeletion < ThinkingSphinx::Deletion
    def perform
      execute Riddle::Query::Delete.new(name, document_ids_for_keys).to_sql
    end
  end

  class PlainDeletion < ThinkingSphinx::Deletion
    def perform
      document_ids_for_keys.each_slice(1000) do |document_ids|
        execute <<-SQL
UPDATE #{name}
SET sphinx_deleted = 1
WHERE id IN (#{document_ids.join(', ')})
        SQL
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
thinking-sphinx-3.1.4 lib/thinking_sphinx/deletion.rb
thinking-sphinx-3.1.3 lib/thinking_sphinx/deletion.rb
thinking-sphinx-3.1.2 lib/thinking_sphinx/deletion.rb