warp-thinking-sphinx 1.2.12
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENCE +20 -0
- data/README.textile +144 -0
- data/VERSION.yml +4 -0
- data/features/a.rb +17 -0
- data/features/alternate_primary_key.feature +27 -0
- data/features/attribute_transformation.feature +22 -0
- data/features/attribute_updates.feature +33 -0
- data/features/datetime_deltas.feature +66 -0
- data/features/delayed_delta_indexing.feature +37 -0
- data/features/deleting_instances.feature +64 -0
- data/features/direct_attributes.feature +11 -0
- data/features/excerpts.feature +13 -0
- data/features/extensible_delta_indexing.feature +9 -0
- data/features/facets.feature +76 -0
- data/features/facets_across_model.feature +29 -0
- data/features/handling_edits.feature +92 -0
- data/features/retry_stale_indexes.feature +24 -0
- data/features/searching_across_models.feature +20 -0
- data/features/searching_by_model.feature +175 -0
- data/features/searching_with_find_arguments.feature +56 -0
- data/features/sphinx_detection.feature +25 -0
- data/features/sphinx_scopes.feature +35 -0
- data/features/step_definitions/alpha_steps.rb +3 -0
- data/features/step_definitions/beta_steps.rb +7 -0
- data/features/step_definitions/common_steps.rb +178 -0
- data/features/step_definitions/datetime_delta_steps.rb +15 -0
- data/features/step_definitions/delayed_delta_indexing_steps.rb +7 -0
- data/features/step_definitions/extensible_delta_indexing_steps.rb +7 -0
- data/features/step_definitions/facet_steps.rb +92 -0
- data/features/step_definitions/find_arguments_steps.rb +36 -0
- data/features/step_definitions/gamma_steps.rb +15 -0
- data/features/step_definitions/scope_steps.rb +11 -0
- data/features/step_definitions/search_steps.rb +89 -0
- data/features/step_definitions/sphinx_steps.rb +31 -0
- data/features/sti_searching.feature +14 -0
- data/features/support/db/active_record.rb +40 -0
- data/features/support/db/database.example.yml +3 -0
- data/features/support/db/fixtures/alphas.rb +10 -0
- data/features/support/db/fixtures/authors.rb +1 -0
- data/features/support/db/fixtures/betas.rb +10 -0
- data/features/support/db/fixtures/boxes.rb +9 -0
- data/features/support/db/fixtures/categories.rb +1 -0
- data/features/support/db/fixtures/cats.rb +3 -0
- data/features/support/db/fixtures/comments.rb +24 -0
- data/features/support/db/fixtures/delayed_betas.rb +10 -0
- data/features/support/db/fixtures/developers.rb +29 -0
- data/features/support/db/fixtures/dogs.rb +3 -0
- data/features/support/db/fixtures/extensible_betas.rb +10 -0
- data/features/support/db/fixtures/gammas.rb +10 -0
- data/features/support/db/fixtures/people.rb +1001 -0
- data/features/support/db/fixtures/posts.rb +6 -0
- data/features/support/db/fixtures/robots.rb +14 -0
- data/features/support/db/fixtures/tags.rb +27 -0
- data/features/support/db/fixtures/thetas.rb +10 -0
- data/features/support/db/migrations/create_alphas.rb +7 -0
- data/features/support/db/migrations/create_animals.rb +5 -0
- data/features/support/db/migrations/create_authors.rb +3 -0
- data/features/support/db/migrations/create_authors_posts.rb +6 -0
- data/features/support/db/migrations/create_betas.rb +5 -0
- data/features/support/db/migrations/create_boxes.rb +5 -0
- data/features/support/db/migrations/create_categories.rb +3 -0
- data/features/support/db/migrations/create_comments.rb +10 -0
- data/features/support/db/migrations/create_delayed_betas.rb +17 -0
- data/features/support/db/migrations/create_developers.rb +9 -0
- data/features/support/db/migrations/create_extensible_betas.rb +5 -0
- data/features/support/db/migrations/create_gammas.rb +3 -0
- data/features/support/db/migrations/create_people.rb +13 -0
- data/features/support/db/migrations/create_posts.rb +5 -0
- data/features/support/db/migrations/create_robots.rb +5 -0
- data/features/support/db/migrations/create_taggings.rb +5 -0
- data/features/support/db/migrations/create_tags.rb +4 -0
- data/features/support/db/migrations/create_thetas.rb +5 -0
- data/features/support/db/mysql.rb +3 -0
- data/features/support/db/postgresql.rb +3 -0
- data/features/support/env.rb +6 -0
- data/features/support/lib/generic_delta_handler.rb +8 -0
- data/features/support/models/alpha.rb +10 -0
- data/features/support/models/animal.rb +5 -0
- data/features/support/models/author.rb +3 -0
- data/features/support/models/beta.rb +8 -0
- data/features/support/models/box.rb +8 -0
- data/features/support/models/cat.rb +3 -0
- data/features/support/models/category.rb +4 -0
- data/features/support/models/comment.rb +10 -0
- data/features/support/models/delayed_beta.rb +7 -0
- data/features/support/models/developer.rb +16 -0
- data/features/support/models/dog.rb +3 -0
- data/features/support/models/extensible_beta.rb +9 -0
- data/features/support/models/gamma.rb +5 -0
- data/features/support/models/person.rb +23 -0
- data/features/support/models/post.rb +20 -0
- data/features/support/models/robot.rb +8 -0
- data/features/support/models/tag.rb +3 -0
- data/features/support/models/tagging.rb +4 -0
- data/features/support/models/theta.rb +7 -0
- data/features/support/post_database.rb +43 -0
- data/features/support/z.rb +19 -0
- data/lib/thinking_sphinx.rb +212 -0
- data/lib/thinking_sphinx/active_record.rb +306 -0
- data/lib/thinking_sphinx/active_record/attribute_updates.rb +48 -0
- data/lib/thinking_sphinx/active_record/delta.rb +87 -0
- data/lib/thinking_sphinx/active_record/has_many_association.rb +28 -0
- data/lib/thinking_sphinx/active_record/scopes.rb +39 -0
- data/lib/thinking_sphinx/adapters/abstract_adapter.rb +42 -0
- data/lib/thinking_sphinx/adapters/mysql_adapter.rb +54 -0
- data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +136 -0
- data/lib/thinking_sphinx/association.rb +243 -0
- data/lib/thinking_sphinx/attribute.rb +340 -0
- data/lib/thinking_sphinx/class_facet.rb +15 -0
- data/lib/thinking_sphinx/configuration.rb +282 -0
- data/lib/thinking_sphinx/core/array.rb +7 -0
- data/lib/thinking_sphinx/core/string.rb +15 -0
- data/lib/thinking_sphinx/deltas.rb +30 -0
- data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
- data/lib/thinking_sphinx/deltas/default_delta.rb +68 -0
- data/lib/thinking_sphinx/deltas/delayed_delta.rb +30 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
- data/lib/thinking_sphinx/deploy/capistrano.rb +100 -0
- data/lib/thinking_sphinx/excerpter.rb +22 -0
- data/lib/thinking_sphinx/facet.rb +125 -0
- data/lib/thinking_sphinx/facet_search.rb +134 -0
- data/lib/thinking_sphinx/field.rb +81 -0
- data/lib/thinking_sphinx/index.rb +99 -0
- data/lib/thinking_sphinx/index/builder.rb +286 -0
- data/lib/thinking_sphinx/index/faux_column.rb +110 -0
- data/lib/thinking_sphinx/property.rb +163 -0
- data/lib/thinking_sphinx/rails_additions.rb +150 -0
- data/lib/thinking_sphinx/search.rb +708 -0
- data/lib/thinking_sphinx/search_methods.rb +421 -0
- data/lib/thinking_sphinx/source.rb +152 -0
- data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
- data/lib/thinking_sphinx/source/sql.rb +127 -0
- data/lib/thinking_sphinx/tasks.rb +165 -0
- data/rails/init.rb +14 -0
- data/spec/lib/thinking_sphinx/active_record/delta_spec.rb +130 -0
- data/spec/lib/thinking_sphinx/active_record/has_many_association_spec.rb +49 -0
- data/spec/lib/thinking_sphinx/active_record/scopes_spec.rb +96 -0
- data/spec/lib/thinking_sphinx/active_record_spec.rb +353 -0
- data/spec/lib/thinking_sphinx/association_spec.rb +239 -0
- data/spec/lib/thinking_sphinx/attribute_spec.rb +507 -0
- data/spec/lib/thinking_sphinx/configuration_spec.rb +268 -0
- data/spec/lib/thinking_sphinx/core/array_spec.rb +9 -0
- data/spec/lib/thinking_sphinx/core/string_spec.rb +9 -0
- data/spec/lib/thinking_sphinx/deltas/job_spec.rb +32 -0
- data/spec/lib/thinking_sphinx/excerpter_spec.rb +57 -0
- data/spec/lib/thinking_sphinx/facet_search_spec.rb +176 -0
- data/spec/lib/thinking_sphinx/facet_spec.rb +333 -0
- data/spec/lib/thinking_sphinx/field_spec.rb +154 -0
- data/spec/lib/thinking_sphinx/index/builder_spec.rb +455 -0
- data/spec/lib/thinking_sphinx/index/faux_column_spec.rb +30 -0
- data/spec/lib/thinking_sphinx/index_spec.rb +45 -0
- data/spec/lib/thinking_sphinx/rails_additions_spec.rb +203 -0
- data/spec/lib/thinking_sphinx/search_methods_spec.rb +152 -0
- data/spec/lib/thinking_sphinx/search_spec.rb +1101 -0
- data/spec/lib/thinking_sphinx/source_spec.rb +227 -0
- data/spec/lib/thinking_sphinx_spec.rb +162 -0
- data/tasks/distribution.rb +55 -0
- data/tasks/rails.rake +1 -0
- data/tasks/testing.rb +83 -0
- data/vendor/after_commit/LICENSE +20 -0
- data/vendor/after_commit/README +16 -0
- data/vendor/after_commit/Rakefile +22 -0
- data/vendor/after_commit/init.rb +8 -0
- data/vendor/after_commit/lib/after_commit.rb +45 -0
- data/vendor/after_commit/lib/after_commit/active_record.rb +114 -0
- data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
- data/vendor/after_commit/test/after_commit_test.rb +53 -0
- data/vendor/delayed_job/lib/delayed/job.rb +251 -0
- data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
- data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
- data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
- data/vendor/riddle/lib/riddle.rb +30 -0
- data/vendor/riddle/lib/riddle/client.rb +635 -0
- data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
- data/vendor/riddle/lib/riddle/client/message.rb +66 -0
- data/vendor/riddle/lib/riddle/client/response.rb +84 -0
- data/vendor/riddle/lib/riddle/configuration.rb +33 -0
- data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
- data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
- data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
- data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
- data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
- data/vendor/riddle/lib/riddle/configuration/section.rb +43 -0
- data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
- data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
- data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
- data/vendor/riddle/lib/riddle/controller.rb +53 -0
- metadata +267 -0
@@ -0,0 +1,48 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
module ActiveRecord
|
3
|
+
module AttributeUpdates
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
after_commit :update_attribute_values
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def update_attribute_values
|
13
|
+
return unless ThinkingSphinx.updates_enabled? && ThinkingSphinx.sphinx_running?
|
14
|
+
|
15
|
+
config = ThinkingSphinx::Configuration.instance
|
16
|
+
client = Riddle::Client.new config.address, config.port
|
17
|
+
|
18
|
+
self.sphinx_indexes.each do |index|
|
19
|
+
attribute_pairs = attribute_values_for_index(index)
|
20
|
+
attribute_names = attribute_pairs.keys
|
21
|
+
attribute_values = attribute_names.collect { |key|
|
22
|
+
attribute_pairs[key]
|
23
|
+
}
|
24
|
+
|
25
|
+
client.update "#{index.name}_core", attribute_names, {
|
26
|
+
sphinx_document_id => attribute_values
|
27
|
+
} if in_core_index?
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def updatable_attributes(index)
|
32
|
+
index.attributes.select { |attrib| attrib.updatable? }
|
33
|
+
end
|
34
|
+
|
35
|
+
def attribute_values_for_index(index)
|
36
|
+
updatable_attributes(index).inject({}) { |hash, attrib|
|
37
|
+
if attrib.type == :datetime
|
38
|
+
hash[attrib.unique_name.to_s] = attrib.live_value(self).to_time.to_i
|
39
|
+
else
|
40
|
+
hash[attrib.unique_name.to_s] = attrib.live_value self
|
41
|
+
end
|
42
|
+
|
43
|
+
hash
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
module ActiveRecord
|
3
|
+
# This module contains all the delta-related code for models. There isn't
|
4
|
+
# really anything you need to call manually in here - except perhaps
|
5
|
+
# index_delta, but not sure what reason why.
|
6
|
+
#
|
7
|
+
module Delta
|
8
|
+
# Code for after_commit callback is written by Eli Miller:
|
9
|
+
# http://elimiller.blogspot.com/2007/06/proper-cache-expiry-with-aftercommit.html
|
10
|
+
# with slight modification from Joost Hietbrink.
|
11
|
+
#
|
12
|
+
def self.included(base)
|
13
|
+
base.class_eval do
|
14
|
+
class << self
|
15
|
+
# Temporarily disable delta indexing inside a block, then perform a single
|
16
|
+
# rebuild of index at the end.
|
17
|
+
#
|
18
|
+
# Useful when performing updates to batches of models to prevent
|
19
|
+
# the delta index being rebuilt after each individual update.
|
20
|
+
#
|
21
|
+
# In the following example, the delta index will only be rebuilt once,
|
22
|
+
# not 10 times.
|
23
|
+
#
|
24
|
+
# SomeModel.suspended_delta do
|
25
|
+
# 10.times do
|
26
|
+
# SomeModel.create( ... )
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
def suspended_delta(reindex_after = true, &block)
|
31
|
+
original_setting = ThinkingSphinx.deltas_enabled?
|
32
|
+
ThinkingSphinx.deltas_enabled = false
|
33
|
+
begin
|
34
|
+
yield
|
35
|
+
ensure
|
36
|
+
ThinkingSphinx.deltas_enabled = original_setting
|
37
|
+
self.index_delta if reindex_after
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Build the delta index for the related model. This won't be called
|
42
|
+
# if running in the test environment.
|
43
|
+
#
|
44
|
+
def index_delta(instance = nil)
|
45
|
+
delta_object.index(self, instance)
|
46
|
+
end
|
47
|
+
|
48
|
+
def delta_object
|
49
|
+
self.sphinx_indexes.first.delta_object
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def toggled_delta?
|
54
|
+
self.class.delta_object.toggled(self)
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
# Set the delta value for the model to be true.
|
60
|
+
def toggle_delta
|
61
|
+
self.class.delta_object.toggle(self) if should_toggle_delta?
|
62
|
+
end
|
63
|
+
|
64
|
+
# Build the delta index for the related model. This won't be called
|
65
|
+
# if running in the test environment.
|
66
|
+
#
|
67
|
+
def index_delta
|
68
|
+
self.class.index_delta(self) if self.class.delta_object.toggled(self)
|
69
|
+
end
|
70
|
+
|
71
|
+
def should_toggle_delta?
|
72
|
+
self.new_record? || indexed_data_changed?
|
73
|
+
end
|
74
|
+
|
75
|
+
def indexed_data_changed?
|
76
|
+
sphinx_indexes.any? { |index|
|
77
|
+
index.fields.any? { |field| field.changed?(self) } ||
|
78
|
+
index.attributes.any? { |attrib|
|
79
|
+
attrib.public? && attrib.changed?(self) && !attrib.updatable?
|
80
|
+
}
|
81
|
+
}
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
module ActiveRecord
|
3
|
+
module HasManyAssociation
|
4
|
+
def search(*args)
|
5
|
+
foreign_key = @reflection.primary_key_name
|
6
|
+
stack = [@reflection.options[:through]].compact
|
7
|
+
|
8
|
+
attribute = nil
|
9
|
+
(@reflection.klass.sphinx_indexes || []).each do |index|
|
10
|
+
attribute = index.attributes.detect { |attrib|
|
11
|
+
attrib.columns.length == 1 &&
|
12
|
+
attrib.columns.first.__name == foreign_key.to_sym
|
13
|
+
}
|
14
|
+
break if attribute
|
15
|
+
end
|
16
|
+
|
17
|
+
raise "Missing Attribute for Foreign Key #{foreign_key}" unless attribute
|
18
|
+
|
19
|
+
options = args.extract_options!
|
20
|
+
options[:with] ||= {}
|
21
|
+
options[:with][attribute.unique_name] = @owner.id
|
22
|
+
|
23
|
+
args << options
|
24
|
+
@reflection.klass.search(*args)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
module ActiveRecord
|
3
|
+
module Scopes
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ThinkingSphinx::ActiveRecord::Scopes::ClassMethods
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def sphinx_scope(method, &block)
|
12
|
+
@sphinx_scopes ||= []
|
13
|
+
@sphinx_scopes << method
|
14
|
+
|
15
|
+
metaclass.instance_eval do
|
16
|
+
define_method(method) do |*args|
|
17
|
+
options = {:classes => classes_option}
|
18
|
+
options.merge! block.call(*args)
|
19
|
+
|
20
|
+
ThinkingSphinx::Search.new(options)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def sphinx_scopes
|
26
|
+
@sphinx_scopes || []
|
27
|
+
end
|
28
|
+
|
29
|
+
def remove_sphinx_scopes
|
30
|
+
sphinx_scopes.each do |scope|
|
31
|
+
metaclass.send(:undef_method, scope)
|
32
|
+
end
|
33
|
+
|
34
|
+
sphinx_scopes.clear
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
class AbstractAdapter
|
3
|
+
def initialize(model)
|
4
|
+
@model = model
|
5
|
+
end
|
6
|
+
|
7
|
+
def setup
|
8
|
+
# Deliberately blank - subclasses should do something though. Well, if
|
9
|
+
# they need to.
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.detect(model)
|
13
|
+
case model.connection.class.name
|
14
|
+
when "ActiveRecord::ConnectionAdapters::MysqlAdapter",
|
15
|
+
"ActiveRecord::ConnectionAdapters::MysqlplusAdapter"
|
16
|
+
ThinkingSphinx::MysqlAdapter.new model
|
17
|
+
when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
|
18
|
+
ThinkingSphinx::PostgreSQLAdapter.new model
|
19
|
+
when "ActiveRecord::ConnectionAdapters::JdbcAdapter"
|
20
|
+
if model.connection.config[:adapter] == "jdbcmysql"
|
21
|
+
ThinkingSphinx::MysqlAdapter.new model
|
22
|
+
elsif model.connection.config[:adapter] == "jdbcpostgresql"
|
23
|
+
ThinkingSphinx::PostgreSQLAdapter.new model
|
24
|
+
else
|
25
|
+
raise "Invalid Database Adapter: Sphinx only supports MySQL and PostgreSQL"
|
26
|
+
end
|
27
|
+
else
|
28
|
+
raise "Invalid Database Adapter: Sphinx only supports MySQL and PostgreSQL, not #{model.connection.class.name}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def quote_with_table(column)
|
33
|
+
"#{@model.quoted_table_name}.#{@model.connection.quote_column_name(column)}"
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def connection
|
39
|
+
@connection ||= @model.connection
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
class MysqlAdapter < AbstractAdapter
|
3
|
+
def setup
|
4
|
+
# Does MySQL actually need to do anything?
|
5
|
+
end
|
6
|
+
|
7
|
+
def sphinx_identifier
|
8
|
+
"mysql"
|
9
|
+
end
|
10
|
+
|
11
|
+
def concatenate(clause, separator = ' ')
|
12
|
+
"CONCAT_WS('#{separator}', #{clause})"
|
13
|
+
end
|
14
|
+
|
15
|
+
def group_concatenate(clause, separator = ' ')
|
16
|
+
"GROUP_CONCAT(DISTINCT #{clause} SEPARATOR '#{separator}')"
|
17
|
+
end
|
18
|
+
|
19
|
+
def cast_to_string(clause)
|
20
|
+
"CAST(#{clause} AS CHAR)"
|
21
|
+
end
|
22
|
+
|
23
|
+
def cast_to_datetime(clause)
|
24
|
+
"UNIX_TIMESTAMP(#{clause})"
|
25
|
+
end
|
26
|
+
|
27
|
+
def cast_to_unsigned(clause)
|
28
|
+
"CAST(#{clause} AS UNSIGNED)"
|
29
|
+
end
|
30
|
+
|
31
|
+
def convert_nulls(clause, default = '')
|
32
|
+
default = "'#{default}'" if default.is_a?(String)
|
33
|
+
|
34
|
+
"IFNULL(#{clause}, #{default})"
|
35
|
+
end
|
36
|
+
|
37
|
+
def boolean(value)
|
38
|
+
value ? 1 : 0
|
39
|
+
end
|
40
|
+
|
41
|
+
def crc(clause, blank_to_null = false)
|
42
|
+
clause = "NULLIF(#{clause},'')" if blank_to_null
|
43
|
+
"CRC32(#{clause})"
|
44
|
+
end
|
45
|
+
|
46
|
+
def utf8_query_pre
|
47
|
+
"SET NAMES utf8"
|
48
|
+
end
|
49
|
+
|
50
|
+
def time_difference(diff)
|
51
|
+
"DATE_SUB(NOW(), INTERVAL #{diff} SECOND)"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
class PostgreSQLAdapter < AbstractAdapter
|
3
|
+
def setup
|
4
|
+
create_array_accum_function
|
5
|
+
create_crc32_function
|
6
|
+
end
|
7
|
+
|
8
|
+
def sphinx_identifier
|
9
|
+
"pgsql"
|
10
|
+
end
|
11
|
+
|
12
|
+
def concatenate(clause, separator = ' ')
|
13
|
+
clause.split(', ').collect { |field|
|
14
|
+
case field
|
15
|
+
when /COALESCE/, "'')"
|
16
|
+
field
|
17
|
+
else
|
18
|
+
"COALESCE(CAST(#{field} as varchar), '')"
|
19
|
+
end
|
20
|
+
}.join(" || '#{separator}' || ")
|
21
|
+
end
|
22
|
+
|
23
|
+
def group_concatenate(clause, separator = ' ')
|
24
|
+
"array_to_string(array_accum(#{clause}), '#{separator}')"
|
25
|
+
end
|
26
|
+
|
27
|
+
def cast_to_string(clause)
|
28
|
+
clause
|
29
|
+
end
|
30
|
+
|
31
|
+
def cast_to_datetime(clause)
|
32
|
+
"cast(extract(epoch from #{clause}) as int)"
|
33
|
+
end
|
34
|
+
|
35
|
+
def cast_to_unsigned(clause)
|
36
|
+
clause
|
37
|
+
end
|
38
|
+
|
39
|
+
def convert_nulls(clause, default = '')
|
40
|
+
default = "'#{default}'" if default.is_a?(String)
|
41
|
+
default = 'NULL' if default.nil?
|
42
|
+
|
43
|
+
"COALESCE(#{clause}, #{default})"
|
44
|
+
end
|
45
|
+
|
46
|
+
def boolean(value)
|
47
|
+
value ? 'TRUE' : 'FALSE'
|
48
|
+
end
|
49
|
+
|
50
|
+
def crc(clause, blank_to_null = false)
|
51
|
+
clause = "NULLIF(#{clause},'')" if blank_to_null
|
52
|
+
"crc32(#{clause})"
|
53
|
+
end
|
54
|
+
|
55
|
+
def utf8_query_pre
|
56
|
+
nil
|
57
|
+
end
|
58
|
+
|
59
|
+
def time_difference(diff)
|
60
|
+
"current_timestamp - interval '#{diff} seconds'"
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def execute(command, output_error = false)
|
66
|
+
connection.execute "begin"
|
67
|
+
connection.execute "savepoint ts"
|
68
|
+
begin
|
69
|
+
connection.execute command
|
70
|
+
rescue StandardError => err
|
71
|
+
puts err if output_error
|
72
|
+
connection.execute "rollback to savepoint ts"
|
73
|
+
end
|
74
|
+
connection.execute "release savepoint ts"
|
75
|
+
connection.execute "commit"
|
76
|
+
end
|
77
|
+
|
78
|
+
def create_array_accum_function
|
79
|
+
if connection.raw_connection.respond_to?(:server_version) && connection.raw_connection.server_version > 80200
|
80
|
+
execute <<-SQL
|
81
|
+
CREATE AGGREGATE array_accum (anyelement)
|
82
|
+
(
|
83
|
+
sfunc = array_append,
|
84
|
+
stype = anyarray,
|
85
|
+
initcond = '{}'
|
86
|
+
);
|
87
|
+
SQL
|
88
|
+
else
|
89
|
+
execute <<-SQL
|
90
|
+
CREATE AGGREGATE array_accum
|
91
|
+
(
|
92
|
+
basetype = anyelement,
|
93
|
+
sfunc = array_append,
|
94
|
+
stype = anyarray,
|
95
|
+
initcond = '{}'
|
96
|
+
);
|
97
|
+
SQL
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def create_crc32_function
|
102
|
+
execute "CREATE LANGUAGE 'plpgsql';"
|
103
|
+
function = <<-SQL
|
104
|
+
CREATE OR REPLACE FUNCTION crc32(word text)
|
105
|
+
RETURNS bigint AS $$
|
106
|
+
DECLARE tmp bigint;
|
107
|
+
DECLARE i int;
|
108
|
+
DECLARE j int;
|
109
|
+
DECLARE word_array bytea;
|
110
|
+
BEGIN
|
111
|
+
i = 0;
|
112
|
+
tmp = 4294967295;
|
113
|
+
word_array = decode(replace(word, E'\\\\', E'\\\\\\\\'), 'escape');
|
114
|
+
LOOP
|
115
|
+
tmp = (tmp # get_byte(word_array, i))::bigint;
|
116
|
+
i = i + 1;
|
117
|
+
j = 0;
|
118
|
+
LOOP
|
119
|
+
tmp = ((tmp >> 1) # (3988292384 * (tmp & 1)))::bigint;
|
120
|
+
j = j + 1;
|
121
|
+
IF j >= 8 THEN
|
122
|
+
EXIT;
|
123
|
+
END IF;
|
124
|
+
END LOOP;
|
125
|
+
IF i >= char_length(word) THEN
|
126
|
+
EXIT;
|
127
|
+
END IF;
|
128
|
+
END LOOP;
|
129
|
+
return (tmp # 4294967295);
|
130
|
+
END
|
131
|
+
$$ IMMUTABLE STRICT LANGUAGE plpgsql;
|
132
|
+
SQL
|
133
|
+
execute function, true
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,243 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
# Association tracks a specific reflection and join to reference data that
|
3
|
+
# isn't in the base model. Very much an internal class for Thinking Sphinx -
|
4
|
+
# perhaps because I feel it's not as strong (or simple) as most of the rest.
|
5
|
+
#
|
6
|
+
class Association
|
7
|
+
attr_accessor :parent, :reflection, :join, :columns
|
8
|
+
|
9
|
+
# Create a new association by passing in the parent association, and the
|
10
|
+
# corresponding reflection instance. If there is no parent, pass in nil.
|
11
|
+
#
|
12
|
+
# top = Association.new nil, top_reflection
|
13
|
+
# child = Association.new top, child_reflection
|
14
|
+
#
|
15
|
+
def initialize(parent, reflection)
|
16
|
+
@parent, @reflection = parent, reflection
|
17
|
+
@columns = []
|
18
|
+
@children = {}
|
19
|
+
end
|
20
|
+
|
21
|
+
# Get the children associations for a given association name. The only time
|
22
|
+
# that there'll actually be more than one association is when the
|
23
|
+
# relationship is polymorphic. To keep things simple though, it will always
|
24
|
+
# be an Array that gets returned (an empty one if no matches).
|
25
|
+
#
|
26
|
+
# # where pages is an association on the class tied to the reflection.
|
27
|
+
# association.children(:pages)
|
28
|
+
#
|
29
|
+
def children(assoc)
|
30
|
+
@children[assoc] ||= Association.children(@reflection.klass, assoc, self)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Get the children associations for a given class, association name and
|
34
|
+
# parent association. Much like the instance method of the same name, it
|
35
|
+
# will return an empty array if no associations have the name, and only
|
36
|
+
# have multiple association instances if the underlying relationship is
|
37
|
+
# polymorphic.
|
38
|
+
#
|
39
|
+
# Association.children(User, :pages, user_association)
|
40
|
+
#
|
41
|
+
def self.children(klass, assoc, parent=nil)
|
42
|
+
ref = klass.reflect_on_association(assoc)
|
43
|
+
|
44
|
+
return [] if ref.nil?
|
45
|
+
return [Association.new(parent, ref)] unless ref.options[:polymorphic]
|
46
|
+
|
47
|
+
# association is polymorphic - create associations for each
|
48
|
+
# non-polymorphic reflection.
|
49
|
+
polymorphic_classes(ref).collect { |klass|
|
50
|
+
Association.new parent, ::ActiveRecord::Reflection::AssociationReflection.new(
|
51
|
+
ref.macro,
|
52
|
+
"#{ref.name}_#{klass.name}".to_sym,
|
53
|
+
casted_options(klass, ref),
|
54
|
+
ref.active_record
|
55
|
+
)
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
# Link up the join for this model from a base join - and set parent
|
60
|
+
# associations' joins recursively.
|
61
|
+
#
|
62
|
+
def join_to(base_join)
|
63
|
+
parent.join_to(base_join) if parent && parent.join.nil?
|
64
|
+
|
65
|
+
@join ||= ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.new(
|
66
|
+
@reflection, base_join, parent ? parent.join : base_join.joins.first
|
67
|
+
)
|
68
|
+
|
69
|
+
@join.aliased_table_name << '_join' if is_many? and !(@join.aliased_table_name =~ /_join$/)
|
70
|
+
end
|
71
|
+
|
72
|
+
def column_def_sql(column_name)
|
73
|
+
if column_name.to_s =~ /_id$/
|
74
|
+
"#{quote_column(column_name)} int(11) NOT NULL"
|
75
|
+
elsif column_name.to_s =~ /_ids$/
|
76
|
+
"#{quote_column(column_name)} BLOB default NULL"
|
77
|
+
else
|
78
|
+
"#{quote_column(column_name)} TEXT default NULL"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Returns the association's flattened columns as an indexed tempory table
|
83
|
+
# to speed up the joins and avoid a huge group by statement
|
84
|
+
#
|
85
|
+
def to_flat_sql
|
86
|
+
primary_key = quote_column(@reflection.primary_key_name)
|
87
|
+
temp_table_name = quote_table_name(@join.aliased_table_name)
|
88
|
+
|
89
|
+
create = "CREATE TEMPORARY TABLE %s (%s, PRIMARY KEY (%s))" % [
|
90
|
+
temp_table_name,
|
91
|
+
(columns.uniq.map(&:__name) << @reflection.primary_key_name).map { |column| column_def_sql(column) }.join(', '),
|
92
|
+
primary_key
|
93
|
+
]
|
94
|
+
|
95
|
+
insert = "INSERT INTO %s (%s)" % [
|
96
|
+
temp_table_name,
|
97
|
+
(columns.uniq.map { |column| quote_column(column.__name) } << primary_key).join(', ')
|
98
|
+
]
|
99
|
+
|
100
|
+
insert << " SELECT %s FROM %s" % [
|
101
|
+
(columns.uniq.map { |column| column_sql(column.__name) } << primary_key).join(', '),
|
102
|
+
@reflection.quoted_table_name
|
103
|
+
]
|
104
|
+
|
105
|
+
insert << " INNER JOIN %s ON %s.%s = %s.%s" % [
|
106
|
+
quote_table_name(@reflection.options[:join_table]),
|
107
|
+
quote_table_name(@reflection.options[:join_table]),
|
108
|
+
quote_column(@reflection.association_foreign_key),
|
109
|
+
@reflection.quoted_table_name,
|
110
|
+
quote_column(@join.primary_key)
|
111
|
+
] if @reflection.macro == :has_and_belongs_to_many
|
112
|
+
|
113
|
+
insert << " GROUP BY %s" % [
|
114
|
+
primary_key
|
115
|
+
]
|
116
|
+
|
117
|
+
[create, insert]
|
118
|
+
end
|
119
|
+
|
120
|
+
# Returns the association's join SQL statements - and it replaces
|
121
|
+
# ::ts_join_alias:: with the aliased table name so the generated reflection
|
122
|
+
# join conditions avoid column name collisions.
|
123
|
+
#
|
124
|
+
def to_sql
|
125
|
+
if is_many?
|
126
|
+
temp_table_name = quote_table_name(@join.aliased_table_name)
|
127
|
+
" LEFT OUTER JOIN %s ON %s.%s = %s.%s" % [
|
128
|
+
temp_table_name,
|
129
|
+
temp_table_name,
|
130
|
+
quote_column(@reflection.primary_key_name),
|
131
|
+
quote_table_name(@join.parent.aliased_table_name),
|
132
|
+
quote_column(@join.parent.primary_key)
|
133
|
+
]
|
134
|
+
else
|
135
|
+
@join.association_join.gsub(/::ts_join_alias::/,
|
136
|
+
"#{@reflection.klass.connection.quote_table_name(@join.parent.aliased_table_name)}"
|
137
|
+
)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# Returns true if the association - or a parent - is a has_many or
|
142
|
+
# has_and_belongs_to_many.
|
143
|
+
#
|
144
|
+
def is_many?
|
145
|
+
case @reflection.macro
|
146
|
+
when :has_many, :has_and_belongs_to_many
|
147
|
+
true
|
148
|
+
else
|
149
|
+
@parent ? @parent.is_many? : false
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def adapter
|
154
|
+
@adapter ||= @reflection.klass.sphinx_database_adapter
|
155
|
+
end
|
156
|
+
|
157
|
+
def column_sql(column)
|
158
|
+
separator = column == :id ? ',' : ' '
|
159
|
+
"#{adapter.group_concatenate(column, separator)} AS #{quote_column(column)}"
|
160
|
+
end
|
161
|
+
|
162
|
+
def quote_column(column)
|
163
|
+
@reflection.klass.connection.quote_column_name(column)
|
164
|
+
end
|
165
|
+
|
166
|
+
def quote_table_name(table_name)
|
167
|
+
@reflection.klass.connection.quote_table_name(table_name)
|
168
|
+
end
|
169
|
+
|
170
|
+
# Returns an array of all the associations that lead to this one - starting
|
171
|
+
# with the top level all the way to the current association object.
|
172
|
+
#
|
173
|
+
def ancestors
|
174
|
+
(parent ? parent.ancestors : []) << self
|
175
|
+
end
|
176
|
+
|
177
|
+
def has_column?(column)
|
178
|
+
@reflection.klass.column_names.include?(column.to_s)
|
179
|
+
end
|
180
|
+
|
181
|
+
def primary_key_from_reflection
|
182
|
+
if @reflection.options[:through]
|
183
|
+
@reflection.source_reflection.options[:foreign_key] ||
|
184
|
+
@reflection.source_reflection.primary_key_name
|
185
|
+
elsif @reflection.macro == :has_and_belongs_to_many
|
186
|
+
@reflection.association_foreign_key
|
187
|
+
else
|
188
|
+
nil
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def table
|
193
|
+
if @reflection.options[:through] ||
|
194
|
+
@reflection.macro == :has_and_belongs_to_many
|
195
|
+
@join.aliased_join_table_name
|
196
|
+
else
|
197
|
+
@join.aliased_table_name
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
private
|
202
|
+
|
203
|
+
# Returns all the objects that could be currently instantiated from a
|
204
|
+
# polymorphic association. This is pretty damn fast if there's an index on
|
205
|
+
# the foreign type column - but if there isn't, it can take a while if you
|
206
|
+
# have a lot of data.
|
207
|
+
#
|
208
|
+
def self.polymorphic_classes(ref)
|
209
|
+
ref.active_record.connection.select_all(
|
210
|
+
"SELECT DISTINCT #{ref.options[:foreign_type]} " +
|
211
|
+
"FROM #{ref.active_record.table_name} " +
|
212
|
+
"WHERE #{ref.options[:foreign_type]} IS NOT NULL"
|
213
|
+
).collect { |row|
|
214
|
+
row[ref.options[:foreign_type]].constantize
|
215
|
+
}
|
216
|
+
end
|
217
|
+
|
218
|
+
# Returns a new set of options for an association that mimics an existing
|
219
|
+
# polymorphic relationship for a specific class. It adds a condition to
|
220
|
+
# filter by the appropriate object.
|
221
|
+
#
|
222
|
+
def self.casted_options(klass, ref)
|
223
|
+
options = ref.options.clone
|
224
|
+
options[:polymorphic] = nil
|
225
|
+
options[:class_name] = klass.name
|
226
|
+
options[:foreign_key] ||= "#{ref.name}_id"
|
227
|
+
|
228
|
+
quoted_foreign_type = klass.connection.quote_column_name ref.options[:foreign_type]
|
229
|
+
case options[:conditions]
|
230
|
+
when nil
|
231
|
+
options[:conditions] = "::ts_join_alias::.#{quoted_foreign_type} = '#{klass.name}'"
|
232
|
+
when Array
|
233
|
+
options[:conditions] << "::ts_join_alias::.#{quoted_foreign_type} = '#{klass.name}'"
|
234
|
+
when Hash
|
235
|
+
options[:conditions].merge!(ref.options[:foreign_type] => klass.name)
|
236
|
+
else
|
237
|
+
options[:conditions] << " AND ::ts_join_alias::.#{quoted_foreign_type} = '#{klass.name}'"
|
238
|
+
end
|
239
|
+
|
240
|
+
options
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|