dm-sphinx-adapter 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,8 @@ lib/dm-sphinx-adapter/sphinx_resource.rb
14
14
  test/data/sphinx.conf
15
15
  test/fixtures/item.rb
16
16
  test/fixtures/item.sql
17
+ test/fixtures/item_resource_explicit.rb
18
+ test/fixtures/item_resource_only.rb
17
19
  test/helper.rb
18
20
  test/test_client.rb
19
21
  test/test_config.rb
data/Rakefile CHANGED
@@ -3,7 +3,7 @@
3
3
  require 'rubygems'
4
4
  require 'hoe'
5
5
 
6
- Hoe.new('dm-sphinx-adapter', '0.3') do |p|
6
+ Hoe.new('dm-sphinx-adapter', '0.4') do |p|
7
7
  p.developer('Shane Hanna', 'shane.hanna@gmail.com')
8
8
  p.extra_deps = [
9
9
  ['dm-core', '~> 0.9.7'],
@@ -1,20 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  Gem::Specification.new do |s|
2
4
  s.name = %q{dm-sphinx-adapter}
3
- s.version = "0.3"
5
+ s.version = "0.4"
4
6
 
5
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
8
  s.authors = ["Shane Hanna"]
7
- s.date = %q{2008-11-18}
9
+ s.date = %q{2008-11-20}
8
10
  s.description = %q{}
9
11
  s.email = ["shane.hanna@gmail.com"]
10
12
  s.extra_rdoc_files = ["History.txt", "LICENCE.txt", "Manifest.txt", "README.txt"]
11
- s.files = ["History.txt", "LICENCE.txt", "Manifest.txt", "README.txt", "Rakefile", "dm-sphinx-adapter.gemspec", "lib/dm-sphinx-adapter.rb", "lib/dm-sphinx-adapter/sphinx_adapter.rb", "lib/dm-sphinx-adapter/sphinx_attribute.rb", "lib/dm-sphinx-adapter/sphinx_client.rb", "lib/dm-sphinx-adapter/sphinx_config.rb", "lib/dm-sphinx-adapter/sphinx_index.rb", "lib/dm-sphinx-adapter/sphinx_resource.rb", "test/data/sphinx.conf", "test/fixtures/item.rb", "test/fixtures/item.sql", "test/helper.rb", "test/test_client.rb", "test/test_config.rb", "test/test_search.rb"]
13
+ s.files = ["History.txt", "LICENCE.txt", "Manifest.txt", "README.txt", "Rakefile", "dm-sphinx-adapter.gemspec", "lib/dm-sphinx-adapter.rb", "lib/dm-sphinx-adapter/sphinx_adapter.rb", "lib/dm-sphinx-adapter/sphinx_attribute.rb", "lib/dm-sphinx-adapter/sphinx_client.rb", "lib/dm-sphinx-adapter/sphinx_config.rb", "lib/dm-sphinx-adapter/sphinx_index.rb", "lib/dm-sphinx-adapter/sphinx_resource.rb", "test/data/sphinx.conf", "test/fixtures/item.rb", "test/fixtures/item.sql", "test/fixtures/item_resource_explicit.rb", "test/fixtures/item_resource_only.rb", "test/helper.rb", "test/test_client.rb", "test/test_config.rb", "test/test_search.rb"]
12
14
  s.has_rdoc = true
13
15
  s.homepage = %q{A Sphinx DataMapper adapter.}
14
16
  s.rdoc_options = ["--main", "README.txt"]
15
17
  s.require_paths = ["lib"]
16
18
  s.rubyforge_project = %q{dm-sphinx-adapter}
17
- s.rubygems_version = %q{1.2.0}
19
+ s.rubygems_version = %q{1.3.0}
18
20
  s.summary = %q{}
19
21
  s.test_files = ["test/test_client.rb", "test/test_config.rb", "test/test_search.rb"]
20
22
 
@@ -22,7 +24,7 @@ Gem::Specification.new do |s|
22
24
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
25
  s.specification_version = 2
24
26
 
25
- if current_version >= 3 then
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
28
  s.add_runtime_dependency(%q<dm-core>, ["~> 0.9.7"])
27
29
  s.add_runtime_dependency(%q<riddle>, ["~> 0.9"])
28
30
  s.add_development_dependency(%q<hoe>, [">= 1.8.2"])
@@ -87,8 +87,7 @@ module DataMapper
87
87
  def indexes(model)
88
88
  indexes = model.sphinx_indexes(repository(self.name).name) if model.respond_to?(:sphinx_indexes)
89
89
  if indexes.nil? or indexes.empty?
90
- # TODO: Is it resource_naming_convention.call(model.name) ?
91
- indexes = [SphinxIndex.new(model, Extlib::Inflection.tableize(model.name))]
90
+ indexes = [SphinxIndex.new(model, model.storage_name)]
92
91
  end
93
92
  indexes
94
93
  end
@@ -110,10 +109,11 @@ module DataMapper
110
109
  search = search_query(query)
111
110
  options = {
112
111
  :match_mode => :extended, # TODO: Modes!
113
- :limit => (query.limit ? query.limit.to_i : 0),
114
- :offset => (query.offset ? query.offset.to_i : 0),
115
112
  :filters => search_filters(query) # By attribute.
116
113
  }
114
+ options[:limit] = query.limit.to_i if query.limit
115
+ options[:offset] = query.offset.to_i if query.offset
116
+
117
117
  if order = search_order(query)
118
118
  options.update(
119
119
  :sort_mode => :extended,
@@ -127,7 +127,7 @@ module DataMapper
127
127
  DataMapper.logger.info(
128
128
  %q{Sphinx (%.3f): search '%s' in '%s' found %d documents} % [res[:time], search, from, res[:total]]
129
129
  )
130
- res[:matches].map{|doc| doc[:doc]}
130
+ res[:matches].map{|doc| {:id => doc[:doc]}}
131
131
  end
132
132
 
133
133
  ##
@@ -6,10 +6,17 @@ require 'riddle'
6
6
  module DataMapper
7
7
  class SphinxClient
8
8
  def initialize(uri_or_options)
9
+ # TODO: Documentation.
9
10
  @config = SphinxConfig.new(uri_or_options)
10
11
  end
11
12
 
12
- # TODO: What about filters?
13
+ ##
14
+ # Search one or more indexes.
15
+ #
16
+ # @param [String] query The sphinx query string.
17
+ # @param [Array, String] indexes A string or array of indexes to search. Default is '*' (all).
18
+ # @param [Hash] options Any options you'd like to pass through to Riddle::Client.
19
+ # @see Riddle::Client
13
20
  def search(query, indexes = '*', options = {})
14
21
  indexes = indexes.join(' ') if indexes.kind_of?(Array)
15
22
 
@@ -64,9 +71,7 @@ module DataMapper
64
71
  end
65
72
 
66
73
  def search(*args)
67
- @client.connect do
68
- super *args
69
- end
74
+ @client.connect{super}
70
75
  end
71
76
 
72
77
  def stop
@@ -13,7 +13,20 @@ module DataMapper
13
13
  # This class just gives you access to handy searchd {} configuration options. It does not validate your
14
14
  # configuration file beyond basic syntax checking.
15
15
  def initialize(uri_or_options = {})
16
- @config = []
16
+ @config = []
17
+ @searchd = {
18
+ 'address' => '0.0.0.0',
19
+ 'log' => 'searchd.log',
20
+ 'max_children' => 0,
21
+ 'max_matches' => 1000,
22
+ 'pid_file' => nil,
23
+ 'port' => 3312,
24
+ 'preopen_indexes' => 0,
25
+ 'query_log' => '',
26
+ 'read_timeout' => 5,
27
+ 'seamless_rotate' => 1,
28
+ 'unlink_old' => 1
29
+ }
17
30
 
18
31
  path = case uri_or_options
19
32
  when Addressable::URI, DataObjects::URI then uri_or_options.path
@@ -75,26 +88,8 @@ module DataMapper
75
88
  ##
76
89
  # Searchd configuration options.
77
90
  #
78
- # Defaults will be applied but no validation is done.
79
- #
80
91
  # @see http://www.sphinxsearch.com/doc.html#confgroup-searchd
81
92
  def searchd
82
- unless @searchd
83
- searchd = @blocks.find{|c| c['type'] =~ /searchd/i} || {}
84
- @searchd = {
85
- 'address' => '0.0.0.0',
86
- 'log' => 'searchd.log',
87
- 'max_children' => 0,
88
- 'max_matches' => 1000,
89
- 'pid_file' => nil,
90
- 'port' => 3312,
91
- 'preopen_indexes' => 0,
92
- 'query_log' => '',
93
- 'read_timeout' => 5,
94
- 'seamless_rotate' => 1,
95
- 'unlink_old' => 1
96
- }.update(searchd)
97
- end
98
93
  @searchd
99
94
  end
100
95
 
@@ -105,6 +100,7 @@ module DataMapper
105
100
  #
106
101
  # @param [String] path Searches path, ./path, /path, /usr/local/etc/sphinx.conf, ./sphinx.conf in that order.
107
102
  def parse(path = '')
103
+ # TODO: Three discrete things going on here, should be three subs.
108
104
  paths = [
109
105
  path,
110
106
  path.gsub(%r{^/}, './'),
@@ -121,7 +117,11 @@ module DataMapper
121
117
  source.gsub!(/\r\n|\r|\n/, "\n") # Everything in \n
122
118
  source.gsub!(/\s*\\\n\s*/, ' ') # Remove unixy line wraps.
123
119
  @in = StringScanner.new(source)
124
- blocks(@blocks = [])
120
+ blocks(blocks = [])
121
+ @in = nil
122
+
123
+ searchd = blocks.find{|c| c['type'] =~ /searchd/i} || {}
124
+ @searchd.update(searchd)
125
125
  end
126
126
 
127
127
  private
@@ -4,27 +4,10 @@ require 'zlib'
4
4
 
5
5
  class Item
6
6
  include DataMapper::Resource
7
- include DataMapper::SphinxResource
8
-
9
- property :id, Integer, :key => true, :writer => :private
10
- property :name, String, :nullable => false, :length => 50
11
- property :likes, Text
7
+ property :id, Serial
8
+ property :name, String, :nullable => false, :length => 50
9
+ property :likes, Text
12
10
  property :updated_on, DateTime
13
11
 
14
12
  is :searchable
15
- repository(:search) do
16
- index :items
17
- index :items_delta, :delta => true
18
-
19
- # TODO: More attributes.
20
- attribute :updated, DateTime
21
- end
22
-
23
- # I'm using my own (unreleased) Digest::CRC32 DataMapper::Type normally.
24
- after :name, :set_id
25
-
26
- protected
27
- def set_id
28
- attribute_set(:id, Zlib.crc32(name))
29
- end
30
- end
13
+ end # Item
@@ -1,5 +1,4 @@
1
1
  drop table if exists delta;
2
-
3
2
  create table delta (
4
3
  name varchar(50) not null,
5
4
  updated_on datetime,
@@ -7,9 +6,8 @@ create table delta (
7
6
  ) engine=innodb default charset=utf8;
8
7
 
9
8
  drop table if exists items;
10
-
11
9
  create table items (
12
- id int(11) not null,
10
+ id int(11) not null auto_increment,
13
11
  name varchar(50) not null,
14
12
  likes text not null,
15
13
  updated_on datetime,
@@ -17,7 +15,7 @@ create table items (
17
15
  index (updated_on)
18
16
  ) engine=innodb default charset=utf8;
19
17
 
20
- insert into items (id, name, likes, updated_on) values
21
- (CRC32('foo'), 'foo', 'I really like foo!', now()),
22
- (CRC32('bar'), 'bar', 'I really like bar!', now()),
23
- (CRC32('baz'), 'baz', 'I really like baz!', now());
18
+ insert into items (name, likes, updated_on) values
19
+ ('foo', 'I really like foo!', now()),
20
+ ('bar', 'I really like bar!', now()),
21
+ ('baz', 'I really like baz!', now());
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'dm-is-searchable'
3
+ require 'zlib'
4
+
5
+ class ItemResourceExplicit
6
+ include DataMapper::Resource
7
+ include DataMapper::SphinxResource
8
+
9
+ property :id, Serial
10
+ property :name, String, :nullable => false, :length => 50
11
+ property :likes, Text
12
+ property :updated_on, DateTime
13
+
14
+ is :searchable
15
+ repository(:search) do
16
+ properties(:search).clear
17
+ index :items
18
+ index :items_delta, :delta => true
19
+ property :name, String
20
+ attribute :updated, DateTime
21
+ end
22
+
23
+ def self.default_storage_name
24
+ 'item'
25
+ end
26
+ end # ItemResourceExplicit
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'dm-is-searchable'
3
+ require 'zlib'
4
+
5
+ class ItemResourceOnly
6
+ include DataMapper::Resource
7
+ include DataMapper::SphinxResource
8
+
9
+ property :id, Serial
10
+ property :name, String, :nullable => false, :length => 50
11
+ property :likes, Text
12
+ property :updated_on, DateTime
13
+
14
+ is :searchable
15
+
16
+ def self.default_storage_name
17
+ 'item'
18
+ end
19
+ end # ItemResourceOnly
20
+
@@ -1,4 +1,7 @@
1
1
  require 'dm-sphinx-adapter'
2
2
  require 'test/fixtures/item'
3
+ require 'test/fixtures/item_resource_only'
4
+ require 'test/fixtures/item_resource_explicit'
3
5
  require 'test/unit'
4
6
 
7
+ # DataMapper::Logger.new(STDOUT, :debug)
@@ -1,5 +1,6 @@
1
1
  require 'helper'
2
2
 
3
+
3
4
  class TestSearch < Test::Unit::TestCase
4
5
  def setup
5
6
  # TODO: A little too brutal for me.
@@ -27,11 +28,25 @@ class TestSearch < Test::Unit::TestCase
27
28
  def test_search
28
29
  assert_nothing_raised{ Item.search }
29
30
  assert_nothing_raised{ Item.search(:name => 'foo') }
31
+ assert !Item.search(:name => 'foo').empty?
32
+ end
33
+
34
+ def test_search_resource_only
35
+ assert_nothing_raised{ ItemResourceOnly.search }
36
+ assert_nothing_raised{ ItemResourceOnly.search(:name => 'foo') }
37
+ assert !ItemResourceOnly.search(:name => 'foo').empty?
38
+ end
39
+
40
+ def test_search_resource_explicit
41
+ assert_nothing_raised{ ItemResourceExplicit.search }
42
+ assert_nothing_raised{ ItemResourceExplicit.search(:name => 'foo') }
43
+ assert !ItemResourceExplicit.search(:name => 'foo').empty?
30
44
  end
31
45
 
32
46
  def test_search_attributes
47
+ # Attributes that exist only in :search and the sphinx.
33
48
  assert_nothing_raised do
34
- Item.search(:updated => (Time.now - 10 .. Time.now + 10))
49
+ ItemResourceExplicit.search(:updated => (Time.now - 10 .. Time.now + 10))
35
50
  end
36
51
  end
37
52
  end # TestSearch
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-sphinx-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.3"
4
+ version: "0.4"
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-11-18 00:00:00 +11:00
12
+ date: 2008-11-20 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -71,6 +71,8 @@ files:
71
71
  - test/data/sphinx.conf
72
72
  - test/fixtures/item.rb
73
73
  - test/fixtures/item.sql
74
+ - test/fixtures/item_resource_explicit.rb
75
+ - test/fixtures/item_resource_only.rb
74
76
  - test/helper.rb
75
77
  - test/test_client.rb
76
78
  - test/test_config.rb