Skip to content

Commit

Permalink
Sphinx 2.0.5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
pat committed Aug 10, 2012
1 parent 96807c7 commit 44e247b
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/thinking_sphinx/auto_version.rb
Expand Up @@ -9,7 +9,7 @@ def self.detect
require 'riddle/1.10' require 'riddle/1.10'
when /2.0.[12]/ when /2.0.[12]/
require 'riddle/2.0.1' require 'riddle/2.0.1'
when /2.0.3/, /2.0.4/, /2.1.\d/ when /2.0.[^12]/, /2.1.\d/
require 'riddle/2.1.0' require 'riddle/2.1.0'
else else
documentation_link = %Q{ documentation_link = %Q{
Expand Down
16 changes: 16 additions & 0 deletions spec/thinking_sphinx/auto_version_spec.rb
Expand Up @@ -62,6 +62,22 @@
ThinkingSphinx::AutoVersion.detect ThinkingSphinx::AutoVersion.detect
end end


it "should require 2.1.0 if using Sphinx 2.0.3" do
ThinkingSphinx::AutoVersion.should_receive(:require).
with('riddle/2.1.0')

@config.stub!(:version => '2.0.4-release')
ThinkingSphinx::AutoVersion.detect
end

it "should require 2.1.0 if using Sphinx 2.0.3" do
ThinkingSphinx::AutoVersion.should_receive(:require).
with('riddle/2.1.0')

@config.stub!(:version => '2.0.5-release')
ThinkingSphinx::AutoVersion.detect
end

it "should require 2.1.0 if using Sphinx 2.1.0 dev" do it "should require 2.1.0 if using Sphinx 2.1.0 dev" do
ThinkingSphinx::AutoVersion.should_receive(:require). ThinkingSphinx::AutoVersion.should_receive(:require).
with('riddle/2.1.0') with('riddle/2.1.0')
Expand Down
2 changes: 1 addition & 1 deletion thinking-sphinx.gemspec
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |s|


s.add_runtime_dependency 'activerecord', ['< 3.0.0', '>= 1.15.6'] s.add_runtime_dependency 'activerecord', ['< 3.0.0', '>= 1.15.6']
s.add_runtime_dependency 'after_commit', '>= 1.0.7' s.add_runtime_dependency 'after_commit', '>= 1.0.7'
s.add_runtime_dependency 'riddle', '>= 1.5.2' s.add_runtime_dependency 'riddle', '>= 1.5.3'


s.add_development_dependency 'appraisal', '0.4.0' s.add_development_dependency 'appraisal', '0.4.0'
s.add_development_dependency 'cucumber', '1.0.2' s.add_development_dependency 'cucumber', '1.0.2'
Expand Down
2 changes: 1 addition & 1 deletion vendor/riddle/lib/riddle/auto_version.rb
Expand Up @@ -10,7 +10,7 @@ def self.configure
require 'riddle/1.10' require 'riddle/1.10'
when /2.0.[12]/ when /2.0.[12]/
require 'riddle/2.0.1' require 'riddle/2.0.1'
when /2.0.3/, /2.0.4/, /2.1.\d/ when /2.0.[^12]/, /2.1.\d/
require 'riddle/2.1.0' require 'riddle/2.1.0'
end end
end end
Expand Down
6 changes: 6 additions & 0 deletions vendor/riddle/lib/riddle/configuration.rb
Expand Up @@ -10,6 +10,8 @@
require 'riddle/configuration/sql_source' require 'riddle/configuration/sql_source'
require 'riddle/configuration/xml_source' require 'riddle/configuration/xml_source'


require 'riddle/configuration/parser'

module Riddle module Riddle
class Configuration class Configuration
class ConfigurationError < StandardError #:nodoc: class ConfigurationError < StandardError #:nodoc:
Expand All @@ -18,6 +20,10 @@ class ConfigurationError < StandardError #:nodoc:
attr_reader :indices, :searchd attr_reader :indices, :searchd
attr_accessor :indexer attr_accessor :indexer


def self.parse!(input)
Riddle::Configuration::Parser.new(input).parse!
end

def initialize def initialize
@indexer = Riddle::Configuration::Indexer.new @indexer = Riddle::Configuration::Indexer.new
@searchd = Riddle::Configuration::Searchd.new @searchd = Riddle::Configuration::Searchd.new
Expand Down
70 changes: 43 additions & 27 deletions vendor/riddle/lib/riddle/configuration/index.rb
Expand Up @@ -16,7 +16,7 @@ def self.settings
:overshort_step, :stopwords_step, :hitless_words :overshort_step, :stopwords_step, :hitless_words
] ]
end end

