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 collapse

@@mutex =
defined?(ActiveSupport::Concurrency::LoadInterlockAwareMonitor) ?
ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new : Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



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

def initialize
  super

  reset
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



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

def batch_size
  @batch_size
end

#configuration_fileObject

Returns the value of attribute configuration_file.



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

def configuration_file
  @configuration_file
end

#controllerObject



34
35
36
37
38
39
40
# File 'lib/thinking_sphinx/configuration.rb', line 34

def controller
  @controller ||= begin
    rc = Riddle::Controller.new self, configuration_file
    rc.bin_path = bin_path.gsub(/([^\/])$/, '\1/') if bin_path.present?
    rc
  end
end

#guarding_strategyObject



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

def guarding_strategy
  @guarding_strategy ||= ThinkingSphinx::Guard::Files
end

#index_pathsObject (readonly)

Returns the value of attribute index_paths.



7
8
9
# File 'lib/thinking_sphinx/configuration.rb', line 7

def index_paths
  @index_paths
end

#index_set_classObject



69
70
71
# File 'lib/thinking_sphinx/configuration.rb', line 69

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

#indexing_strategyObject



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

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

#indices_locationObject

Returns the value of attribute indices_location.



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

def indices_location
  @indices_location
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.instanceObject



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

def self.instance
  @instance ||= new
end

.resetObject



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

def self.reset
  @instance = nil
end

Instance Method Details

#bin_pathObject



30
31
32
# File 'lib/thinking_sphinx/configuration.rb', line 30

def bin_path
  settings['bin_path']
end

#engine_index_pathsObject



52
53
54
55
56
# File 'lib/thinking_sphinx/configuration.rb', line 52

def engine_index_paths
  return [] unless defined?(Rails)

  engine_indice_paths.flatten.compact.sort
end

#engine_indice_pathsObject



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

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



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

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

#framework=(framework) ⇒ Object



46
47
48
49
50
# File 'lib/thinking_sphinx/configuration.rb', line 46

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

#indices_for_references(*references) ⇒ Object



77
78
79
# File 'lib/thinking_sphinx/configuration.rb', line 77

def indices_for_references(*references)
  index_set_class.new(:references => references).to_a
end

#next_offset(reference) ⇒ Object



81
82
83
# File 'lib/thinking_sphinx/configuration.rb', line 81

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

#preload_index(file) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/thinking_sphinx/configuration.rb', line 100

def preload_index(file)
  if ActiveRecord::VERSION::MAJOR <= 5
    ActiveSupport::Dependencies.require_or_load file
  else
    load file
  end
end

#preload_indicesObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/thinking_sphinx/configuration.rb', line 85

def preload_indices
  @@mutex.synchronize do
    return if @preloaded_indices

    index_paths.each do |path|
      Dir["#{path}/**/*.rb"].sort.each { |file| preload_index file }
    end

    normalise
    verify

    @preloaded_indices = true
  end
end

#renderObject



108
109
110
111
112
# File 'lib/thinking_sphinx/configuration.rb', line 108

def render
  preload_indices

  super
end

#render_to_fileObject



114
115
116
117
118
119
120
# File 'lib/thinking_sphinx/configuration.rb', line 114

def render_to_file
  unless settings['skip_directory_creation'] || searchd.binlog_path.blank?
    FileUtils.mkdir_p searchd.binlog_path
  end

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

#settingsObject



122
123
124
# File 'lib/thinking_sphinx/configuration.rb', line 122

def settings
  @settings ||= ThinkingSphinx::Settings.call self
end

#setupObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/thinking_sphinx/configuration.rb', line 126

def setup
  @configuration_file = settings['configuration_file']
  @index_paths = engine_index_paths +
    [Pathname.new(framework.root).join('app', 'indices').to_s]
  @indices_location = settings['indices_location']
  @version = settings['version'] || '2.2.11'
  @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