Module: Ultrasphinx

Defined in:
lib/ultrasphinx/spell.rb,
lib/ultrasphinx/fields.rb,
lib/ultrasphinx/search.rb,
lib/ultrasphinx/configure.rb,
lib/ultrasphinx/ultrasphinx.rb,
lib/ultrasphinx/search/parser.rb,
lib/ultrasphinx/search/internals.rb

Defined Under Namespace

Modules: DateSelf, NumericSelf, Spell Classes: ConfigurationError, Configure, DaemonError, Error, Fields, Search, UsageError

Constant Summary collapse

SUBDIR =

Internal file paths

"config/ultrasphinx"
DIR =
"#{RAILS_ROOT}/#{SUBDIR}"
THIS_DIR =
File.expand_path(File.dirname(__FILE__))
CONF_PATH =
"#{DIR}/#{RAILS_ENV}.conf"
ENV_BASE_PATH =
"#{DIR}/#{RAILS_ENV}.base"
GENERIC_BASE_PATH =
"#{DIR}/default.base"
BASE_PATH =
(File.exist?(ENV_BASE_PATH) ? ENV_BASE_PATH : GENERIC_BASE_PATH)
MAX_INT =

Some miscellaneous constants

2**32-1
MAX_WORDS =

maximum number of stopwords built

2**16
UNIFIED_INDEX_NAME =
"complete"
CONFIG_MAP =
{
  # These must be symbols for key mapping against Rails itself
  :username => 'sql_user',
  :password => 'sql_pass',
  :host => 'sql_host',
  :database => 'sql_db',
  :port => 'sql_port',
  :socket => 'sql_sock'
}
CONNECTION_DEFAULTS =
{
  :host => 'localhost'
}
ADAPTER_SQL_FUNCTIONS =
{
  'mysql' => {
    'group_concat' => "CAST(GROUP_CONCAT(DISTINCT ? SEPARATOR ' ') AS CHAR)",
    'stored_procedures' => {}
  },
  'postgresql' => {
    'group_concat' => "GROUP_CONCAT(?)",
    'stored_procedures' => Hash[*(
      ['hex_to_int', 'group_concat', 'concat_ws', 'unix_timestamp', 'crc32'].map do |name|
        [name, load_stored_procedure(name)]
      end.flatten
      )
    ]
  }      
}
ADAPTER_DEFAULTS =
{
    'mysql' => %(
type = mysql
sql_query_pre = SET SESSION group_concat_max_len = 65535
sql_query_pre = SET NAMES utf8
  ), 
    'postgresql' => %(
type = pgsql
sql_query_pre = ) + ADAPTER_SQL_FUNCTIONS['postgresql']['stored_procedures'].values.join(' ') + %(
  )
}
ADAPTER =
ActiveRecord::Base.connection.instance_variable_get("@config")[:adapter] rescue 'mysql'
INDEXER_SETTINGS =

Introspect on the existing generated conf files

options_for('indexer', BASE_PATH)
CLIENT_SETTINGS =
options_for('client', BASE_PATH)
DAEMON_SETTINGS =
options_for('searchd', BASE_PATH)
SOURCE_SETTINGS =
options_for('source', BASE_PATH)
INDEX_SETTINGS =
options_for('index', BASE_PATH)
DICTIONARY =
CLIENT_SETTINGS['dictionary_name'] || 'ap'
STOPWORDS_PATH =
"#{Ultrasphinx::INDEX_SETTINGS['path']}/#{DICTIONARY}-stopwords.txt"
MODEL_CONFIGURATION =
{}

Class Method Summary collapse

Class Method Details

.load_stored_procedure(name) ⇒ Object



55
56
57
# File 'lib/ultrasphinx/ultrasphinx.rb', line 55

def self.load_stored_procedure(name)
  open("#{THIS_DIR}/postgresql/#{name}.sql").read.gsub(/\s+/, ' ')
end

.options_for(heading, path) ⇒ Object

Configuration file parser.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ultrasphinx/ultrasphinx.rb', line 109

def self.options_for(heading, path)
  section = open(path).read[/^#{heading.gsub('/', '__')}\s*?\{(.*?)\}/m, 1]    
  
  unless section
    Ultrasphinx.say "warning; heading #{heading} not found in #{path}; it may be corrupted. "
    {}
  else      
    options = section.split("\n").map do |line|
      line =~ /\s*(.*?)\s*=\s*([^\#]*)/
      $1 ? [$1, $2.strip] : []
    end      
    Hash[*options.flatten] 
  end
  
end

.say(msg) ⇒ Object

Logger.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ultrasphinx/ultrasphinx.rb', line 95

def self.say msg
  if with_rake
    puts msg[0..0].upcase + msg[1..-1]
  else
    msg = "** ultrasphinx: #{msg}"
    if defined? RAILS_DEFAULT_LOGGER
      RAILS_DEFAULT_LOGGER.warn msg
    else
      STDERR.puts msg
    end
  end
end

.verify_database_nameObject

Complain if the database names go out of sync.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ultrasphinx/ultrasphinx.rb', line 143

def self.verify_database_name
  if File.exist? CONF_PATH
    begin
      if options_for(
        "source #{MODEL_CONFIGURATION.keys.first.tableize}", 
        CONF_PATH
      )['sql_db'] != ActiveRecord::Base.connection.instance_variable_get("@config")[:database]
        say "warning; configured database name is out-of-date"
        say "please run 'rake ultrasphinx:configure'"
      end 
    rescue Object
    end
  end
end