lib/ultrasphinx/ultrasphinx.rb in ultrasphinx-1.5.2 vs lib/ultrasphinx/ultrasphinx.rb in ultrasphinx-1.5.3

- old
+ new

@@ -1,22 +1,24 @@ module Ultrasphinx - class Exception < ::Exception #:nodoc: + class Error < ::StandardError #:nodoc: end - class ConfigurationError < Exception #:nodoc: + class ConfigurationError < Error #:nodoc: end - class DaemonError < Exception #:nodoc: + class DaemonError < Error #:nodoc: end - class UsageError < Exception #:nodoc: + class UsageError < Error #:nodoc: end # Internal file paths SUBDIR = "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" @@ -53,33 +55,38 @@ } CONNECTION_DEFAULTS = { :host => 'localhost' } - - ADAPTER_DEFAULTS = { - 'mysql' => %( -type = mysql -sql_query_pre = SET SESSION group_concat_max_len = 65535 -sql_query_pre = SET NAMES utf8 - ), - 'postgresql' => %( -type = pgsql - )} - + ADAPTER_SQL_FUNCTIONS = { 'mysql' => { 'group_by' => 'GROUP BY id', - 'timestamp' => 'UNIX_TIMESTAMP(' + 'timestamp' => 'UNIX_TIMESTAMP(?)', + 'hash' => 'CRC32(?)' }, 'postgresql' => { 'group_by' => '', - 'timestamp' => 'EXTRACT(EPOCH FROM ' + 'timestamp' => 'EXTRACT(EPOCH FROM ?)', + 'hash' => 'hex_to_int(SUBSTRING(MD5(?) FROM 1 FOR 8))', + 'hash_stored_procedure' => open("#{THIS_DIR}/hex_to_int.sql").read.gsub("\n", ' ') } } - ADAPTER = ActiveRecord::Base.connection.instance_variable_get("@config")[:adapter] + 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']['hash_stored_procedure'] + %( + ) +} + + ADAPTER = ActiveRecord::Base.connection.instance_variable_get("@config")[:adapter] rescue 'mysql' mattr_accessor :with_rake # Logger. def self.say msg @@ -95,11 +102,11 @@ end end # Configuration file parser. def self.options_for(heading, path) - section = open(path).read[/^#{heading}\s*?\{(.*?)\}/m, 1] + 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 @@ -119,11 +126,14 @@ SOURCE_SETTINGS = options_for('source', BASE_PATH) INDEX_SETTINGS = options_for('index', BASE_PATH) # Make sure there's a trailing slash INDEX_SETTINGS['path'] = INDEX_SETTINGS['path'].chomp("/") + "/" + + DICTIONARY = CLIENT_SETTINGS['dictionary_name'] || 'ap' + raise ConfigurationError, "Aspell does not support dictionary names longer than two letters" if DICTIONARY.size > 2 - STOPWORDS_PATH = "#{Ultrasphinx::INDEX_SETTINGS['path']}/stopwords.txt" + STOPWORDS_PATH = "#{Ultrasphinx::INDEX_SETTINGS['path']}/#{DICTIONARY}-stopwords.txt" MODEL_CONFIGURATION = {} # Complain if the database names go out of sync. def self.verify_database_name