shanna-dm-sphinx-adapter 0.5 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/Manifest.txt +12 -19
  2. data/README.txt +30 -38
  3. data/Rakefile +2 -3
  4. data/dm-sphinx-adapter.gemspec +6 -9
  5. data/lib/dm-sphinx-adapter/adapter.rb +87 -73
  6. data/lib/dm-sphinx-adapter/attribute.rb +56 -12
  7. data/lib/dm-sphinx-adapter/index.rb +14 -1
  8. data/lib/dm-sphinx-adapter/query.rb +19 -13
  9. data/lib/dm-sphinx-adapter/resource.rb +20 -13
  10. data/lib/dm-sphinx-adapter.rb +14 -11
  11. data/lib/riddle/client/filter.rb +53 -0
  12. data/lib/riddle/client/message.rb +65 -0
  13. data/lib/riddle/client/response.rb +84 -0
  14. data/lib/riddle/client.rb +619 -0
  15. data/lib/riddle.rb +28 -0
  16. data/test/files/model.rb +23 -0
  17. data/test/files/mysql5.sphinx.conf +97 -0
  18. data/test/files/mysql5.sql +26 -0
  19. data/test/helper.rb +51 -0
  20. data/test/test_adapter.rb +74 -28
  21. data/test/test_attribute.rb +36 -0
  22. data/test/test_index.rb +30 -0
  23. data/test/test_query.rb +47 -32
  24. data/test/test_resource.rb +17 -0
  25. metadata +18 -40
  26. data/lib/dm-sphinx-adapter/client.rb +0 -84
  27. data/lib/dm-sphinx-adapter/config.rb +0 -74
  28. data/lib/dm-sphinx-adapter/config_parser.rb +0 -67
  29. data/test/files/dm_sphinx_adapter_test.sql +0 -21
  30. data/test/files/resource_explicit.rb +0 -25
  31. data/test/files/resource_resource.rb +0 -19
  32. data/test/files/resource_searchable.rb +0 -16
  33. data/test/files/resource_storage_name.rb +0 -11
  34. data/test/files/resource_vanilla.rb +0 -7
  35. data/test/files/sphinx.conf +0 -78
  36. data/test/test_adapter_explicit.rb +0 -48
  37. data/test/test_adapter_resource.rb +0 -25
  38. data/test/test_adapter_searchable.rb +0 -23
  39. data/test/test_adapter_vanilla.rb +0 -46
  40. data/test/test_client.rb +0 -31
  41. data/test/test_config.rb +0 -75
  42. data/test/test_config_parser.rb +0 -29
  43. data/test/test_type_attribute.rb +0 -8
  44. data/test/test_type_index.rb +0 -8
