sunspot 0.10.9 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +30 -0
- data/README.rdoc +59 -29
- data/Rakefile +2 -0
- data/TODO +9 -19
- data/VERSION.yml +2 -3
- data/bin/sunspot-installer +19 -0
- data/bin/sunspot-solr +28 -53
- data/installer/config/schema.yml +71 -0
- data/lib/sunspot/configuration.rb +0 -10
- data/lib/sunspot/dsl/field_query.rb +123 -7
- data/lib/sunspot/dsl/fields.rb +17 -4
- data/lib/sunspot/dsl/query.rb +11 -3
- data/lib/sunspot/dsl/scope.rb +6 -2
- data/lib/sunspot/field.rb +4 -7
- data/lib/sunspot/field_factory.rb +8 -5
- data/lib/sunspot/indexer.rb +22 -20
- data/lib/sunspot/installer/library_installer.rb +45 -0
- data/lib/sunspot/installer/schema_builder.rb +219 -0
- data/lib/sunspot/installer/solrconfig_updater.rb +90 -0
- data/lib/sunspot/installer/task_helper.rb +18 -0
- data/lib/sunspot/installer.rb +31 -0
- data/lib/sunspot/query/abstract_field_facet.rb +5 -1
- data/lib/sunspot/query/connective.rb +3 -1
- data/lib/sunspot/query/field_facet.rb +33 -1
- data/lib/sunspot/query/filter.rb +38 -0
- data/lib/sunspot/query/local.rb +10 -11
- data/lib/sunspot/query/query.rb +5 -12
- data/lib/sunspot/query/restriction.rb +2 -1
- data/lib/sunspot/query/scope.rb +1 -1
- data/lib/sunspot/query.rb +3 -3
- data/lib/sunspot/schema.rb +5 -1
- data/lib/sunspot/search/field_facet.rb +59 -15
- data/lib/sunspot/search/hit.rb +21 -10
- data/lib/sunspot/search/query_facet.rb +2 -2
- data/lib/sunspot/search.rb +86 -33
- data/lib/sunspot/server.rb +148 -0
- data/lib/sunspot/session.rb +39 -51
- data/lib/sunspot/session_proxy/abstract_session_proxy.rb +29 -0
- data/lib/sunspot/session_proxy/class_sharding_session_proxy.rb +66 -0
- data/lib/sunspot/session_proxy/id_sharding_session_proxy.rb +89 -0
- data/lib/sunspot/session_proxy/master_slave_session_proxy.rb +43 -0
- data/lib/sunspot/session_proxy/sharding_session_proxy.rb +206 -0
- data/lib/sunspot/session_proxy/thread_local_session_proxy.rb +30 -0
- data/lib/sunspot/session_proxy.rb +71 -0
- data/lib/sunspot/setup.rb +24 -10
- data/lib/sunspot/type.rb +163 -101
- data/lib/sunspot/util.rb +44 -2
- data/lib/sunspot/version.rb +3 -0
- data/lib/sunspot.rb +50 -17
- data/solr/etc/jetty.xml +4 -2
- data/solr/solr/conf/admin-extra.html +31 -0
- data/solr/solr/conf/mapping-ISOLatin1Accent.txt +246 -0
- data/solr/solr/conf/schema.xml +212 -50
- data/solr/solr/conf/scripts.conf +24 -0
- data/solr/solr/conf/solrconfig.xml +473 -266
- data/solr/solr/conf/spellings.txt +2 -0
- data/solr/solr/conf/stopwords.txt +1 -0
- data/solr/solr/lib/lucene-spatial-2.9.1.jar +0 -0
- data/solr/solr/lib/solr-spatial-light-0.0.3.jar +0 -0
- data/solr/webapps/solr.war +0 -0
- data/spec/api/binding_spec.rb +38 -0
- data/spec/api/indexer/attributes_spec.rb +37 -4
- data/spec/api/indexer/dynamic_fields_spec.rb +10 -1
- data/spec/api/indexer/removal_spec.rb +8 -1
- data/spec/api/indexer_spec.rb +10 -0
- data/spec/api/query/dsl_spec.rb +6 -0
- data/spec/api/query/dynamic_fields_spec.rb +9 -9
- data/spec/api/query/facet_local_params_spec.rb +103 -0
- data/spec/api/query/local_spec.rb +13 -37
- data/spec/api/query/ordering_pagination_spec.rb +0 -6
- data/spec/api/search/dynamic_fields_spec.rb +3 -3
- data/spec/api/search/faceting_spec.rb +113 -10
- data/spec/api/search/hits_spec.rb +49 -0
- data/spec/api/search/results_spec.rb +8 -1
- data/spec/api/server_spec.rb +85 -0
- data/spec/api/session_proxy/class_sharding_session_proxy_spec.rb +85 -0
- data/spec/api/session_proxy/id_sharding_session_proxy_spec.rb +30 -0
- data/spec/api/session_proxy/master_slave_session_proxy_spec.rb +41 -0
- data/spec/api/session_proxy/sharding_session_proxy_spec.rb +77 -0
- data/spec/api/session_proxy/spec_helper.rb +9 -0
- data/spec/api/session_proxy/thread_local_session_proxy_spec.rb +33 -0
- data/spec/ext.rb +11 -0
- data/spec/helpers/search_helper.rb +6 -8
- data/spec/integration/faceting_spec.rb +46 -4
- data/spec/integration/indexing_spec.rb +27 -1
- data/spec/integration/local_search_spec.rb +25 -7
- data/spec/integration/scoped_search_spec.rb +48 -36
- data/spec/mocks/comment.rb +7 -5
- data/spec/mocks/connection.rb +14 -13
- data/spec/mocks/mock_class_sharding_session_proxy.rb +24 -0
- data/spec/mocks/mock_record.rb +7 -3
- data/spec/mocks/mock_sharding_session_proxy.rb +15 -0
- data/spec/mocks/photo.rb +4 -3
- data/spec/mocks/post.rb +1 -1
- data/spec/spec_helper.rb +2 -1
- data/tasks/gemspec.rake +26 -36
- data/tasks/rdoc.rake +9 -4
- metadata +203 -203
- data/bin/sunspot-configure-solr +0 -40
- data/solr/solr/lib/geoapi-nogenerics-2.1-M2.jar +0 -0
- data/solr/solr/lib/gt2-referencing-2.3.1.jar +0 -0
- data/solr/solr/lib/jsr108-0.01.jar +0 -0
- data/solr/solr/lib/locallucene.jar +0 -0
- data/solr/solr/lib/localsolr.jar +0 -0
- data/templates/schema.xml.erb +0 -36
@@ -0,0 +1,148 @@
|
|
1
|
+
require 'escape'
|
2
|
+
require 'set'
|
3
|
+
require 'tempfile'
|
4
|
+
|
5
|
+
module Sunspot #:nodoc:
|
6
|
+
class Server
|
7
|
+
# Raised if #stop is called but the server is not running
|
8
|
+
ServerError = Class.new(RuntimeError)
|
9
|
+
AlreadyRunningError = Class.new(ServerError)
|
10
|
+
NotRunningError = Class.new(ServerError)
|
11
|
+
|
12
|
+
# Name of the sunspot executable (shell script)
|
13
|
+
SOLR_START_JAR = File.expand_path(
|
14
|
+
File.join(File.dirname(__FILE__), '..', '..', 'solr', 'start.jar')
|
15
|
+
)
|
16
|
+
|
17
|
+
LOG_LEVELS = Set['SEVERE', 'WARNING', 'INFO', 'CONFIG', 'FINE', 'FINER', 'FINEST']
|
18
|
+
|
19
|
+
attr_accessor :min_memory, :max_memory, :port, :solr_data_dir, :solr_home, :log_file
|
20
|
+
attr_writer :pid_dir, :pid_file, :log_level, :solr_data_dir, :solr_home
|
21
|
+
|
22
|
+
#
|
23
|
+
# Start the sunspot-solr server. Bootstrap solr_home first,
|
24
|
+
# if neccessary.
|
25
|
+
#
|
26
|
+
# ==== Returns
|
27
|
+
#
|
28
|
+
# Boolean:: success
|
29
|
+
#
|
30
|
+
def start
|
31
|
+
if File.exist?(pid_path)
|
32
|
+
existing_pid = IO.read(pid_path).to_i
|
33
|
+
begin
|
34
|
+
Process.kill(0, existing_pid)
|
35
|
+
raise(AlreadyRunningError, "Server is already running with PID #{existing_pid}")
|
36
|
+
rescue Errno::ESRCH
|
37
|
+
STDERR.puts("Removing stale PID file at #{pid_path}")
|
38
|
+
FileUtils.rm(pid_path)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
fork do
|
42
|
+
pid = fork do
|
43
|
+
Process.setsid
|
44
|
+
STDIN.reopen('/dev/null')
|
45
|
+
STDOUT.reopen('/dev/null', 'a')
|
46
|
+
STDERR.reopen(STDOUT)
|
47
|
+
run
|
48
|
+
end
|
49
|
+
FileUtils.mkdir_p(pid_dir)
|
50
|
+
File.open(pid_path, 'w') do |file|
|
51
|
+
file << pid
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
#
|
57
|
+
# Run the sunspot-solr server in the foreground. Boostrap
|
58
|
+
# solr_home first, if neccessary.
|
59
|
+
#
|
60
|
+
# ==== Returns
|
61
|
+
#
|
62
|
+
# Boolean:: success
|
63
|
+
#
|
64
|
+
def run
|
65
|
+
command = ['java']
|
66
|
+
command << "-Xms#{min_memory}" if min_memory
|
67
|
+
command << "-Xmx#{max_memory}" if max_memory
|
68
|
+
command << "-Djetty.port=#{port}" if port
|
69
|
+
command << "-Dsolr.data.dir=#{solr_data_dir}" if solr_data_dir
|
70
|
+
command << "-Dsolr.solr.home=#{solr_home}" if solr_home
|
71
|
+
command << "-Djava.util.logging.config.file=#{logging_config_path}" if logging_config_path
|
72
|
+
command << '-jar' << File.basename(SOLR_START_JAR)
|
73
|
+
FileUtils.cd(File.dirname(SOLR_START_JAR)) do
|
74
|
+
exec(Escape.shell_command(command))
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# Stop the sunspot-solr server.
|
80
|
+
#
|
81
|
+
# ==== Returns
|
82
|
+
#
|
83
|
+
# Boolean:: success
|
84
|
+
#
|
85
|
+
def stop
|
86
|
+
if File.exist?(pid_path)
|
87
|
+
pid = IO.read(pid_path).to_i
|
88
|
+
begin
|
89
|
+
Process.kill('TERM', pid)
|
90
|
+
rescue Errno::ESRCH
|
91
|
+
raise NotRunningError, "Process with PID #{pid} is no longer running"
|
92
|
+
ensure
|
93
|
+
FileUtils.rm(pid_path)
|
94
|
+
end
|
95
|
+
else
|
96
|
+
raise NotRunningError, "No PID file at #{pid_path}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def log_level=(level)
|
101
|
+
unless LOG_LEVELS.include?(level.to_s.upcase)
|
102
|
+
raise(ArgumentError, "#{level} is not a valid log level: Use one of #{LOG_LEVELS.to_a.join(',')}")
|
103
|
+
end
|
104
|
+
@log_level = level.to_s.upcase
|
105
|
+
end
|
106
|
+
|
107
|
+
def log_level
|
108
|
+
@log_level || 'WARNING'
|
109
|
+
end
|
110
|
+
|
111
|
+
def pid_path
|
112
|
+
File.join(pid_dir, pid_file)
|
113
|
+
end
|
114
|
+
|
115
|
+
def pid_file
|
116
|
+
@pid_file || 'sunspot-solr.pid'
|
117
|
+
end
|
118
|
+
|
119
|
+
def pid_dir
|
120
|
+
File.expand_path(@pid_dir || FileUtils.pwd)
|
121
|
+
end
|
122
|
+
|
123
|
+
def solr_data_dir
|
124
|
+
File.expand_path(@solr_data_dir || Dir.tmpdir)
|
125
|
+
end
|
126
|
+
|
127
|
+
def solr_home
|
128
|
+
File.expand_path(@solr_home || File.join(File.dirname(SOLR_START_JAR), 'solr'))
|
129
|
+
end
|
130
|
+
|
131
|
+
private
|
132
|
+
|
133
|
+
def logging_config_path
|
134
|
+
return @logging_config_path if defined?(@logging_config_path)
|
135
|
+
@logging_config_path =
|
136
|
+
if log_file
|
137
|
+
logging_config = Tempfile.new('logging.properties')
|
138
|
+
logging_config.puts("handlers = java.util.logging.FileHandler")
|
139
|
+
logging_config.puts("java.util.logging.FileHandler.level = #{log_level.to_s.upcase}")
|
140
|
+
logging_config.puts("java.util.logging.FileHandler.pattern = #{log_file}")
|
141
|
+
logging_config.puts("java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter")
|
142
|
+
logging_config.flush
|
143
|
+
logging_config.close
|
144
|
+
logging_config.path
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
data/lib/sunspot/session.rb
CHANGED
@@ -29,37 +29,28 @@ module Sunspot
|
|
29
29
|
# connection. Usually you will want to stick with the default arguments
|
30
30
|
# when instantiating your own sessions.
|
31
31
|
#
|
32
|
-
def initialize(config = Configuration.build, connection = nil
|
32
|
+
def initialize(config = Configuration.build, connection = nil)
|
33
33
|
@config = config
|
34
34
|
yield(@config) if block_given?
|
35
35
|
@connection = connection
|
36
|
-
@master_connection = master_connection
|
37
36
|
@deletes = @adds = 0
|
38
37
|
end
|
39
38
|
|
40
39
|
#
|
41
40
|
# See Sunspot.new_search
|
42
41
|
#
|
43
|
-
def new_search(*types)
|
42
|
+
def new_search(*types, &block)
|
44
43
|
types.flatten!
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
setup =
|
49
|
-
if types.length == 1
|
50
|
-
Setup.for(types.first)
|
51
|
-
else
|
52
|
-
CompositeSetup.for(types)
|
53
|
-
end
|
54
|
-
Search.new(connection, setup, Query::Query.new(types), @config)
|
44
|
+
search = Search.new(connection, setup_for_types(types), Query::Query.new(types), @config)
|
45
|
+
search.build(&block) if block
|
46
|
+
search
|
55
47
|
end
|
56
48
|
|
57
49
|
#
|
58
50
|
# See Sunspot.search
|
59
51
|
#
|
60
52
|
def search(*types, &block)
|
61
|
-
search = new_search(*types)
|
62
|
-
search.build(&block) if block
|
53
|
+
search = new_search(*types, &block)
|
63
54
|
search.execute!
|
64
55
|
end
|
65
56
|
|
@@ -85,17 +76,30 @@ module Sunspot
|
|
85
76
|
#
|
86
77
|
def commit
|
87
78
|
@adds = @deletes = 0
|
88
|
-
|
79
|
+
connection.commit
|
89
80
|
end
|
90
81
|
|
91
82
|
#
|
92
83
|
# See Sunspot.remove
|
93
84
|
#
|
94
|
-
def remove(*objects)
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
85
|
+
def remove(*objects, &block)
|
86
|
+
if block
|
87
|
+
types = objects
|
88
|
+
conjunction = Query::Connective::Conjunction.new
|
89
|
+
if types.length == 1
|
90
|
+
conjunction.add_restriction(TypeField.instance, Query::Restriction::EqualTo, types.first)
|
91
|
+
else
|
92
|
+
conjunction.add_restriction(TypeField.instance, Query::Restriction::AnyOf, types)
|
93
|
+
end
|
94
|
+
dsl = DSL::Scope.new(conjunction, setup_for_types(types))
|
95
|
+
Util.instance_eval_or_call(dsl, &block)
|
96
|
+
indexer.remove_by_scope(conjunction)
|
97
|
+
else
|
98
|
+
objects.flatten!
|
99
|
+
@deletes += objects.length
|
100
|
+
objects.each do |object|
|
101
|
+
indexer.remove(object)
|
102
|
+
end
|
99
103
|
end
|
100
104
|
end
|
101
105
|
|
@@ -135,7 +139,7 @@ module Sunspot
|
|
135
139
|
classes.flatten!
|
136
140
|
if classes.empty?
|
137
141
|
@deletes += 1
|
138
|
-
|
142
|
+
indexer.remove_all
|
139
143
|
else
|
140
144
|
@deletes += classes.length
|
141
145
|
classes.each { |clazz| indexer.remove_all(clazz) }
|
@@ -195,42 +199,26 @@ module Sunspot
|
|
195
199
|
#
|
196
200
|
# ==== Returns
|
197
201
|
#
|
198
|
-
#
|
202
|
+
# RSolr::Connection::Base:: The connection for this session
|
199
203
|
#
|
200
204
|
def connection
|
201
205
|
@connection ||=
|
202
|
-
|
203
|
-
connection = self.class.connection_class.connect(
|
204
|
-
:url => config.solr.url
|
205
|
-
)
|
206
|
-
connection
|
207
|
-
end
|
206
|
+
self.class.connection_class.connect(:url => config.solr.url)
|
208
207
|
end
|
209
208
|
|
210
|
-
|
211
|
-
|
212
|
-
# if it does not already exist.
|
213
|
-
#
|
214
|
-
# ==== Returns
|
215
|
-
#
|
216
|
-
# Solr::Connection:: The connection for this session
|
217
|
-
#
|
218
|
-
def master_connection
|
219
|
-
@master_connection ||=
|
220
|
-
begin
|
221
|
-
if config.master_solr.url && config.master_solr.url != config.solr.url
|
222
|
-
master_connection = self.class.connection_class.new(
|
223
|
-
RSolr::Connection::NetHttp.new(:url => config.master_solr.url)
|
224
|
-
)
|
225
|
-
master_connection
|
226
|
-
else
|
227
|
-
connection
|
228
|
-
end
|
229
|
-
end
|
209
|
+
def indexer
|
210
|
+
@indexer ||= Indexer.new(connection)
|
230
211
|
end
|
231
212
|
|
232
|
-
def
|
233
|
-
|
213
|
+
def setup_for_types(types)
|
214
|
+
if types.empty?
|
215
|
+
raise(ArgumentError, "You must specify at least one type to search")
|
216
|
+
end
|
217
|
+
if types.length == 1
|
218
|
+
Setup.for(types.first)
|
219
|
+
else
|
220
|
+
CompositeSetup.for(types)
|
221
|
+
end
|
234
222
|
end
|
235
223
|
end
|
236
224
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module SessionProxy
|
3
|
+
class AbstractSessionProxy #:nodoc:
|
4
|
+
class <<self
|
5
|
+
def delegate(*args)
|
6
|
+
options = Util.extract_options_from(args)
|
7
|
+
delegate = options[:to]
|
8
|
+
args.each do |method|
|
9
|
+
module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
10
|
+
def #{method}(*args, &block)
|
11
|
+
#{delegate}.#{method}(*args, &block)
|
12
|
+
end
|
13
|
+
RUBY
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def not_supported(*methods)
|
18
|
+
methods.each do |method|
|
19
|
+
module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
20
|
+
def #{method}(*args, &block)
|
21
|
+
raise NotSupportedError, "#{name} does not support #{method.inspect}"
|
22
|
+
end
|
23
|
+
RUBY
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module SessionProxy
|
3
|
+
#
|
4
|
+
# An abstract subclass of ShardingSessionProxy that shards by class.
|
5
|
+
# Concrete subclasses should not override the #session_for method, but
|
6
|
+
# should instead implement the #session_for_class method. They must also
|
7
|
+
# still implement the #all_sessions method.
|
8
|
+
#
|
9
|
+
# Unlike its parent class, ClassShardingSessionProxy implements
|
10
|
+
# #remove_by_id and all flavors of #remove_all.
|
11
|
+
#
|
12
|
+
class ClassShardingSessionProxy < ShardingSessionProxy
|
13
|
+
#
|
14
|
+
# Remove the Session object pointing at the shard that indexes the given
|
15
|
+
# class.
|
16
|
+
#
|
17
|
+
# <strong>Concrete subclasses must implement this method.</strong>
|
18
|
+
#
|
19
|
+
def session_for_class(clazz)
|
20
|
+
raise NotImplementedError
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# See Sunspot.remove_by_id
|
25
|
+
#
|
26
|
+
def remove_by_id(clazz, id)
|
27
|
+
session_for_class(clazz).remove_by_id(clazz, id)
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# See Sunspot.remove_by_id!
|
32
|
+
#
|
33
|
+
def remove_by_id!(clazz, id)
|
34
|
+
session_for_class(clazz).remove_by_id!(clazz, id)
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# See Sunspot.remove_all
|
39
|
+
#
|
40
|
+
def remove_all(clazz = nil)
|
41
|
+
if clazz
|
42
|
+
session_for_class(clazz).remove_all(clazz)
|
43
|
+
else
|
44
|
+
all_sessions.each { |session| session.remove_all }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
#
|
49
|
+
# See Sunspot.remove_all!
|
50
|
+
#
|
51
|
+
def remove_all!(clazz = nil)
|
52
|
+
if clazz
|
53
|
+
session_for_class(clazz).remove_all!(clazz)
|
54
|
+
else
|
55
|
+
all_sessions.each { |session| session.remove_all! }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def session_for(object)
|
62
|
+
session_for_class(object.class)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module SessionProxy
|
3
|
+
#
|
4
|
+
# A concrete implementation of ShardingSessionProxy that determines the
|
5
|
+
# shard for a given object based on the hash of its class and ID.
|
6
|
+
#
|
7
|
+
# <strong>If you change the number of shard sessions that this proxy
|
8
|
+
# encapsulates, all objects will point to a different shard.</strong> If you
|
9
|
+
# plan on adding more shards over time, consider your own
|
10
|
+
# ShardingSessionProxy implementation that does not determine the session
|
11
|
+
# using modular arithmetic (e.g., IDs 1-10000 go to shard 1, 10001-20000 go
|
12
|
+
# to shard 2, etc.)
|
13
|
+
#
|
14
|
+
# This implementation will, on average, yield an even distribution of
|
15
|
+
# objects across shards.
|
16
|
+
#
|
17
|
+
# Unlike the abstract ShardingSessionProxy, this proxy supports the
|
18
|
+
# #remove_by_id method.
|
19
|
+
#
|
20
|
+
class IdShardingSessionProxy < ShardingSessionProxy
|
21
|
+
#
|
22
|
+
# The shard sessions encapsulated by this class.
|
23
|
+
#
|
24
|
+
attr_reader :sessions
|
25
|
+
alias_method :all_sessions, :sessions #:nodoc:
|
26
|
+
|
27
|
+
#
|
28
|
+
# Initialize with a search session (see ShardingSessionProxy.new) and a
|
29
|
+
# collection of one or more shard sessions. See note about changing the
|
30
|
+
# number of shard sessions in the documentation for this class.
|
31
|
+
#
|
32
|
+
def initialize(search_session, shard_sessions)
|
33
|
+
super(search_session)
|
34
|
+
@sessions = shard_sessions
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# Return a session based on the hash of the class and ID, modulo the
|
39
|
+
# number of shard sessions.
|
40
|
+
#
|
41
|
+
def session_for(object) #:nodoc:
|
42
|
+
session_for_index_id(Adapters::InstanceAdapter.adapt(object).index_id)
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# See Sunspot.remove_by_id
|
47
|
+
#
|
48
|
+
def remove_by_id(clazz, id)
|
49
|
+
session_for_index_id(
|
50
|
+
Adapters::InstanceAdapter.index_id_for(clazz, id)
|
51
|
+
).remove_by_id(clazz, id)
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# See Sunspot.remove_by_id!
|
56
|
+
#
|
57
|
+
def remove_by_id!(clazz, id)
|
58
|
+
session_for_index_id(
|
59
|
+
Adapters::InstanceAdapter.index_id_for(clazz, id)
|
60
|
+
).remove_by_id!(clazz, id)
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def session_for_index_id(index_id)
|
66
|
+
@sessions[id_hash(index_id) % @sessions.length]
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# This method is implemented explicitly instead of using String#hash to
|
71
|
+
# give predictable behavior across different Ruby interpreters.
|
72
|
+
#
|
73
|
+
if "".respond_to?(:bytes) # Ruby 1.9
|
74
|
+
def id_hash(id)
|
75
|
+
id.bytes.inject { |hash, byte| hash * 31 + byte }
|
76
|
+
end
|
77
|
+
else
|
78
|
+
def id_hash(id)
|
79
|
+
hash, i, len = 0, 0, id.length
|
80
|
+
while i < len
|
81
|
+
hash = hash * 31 + id[i]
|
82
|
+
i += 1
|
83
|
+
end
|
84
|
+
hash
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'abstract_session_proxy')
|
2
|
+
|
3
|
+
module Sunspot
|
4
|
+
module SessionProxy
|
5
|
+
#
|
6
|
+
# This session proxy implementation allows Sunspot to be used with a
|
7
|
+
# master/slave Solr deployment. All write methods are delegated to a master
|
8
|
+
# session, and read methods are delegated to a slave session.
|
9
|
+
#
|
10
|
+
class MasterSlaveSessionProxy < AbstractSessionProxy
|
11
|
+
#
|
12
|
+
# The session that connects to the master Solr instance.
|
13
|
+
#
|
14
|
+
attr_reader :master_session
|
15
|
+
#
|
16
|
+
# The session that connects to the slave Solr instance.
|
17
|
+
#
|
18
|
+
attr_reader :slave_session
|
19
|
+
|
20
|
+
delegate :batch, :commit, :commit_if_delete_dirty, :commit_if_dirty,
|
21
|
+
:config, :delete_dirty?, :dirty?, :index, :index!, :remove,
|
22
|
+
:remove!, :remove_all, :remove_all!, :remove_by_id,
|
23
|
+
:remove_by_id!, :to => :master_session
|
24
|
+
delegate :new_search, :search, :to => :slave_session
|
25
|
+
|
26
|
+
def initialize(master_session, slave_session)
|
27
|
+
@master_session, @slave_session = master_session, slave_session
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# By default, return the configuration for the master session. If the
|
32
|
+
# +delegate+ param is +:slave+, then return config for the slave session.
|
33
|
+
#
|
34
|
+
def config(delegate = :master)
|
35
|
+
case delegate
|
36
|
+
when :master then @master_session.config
|
37
|
+
when :slave then @slave_session.config
|
38
|
+
else raise(ArgumentError, "Expected :master or :slave")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|