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
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#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(*args) ⇒ 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(arg) ⇒ Boolean
Processes the given delta index, and then merges the relevant core and delta indexes together.
-
#index(arg, instance = nil) ⇒ Object
Does absolutely nothing, beyond returning true.
-
#initialize(arg, options = {}) ⇒ DatetimeDelta
constructor
Initialises the Delta object for the given index and settings.
-
#reset_query(model = nil) ⇒ 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
(also: #toggled)
Report whether a given instance is considered toggled (part of the next delta process).
Constructor Details
#initialize(arg, 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.
48 49 50 51 52 53 54 55 56 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 48 def initialize(arg, = {}) if ThinkingSphinx.respond_to?(:context) # Thinking Sphinx v2 @index = arg else # Thinking Sphinx v3 @adapter = arg end @column = [:column] || :updated_at @threshold = [:threshold] || 1.day end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
13 14 15 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 13 def adapter @adapter end |
#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
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 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 15 def self.index if ThinkingSphinx.respond_to?(:context) # Thinking Sphinx v2 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 else # Thinking Sphinx v3 configuration = ThinkingSphinx::Configuration.instance configuration.preload_indices configuration.indices.each do |index| if (index.delta?) if (index.delta_processor.respond_to?(:delayed_index)) index.delta_processor.delayed_index(index) end end end end end |
Instance Method Details
#clause(*args) ⇒ 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.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 156 def clause(*args) model = (args.length >= 2 ? args[0] : nil) is_delta = (args.length >= 2 ? args[1] : args[0]) || false table_name = (model.nil? ? adapter.quoted_table_name : model.quoted_table_name) column_name = (model.nil? ? adapter.quote(@column.to_s) : model.connection.quote_column_name(@column.to_s)) if is_delta if adapter.class.name.downcase[/postgres/] "#{table_name}.#{column_name} > current_timestamp - interval '#{@threshold} seconds'" elsif adapter.class.name.downcase[/mysql/] "#{table_name}.#{column_name} > DATE_SUB(NOW(), INTERVAL #{@threshold} SECOND)" else raise 'Unknown adapter type.' end else nil end end |
#delayed_index(arg) ⇒ Boolean
Processes the given delta index, 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 config.settings to true.
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 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 79 def delayed_index(arg) config = ThinkingSphinx::Configuration.instance if ThinkingSphinx.respond_to?(:context) # Thinking Sphinx v2 model = arg 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? else # Thinking Sphinx v3 delta_index = arg controller = config.controller output = controller.index(delta_index.name) output = "" unless output.is_a?(String) # Riddle::Controller.index may return true, false, nil or String, depending on its options[:verbose] value rotate = (controller.running? ? ' --rotate' : '') unless(ENV['DISABLE_MERGE'] == 'true') core_index = config.indices.select{|idx|idx.reference == delta_index.reference && idx.delta? == false}.first if (core_index) output += `#{controller.bin_path}#{controller.indexer_binary_name} --config #{config.configuration_file}#{rotate} --merge #{core_index.name} #{delta_index.name} --merge-dst-range sphinx_deleted 0 0` end end puts output unless config.settings['quiet_deltas'] end true end |
#index(arg, instance = nil) ⇒ Object
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.
64 65 66 67 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 64 def index(arg, instance=nil) # do nothing true end |
#reset_query(model = nil) ⇒ 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.
139 140 141 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 139 def reset_query(model=nil) 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.
116 117 118 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 116 def toggle(instance) # do nothing end |
#toggled?(instance) ⇒ Boolean Also known as: toggled
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.
128 129 130 131 |
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 128 def toggled?(instance) res = instance.send(@column) res && (res > @threshold.ago) end |