module ThinkingSphinx

Constants

Version

Public Class Methods

context() click to toggle source
# File lib/thinking_sphinx.rb, line 76
def self.context
  if @@context.nil?
    mutex.synchronize do
      if @@context.nil?
        @@context = ThinkingSphinx::Context.new
        @@context.prepare
      end
    end
  end

  @@context
end
define_indexes=(value) click to toggle source

Enable/disable indexes - you may want to do this while migrating data.

ThinkingSphinx.define_indexes = false
# File lib/thinking_sphinx.rb, line 109
def self.define_indexes=(value)
  mutex.synchronize do
    @@define_indexes = value
  end
end
define_indexes?() click to toggle source

Check if index definition is disabled.

# File lib/thinking_sphinx.rb, line 101
def self.define_indexes?
  @@define_indexes
end
deltas_enabled=(value) click to toggle source

Enable/disable delta indexing.

ThinkingSphinx.deltas_enabled = false
# File lib/thinking_sphinx.rb, line 135
def self.deltas_enabled=(value)
  mutex.synchronize do
    @@deltas_enabled = value
  end
end
deltas_enabled?() click to toggle source

Check if delta indexing is enabled/disabled.

# File lib/thinking_sphinx.rb, line 117
def self.deltas_enabled?
  if @@deltas_enabled.nil?
    mutex.synchronize do
      if @@deltas_enabled.nil?
        @@deltas_enabled = (
          ThinkingSphinx::Configuration.environment != "test"
        )
      end
    end
  end

  @@deltas_enabled && !deltas_suspended?
end
deltas_suspended=(value) click to toggle source

Suspend/resume delta indexing.

ThinkingSphinx.deltas_suspended = false
# File lib/thinking_sphinx.rb, line 155
def self.deltas_suspended=(value)
  Thread.current[:thinking_sphinx_deltas_suspended] = value
end
deltas_suspended?() click to toggle source

Check if delta indexing is suspended.

# File lib/thinking_sphinx.rb, line 143
def self.deltas_suspended?
  if Thread.current[:thinking_sphinx_deltas_suspended].nil?
    Thread.current[:thinking_sphinx_deltas_suspended] = false
  end

  Thread.current[:thinking_sphinx_deltas_suspended]
end
jruby?() click to toggle source
# File lib/thinking_sphinx.rb, line 279
def self.jruby?
  defined?(JRUBY_VERSION)
end
microsoft?() click to toggle source
# File lib/thinking_sphinx.rb, line 275
def self.microsoft?
  RUBY_PLATFORM =~ %rmswin/
end
mutex() click to toggle source
# File lib/thinking_sphinx.rb, line 72
def self.mutex
  @@sphinx_mutex
end
mysql?() click to toggle source
# File lib/thinking_sphinx.rb, line 283
def self.mysql?
  ::ActiveRecord::Base.connection.class.name.demodulize == "MysqlAdapter" ||
  ::ActiveRecord::Base.connection.class.name.demodulize == "Mysql2Adapter" ||
  ::ActiveRecord::Base.connection.class.name.demodulize == "MysqlplusAdapter" || (
    jruby? && ::ActiveRecord::Base.connection.config[:adapter] == "jdbcmysql"
  )
end
pid_active?(pid) click to toggle source
# File lib/thinking_sphinx.rb, line 267
def self.pid_active?(pid)
  !!Process.kill(0, pid.to_i)
rescue Errno::EPERM => e
  true
rescue Exception => e
  false
end
rails_3_1?() click to toggle source
# File lib/thinking_sphinx.rb, line 291
def self.rails_3_1?
  !!defined?(::ActiveRecord::Associations::CollectionProxy)
end
remote_sphinx=(value) click to toggle source

Tells Thinking Sphinx that Sphinx is running on a different machine, and thus it can’t reliably guess whether it is running or not (ie: the sphinx_running? method), and so just assumes it is.

Useful for multi-machine deployments. Set it in your production.rb file.