attr_accessor :name, :parent, :type, :sources, :path, :docinfo, :mlock, attr_accessor :name, :parent, :type, :sources, :path, :docinfo, :mlock,
:morphologies, :dict, :index_sp, :index_zones, :min_stemming_len, :morphologies, :dict, :index_sp, :index_zones, :min_stemming_len,
:stopword_files, :wordform_files, :exception_files, :min_word_len, :stopword_files, :wordform_files, :exception_files, :min_word_len,
Expand All @@ -29,7 +29,7 @@ def self.settings
:inplace_hit_gap, :inplace_docinfo_gap, :inplace_reloc_factor, :inplace_hit_gap, :inplace_docinfo_gap, :inplace_reloc_factor,
:inplace_write_factor, :index_exact_words, :overshort_step, :inplace_write_factor, :index_exact_words, :overshort_step,
:stopwords_step, :hitless_words :stopwords_step, :hitless_words

def initialize(name, *sources) def initialize(name, *sources)
@name = name @name = name
@sources = sources @sources = sources
Expand All @@ -44,86 +44,102 @@ def initialize(name, *sources)
@phrase_boundaries = [] @phrase_boundaries = []
@html_remove_element_tags = [] @html_remove_element_tags = []
end end

def source def source
@sources.collect { |s| s.name } @sources.collect { |s| s.name }
end end

def morphology def morphology
nil_join @morphologies, ", " nil_join @morphologies, ", "
end end

def morphology=(morphology) def morphology=(morphology)
@morphologies = nil_split morphology, /,\s?/ @morphologies = nil_split morphology, /,\s?/
end end

def stopwords def stopwords
nil_join @stopword_files, " " nil_join @stopword_files, " "
end end

def stopwords=(stopwords) def stopwords=(stopwords)
@stopword_files = nil_split stopwords, ' ' @stopword_files = nil_split stopwords, ' '
end end

def wordforms def wordforms
nil_join @wordform_files, " " nil_join @wordform_files, " "
end end

def wordforms=(wordforms) def wordforms=(wordforms)
@wordform_files = nil_split wordforms, ' ' @wordform_files = nil_split wordforms, ' '
end end

def exceptions def exceptions
nil_join @exception_files, " " nil_join @exception_files, " "
end end

def exceptions=(exceptions) def exceptions=(exceptions)
@exception_files = nil_split exceptions, ' ' @exception_files = nil_split exceptions, ' '
end end

def ignore_chars def ignore_chars
nil_join @ignore_characters, ", " nil_join @ignore_characters, ", "
end end

def ignore_chars=(ignore_chars) def ignore_chars=(ignore_chars)
@ignore_characters = nil_split ignore_chars, /,\s?/ @ignore_characters = nil_split ignore_chars, /,\s?/
end end

def prefix_fields def prefix_fields
nil_join @prefix_field_names, ", " nil_join @prefix_field_names, ", "
end end


def prefix_fields=(fields)
if fields.is_a?(Array)
@prefix_field_names = fields
else
@prefix_field_names = fields.split(/,\s*/)
end
end

def infix_fields def infix_fields
nil_join @infix_field_names, ", " nil_join @infix_field_names, ", "
end end


def infix_fields=(fields)
if fields.is_a?(Array)
@infix_field_names = fields
else
@infix_field_names = fields.split(/,\s*/)
end
end

def ngram_chars def ngram_chars
nil_join @ngram_characters, ", " nil_join @ngram_characters, ", "
end end

def ngram_chars=(ngram_chars) def ngram_chars=(ngram_chars)
@ngram_characters = nil_split ngram_chars, /,\s?/ @ngram_characters = nil_split ngram_chars, /,\s?/
end end

def phrase_boundary def phrase_boundary
nil_join @phrase_boundaries, ", " nil_join @phrase_boundaries, ", "
end end

def phrase_boundary=(phrase_boundary) def phrase_boundary=(phrase_boundary)
@phrase_boundaries = nil_split phrase_boundary, /,\s?/ @phrase_boundaries = nil_split phrase_boundary, /,\s?/
end end

def html_remove_elements def html_remove_elements
nil_join @html_remove_element_tags, ", " nil_join @html_remove_element_tags, ", "
end end

def html_remove_elements=(html_remove_elements) def html_remove_elements=(html_remove_elements)
@html_remove_element_tags = nil_split html_remove_elements, /,\s?/ @html_remove_element_tags = nil_split html_remove_elements, /,\s?/
end end

