Class: ThinkingSphinx::Deltas::DatetimeDelta
- Inherits:
-
DefaultDelta
- Object
- DefaultDelta
- ThinkingSphinx::Deltas::DatetimeDelta
- Defined in:
- lib/thinking_sphinx/deltas/datetime_delta.rb
Overview
Datetime Deltas for Thinking Sphinx
This documentation is aimed at those reading the code. If you’re looking for a guide to Thinking Sphinx and/or deltas, I recommend you start with the Thinking Sphinx site instead - or the README for this library at the very least.
Instance Attribute Summary collapse
-
#column ⇒ Object
Returns the value of attribute column.
-
#threshold ⇒ Object
Returns the value of attribute threshold.
Class Method Summary collapse
Instance Method Summary collapse
-
#clause(model, toggled) ⇒ String, NilClass
A SQL condition (as part of the WHERE clause) that limits the result set to just the delta data, or all data, depending on whether the toggled argument is true or not.
-
#delayed_index(model) ⇒ Boolean
Processes the delta index for the given model, and then merges the relevant core and delta indexes together.
-
#index(model, instance = nil) ⇒ Boolean
Does absolutely nothing, beyond returning true.
-
#initialize(index, options = {}) ⇒ DatetimeDelta
constructor
Initialises the Delta object for the given index and settings.
-
#reset_query(model) ⇒ NilClass
Returns the SQL query that resets the model data after a normal index.
-
#toggle(instance) ⇒ Object
Toggles the given instance to be flagged as part of the next delta indexing.
-
#toggled(instance) ⇒ Boolean
Report whether a given instance is considered toggled (part of the next delta process).
Constructor Details
#initialize(index, options = {}) ⇒ DatetimeDelta
Initialises the Delta object for the given index and settings. All handled by Thinking Sphinx, so you shouldn’t need to call this method yourself in general day-to-day situations.
45 46 47 48 49 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 45 def initialize(index, = {}) @index = index @column = .delete(:delta_column) || :updated_at @threshold = .delete(:threshold) || 1.day end |
Instance Attribute Details
#column ⇒ Object
Returns the value of attribute column.
12 13 14 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 12 def column @column end |
#threshold ⇒ Object
Returns the value of attribute threshold.
12 13 14 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 12 def threshold @threshold end |
Class Method Details
.index ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 14 def self.index ThinkingSphinx.context.indexed_models.collect { |model| model.constantize }.select { |model| model.define_indexes model.delta_indexed_by_sphinx? }.each do |model| model.sphinx_indexes.select { |index| index.delta? && index.delta_object.respond_to?(:delayed_index) }.each { |index| index.delta_object.delayed_index(index.model) } end end |
Instance Method Details
#clause(model, toggled) ⇒ String, NilClass
A SQL condition (as part of the WHERE clause) that limits the result set to just the delta data, or all data, depending on whether the toggled argument is true or not. For datetime deltas, the former value is a check on the delta column being within the threshold. In the latter’s case, no condition is needed, so nil is returned.
137 138 139 140 141 142 143 144 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 137 def clause(model, toggled) if toggled "#{model.quoted_table_name}.#{model.connection.quote_column_name(@column.to_s)}" + " > #{adapter.time_difference(@threshold)}" else nil end end |
#delayed_index(model) ⇒ Boolean
Processes the delta index for the given model, and then merges the relevant core and delta indexes together. By default, the output of these indexer commands are printed to stdout. If you’d rather it didn’t, set ThinkingSphinx.suppress_delta_output to true.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 76 def delayed_index(model) config = ThinkingSphinx::Configuration.instance rotate = ThinkingSphinx.sphinx_running? ? " --rotate" : "" output = `#{config.bin_path}#{config.indexer_binary_name} --config #{config.config_file}#{rotate} #{model.delta_index_names.join(' ')}` model.sphinx_indexes.select(&:delta?).each do |index| output += `#{config.bin_path}#{config.indexer_binary_name} --config #{config.config_file}#{rotate} --merge #{index.core_name} #{index.delta_name} --merge-dst-range sphinx_deleted 0 0` end unless ENV['DISABLE_MERGE'] == 'true' puts output unless ThinkingSphinx.suppress_delta_output? true end |
#index(model, instance = nil) ⇒ Boolean
Does absolutely nothing, beyond returning true. Thinking Sphinx expects this method, though, and we don’t want to use the inherited behaviour from DefaultDelta.
All the real indexing logic is done by the delayed_index method.
63 64 65 66 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 63 def index(model, instance = nil) # do nothing true end |
#reset_query(model) ⇒ NilClass
Returns the SQL query that resets the model data after a normal index. For datetime deltas, nothing needs to be done, so this method returns nil.
120 121 122 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 120 def reset_query(model) nil end |
#toggle(instance) ⇒ Object
Toggles the given instance to be flagged as part of the next delta indexing. For datetime deltas, this means do nothing at all.
97 98 99 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 97 def toggle(instance) # do nothing end |
#toggled(instance) ⇒ Boolean
Report whether a given instance is considered toggled (part of the next delta process). For datetime deltas, this is true if the delta column (updated_at by default) has a value within the threshold. Otherwise, false is returned.
109 110 111 112 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 109 def toggled(instance) res = instance.send(@column) res && (res > @threshold.ago) end |