sunspot_plus 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Louis Gillies
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ = sunspot_plus
2
+
3
+ Extension to sunport_rails
4
+ Currently includes a delayed_job proxy to queue Active Record re-indexing.
5
+ Particularly useful for using 3rd party or remote solr servers over http as often the round trip can delay page response times.
6
+
7
+ == Usage
8
+
9
+ * Use rails initializer to create the sunspot session
10
+
11
+ require 'sunspot_rails'
12
+ # set the session to the delayed_job handler - this will send all model CRUD reindexing to delayed_job
13
+ Sunspot.session = Sunspot::SessionProxy::DelayedJobSessionProxy.new(Sunspot.session)
14
+
15
+ * Define your searchable blocks as usual in ActiveRecord models.
16
+ class Model < ActiveRecord::Base
17
+ searchable do
18
+ text :name
19
+ end
20
+ end
21
+
22
+ == Note on Patches/Pull Requests
23
+
24
+ * Fork the project.
25
+ * Make your feature addition or bug fix.
26
+ * Add tests for it. This is important so I don't break it in a
27
+ future version unintentionally.
28
+ * Commit, do not mess with rakefile, version, or history.
29
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
30
+ * Send me a pull request. Bonus points for topic branches.
31
+
32
+ == Copyright
33
+
34
+ Copyright (c) 2010 Louis Gillies. See LICENSE for details.
@@ -0,0 +1,5 @@
1
+ require 'sunspot'
2
+ require 'sunspot_plus/session_proxy/delayed_job_session_proxy'
3
+ require 'sunspot_plus/session_proxy/silent_fail_session_proxy'
4
+ require 'sunspot_plus/session_proxy/delayed_job/indexing_job'
5
+ require 'sunspot_plus/session_proxy/delayed_job/safe_configuration'
@@ -0,0 +1,33 @@
1
+ module Sunspot
2
+ module SessionProxy
3
+ #
4
+ # Queue indexing to job.
5
+ #
6
+ #
7
+
8
+ module DelayedJob
9
+ class IndexingJob
10
+
11
+ attr_reader :session, :proxy, :method
12
+
13
+ def initialize(proxy, method, *args, &block)
14
+ @session = proxy.session
15
+ @proxy = proxy
16
+ @method = method
17
+ @args = args
18
+ end
19
+
20
+ def rebuild_config
21
+ @session.instance_variable_set("@config", proxy.class.rebuild_config(session.config))
22
+ end
23
+
24
+ def perform
25
+ # Delayed job serializes ruby objects to yaml.
26
+ # Yaml doesn't rebuild the LightConfiguration object correctly so we need to brute force a rebuild of the configuration
27
+ rebuild_config
28
+ @args ? @session.send(method, *@args) : @session.send(method)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,36 @@
1
+
2
+ module Sunspot
3
+ module SessionProxy
4
+ module DelayedJob
5
+
6
+ # Problems arise when trying to load a yaml serialised sunspot config
7
+ # The recursive properties set up from the build blocks can't be accessed and the methods raise a not defined error.
8
+ # To see an example of this.
9
+ # config = Sunspot::Configuration.build
10
+ # config.solr.url => "http://127.0.0.1:8983/solr"
11
+ # YAML.load(YAML.dump(config).solr.url => undefined method solr raised.
12
+ # This class will attempt to wrap and rebuild the original config and create a safe version of it.
13
+ class SafeConfiguration
14
+ def initialize(config)
15
+ @config = config
16
+ singleton = (class <<self; self; end)
17
+ @properties = @config.instance_variable_get("@properties") || {}
18
+ @properties.keys.each do |property|
19
+ singleton.module_eval do
20
+ define_method property do
21
+ if @properties[property].is_a?(LightConfig::Configuration)
22
+ self.class.new(@properties[property])
23
+ else
24
+ @properties[property]
25
+ end
26
+ end
27
+ define_method "#{property}=" do |value|
28
+ @properties[property] = value
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,61 @@
1
+ module Sunspot
2
+ module SessionProxy
3
+ #
4
+ # Queue commits to delayed_job.
5
+ #
6
+ #
7
+ # To configure, add this to an initializer:
8
+ # Sunspot.session = DelayedJobSessionProxy.new(Sunspot.session)
9
+ #
10
+ #
11
+
12
+ class DelayedJobSessionProxy < Sunspot::SessionProxy::AbstractSessionProxy
13
+
14
+ attr_reader :session
15
+ attr_writer :config
16
+
17
+ delegate :new_search, :search, :new_more_like_this, :more_like_this, :config, :index,
18
+ :delete_dirty?, :dirty?, :remove, :remove!, :remove_all, :remove_all!, :remove_by_id, :remove_by_id!,
19
+ :to => :session
20
+
21
+ def batch(&block)
22
+ Delayed::Job.enqueue Sunspot::SessionProxy::DelayedJob::IndexingJob.new(self, :batch, &block)
23
+ end
24
+
25
+ def commit
26
+ Delayed::Job.enqueue Sunspot::SessionProxy::DelayedJob::IndexingJob.new(self, :commit)
27
+ end
28
+
29
+ #
30
+ # We can't delegate to @session as commit needs to be sent to delayed job.
31
+ #
32
+ def index!(*objects)
33
+ @session.index(*objects)
34
+ commit
35
+ end
36
+ #
37
+ # We can't delegate to @session as commit needs to be sent to delayed job.
38
+ #
39
+ def commit_if_dirty
40
+ commit if @session.dirty?
41
+ end
42
+
43
+ #
44
+ # We can't delegate to @session as commit needs to be sent to delayed job.
45
+ #
46
+ def commit_if_delete_dirty
47
+ commit if @session.delete_dirty?
48
+ end
49
+
50
+ def self.rebuild_config(config)
51
+ Sunspot::SessionProxy::DelayedJob::SafeConfiguration.new(config)
52
+ end
53
+
54
+ def initialize(session)
55
+ @config = session.config
56
+ @session = session
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,35 @@
1
+ module Sunspot
2
+ module SessionProxy
3
+ #
4
+ # Silently fail instead of raising an exception when an error occurs while writing to Solr.
5
+ # NOTE: does not fail for reads; you should catch those exceptions, for example in a rescue_from statement.
6
+ #
7
+ # To configure, add this to an initializer:
8
+ # Sunspot.session = SilentFailSessionProxy.new(Sunspot.session)
9
+ #
10
+ #
11
+ #
12
+ class SilentFailSessionProxy < Sunspot::SessionProxy::AbstractSessionProxy
13
+
14
+ attr_reader :session
15
+ delegate :new_search, :search, :configuration, :config, :to => :session
16
+
17
+ [:index, :index!, :commit, :remove, :remove!, :remove_by_id,
18
+ :remove_by_id!, :remove_all, :remove_all!, :dirty?, :commit_if_dirty, :batch].each do |method|
19
+ module_eval(<<-RUBY)
20
+ def #{method}(*args, &block)
21
+ begin
22
+ session.#{method}(*args, &block)
23
+ rescue => e
24
+ Rails.logger.error(e.message)
25
+ end
26
+ end
27
+ RUBY
28
+ end
29
+
30
+ def initialize(session)
31
+ @session = session
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ module Delayed
2
+ class Job
3
+ def self.enqueue(object)
4
+ true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'sunspot'
5
+ require 'sunspot_plus'
6
+ require 'sunspot/session_proxy/abstract_session_proxy'
7
+ require 'spec'
8
+ require 'spec/autorun'
9
+ require 'helpers/delayed_job_stub'
10
+
11
+ Spec::Runner.configure do |config|
12
+
13
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
+
3
+ describe Sunspot::SessionProxy::DelayedJob::SafeConfiguration do
4
+ # TODO test hacked config rebuild to overcome YAML serialization bug in sunspot config object.
5
+ end
@@ -0,0 +1,71 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Sunspot::SessionProxy::DelayedJobSessionProxy do
4
+
5
+
6
+
7
+ before(:each) do
8
+
9
+ @original_session = Sunspot::Session.new
10
+ @proxy = Sunspot::SessionProxy::DelayedJobSessionProxy.new( @original_session )
11
+ end
12
+
13
+
14
+ it "should wrap a current session" do
15
+ @proxy.session.should == @original_session
16
+ end
17
+
18
+ context "methods that should be delegated to the default session" do
19
+ ([:new_search, :search, :new_more_like_this, :more_like_this, :config, :index,
20
+ :delete_dirty?, :dirty?, :remove, :remove!, :remove_all, :remove_all!, :remove_by_id, :remove_by_id!]).each do |method|
21
+ it "should delegate #{method.inspect} to its session" do
22
+ args = Array.new(Sunspot::Session.instance_method(method).arity.abs) do
23
+ stub('arg')
24
+ end
25
+ @proxy.session.should_receive(method).with(*args)
26
+ @proxy.send(method, *args)
27
+ end
28
+ end
29
+ end
30
+
31
+ context "methods that commit should queue to delayed job." do
32
+ describe "index" do
33
+ it "should delegate index to @session" do
34
+ @proxy.session.should_receive(:index)
35
+ @proxy.should_receive(:commit)
36
+ @proxy.index!(mock("Model"))
37
+ end
38
+ end
39
+
40
+ describe "commit_if_dirty" do
41
+ it "should delegate dirty? to @session" do
42
+ @proxy.session.should_receive(:dirty?).and_return(true)
43
+ @proxy.should_receive(:commit)
44
+ @proxy.commit_if_dirty
45
+ end
46
+ end
47
+
48
+ describe "commit_if_delete_dirty" do
49
+ it "should delegate delete_dirty? to @session" do
50
+ @proxy.session.should_receive(:delete_dirty?).and_return(true)
51
+ @proxy.should_receive(:commit)
52
+ @proxy.commit_if_delete_dirty
53
+ end
54
+ end
55
+ end
56
+
57
+ context "methods that should be queued" do
58
+ [:commit, :batch].each do |method|
59
+ it "should send #{method} to the delayed_job queue" do
60
+ args = Array.new(Sunspot::Session.instance_method(method).arity.abs) do
61
+ stub('arg')
62
+ end
63
+ Delayed::Job.should_receive(:enqueue)
64
+ @proxy.send(method, *args)
65
+ end
66
+ end
67
+ end
68
+
69
+ it_should_behave_like 'session proxy'
70
+ end
71
+
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ shared_examples_for 'session proxy' do
4
+ Sunspot::SessionProxy::AbstractSessionProxy.public_instance_methods(false).each do |method|
5
+ it "should respond to #{method.inspect}" do
6
+ @proxy.should respond_to(method)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "SunspotPlus" do
4
+
5
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sunspot_plus
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 2
10
+ version: 0.2.2
11
+ platform: ruby
12
+ authors:
13
+ - Louis Gillies
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-25 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: sunspot
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: delayed_job
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - "="
56
+ - !ruby/object:Gem::Version
57
+ hash: 27
58
+ segments:
59
+ - 1
60
+ - 3
61
+ - 0
62
+ version: 1.3.0
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: yard
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ type: :development
78
+ version_requirements: *id004
79
+ description: "A library of extensions for outoftime's sunspot gem for solr indexing server. Using the session adapter design pattern to add support for:\n 1) delayed_job to move indexing commits out of process.\n "
80
+ email: louisgillies@yahoo.co.uk
81
+ executables: []
82
+
83
+ extensions: []
84
+
85
+ extra_rdoc_files:
86
+ - LICENSE
87
+ - README.rdoc
88
+ files:
89
+ - lib/sunspot_plus.rb
90
+ - lib/sunspot_plus/session_proxy/delayed_job/indexing_job.rb
91
+ - lib/sunspot_plus/session_proxy/delayed_job/safe_configuration.rb
92
+ - lib/sunspot_plus/session_proxy/delayed_job_session_proxy.rb
93
+ - lib/sunspot_plus/session_proxy/silent_fail_session_proxy.rb
94
+ - LICENSE
95
+ - README.rdoc
96
+ - spec/helpers/delayed_job_stub.rb
97
+ - spec/spec_helper.rb
98
+ - spec/sunspot/session_proxy/delayed_job/safe_configuration_spec.rb
99
+ - spec/sunspot/session_proxy/delayed_job_session_proxy_spec.rb
100
+ - spec/sunspot/session_proxy/spec_helper.rb
101
+ - spec/sunspot_plus_spec.rb
102
+ has_rdoc: true
103
+ homepage: http://github.com/playgood/sunspot_plus
104
+ licenses: []
105
+
106
+ post_install_message:
107
+ rdoc_options: []
108
+
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ hash: 3
126
+ segments:
127
+ - 0
128
+ version: "0"
129
+ requirements: []
130
+
131
+ rubyforge_project:
132
+ rubygems_version: 1.4.2
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: Extensions for outoftime's sunspot gem.
136
+ test_files:
137
+ - spec/helpers/delayed_job_stub.rb
138
+ - spec/spec_helper.rb
139
+ - spec/sunspot/session_proxy/delayed_job/safe_configuration_spec.rb
140
+ - spec/sunspot/session_proxy/delayed_job_session_proxy_spec.rb
141
+ - spec/sunspot/session_proxy/spec_helper.rb
142
+ - spec/sunspot_plus_spec.rb