class ThinkingSphinx::Configuration

Attributes

batch_size[RW]
configuration_file[RW]
controller[W]
index_paths[R]
index_set_class[W]
indexing_strategy[W]
indices_location[RW]
version[RW]

Public Class Methods

instance() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 18
def self.instance
  @instance ||= new
end
new() click to toggle source
Calls superclass method
# File lib/thinking_sphinx/configuration.rb, line 12
def initialize
  super

  reset
end
reset() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 22
def self.reset
  @instance = nil
end

Public Instance Methods

bin_path() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 26
def bin_path
  settings['bin_path']
end
controller() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 30
def controller
  @controller ||= begin
    rc = ThinkingSphinx::Controller.new self, configuration_file
    rc.bin_path = bin_path.gsub(/([^\/])$/, '\1/') if bin_path.present?
    rc
  end
end
engine_index_paths() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 48
def engine_index_paths
  return [] unless defined?(Rails)

  engine_indice_paths.flatten.compact.sort
end
engine_indice_paths() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 54
def engine_indice_paths
  Rails::Engine.subclasses.collect(&:instance).collect do |engine|
    engine.paths.add 'app/indices' unless engine.paths['app/indices']
    engine.paths['app/indices'].existent
  end
end
framework() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 38
def framework
  @framework ||= ThinkingSphinx::Frameworks.current
end
framework=(framework) click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 42
def framework=(framework)
  @framework = framework
  reset
  framework
end
index_set_class() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 61
def index_set_class
  @index_set_class ||= ThinkingSphinx::IndexSet
end
indexing_strategy() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 65
def indexing_strategy
  @indexing_strategy ||= ThinkingSphinx::IndexingStrategies::AllAtOnce
end
indices_for_references(*references) click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 69
def indices_for_references(*references)
  index_set_class.new(:references => references).to_a
end
next_offset(reference) click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 73
def next_offset(reference)
  @offsets[reference] ||= @offsets.keys.count
end
preload_indices() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 77
def preload_indices
  @@mutex.synchronize do
    return if @preloaded_indices

    index_paths.each do |path|
      Dir["#{path}/**/*.rb"].sort.each do |file|
        ActiveSupport::Dependencies.require_or_load file
      end
    end

    normalise
    verify

    @preloaded_indices = true
  end
end
render() click to toggle source
Calls superclass method
# File lib/thinking_sphinx/configuration.rb, line 94
def render
  preload_indices

  super
end
render_to_file() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 100
def render_to_file
  FileUtils.mkdir_p searchd.binlog_path unless searchd.binlog_path.blank?

  open(configuration_file, 'w') { |file| file.write render }
end
settings() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 106
def settings
  @settings ||= File.exists?(settings_file) ? settings_to_hash : {}
end
setup() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 110
def setup
  @configuration_file = settings['configuration_file'] || framework_root.join(
    'config', "#{environment}.sphinx.conf"
  ).to_s
  @index_paths = engine_index_paths + [framework_root.join('app', 'indices').to_s]
  @indices_location = settings['indices_location'] || framework_root.join(
    'db', 'sphinx', environment
  ).to_s
  @version = settings['version'] || '2.1.4'
  @batch_size = settings['batch_size'] || 1000

  if settings['common_sphinx_configuration']
    common.common_sphinx_configuration  = true
    indexer.common_sphinx_configuration = true
  end

  configure_searchd

  apply_sphinx_settings!

  @offsets = {}
end

Private Instance Methods

apply_sphinx_settings!() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 135
def apply_sphinx_settings!
  sphinx_sections.each do |object|
    settings.each do |key, value|
      next unless object.class.settings.include?(key.to_sym)

      object.send("#{key}=", value)
    end
  end
end
configure_searchd() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 145
def configure_searchd
  configure_searchd_log_files

  searchd.binlog_path = tmp_path.join('binlog', environment).to_s
  searchd.address = settings['address'].presence || Defaults::ADDRESS
  searchd.mysql41 = settings['mysql41'] || settings['port'] || Defaults::PORT
  searchd.workers = 'threads'
  searchd.mysql_version_string = '5.5.21' if RUBY_PLATFORM == 'java'
end
configure_searchd_log_files() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 155
def configure_searchd_log_files
  searchd.pid_file = log_root.join("#{environment}.sphinx.pid").to_s
  searchd.log = log_root.join("#{environment}.searchd.log").to_s
  searchd.query_log = log_root.join("#{environment}.searchd.query.log").to_s
end
framework_root() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 161
def framework_root
  Pathname.new(framework.root)
end
log_root() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 165
def log_root
  real_path 'log'
end
normalise() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 169
def normalise
  if settings['distributed_indices'].nil? || settings['distributed_indices']
    ThinkingSphinx::Configuration::DistributedIndices.new(indices).reconcile
  end

  ThinkingSphinx::Configuration::ConsistentIds.new(indices).reconcile
  ThinkingSphinx::Configuration::MinimumFields.new(indices).reconcile
end
real_path(*arguments) click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 178
def real_path(*arguments)
  path = framework_root.join(*arguments)
  path.exist? ? path.realpath : path
end
reset() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 183
def reset
  @settings = nil
  setup
end
settings_file() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 188
def settings_file
  framework_root.join 'config', 'thinking_sphinx.yml'
end
settings_to_hash() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 192
def settings_to_hash
  input    = File.read settings_file
  input    = ERB.new(input).result if defined?(ERB)

  contents = YAML.load input
  contents && contents[environment] || {}
end
sphinx_sections() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 200
def sphinx_sections
  sections = [indexer, searchd]
  sections.unshift common if settings['common_sphinx_configuration']
  sections
end
tmp_path() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 206
def tmp_path
  real_path 'tmp'
end
verify() click to toggle source
# File lib/thinking_sphinx/configuration.rb, line 210
def verify
  ThinkingSphinx::Configuration::DuplicateNames.new(indices).reconcile
end