summaryrefslogtreecommitdiff
path: root/vendor/plugins/thinking-sphinx/lib/thinking_sphinx.rb
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2009-04-28 00:15:53 +0200
committerhukl <contact@smyck.org>2009-05-01 17:14:02 +0200
commit4bd16f053847f2efe347ebda9136ef2233ee0d2c (patch)
treef4c11f89455de991c8d87726d5757b752e7129e2 /vendor/plugins/thinking-sphinx/lib/thinking_sphinx.rb
parentd3a9b46ba5c863a0ff377dcffae9a494fe476e02 (diff)
added thinking_sphinx plugin for fulltext search on nodes and heads
Diffstat (limited to 'vendor/plugins/thinking-sphinx/lib/thinking_sphinx.rb')
-rw-r--r--vendor/plugins/thinking-sphinx/lib/thinking_sphinx.rb180
1 files changed, 180 insertions, 0 deletions
diff --git a/vendor/plugins/thinking-sphinx/lib/thinking_sphinx.rb b/vendor/plugins/thinking-sphinx/lib/thinking_sphinx.rb
new file mode 100644
index 0000000..745643f
--- /dev/null
+++ b/vendor/plugins/thinking-sphinx/lib/thinking_sphinx.rb
@@ -0,0 +1,180 @@
1Dir[File.join(File.dirname(__FILE__), '../vendor/*/lib')].each do |path|
2 $LOAD_PATH.unshift path
3end
4
5require 'active_record'
6require 'riddle'
7require 'after_commit'
8
9require 'thinking_sphinx/core/string'
10require 'thinking_sphinx/active_record'
11require 'thinking_sphinx/association'
12require 'thinking_sphinx/attribute'
13require 'thinking_sphinx/collection'
14require 'thinking_sphinx/configuration'
15require 'thinking_sphinx/facet'
16require 'thinking_sphinx/class_facet'
17require 'thinking_sphinx/facet_collection'
18require 'thinking_sphinx/field'
19require 'thinking_sphinx/index'
20require 'thinking_sphinx/rails_additions'
21require 'thinking_sphinx/search'
22require 'thinking_sphinx/deltas'
23
24require 'thinking_sphinx/adapters/abstract_adapter'
25require 'thinking_sphinx/adapters/mysql_adapter'
26require 'thinking_sphinx/adapters/postgresql_adapter'
27
28ActiveRecord::Base.send(:include, ThinkingSphinx::ActiveRecord)
29
30Merb::Plugins.add_rakefiles(
31 File.join(File.dirname(__FILE__), "thinking_sphinx", "tasks")
32) if defined?(Merb)
33
34module ThinkingSphinx
35 module Version #:nodoc:
36 Major = 1
37 Minor = 1
38 Tiny = 6
39
40 String = [Major, Minor, Tiny].join('.')
41 end
42
43 # A ConnectionError will get thrown when a connection to Sphinx can't be
44 # made.
45 class ConnectionError < StandardError
46 end
47
48 # A StaleIdsException is thrown by Collection.instances_from_matches if there
49 # are records in Sphinx but not in the database, so the search can be retried.
50 class StaleIdsException < StandardError
51 attr_accessor :ids
52 def initialize(ids)
53 self.ids = ids
54 end
55 end
56
57 # The collection of indexed models. Keep in mind that Rails lazily loads
58 # its classes, so this may not actually be populated with _all_ the models
59 # that have Sphinx indexes.
60 def self.indexed_models
61 @@indexed_models ||= []
62 end
63
64 def self.unique_id_expression(offset = nil)
65 "* #{ThinkingSphinx.indexed_models.size} + #{offset || 0}"
66 end
67
68 # Check if index definition is disabled.
69 #
70 def self.define_indexes?
71 @@define_indexes = true unless defined?(@@define_indexes)
72 @@define_indexes == true
73 end
74
75 # Enable/disable indexes - you may want to do this while migrating data.
76 #
77 # ThinkingSphinx.define_indexes = false
78 #
79 def self.define_indexes=(value)
80 @@define_indexes = value
81 end
82
83 @@deltas_enabled = nil
84
85 # Check if delta indexing is enabled.
86 #
87 def self.deltas_enabled?
88 @@deltas_enabled = (ThinkingSphinx::Configuration.environment != 'test') if @@deltas_enabled.nil?
89 @@deltas_enabled
90 end
91
92 # Enable/disable all delta indexing.
93 #
94 # ThinkingSphinx.deltas_enabled = false
95 #
96 def self.deltas_enabled=(value)
97 @@deltas_enabled = value
98 end
99
100 @@updates_enabled = nil
101
102 # Check if updates are enabled. True by default, unless within the test
103 # environment.
104 #
105 def self.updates_enabled?
106 @@updates_enabled = (ThinkingSphinx::Configuration.environment != 'test') if @@updates_enabled.nil?
107 @@updates_enabled
108 end
109
110 # Enable/disable updates to Sphinx
111 #
112 # ThinkingSphinx.updates_enabled = false
113 #
114 def self.updates_enabled=(value)
115 @@updates_enabled = value
116 end
117
118 @@suppress_delta_output = false
119
120 def self.suppress_delta_output?
121 @@suppress_delta_output
122 end
123
124 def self.suppress_delta_output=(value)
125 @@suppress_delta_output = value
126 end
127
128 # Checks to see if MySQL will allow simplistic GROUP BY statements. If not,
129 # or if not using MySQL, this will return false.
130 #
131 def self.use_group_by_shortcut?
132 !!(
133 mysql? && ::ActiveRecord::Base.connection.select_all(
134 "SELECT @@global.sql_mode, @@session.sql_mode;"
135 ).all? { |key,value| value.nil? || value[/ONLY_FULL_GROUP_BY/].nil? }
136 )
137 end
138
139 def self.sphinx_running?
140 !!sphinx_pid && pid_active?(sphinx_pid)
141 end
142
143 def self.sphinx_pid
144 pid_file = ThinkingSphinx::Configuration.instance.pid_file
145 cat_command = 'cat'
146 return nil unless File.exists?(pid_file)
147
148 if microsoft?
149 pid_file.gsub!('/', '\\')
150 cat_command = 'type'
151 end
152
153 `#{cat_command} #{pid_file}`[/\d+/]
154 end
155
156 def self.pid_active?(pid)
157 return true if microsoft?
158
159 begin
160 # In JRuby this returns -1 if the process doesn't exist
161 Process.getpgid(pid.to_i) != -1
162 rescue Exception => e
163 false
164 end
165 end
166
167 def self.microsoft?
168 RUBY_PLATFORM =~ /mswin/
169 end
170
171 def self.jruby?
172 defined?(JRUBY_VERSION)
173 end
174
175 def self.mysql?
176 ::ActiveRecord::Base.connection.class.name.demodulize == "MysqlAdapter" || (
177 jruby? && ::ActiveRecord::Base.connection.config[:adapter] == "jdbcmysql"
178 )
179 end
180end