forked from pat/thinking-sphinx
/
thinking_sphinx.rb
230 lines (195 loc) · 6.59 KB
/
thinking_sphinx.rb
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
49
50
51
52
53
54
55
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
require 'active_record'
require 'yaml'
require 'cgi'
require 'riddle'
require 'thinking_sphinx/auto_version'
require 'thinking_sphinx/core/array'
require 'thinking_sphinx/core/string'
require 'thinking_sphinx/property'
require 'thinking_sphinx/active_record'
require 'thinking_sphinx/association'
require 'thinking_sphinx/attribute'
require 'thinking_sphinx/configuration'
require 'thinking_sphinx/context'
require 'thinking_sphinx/excerpter'
require 'thinking_sphinx/facet'
require 'thinking_sphinx/class_facet'
require 'thinking_sphinx/facet_search'
require 'thinking_sphinx/field'
require 'thinking_sphinx/index'
require 'thinking_sphinx/source'
require 'thinking_sphinx/rails_additions'
require 'thinking_sphinx/search'
require 'thinking_sphinx/search_methods'
require 'thinking_sphinx/deltas'
require 'thinking_sphinx/adapters/abstract_adapter'
require 'thinking_sphinx/adapters/mysql_adapter'
require 'thinking_sphinx/adapters/postgresql_adapter'
ActiveRecord::Base.send(:include, ThinkingSphinx::ActiveRecord)
Merb::Plugins.add_rakefiles(
File.join(File.dirname(__FILE__), "thinking_sphinx", "tasks")
) if defined?(Merb)
module ThinkingSphinx
# A ConnectionError will get thrown when a connection to Sphinx can't be
# made.
class ConnectionError < StandardError
end
# A StaleIdsException is thrown by Collection.instances_from_matches if there
# are records in Sphinx but not in the database, so the search can be retried.
class StaleIdsException < StandardError
attr_accessor :ids
def initialize(ids)
self.ids = ids
end
end
# The current version of Thinking Sphinx.
#
# @return [String] The version number as a string
#
def self.version
open(File.join(File.dirname(__FILE__), '../VERSION')) { |f|
f.read.strip
}
end
# The collection of indexed models. Keep in mind that Rails lazily loads
# its classes, so this may not actually be populated with _all_ the models
# that have Sphinx indexes.
def self.context
if Thread.current[:thinking_sphinx_context].nil?
Thread.current[:thinking_sphinx_context] = ThinkingSphinx::Context.new
Thread.current[:thinking_sphinx_context].prepare
end
Thread.current[:thinking_sphinx_context]
end
def self.reset_context!
Thread.current[:thinking_sphinx_context] = nil
end
def self.unique_id_expression(offset = nil)
"* #{context.indexed_models.size} + #{offset || 0}"
end
# Check if index definition is disabled.
#
def self.define_indexes?
if Thread.current[:thinking_sphinx_define_indexes].nil?
Thread.current[:thinking_sphinx_define_indexes] = true
end
Thread.current[:thinking_sphinx_define_indexes]
end
# Enable/disable indexes - you may want to do this while migrating data.
#
# ThinkingSphinx.define_indexes = false
#
def self.define_indexes=(value)
Thread.current[:thinking_sphinx_define_indexes] = value
end
# Check if delta indexing is enabled.
#
def self.deltas_enabled?
if Thread.current[:thinking_sphinx_deltas_enabled].nil?
Thread.current[:thinking_sphinx_deltas_enabled] = (
ThinkingSphinx::Configuration.environment != "test"
)
end
Thread.current[:thinking_sphinx_deltas_enabled]
end
# Enable/disable all delta indexing.
#
# ThinkingSphinx.deltas_enabled = false
#
def self.deltas_enabled=(value)
Thread.current[:thinking_sphinx_deltas_enabled] = value
end
# Check if updates are enabled. True by default, unless within the test
# environment.
#
def self.updates_enabled?
if Thread.current[:thinking_sphinx_updates_enabled].nil?
Thread.current[:thinking_sphinx_updates_enabled] = (
ThinkingSphinx::Configuration.environment != "test"
)
end
Thread.current[:thinking_sphinx_updates_enabled]
end
# Enable/disable updates to Sphinx
#
# ThinkingSphinx.updates_enabled = false
#
def self.updates_enabled=(value)
Thread.current[:thinking_sphinx_updates_enabled] = value
end
def self.suppress_delta_output?
Thread.current[:thinking_sphinx_suppress_delta_output] ||= false
end
def self.suppress_delta_output=(value)
Thread.current[:thinking_sphinx_suppress_delta_output] = value
end
# Checks to see if MySQL will allow simplistic GROUP BY statements. If not,
# or if not using MySQL, this will return false.
#
def self.use_group_by_shortcut?
if Thread.current[:thinking_sphinx_use_group_by_shortcut].nil?
Thread.current[:thinking_sphinx_use_group_by_shortcut] = !!(
mysql? && ::ActiveRecord::Base.connection.select_all(
"SELECT @@global.sql_mode, @@session.sql_mode;"
).all? { |key,value| value.nil? || value[/ONLY_FULL_GROUP_BY/].nil? }
)
end
Thread.current[:thinking_sphinx_use_group_by_shortcut]
end
# An indication of whether Sphinx is running on a remote machine instead of
# the same machine.
#
def self.remote_sphinx?
Thread.current[:thinking_sphinx_remote_sphinx] ||= false
end
# Tells Thinking Sphinx that Sphinx is running on a different machine, and
# thus it can't reliably guess whether it is running or not (ie: the
# #sphinx_running? method), and so just assumes it is.
#
# Useful for multi-machine deployments. Set it in your production.rb file.
#
# ThinkingSphinx.remote_sphinx = true
#
def self.remote_sphinx=(value)
Thread.current[:thinking_sphinx_remote_sphinx] = value
end
# Check if Sphinx is running. If remote_sphinx is set to true (indicating
# Sphinx is on a different machine), this will always return true, and you
# will have to handle any connection errors yourself.
#
def self.sphinx_running?
remote_sphinx? || sphinx_running_by_pid?
end
# Check if Sphinx is actually running, provided the pid is on the same
# machine as this code.
#
def self.sphinx_running_by_pid?
!!sphinx_pid && pid_active?(sphinx_pid)
end
def self.sphinx_pid
if File.exists?(ThinkingSphinx::Configuration.instance.pid_file)
File.read(ThinkingSphinx::Configuration.instance.pid_file)[/\d+/]
else
nil
end
end
def self.pid_active?(pid)
!!Process.kill(0, pid.to_i)
rescue Exception => e
false
end
def self.microsoft?
RUBY_PLATFORM =~ /mswin/
end
def self.jruby?
defined?(JRUBY_VERSION)
end
def self.mysql?
::ActiveRecord::Base.connection.class.name.demodulize == "MysqlAdapter" ||
::ActiveRecord::Base.connection.class.name.demodulize == "MysqlplusAdapter" || (
jruby? && ::ActiveRecord::Base.connection.config[:adapter] == "jdbcmysql"
)
end
extend ThinkingSphinx::SearchMethods::ClassMethods
end
ThinkingSphinx::AutoVersion.detect