public
Rubygem
Fork of freelancing-god/thinking-sphinx
Description: Sphinx plugin for Rails and Merb
Homepage: http://ts.freelancing-gods.com
Clone URL: git://github.com/jaikoo/thinking-sphinx.git
Added check to whether model instance exists within the core index before 
trying to update the deleted attribute
Tue Jun 24 14:41:15 -0700 2008
commit  b47e521b54d14315776bff5d8a56a15e7f0218ef
tree    836be3c062c8f33e25e71a523f0506866eaa9e0a
parent  19c7541a1b99d4933537dbe2f1b7fcf2e4f9fccf
...
114
115
116
 
 
 
 
117
118
119
...
122
123
124
125
 
126
127
128
...
114
115
116
117
118
119
120
121
122
123
...
126
127
128
 
129
130
131
132
0
@@ -114,6 +114,10 @@ module ThinkingSphinx
0
       )
0
     end
0
     
0
+ def in_core_index?
0
+ @in_core_index ||= self.class.search_for_id(self.id, "#{self.class.name.downcase}_core")
0
+ end
0
+
0
     def toggle_deleted
0
       config = ThinkingSphinx::Configuration.new
0
       client = Riddle::Client.new config.address, config.port
0
@@ -122,7 +126,7 @@ module ThinkingSphinx
0
         "#{self.class.indexes.first.name}_core",
0
         ['sphinx_deleted'],
0
         {self.id => 1}
0
- )
0
+ ) if self.in_core_index?
0
       
0
       client.update(
0
         "#{self.class.indexes.first.name}_delta",
...
28
29
30
 
 
 
 
 
 
 
31
32
33
...
28
29
30
31
32
33
34
35
36
37
38
39
40
0
@@ -28,6 +28,13 @@ module ThinkingSphinx
0
               args << options
0
               ThinkingSphinx::Search.search(*args)
0
             end
0
+
0
+ def search_for_id(*args)
0
+ options = args.extract_options!
0
+ options[:class] = self
0
+ args << options
0
+ ThinkingSphinx::Search.search_for_id(*args)
0
+ end
0
           end
0
         end
0
       end
...
182
183
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
186
187
...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
0
@@ -182,6 +182,35 @@ module ThinkingSphinx
0
         end
0
       end
0
       
0
+ # Checks if a document with the given id exists within a specific index.
0
+ # Expected parameters:
0
+ #
0
+ # - ID of the document
0
+ # - Index to check within
0
+ # - Options hash (defaults to {})
0
+ #
0
+ # Example:
0
+ #
0
+ # ThinkingSphinx::Search.search_for_id(10, "user_core", :class => User)
0
+ #
0
+ def search_for_id(*args)
0
+ options = args.extract_options!
0
+ client = client_from_options options
0
+
0
+ query, filters = search_conditions(
0
+ options[:class], options[:conditions] || {}
0
+ )
0
+ client.filters += filters
0
+ client.match_mode = :extended unless query.empty?
0
+ client.id_range = args.first..args.first
0
+
0
+ begin
0
+ return client.query(query, args[1])[:matches].length > 0
0
+ rescue Errno::ECONNREFUSED => err
0
+ raise ThinkingSphinx::ConnectionError, "Connection to Sphinx Daemon (searchd) failed."
0
+ end
0
+ end
0
+
0
       private
0
       
0
       # This method handles the common search functionality, and returns both
...
9
10
11
 
 
 
 
12
13
14
15
16
17
18
19
20
21
22
23
...
40
41
42
43
44
45
46
47
48
49
...
60
61
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
64
...
9
10
11
12
13
14
15
16
17
18
19
20
 
 
 
 
21
22
23
...
40
41
42
 
 
 
 
43
44
45
...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
0
@@ -9,15 +9,15 @@ describe "ThinkingSphinx::ActiveRecord::Search" do
0
     ActiveRecord::Base.methods.should include("search")
0
   end
0
   
0
+ it "should add search_for_id to ActiveRecord::Base" do
0
+ ActiveRecord::Base.methods.should include("search_for_id")
0
+ end
0
+
0
   describe "search_for_ids method" do
0
     before :each do
0
       ThinkingSphinx::Search.stub_method(:search_for_ids => true)
0
     end
0
     
0
- after :each do
0
- ThinkingSphinx::Search.unstub_method(:search_for_ids)
0
- end
0
-
0
     it "should call ThinkingSphinx::Search#search_for_ids with the class option set" do
0
       Person.search_for_ids("search")
0
       
0
@@ -40,10 +40,6 @@ describe "ThinkingSphinx::ActiveRecord::Search" do
0
       ThinkingSphinx::Search.stub_method(:search => true)
0
     end
0
     
0
- after :each do
0
- ThinkingSphinx::Search.unstub_method(:search)
0
- end
0
-
0
     it "should call ThinkingSphinx::Search#search with the class option set" do
0
       Person.search("search")
0
       
0
@@ -60,4 +56,26 @@ describe "ThinkingSphinx::ActiveRecord::Search" do
0
       )
0
     end
0
   end