def render def render
raise ConfigurationError, "#{@name} #{@sources.inspect} #{@path} #{@parent}" unless valid? raise ConfigurationError, "#{@name} #{@sources.inspect} #{@path} #{@parent}" unless valid?

inherited_name = "#{name}" inherited_name = "#{name}"
inherited_name << " : #{parent}" if parent inherited_name << " : #{parent}" if parent
( (
Expand All @@ -133,17 +149,17 @@ def render
["}", ""] ["}", ""]
).join("\n") ).join("\n")
end end

def valid? def valid?
(!@name.nil?) && (!( @sources.length == 0 || @path.nil? ) || !@parent.nil?) (!@name.nil?) && (!( @sources.length == 0 || @path.nil? ) || !@parent.nil?)
end end

private private

def nil_split(string, pattern) def nil_split(string, pattern)
(string || "").split(pattern) (string || "").split(pattern)
end end

def nil_join(array, delimiter) def nil_join(array, delimiter)
if array.length == 0 if array.length == 0
nil nil
Expand Down
159 changes: 159 additions & 0 deletions vendor/riddle/lib/riddle/configuration/parser.rb
@@ -0,0 +1,159 @@
# encoding: UTF-8
require 'stringio'

class Riddle::Configuration::Parser
SOURCE_CLASSES = {
'mysql' => Riddle::Configuration::SQLSource,
'pgsql' => Riddle::Configuration::SQLSource,
'mssql' => Riddle::Configuration::SQLSource,
'xmlpipe' => Riddle::Configuration::XMLSource,
'xmlpipe2' => Riddle::Configuration::XMLSource,
'odbc' => Riddle::Configuration::SQLSource
}

INDEX_CLASSES = {
'plain' => Riddle::Configuration::Index,
'distributed' => Riddle::Configuration::DistributedIndex,
'rt' => Riddle::Configuration::RealtimeIndex
}

def initialize(input)
@input = input
end

def parse!
set_indexer
set_searchd
set_sources
set_indices

configuration
end

private

def inner
@inner ||= InnerParser.new(@input).parse!
end

def configuration
@configuration ||= Riddle::Configuration.new
end

def sources
@sources ||= {}
end

def each_with_prefix(prefix)
inner.keys.select { |key| key[/^#{prefix}\s+/] }.each do |key|
yield key.gsub(/^#{prefix}\s+/, ''), inner[key]
end
end

def set_indexer
set_settings configuration.indexer, inner['indexer']
end

def set_searchd
set_settings configuration.searchd, inner['searchd']
end

def set_sources
each_with_prefix 'source' do |name, settings|
names = name.split(/\s*:\s*/)
type = settings.delete('type').first
source = SOURCE_CLASSES[type].new names.first, type
source.parent = names.last if names.length > 1

set_settings source, settings

sources[source.name] = source
end
end

def set_indices
each_with_prefix 'index' do |name, settings|
names = name.split(/\s*:\s*/)
type = (settings.delete('type') || ['plain']).first
index = INDEX_CLASSES[type].new names.first
index.parent = names.last if names.length > 1

(settings.delete('source') || []).each do |source_name|
index.sources << sources[source_name]
end

set_settings index, settings

configuration.indices << index
end
end

def set_settings(object, hash)
hash.each do |key, values|
values.each do |value|
set_setting object, key, value
end
end
end

def set_setting(object, key, value)
if object.send(key).is_a?(Array)
object.send(key) << value
else
object.send "#{key}=", value
end
end

class InnerParser
SETTING_PATTERN = /^(\w+)\s*=\s*(.*)$/

def initialize(input)
@stream = StringIO.new(input)
@sections = {}
end

def parse!
while label = next_line do
@sections[label] = next_settings
end

@sections
end

private

def next_line
line = @stream.gets
return line if line.nil?

line = line.strip
line.empty? ? next_line : line
end

def next_settings
settings = Hash.new { |hash, key| hash[key] = [] }
line = ''
while line.empty? || line == '{' do
line = next_line
end

while line != '}' do
begin
key, value = *SETTING_PATTERN.match(line).captures
settings[key] << value
while value[/\\$/] do
value = next_line
settings[key].last << "\n" << value
end
rescue => error
raise error, "Error handling line '#{line}': #{error.message}",
error.backtrace
end

line = next_line
end

settings
end
end
end

0 comments on commit 44e247b

Please sign in to comment.