@@ -0,0 +1,97 @@
1
+ # searchd and indexer must be run from the root directory of this lib.
2
+
3
+ indexer
4
+ {
5
+ mem_limit = 64M
6
+ }
7
+
8
+ searchd
9
+ {
10
+ address = localhost
11
+ port = 3312
12
+ log = test/files/tmp/sphinx.log
13
+ query_log = test/files/tmp/sphinx.query.log
14
+ read_timeout = 5
15
+ pid_file = test/files/tmp/sphinx.pid
16
+ max_matches = 1000
17
+ }
18
+
19
+ source items
20
+ {
21
+ type = mysql
22
+ sql_host = localhost
23
+ sql_user = root
24
+ sql_pass =
25
+ sql_db = dm_sphinx_adapter_test
26
+
27
+ sql_query_pre = set names utf8
28
+ sql_query_pre = \
29
+ replace into delta (name, updated_on) ( \
30
+ select 'items', t_datetime \
31
+ from items \
32
+ order by t_datetime desc \
33
+ limit 1\
34
+ )
35
+ sql_query_info = select * from items where id = $id
36
+
37
+ sql_query_pre = set names utf8
38
+ sql_query = \
39
+ select \
40
+ id, \
41
+ t_string, \
42
+ t_text, \
43
+ t_decimal, \
44
+ t_float, \
45
+ t_integer, \
46
+ unix_timestamp(t_datetime) as t_datetime \
47
+ from items \
48
+ where t_datetime <= ( \
49
+ select updated_on \
50
+ from delta \
51
+ where name = 'items' \
52
+ )
53
+
54
+ sql_attr_float = t_decimal
55
+ sql_attr_float = t_float
56
+ sql_attr_uint = t_integer
57
+ sql_attr_timestamp = t_datetime
58
+ }
59
+
60
+ source items_delta : items {
61
+ sql_query_pre = set names utf8
62
+ sql_query_pre =
63
+ sql_query = \
64
+ select \
65
+ id, \
66
+ t_string, \
67
+ t_text, \
68
+ t_decimal, \
69
+ t_float, \
70
+ t_integer, \
71
+ unix_timestamp(t_datetime) as t_datetime \
72
+ from items \
73
+ where t_datetime > ( \
74
+ select updated_on \
75
+ from delta \
76
+ where name = 'items' \
77
+ )
78
+ }
79
+
80
+ index items_main
81
+ {
82
+ source = items
83
+ path = test/files/tmp/items_main
84
+ }
85
+
86
+ index items_delta : items_main
87
+ {
88
+ source = items_delta
89
+ path = test/files/tmp/items_delta
90
+ }
91
+
92
+ index items
93
+ {
94
+ type = distributed
95
+ local = items_main
96
+ local = items_delta
97
+ }
@@ -0,0 +1,26 @@
1
+ drop table if exists delta;
2
+ create table delta (
3
+ name varchar(50) not null,
4
+ updated_on datetime,
5
+ primary key (name)
6
+ ) engine=innodb default charset=utf8;
7
+
8
+ insert into delta (name, updated_on) values
9
+ ('items', now());
10
+
11
+ drop table if exists items;
12
+ create table items (
13
+ id int(11) not null auto_increment,
14
+ t_string varchar(50),
15
+ t_text text,
16
+ t_decimal decimal(30,10),
17
+ t_float float,
18
+ t_integer int,
19
+ t_datetime datetime,
20
+ primary key (id)
21
+ ) engine=innodb default charset=utf8;
22
+
23
+ insert into items (t_string, t_text, t_decimal, t_float, t_integer, t_datetime) values
24
+ ('one', 'text one!', '10.50', '100.50', '1000', now()),
25
+ ('two', 'text two!', '20.50', '200.50', '2000', now()),
26
+ ('three', 'text three!', '30.50', '300.50', '3000', now());
data/test/helper.rb ADDED
@@ -0,0 +1,51 @@
1
+ $VERBOSE = false # Shitloads of warnings in dm :(
2
+ require 'rubygems'
3
+ require 'extlib'
4
+ require 'extlib/hook'
5
+ require 'pathname'
6
+ require 'shoulda'
7
+ require 'test/unit'
8
+
9
+ base = Pathname.new(__FILE__).dirname + '..'
10
+ %w{lib test}.each{|p| $:.unshift base + p}
11
+
12
+ require 'dm-sphinx-adapter'
13
+
14
+ # Sphinx runner.
15
+ Dir.chdir(base)
16
+ config = base + 'test' + 'files' + 'mysql5.sphinx.conf'
17
+ begin
18
+ TCPSocket.new('localhost', '3312')
19
+ rescue
20
+ puts 'Starting Sphinx...'
21
+ system("searchd --config #{config}") || exit
22
+ system('ps aux | grep searchd')
23
+ end
24
+
25
+ class Test::Unit::TestCase
26
+ include Extlib::Hook
27
+
28
+ before :setup do
29
+ files = Pathname.new(__FILE__).dirname + 'files'
30
+
31
+ mysql = `mysql5 dm_sphinx_adapter_test < #{files + 'mysql5.sql'} 2>&1`
32
+ raise %{Re-create database failed:\n #{mysql}} unless mysql.blank?
33
+
34
+ indexer = `indexer --config #{files + 'mysql5.sphinx.conf'} --all --rotate`
35
+ raise %{Re-create index failed:\n #{indexer}} if indexer =~ /error|fatal/i
36
+
37
+ DataMapper.setup(:default, :adapter => 'mysql', :database => 'dm_sphinx_adapter_test')
38
+ sleep 1; # Give sphinx a chance to catch up before test runs.
39
+ end
40
+
41
+ # after :teardown do
42
+ def teardown
43
+ descendants = DataMapper::Resource.descendants.dup.to_a
44
+ while model = descendants.shift
45
+ descendants.concat(model.descendants) if model.respond_to?(:descendants)
46
+ Object.send(:remove_const, model.name.to_sym)
47
+ DataMapper::Resource.descendants.delete(model)
48
+ end
49
+ end
50
+ end
51
+
data/test/test_adapter.rb CHANGED
@@ -1,38 +1,84 @@
1
- require 'dm-sphinx-adapter'
2
- require 'test/unit'
3
-
4
- # DataMapper::Logger.new(STDOUT, :debug)
1
+ require File.join(File.dirname(__FILE__), 'helper')
5
2
 
6
3
  class TestAdapter < Test::Unit::TestCase
7
- def setup
8
- # TODO: A little too brutal even by my standards.
9
- Dir.chdir(File.join(File.dirname(__FILE__), 'files')) do
10
- system 'mysql -u root dm_sphinx_adapter_test < dm_sphinx_adapter_test.sql' \
11
- or raise %q{Tests require the dm_sphinx_adapter_test database.}
4
+ context 'DM::A::Sphinx::Adapter class' do
5
+ setup do
6
+ DataMapper.setup(:adapter, :adapter => 'sphinx')
7
+ load File.join(File.dirname(__FILE__), 'files', 'model.rb')
8
+ @it = repository(:adapter)
9
+ @resource = Item
12
10
  end
13
11
 
14
- DataMapper.setup(:default, 'mysql://localhost/dm_sphinx_adapter_test')
12
+ context '#create' do
13
+ should 'should return zero records created' do
14
+ assert_equal 0, @it.create(create_resource)
15
+ end
16
+ end
15
17
 
16
- @config = Pathname.new(__FILE__).dirname.expand_path / 'files' / 'sphinx.conf'
17
- @client = DataMapper::Adapters::Sphinx::ManagedClient.new(:config => @config)
18
- @client.index
19
- sleep 1
20
- end
18
+ context '#delete' do
19
+ should 'should return zero records deleted' do
20
+ assert_equal 0, @it.delete(create_resource)
21
+ end
22
+ end
21
23
 
22
- def test_unmanaged_setup
23
- assert DataMapper.setup(:sphinx, :adapter => 'sphinx')
24
- assert_kind_of DataMapper::Adapters::SphinxAdapter, repository(:sphinx).adapter
25
- assert_kind_of DataMapper::Adapters::Sphinx::Client, repository(:sphinx).adapter.client
26
- end
24
+ context '#read_many' do
25
+ context 'conditions' do
26
+ should 'return all objects when nil' do
27
+ assert_equal [{:id => 1}, {:id => 2}, {:id => 3}], @it.read_many(query)
28
+ end
27
29
 
28
- def test_managed_setup
29
- assert DataMapper.setup(:sphinx, :adapter => 'sphinx', :config => @config, :managed => true)
30
- assert_kind_of DataMapper::Adapters::SphinxAdapter, repository(:sphinx).adapter
31
- assert_kind_of DataMapper::Adapters::Sphinx::ManagedClient, repository(:sphinx).adapter.client
32
- end
30
+ should 'return subset of objects for conditions' do
31
+ assert_equal [{:id => 2}], @it.read_many(query(:t_string => 'two'))
32
+ end
33
+ end
34
+
35
+ context 'offsets' do
36
+ should 'be able to offset the objects' do
37
+ assert_equal [{:id => 1}, {:id => 2}, {:id => 3}], @it.read_many(query(:offset => 0))
38
+ assert_equal [{:id => 2}, {:id => 3}], @it.read_many(query(:offset => 1))
39
+ assert_equal [], @it.read_many(query(:offset => 3))
40
+ end
41
+ end
42
+
43
+ context 'limits' do
44
+ should 'be able to limit the objects' do
45
+ assert_equal [{:id => 1}], @it.read_many(query(:limit => 1))
46
+ assert_equal [{:id => 1}, {:id => 2}], @it.read_many(query(:limit => 2))
47
+ end
48
+ end
49
+ end
33
50
 
34
- def teardown
35
- @client.stop
36
- sleep 1
51
+ context '#read_one' do
52
+ should 'return the first object of a #read_many' do
53
+ assert_equal @it.read_many(query).first, @it.read_one(query)
54
+
55
+ query = query(:t_string => 'two')
56
+ assert_equal @it.read_many(query).first, @it.read_one(query)
57
+ end
58
+ end
37
59
  end
60
+
61
+ protected
62
+ def query(conditions = {})
63
+ DataMapper::Query.new(repository(:adapter), @resource, conditions)
64
+ end
65
+
66
+ def resource(options = {})
67
+ now = Time.now
68
+ attributes = {
69
+ :t_string => now.to_s,
70
+ :t_text => "text #{now.to_s}!",
71
+ :t_decimal => now.to_i * 0.001,
72
+ :t_float => now.to_i * 0.0001,
73
+ :t_integer => now.to_i,
74
+ :t_datetime => now
75
+ }.update(options)
76
+ @resource.new(attributes)
77
+ end
78
+
79
+ def create_resource(options = {})
80
+ repository(:adapter) do
81
+ @resource.create(resource(options).attributes.except(:id))
82
+ end
83
+ end
38
84
  end
@@ -0,0 +1,36 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TestAttribute < Test::Unit::TestCase
4
+ context 'DM::A::Sphinx::Attribute instance' do
5
+ should 'typecast DateTime to Integer'
6
+ should 'typecast Date to Integer'
7
+ should 'typecast Time to Integer'
8
+ should 'typecast BigDecimal to Float'
9
+ end
10
+
11
+ context 'DM::A::Sphinx::Resource#attribute class method' do
12
+ setup do
13
+ class ::Resource
14
+ include DataMapper::SphinxResource
15
+ end
16
+ end
17
+
18
+ DataMapper::Adapters::Sphinx::Attribute::TYPES.each do |type|
19
+ should "accept a #{type} type" do
20
+ assert_nothing_raised do
21
+ Resource.class_eval do
22
+ attribute :name, type
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ should 'raise ArgumentError for unsupported type' do
29
+ assert_raise(ArgumentError) do
30
+ Resource.class_eval do
31
+ attribute :name, Test::Unit::TestCase
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TestIndex < Test::Unit::TestCase
4
+ context 'DM::A::Sphinx::Index instance' do
5
+ should 'respond to delta?'
6
+ end
7
+
8
+ context 'DM::A::Sphinx::Resource class' do
9
+ setup do
10
+ class ::Resource
11
+ include DataMapper::SphinxResource
12
+ end
13
+ end
14
+
15
+ context '#index method' do
16
+ should 'append an index' do
17
+ assert_nothing_raised do
18
+ Resource.class_eval do
19
+ index :name
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ context '#sphinx_indexes method' do
26
+ should 'return DM::A::Sphinx::Index objects'
27
+ should 'return delta indexes at the end of the list'
28
+ end
29
+ end
30
+ end
data/test/test_query.rb CHANGED
@@ -1,47 +1,62 @@
1
- require 'test/unit'
2
- require 'dm-sphinx-adapter'
3
- require 'files/resource_explicit'
1
+ require File.join(File.dirname(__FILE__), 'helper')
4
2
 
5
3
  class TestQuery < Test::Unit::TestCase
6
- def setup
7
- DataMapper.setup(:default, :adapter => 'sphinx')
8
- @repository = repository(:default)
9
- end
4
+ context 'DM::A::Sphinx::Query conditions' do
5
+ setup do
6
+ DataMapper.setup(:adapter, :adapter => 'sphinx')
7
+ load File.join(File.dirname(__FILE__), 'files', 'model.rb')
8
+ @adapter = repository(:adapter)
9
+ @resource = Item
10
+ end
10
11
 
11
- def test_initialize
12
- assert_nothing_raised{ query }
13
- assert_equal '', query.to_s
14
- end
12
+ should 'treat nil operator as extended field match' do
13
+ assert_equal '@t_string "foo"', query_string(:t_string => 'foo')
14
+ end
15
15
 
16
- def test_eql
17
- assert_equal '@name "foo"', query(:name => 'foo').to_s
18
- assert_equal '@name "foo"', query(:name.eql => 'foo').to_s
19
- assert_equal '@name "foo"', query(:name.like => 'foo').to_s
20
- assert_equal '@name "foo bar"', query(:name => %w(foo bar)).to_s
21
- end
16
+ should 'treat .eql operator as extended field match' do
17
+ assert_equal '@t_string "foo"', query_string(:t_string.eql => 'foo')
18
+ end
22
19
 
23
- def test_not
24
- assert_equal '@name -"foo"', query(:name.not => 'foo').to_s
25
- assert_equal '@name -"foo bar"', query(:name.not => %w(foo bar)).to_s
26
- end
20
+ should 'treat .like operator as extended field match' do
21
+ assert_equal '@t_string "foo"', query_string(:t_string.like => 'foo')
22
+ end
27
23
 
28
- def test_in
29
- assert_equal '@name ("foo" | "bar")', query(:name.in => %w{foo bar}).to_s
30
- end
24
+ should 'treat Array as extended field AND match' do
25
+ assert_equal '@t_string "foo bar"', query_string(:t_string => %w{foo bar})
26
+ end
31
27
 
32
- def test_and
33
- # When is DM going to switch conditions to an array? :(
34
- assert /(?:@name "b" )?@name "a"(?: @name "b")?/.match(query(:name.eql => 'a', :name.eql => 'b').to_s)
35
- end
28
+ should 'treat .not opeartor as extended field NOT match' do
29
+ assert_equal '@t_string -"foo"', query_string(:t_string.not => 'foo')
30
+ end
31
+
32
+ should 'treat Array .not operator as extended field NOT match' do
33
+ assert_equal '@t_string -"foo bar"', query_string(:t_string.not => %w{foo bar})
34
+ end
36
35
 
37
- def test_raw
38
- assert_equal '"foo bar"~10', query(:conditions => ['"foo bar"~10']).to_s
36
+ should 'treat .in operator as extended OR match' do
37
+ assert_equal '@t_string ("foo" | "bar")', query_string(:t_string.in => %w{foo bar})
38
+ end
39
+
40
+ should 'treat multiple .eql operators as AND search' do
41
+ # When is DM going to switch conditions to an array? :(
42
+ assert /(?:@t_string "b" )?@t_string "a"(?: @t_string "b")?/.match(
43
+ query_string(:t_string.eql => 'a', :t_string.eql => 'b')
44
+ )
45
+ end
46
+
47
+ should 'leave raw conditions as they are' do
48
+ assert_equal '"foo bar"~10', query_string(:conditions => ['"foo bar"~10'])
49
+ end
39
50
  end
40
51
 
41
52
  protected
42
53
  def query(conditions = {})
43
54
  DataMapper::Adapters::Sphinx::Query.new(
44
- DataMapper::Query.new(@repository, Explicit, conditions)
55
+ DataMapper::Query.new(@adapter, @resource, conditions)
45
56
  )
46
57
  end
47
- end # TestQuery
58
+
59
+ def query_string(conditions = {})
60
+ query(conditions).to_s
61
+ end
62
+ end
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TestResource < Test::Unit::TestCase
4
+ context 'DM::A::Sphinx::Resource module' do
5
+ setup do
6
+ class ::Resource
7
+ include DataMapper::SphinxResource
8
+ end
9
+ end
10
+
11
+ [:index, :sphinx_indexes, :attribute, :sphinx_attributes].each do |method|
12
+ should "respond to #{method}" do
13
+ assert_respond_to Resource, method
14
+ end
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shanna-dm-sphinx-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.5"
4
+ version: "0.6"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Hanna
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-01 00:00:00 -08:00
12
+ date: 2008-12-13 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -21,15 +21,6 @@ dependencies:
21
21
  - !ruby/object:Gem::Version
22
22
  version: 0.9.7
23
23
  version:
24
- - !ruby/object:Gem::Dependency
25
- name: riddle
26
- version_requirement:
27
- version_requirements: !ruby/object:Gem::Requirement
28
- requirements:
29
- - - ~>
30
- - !ruby/object:Gem::Version
31
- version: "0.9"
32
- version:
33
24
  - !ruby/object:Gem::Dependency
34
25
  name: hoe
35
26
  version_requirement:
@@ -61,32 +52,25 @@ files:
61
52
  - lib/dm-sphinx-adapter.rb
62
53
  - lib/dm-sphinx-adapter/adapter.rb
63
54
  - lib/dm-sphinx-adapter/attribute.rb
64
- - lib/dm-sphinx-adapter/client.rb
65
- - lib/dm-sphinx-adapter/config.rb
66
- - lib/dm-sphinx-adapter/config_parser.rb
67
55
  - lib/dm-sphinx-adapter/index.rb
68
56
  - lib/dm-sphinx-adapter/query.rb
69
57
  - lib/dm-sphinx-adapter/resource.rb
70
- - test/files/dm_sphinx_adapter_test.sql
71
- - test/files/resource_explicit.rb
72
- - test/files/resource_resource.rb
73
- - test/files/resource_searchable.rb
74
- - test/files/resource_storage_name.rb
75
- - test/files/resource_vanilla.rb
76
- - test/files/sphinx.conf
58
+ - lib/riddle.rb
59
+ - lib/riddle/client.rb
60
+ - lib/riddle/client/filter.rb
61
+ - lib/riddle/client/message.rb
62
+ - lib/riddle/client/response.rb
63
+ - test/files/model.rb
64
+ - test/files/mysql5.sphinx.conf
65
+ - test/files/mysql5.sql
66
+ - test/helper.rb
77
67
  - test/test_adapter.rb
78
- - test/test_adapter_explicit.rb
79
- - test/test_adapter_resource.rb
80
- - test/test_adapter_searchable.rb
81
- - test/test_adapter_vanilla.rb
82
- - test/test_client.rb
83
- - test/test_config.rb
84
- - test/test_config_parser.rb
68
+ - test/test_attribute.rb
69
+ - test/test_index.rb
85
70
  - test/test_query.rb
86
- - test/test_type_attribute.rb
87
- - test/test_type_index.rb
71
+ - test/test_resource.rb
88
72
  has_rdoc: true
89
- homepage: http://rubyforge.org/projects/dm-sphinx/
73
+ homepage: http://dm-sphinx.rubyforge.org
90
74
  post_install_message:
91
75
  rdoc_options:
92
76
  - --main
@@ -114,13 +98,7 @@ specification_version: 2
114
98
  summary: A DataMapper Sphinx adapter.
115
99
  test_files:
116
100
  - test/test_adapter.rb
117
- - test/test_adapter_explicit.rb
118
- - test/test_adapter_resource.rb
119
- - test/test_adapter_searchable.rb
120
- - test/test_adapter_vanilla.rb
121
- - test/test_client.rb
122
- - test/test_config.rb
123
- - test/test_config_parser.rb
101
+ - test/test_attribute.rb
102
+ - test/test_index.rb
124
103
  - test/test_query.rb
125
- - test/test_type_attribute.rb
126
- - test/test_type_index.rb
104
+ - test/test_resource.rb
@@ -1,84 +0,0 @@
1
- require 'rubygems'
2
-
3
- gem 'riddle', '~> 0.9'
4
- require 'riddle'
5
-
6
- module DataMapper
7
- module Adapters
8
- module Sphinx
9
- class Client
10
- def initialize(uri_or_options = {})
11
- @config = Sphinx::Config.new(uri_or_options)
12
- end
13
-
14
- ##
15
- # Search one or more indexes.
16
- #
17
- # @param [String] query The sphinx query string.
18
- # @param [Array, String] indexes A string or array of indexes to search. Default is '*' (all).
19
- # @param [Hash] options Any options you'd like to pass through to Riddle::Client.
20
- # @see Riddle::Client
21
- def search(query, indexes = '*', options = {})
22
- indexes = indexes.join(' ') if indexes.kind_of?(Array)
23
-
24
- client = Riddle::Client.new(@config.address, @config.port)
25
- options.each{|k, v| client.method("#{k}=".to_sym).call(v) if client.respond_to?("#{k}=".to_sym)}
26
- client.query(query, indexes.to_s)
27
- end
28
-
29
- ##
30
- # Index one or more indexes.
31
- #
32
- # @param [Array, String] indexes Defaults to --all if indexes is nil or '*'.
33
- def index(indexes = nil, options = {})
34
- indexes = indexes.join(' ') if indexes.kind_of?(Array)
35
-
36
- command = @config.indexer_bin
37
- command << " --rotate" if running?
38
- command << ((indexes.nil? || indexes == '*') ? ' --all' : " #{indexes.to_s}")
39
- warn "Sphinx: Indexer #{$1}" if `#{command}` =~ /(?:error|fatal|warning):?\s*([^\n]+)/i
40
- end
41
-
42
- protected
43
-
44
- ##
45
- # Is the client running.
46
- #
47
- # Tests the address and port set in the configuration file.
48
- def running?
49
- !!TCPSocket.new(@config.address, @config.port) rescue nil
50
- end
51
- end # Client
52
-
53
- ##
54
- # Managed searchd if you don't already have god/monit doing the job for you.
55
- #
56
- # Requires you have daemon_controller installed.
57
- # @see http://github.com/FooBarWidget/daemon_controller/tree/master
58
- class ManagedClient < Client
59
- def initialize(url_or_options = {})
60
- super
61
-
62
- # Fire up searchd.
63
- require 'daemon_controller'
64
- @client = DaemonController.new(
65
- :identifier => 'Sphinx searchd',
66
- :start_command => @config.searchd_bin,
67
- :stop_command => "#{@config.searchd_bin} --stop",
68
- :ping_command => method(:running?),
69
- :pid_file => @config.pid_file,
70
- :log_file => @config.log
71
- )
72
- end
73
-
74
- def search(*args)
75
- @client.connect{super}
76
- end
77
-
78
- def stop
79
- @client.stop if @client.running?
80
- end
81
- end # ManagedClient
82
- end # Sphinx
83
- end # Adapters
84
- end # DataMapper