0
+
0
+ describe "search_for_id method" do
0
+ before :each do
0
+ ThinkingSphinx::Search.stub_method(:search_for_id => true)
0
+ end
0
+
0
+ it "should call ThinkingSphinx::Search#search with the class option set" do
0
+ Person.search_for_id(10)
0
+
0
+ ThinkingSphinx::Search.should have_received(:search_for_id).with(
0
+ 10, :class => Person
0
+ )
0
+ end
0
+
0
+ it "should override the class option" do
0
+ Person.search_for_id(10, :class => Friendship)
0
+
0
+ ThinkingSphinx::Search.should have_received(:search_for_id).with(
0
+ 10, :class => Person
0
+ )
0
+ end
0
+ end
0
 end
0
\ No newline at end of file
...
100
101
102
 
 
 
 
 
 
 
 
 
 
103
104
105
...
112
113
114
 
115
116
117
118
119
120
121
 
 
 
 
 
122
123
124
...
128
129
130
131
 
132
133
134
...
136
137
138
 
 
 
 
 
 
 
 
 
 
139
140
141
...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
...
122
123
124
125
126
127
 
 
 
 
 
128
129
130
131
132
133
134
135
...
139
140
141
 
142
143
144
145
...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
0
@@ -100,6 +100,16 @@ describe "ThinkingSphinx::ActiveRecord" do
0
     end
0
   end
0
   
0
+ describe "in_core_index? method" do
0
+ it "should return the model's corresponding search_for_id value" do
0
+ Person.stub_method(:search_for_id => :searching_for_id)
0
+
0
+ person = Person.find(:first)
0
+ person.in_core_index?.should == :searching_for_id
0
+ Person.should have_received(:search_for_id).with(person.id, "person_core")
0
+ end
0
+ end
0
+
0
   describe "toggle_deleted method" do
0
     before :each do
0
       @configuration = ThinkingSphinx::Configuration.stub_instance(
0
@@ -112,13 +122,14 @@ describe "ThinkingSphinx::ActiveRecord" do
0
       ThinkingSphinx::Configuration.stub_method(:new => @configuration)
0
       Riddle::Client.stub_method(:new => @client)
0
       Person.indexes.each { |index| index.stub_method(:delta? => false) }
0
+ @person.stub_method(:in_core_index? => true)
0
     end
0
     
0
- after :each do
0
- ThinkingSphinx::Configuration.unstub_method(:new)
0
- Riddle::Client.unstub_method(:new)
0
- Person.indexes.each { |index| index.unstub_method(:delta?) }
0
- end
0
+ # after :each do
0
+ # ThinkingSphinx::Configuration.unstub_method(:new)
0
+ # Riddle::Client.unstub_method(:new)
0
+ # Person.indexes.each { |index| index.unstub_method(:delta?) }
0
+ # end
0
     
0
     it "should create a client using the Configuration's address and port" do
0
       @person.toggle_deleted
0
@@ -128,7 +139,7 @@ describe "ThinkingSphinx::ActiveRecord" do
0
       )
0
     end
0
     
0
- it "should update the core index's deleted flag" do
0
+ it "should update the core index's deleted flag if in core index" do
0
       @person.toggle_deleted
0
       
0
       @client.should have_received(:update).with(
0
@@ -136,6 +147,16 @@ describe "ThinkingSphinx::ActiveRecord" do
0
       )
0
     end
0
     
0
+ it "shouldn't update the core index's deleted flag if the record isn't in it" do
0
+ @person.stub_method(:in_core_index? => false)
0
+
0
+ @person.toggle_deleted
0
+
0
+ @client.should_not have_received(:update).with(
0
+ "person_core", ["sphinx_deleted"], {@person.id => 1}
0
+ )
0
+ end
0
+
0
     it "should update the delta index's deleted flag if delta indexing is enabled and the instance's delta is true" do
0
       Person.indexes.each { |index| index.stub_method(:delta? => true) }
0
       @person.delta = true
...
1
2
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
5
6
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
0
@@ -1,6 +1,48 @@
0
 require 'spec/spec_helper'
0
 
0
 describe ThinkingSphinx::Search do
0
+ describe "search_for_id method" do
0
+ before :each do
0
+ @client = Riddle::Client.stub_instance(
0
+ :filters => [],
0
+ :filters= => true,
0
+ :id_range= => true,
0
+ :query => {
0
+ :matches => []
0
+ }
0
+ )
0
+
0
+ ThinkingSphinx::Search.stub_methods(
0
+ :client_from_options => @client,
0
+ :search_conditions => ["", []]
0
+ )
0
+ end
0
+
0
+ it "should set the client id range to focus on the given id" do
0
+ ThinkingSphinx::Search.search_for_id 42, "an_index"
0
+
0
+ @client.should have_received(:id_range=).with(42..42)
0
+ end
0
+
0
+ it "should query on the given index" do
0
+ ThinkingSphinx::Search.search_for_id 42, "an_index"
0
+
0
+ @client.should have_received(:query).with("", "an_index")
0
+ end
0
+
0
+ it "should return true if a record is returned" do
0
+ @client.stub_method(:query => {
0
+ :matches => [24]
0
+ })
0
+
0
+ ThinkingSphinx::Search.search_for_id(42, "an_index").should be_true
0
+ end
0
+
0
+ it "should return false if no records are returned" do
0
+ ThinkingSphinx::Search.search_for_id(42, "an_index").should be_false
0
+ end
0
+ end
0
+
0
   describe "instance_from_result method" do
0
     before :each do
0
       Person.stub_method(:find => true)

Comments

    No one has commented yet.