Class: ThinkingSphinx::Configuration

Inherits:
Riddle::Configuration
  • Object
show all
Defined in:
lib/thinking_sphinx/configuration.rb

Defined Under Namespace

Modules: Defaults Classes: ConsistentIds, DistributedIndices, DuplicateNames, MinimumFields

Constant Summary

@@mutex =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration



12
13
14
15
16
# File 'lib/thinking_sphinx/configuration.rb', line 12

def initialize
  super

  reset
end

Instance Attribute Details

#configuration_fileObject

Returns the value of attribute configuration_file



4
5
6
# File 'lib/thinking_sphinx/configuration.rb', line 4

def configuration_file
  @configuration_file
end

#controllerObject



30
31
32
33
34
35
36
# 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

#index_pathsObject (readonly)

Returns the value of attribute index_paths



5
6
7
# File 'lib/thinking_sphinx/configuration.rb', line 5

def index_paths
  @index_paths
end

#index_set_classObject



61
62
63
# File 'lib/thinking_sphinx/configuration.rb', line 61

def index_set_class
  @index_set_class ||= ThinkingSphinx::IndexSet
end

#indexing_strategyObject



65
66
67
# File 'lib/thinking_sphinx/configuration.rb', line 65

def indexing_strategy
  @indexing_strategy ||= ThinkingSphinx::IndexingStrategies::AllAtOnce
end

#indices_locationObject

Returns the value of attribute indices_location



4
5
6
# File 'lib/thinking_sphinx/configuration.rb', line 4

def indices_location
  @indices_location
end

#versionObject

Returns the value of attribute version



4
5
6
# File 'lib/thinking_sphinx/configuration.rb', line 4

def version
  @version
end

Class Method Details

.instanceObject



18
19
20
# File 'lib/thinking_sphinx/configuration.rb', line 18

def self.instance
  @instance ||= new
end

.resetObject



22
23
24
# File 'lib/thinking_sphinx/configuration.rb', line 22

def self.reset
  @instance = nil
end

Instance Method Details

#bin_pathObject



26
27
28
# File 'lib/thinking_sphinx/configuration.rb', line 26

def bin_path
  settings['bin_path']
end

#engine_index_pathsObject



48
49
50
51
52
# File 'lib/thinking_sphinx/configuration.rb', line 48

def engine_index_paths
  return [] unless defined?(Rails)

  engine_indice_paths.flatten.compact
end

#engine_indice_pathsObject



54
55
56
57
58
59
# 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

#frameworkObject



38
39
40
# File 'lib/thinking_sphinx/configuration.rb', line 38

def framework
  @framework ||= ThinkingSphinx::Frameworks.current
end

#framework=(framework) ⇒ Object



42
43
44
45
46
# File 'lib/thinking_sphinx/configuration.rb', line 42

def framework=(framework)
  @framework = framework
  reset
  framework
end

#indices_for_references(*references) ⇒ Object



69
70
71
# 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) ⇒ Object



73
74
75
# File 'lib/thinking_sphinx/configuration.rb', line 73

def next_offset(reference)
  @offsets[reference] ||= @offsets.keys.count
end

#preload_indicesObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

    if settings['distributed_indices'].nil? || settings['distributed_indices']
      ThinkingSphinx::Configuration::DistributedIndices.new(indices).reconcile
    end

    @preloaded_indices = true
  end
end

#renderObject



95
96
97
98
99
100
101
102
103
# File 'lib/thinking_sphinx/configuration.rb', line 95

def render
  preload_indices

  ThinkingSphinx::Configuration::ConsistentIds.new(indices).reconcile
  ThinkingSphinx::Configuration::MinimumFields.new(indices).reconcile
  ThinkingSphinx::Configuration::DuplicateNames.new(indices).reconcile

  super
end

#render_to_fileObject



105
106
107
108
109
# File 'lib/thinking_sphinx/configuration.rb', line 105

def render_to_file
  FileUtils.mkdir_p searchd.binlog_path unless searchd.binlog_path.blank?

  open(configuration_file, 'w') { |file| file.write render }
end

#settingsObject



111
112
113
# File 'lib/thinking_sphinx/configuration.rb', line 111

def settings
  @settings ||= File.exists?(settings_file) ? settings_to_hash : {}
end

#setupObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/thinking_sphinx/configuration.rb', line 115

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'

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

  configure_searchd

  apply_sphinx_settings!

  @offsets = {}
end