lib/thinking_sphinx.rb in dpickett-thinking-sphinx-1.1.4 vs lib/thinking_sphinx.rb in dpickett-thinking-sphinx-1.1.12

- old
+ new

@@ -5,10 +5,11 @@ 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' @@ -33,11 +34,11 @@ module ThinkingSphinx module Version #:nodoc: Major = 1 Minor = 1 - Tiny = 4 + Tiny = 12 String = [Major, Minor, Tiny].join('.') end # A ConnectionError will get thrown when a connection to Sphinx can't be @@ -59,10 +60,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 @@ -123,23 +128,54 @@ # 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 def self.sphinx_running? - !!sphinx_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" || ( + jruby? && ::ActiveRecord::Base.connection.config[:adapter] == "jdbcmysql" + ) end end