Remove dated config.gem
[gitorious:pkong23s-mainline.git] / vendor / plugins / ultrasphinx / vendor / riddle / spec / sphinx_helper.rb
1 require 'mysql'
2 require 'erb'
3 require 'yaml'
4
5 class SphinxHelper
6   attr_accessor :host, :username, :password
7   attr_reader   :path
8   
9   def initialize
10     @host     = "localhost"
11     @username = "anonymous"
12     @password = ""
13
14     if File.exist?("spec/fixtures/sql/conf.yml")
15       config    = YAML.load(File.open("spec/fixtures/sql/conf.yml"))
16       @host     = config["host"]
17       @username = config["username"]
18       @password = config["password"]
19     end
20     
21     @path = File.expand_path(File.dirname(__FILE__))
22   end
23   
24   def setup_mysql
25     server = Mysql.new @host, @username, @password
26
27     unless server.list_dbs.include?("riddle_sphinx_spec")
28       server.create_db "riddle_sphinx_spec"
29     end
30
31     server.query "USE riddle_sphinx_spec;"
32     
33     structure = File.open("spec/fixtures/sql/structure.sql") { |f| f.read }
34     # Block ensures multiple statements can be run
35     server.query(structure) { }
36     data      = File.open("spec/fixtures/sql/data.sql") { |f|
37       while line = f.gets
38         server.query line
39       end
40     }
41
42     server.close
43   end
44   
45   def reset
46     setup_mysql
47     index
48   end
49   
50   def generate_configuration
51     template = File.open("spec/fixtures/sphinx/configuration.erb") { |f| f.read }
52     File.open("spec/fixtures/sphinx/spec.conf", "w") { |f|
53       f.puts ERB.new(template).result(binding)
54     }
55   end
56   
57   def index
58     cmd = "indexer --config #{@path}/fixtures/sphinx/spec.conf --all"
59     cmd << " --rotate" if running?
60     `#{cmd}`
61   end
62   
63   def start
64     return if running?
65
66     cmd = "searchd --config #{@path}/fixtures/sphinx/spec.conf"
67     `#{cmd}`
68
69     sleep(1)
70
71     unless running?
72       puts "Failed to start searchd daemon. Check fixtures/sphinx/searchd.log."
73     end
74   end
75   
76   def stop
77     return unless running?
78     `kill #{pid}`
79   end
80   
81   def pid
82     if File.exists?("#{@path}/fixtures/sphinx/searchd.pid")
83       `cat #{@path}/fixtures/sphinx/searchd.pid`[/\d+/]
84     else
85       nil
86     end
87   end
88
89   def running?
90     pid && `ps #{pid} | wc -l`.to_i > 1
91   end
92 end