ThinkingSphinx.remote_sphinx = true
# File lib/thinking_sphinx.rb, line 238
def self.remote_sphinx=(value)
  mutex.synchronize do
    @@remote_sphinx = value
  end
end
remote_sphinx?() click to toggle source

An indication of whether Sphinx is running on a remote machine instead of the same machine.

# File lib/thinking_sphinx.rb, line 226
def self.remote_sphinx?
  @@remote_sphinx
end
reset_context!(context = nil) click to toggle source
# File lib/thinking_sphinx.rb, line 89
def self.reset_context!(context = nil)
  mutex.synchronize do
    @@context = context
  end
end
reset_use_group_by_shortcut() click to toggle source
# File lib/thinking_sphinx.rb, line 217
def self.reset_use_group_by_shortcut
  mutex.synchronize do
    @@use_group_by_shortcut = nil
  end
end
sphinx_pid() click to toggle source
# File lib/thinking_sphinx.rb, line 259
def self.sphinx_pid
  if File.exists?(ThinkingSphinx::Configuration.instance.pid_file)
    File.read(ThinkingSphinx::Configuration.instance.pid_file)[%r\d+/]
  else
    nil
  end
end
sphinx_running?() click to toggle source

Check if Sphinx is running. If remote_sphinx is set to true (indicating Sphinx is on a different machine), this will always return true, and you will have to handle any connection errors yourself.

# File lib/thinking_sphinx.rb, line 248
def self.sphinx_running?
  remote_sphinx? || sphinx_running_by_pid?
end
sphinx_running_by_pid?() click to toggle source

Check if Sphinx is actually running, provided the pid is on the same machine as this code.

# File lib/thinking_sphinx.rb, line 255
def self.sphinx_running_by_pid?
  !!sphinx_pid && pid_active?(sphinx_pid)
end
suppress_delta_output=(value) click to toggle source
# File lib/thinking_sphinx.rb, line 190
def self.suppress_delta_output=(value)
  mutex.synchronize do
    @@suppress_delta_output = value
  end
end
suppress_delta_output?() click to toggle source
# File lib/thinking_sphinx.rb, line 186
def self.suppress_delta_output?
  @@suppress_delta_output
end
unique_id_expression(adapter, offset = nil) click to toggle source
# File lib/thinking_sphinx.rb, line 95
def self.unique_id_expression(adapter, offset = nil)
  "* #{adapter.cast_to_int context.indexed_models.size} + #{offset || 0}"
end
updates_enabled=(value) click to toggle source

Enable/disable updates to Sphinx

ThinkingSphinx.updates_enabled = false
# File lib/thinking_sphinx.rb, line 180
def self.updates_enabled=(value)
  mutex.synchronize do
    @@updates_enabled = value
  end
end
updates_enabled?() click to toggle source

Check if updates are enabled. True by default, unless within the test environment.

# File lib/thinking_sphinx.rb, line 162
def self.updates_enabled?
  if @@updates_enabled.nil?
    mutex.synchronize do
      if @@updates_enabled.nil?
        @@updates_enabled = (
          ThinkingSphinx::Configuration.environment != "test"
        )
      end
    end
  end

  @@updates_enabled
end
use_group_by_shortcut?() click to toggle source

Checks to see if MySQL will allow simplistic GROUP BY statements. If not, or if not using MySQL, this will return false.

# File lib/thinking_sphinx.rb, line 199
def self.use_group_by_shortcut?
  if @@use_group_by_shortcut.nil?
    mutex.synchronize do
      if @@use_group_by_shortcut.nil?
        @@use_group_by_shortcut = !!(
          mysql? && ::ActiveRecord::Base.connection.select_all(
            "SELECT @@global.sql_mode, @@session.sql_mode;"
          ).all? { |key, value|
            value.nil? || value[%rONLY_FULL_GROUP_BY/].nil?
          }
        )
      end
    end
  end

  @@use_group_by_shortcut
end