lib/thinking_sphinx.rb in pixeltrix-thinking-sphinx-1.1.5 vs lib/thinking_sphinx.rb in pixeltrix-thinking-sphinx-1.2.1

- old
+ new

@@ -5,21 +5,25 @@ require 'active_record' require 'riddle' require 'after_commit' require 'thinking_sphinx/core/string' +require 'thinking_sphinx/property' require 'thinking_sphinx/active_record' require 'thinking_sphinx/association' require 'thinking_sphinx/attribute' -require 'thinking_sphinx/collection' require 'thinking_sphinx/configuration' +require 'thinking_sphinx/excerpter' require 'thinking_sphinx/facet' -require 'thinking_sphinx/facet_collection' +require 'thinking_sphinx/class_facet' +require 'thinking_sphinx/facet_search' require 'thinking_sphinx/field' require 'thinking_sphinx/index' +require 'thinking_sphinx/source' require 'thinking_sphinx/rails_additions' require 'thinking_sphinx/search' +require 'thinking_sphinx/search_methods' require 'thinking_sphinx/deltas' require 'thinking_sphinx/adapters/abstract_adapter' require 'thinking_sphinx/adapters/mysql_adapter' require 'thinking_sphinx/adapters/postgresql_adapter' @@ -31,12 +35,12 @@ ) if defined?(Merb) module ThinkingSphinx module Version #:nodoc: Major = 1 - Minor = 1 - Tiny = 3 + Minor = 2 + Tiny = 1 String = [Major, Minor, Tiny].join('.') end # A ConnectionError will get thrown when a connection to Sphinx can't be @@ -58,10 +62,14 @@ # that have Sphinx indexes. def self.indexed_models @@indexed_models ||= [] end + def self.unique_id_expression(offset = nil) + "* #{ThinkingSphinx.indexed_models.size} + #{offset || 0}" + end + # Check if index definition is disabled. # def self.define_indexes? @@define_indexes = true unless defined?(@@define_indexes) @@define_indexes == true @@ -122,23 +130,89 @@ # Checks to see if MySQL will allow simplistic GROUP BY statements. If not, # or if not using MySQL, this will return false. # def self.use_group_by_shortcut? - ::ActiveRecord::ConnectionAdapters.constants.include?("MysqlAdapter") && - ::ActiveRecord::Base.connection.is_a?( - ::ActiveRecord::ConnectionAdapters::MysqlAdapter - ) && - ::ActiveRecord::Base.connection.select_all( - "SELECT @@global.sql_mode, @@session.sql_mode;" - ).all? { |key,value| value.nil? || value[/ONLY_FULL_GROUP_BY/].nil? } + !!( + mysql? && ::ActiveRecord::Base.connection.select_all( + "SELECT @@global.sql_mode, @@session.sql_mode;" + ).all? { |key,value| value.nil? || value[/ONLY_FULL_GROUP_BY/].nil? } + ) end + @@remote_sphinx = false + + # An indication of whether Sphinx is running on a remote machine instead of + # the same machine. + # + def self.remote_sphinx? + @@remote_sphinx + end + + # Tells Thinking Sphinx that Sphinx is running on a different machine, and + # thus it can't reliably guess whether it is running or not (ie: the + # #sphinx_running? method), and so just assumes it is. + # + # Useful for multi-machine deployments. Set it in your production.rb file. + # + # ThinkingSphinx.remote_sphinx = true + # + def self.remote_sphinx=(value) + @@remote_sphinx = value + end + + # Check if Sphinx is running. If remote_sphinx is set to true (indicating + # Sphinx is on a different machine), this will always return true, and you + # will have to handle any connection errors yourself. + # def self.sphinx_running? - !!sphinx_pid + remote_sphinx? || sphinx_running_by_pid? end + # Check if Sphinx is actually running, provided the pid is on the same + # machine as this code. + # + def self.sphinx_running_by_pid? + !!sphinx_pid && pid_active?(sphinx_pid) + end + def self.sphinx_pid - pid_file = ThinkingSphinx::Configuration.instance.pid_file - `cat #{pid_file}`[/\d+/] if File.exists?(pid_file) + pid_file = ThinkingSphinx::Configuration.instance.pid_file + cat_command = 'cat' + return nil unless File.exists?(pid_file) + + if microsoft? + pid_file.gsub!('/', '\\') + cat_command = 'type' + end + + `#{cat_command} \"#{pid_file}\"`[/\d+/] end + + def self.pid_active?(pid) + return true if microsoft? + + begin + # In JRuby this returns -1 if the process doesn't exist + Process.getpgid(pid.to_i) != -1 + rescue Exception => e + false + end + end + + def self.microsoft? + RUBY_PLATFORM =~ /mswin/ + end + + def self.jruby? + defined?(JRUBY_VERSION) + end + + def self.mysql? + ::ActiveRecord::Base.connection.class.name.demodulize == "MysqlAdapter" || + ::ActiveRecord::Base.connection.class.name.demodulize == "MysqlplusAdapter" || ( + jruby? && ::ActiveRecord::Base.connection.config[:adapter] == "jdbcmysql" + ) + end + + extend ThinkingSphinx::SearchMethods::ClassMethods end