From 4bd16f053847f2efe347ebda9136ef2233ee0d2c Mon Sep 17 00:00:00 2001 From: hukl Date: Tue, 28 Apr 2009 00:15:53 +0200 Subject: added thinking_sphinx plugin for fulltext search on nodes and heads --- .../plugins/thinking-sphinx/spec/sphinx_helper.rb | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 vendor/plugins/thinking-sphinx/spec/sphinx_helper.rb (limited to 'vendor/plugins/thinking-sphinx/spec/sphinx_helper.rb') diff --git a/vendor/plugins/thinking-sphinx/spec/sphinx_helper.rb b/vendor/plugins/thinking-sphinx/spec/sphinx_helper.rb new file mode 100644 index 00000000..4e67523e --- /dev/null +++ b/vendor/plugins/thinking-sphinx/spec/sphinx_helper.rb @@ -0,0 +1,114 @@ +require 'active_record' +prefix = defined?(JRUBY_VERSION) ? "jdbc" : "" +require "active_record/connection_adapters/#{prefix}mysql_adapter" +begin + require "active_record/connection_adapters/#{prefix}postgresql_adapter" +rescue LoadError + # No postgres? no prob... +end +require 'yaml' + +class SphinxHelper + attr_accessor :host, :username, :password + attr_reader :path + + def initialize + @host = "localhost" + @username = "thinking_sphinx" + @password = "" + + if File.exist?("spec/fixtures/database.yml") + config = YAML.load(File.open("spec/fixtures/database.yml")) + @host = config["host"] + @username = config["username"] + @password = config["password"] + end + + @path = File.expand_path(File.dirname(__FILE__)) + end + + def mysql_adapter + defined?(JRUBY_VERSION) ? 'jdbcmysql' : 'mysql' + end + + def setup_mysql + ActiveRecord::Base.establish_connection( + :adapter => mysql_adapter, + :database => 'thinking_sphinx', + :username => @username, + :password => @password, + :host => @host + ) + ActiveRecord::Base.logger = Logger.new(File.open("tmp/activerecord.log", "a")) + + structure = File.open("spec/fixtures/structure.sql") { |f| f.read.chomp } + structure.split(';').each { |table| + ActiveRecord::Base.connection.execute table + } + + File.open("spec/fixtures/data.sql") { |f| + while line = f.gets + ActiveRecord::Base.connection.execute line unless line.blank? + end + } + end + + def setup_sphinx + @configuration = ThinkingSphinx::Configuration.instance.reset + File.open("spec/fixtures/sphinx/database.yml", "w") do |file| + YAML.dump({@configuration.environment => { + :adapter => mysql_adapter, + :host => @host, + :database => "thinking_sphinx", + :username => @username, + :password => @password + }}, file) + end + FileUtils.mkdir_p(@configuration.searchd_file_path) + + @configuration.database_yml_file = "spec/fixtures/sphinx/database.yml" + @configuration.build + + index + end + + def reset + setup_mysql + end + + def index + cmd = "indexer --config #{@configuration.config_file} --all" + cmd << " --rotate" if running? + `#{cmd}` + end + + def start + return if running? + + cmd = "searchd --config #{@configuration.config_file}" + `#{cmd}` + + sleep(1) + + unless running? + puts "Failed to start searchd daemon. Check #{@configuration.searchd_log_file}." + end + end + + def stop + return unless running? + `kill #{pid}` + end + + def pid + if File.exists?("#{@configuration.pid_file}") + `cat #{@configuration.pid_file}`[/\d+/] + else + nil + end + end + + def running? + pid && `ps #{pid} | wc -l`.to_i > 1 + end +end -- cgit v1.3