Class: ThinkingSphinx::Deltas::DatetimeDelta

Inherits:
DefaultDelta
  • Object
show all
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.

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • arg

    Depending on the version of Thinking Sphinx For TS v2: the index For TS v3: the database adapter



52
53
54
55
56
57
58
59
60
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 52

def initialize(arg, options = {})
  if self.class.thinking_sphinx_v2?
    @index    = arg
  else # Thinking Sphinx v3
    @adapter  = arg
  end
  @column     = options[:column]    || :updated_at
  @threshold  = options[:threshold] || 1.day
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



13
14
15
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 13

def adapter
  @adapter
end

#columnObject

Returns the value of attribute column.



12
13
14
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 12

def column
  @column
end

#thresholdObject

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

.indexObject



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 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

.thinking_sphinx_v2?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 42

def self.thinking_sphinx_v2?
  ThinkingSphinx.respond_to?(:sphinx_pid)
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.

Parameters:

  • args

    Depends on version of Thinking Sphinx: For ThinkingSphinx v2 this should be: def clause(model, is_delta) For ThinkingSphinx v3 this should be: def clause(is_delta=false)

  • model: (Class)

    The ActiveRecord model for which the clause is for

  • is_delta: (Boolean)

    Whether the clause is for the core or delta index

Returns:

  • (String, NilClass)

    The SQL condition if the is_delta is true, otherwise nil



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 160

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.

Parameters:

  • arg

    Depending on the version of Thinking Sphinx For TS v2: the ActiveRecord model to index For TS v3: the delta index to index

Returns:

  • (Boolean)

    true



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
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 83

def delayed_index(arg)
  config = ThinkingSphinx::Configuration.instance
  if self.class.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.



68
69
70
71
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 68

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.

Returns:

  • (NilClass)

    Always nil



143
144
145
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 143

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.

Parameters:

  • instance (ActiveRecord::Base)

    the instance to be toggled



120
121
122
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 120

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.

Parameters:

  • instance (ActiveRecord::Base)

    the instance to check

Returns:

  • (Boolean)

    True if within the threshold window, otherwise false.



132
133
134
135
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 132

def toggled?(instance)
  res = instance.send(@column)
  res && (res > @threshold.ago)
end