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:

Author:

  • Patrick Allan

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Examples:

ThinkingSphinx::Deltas::DatetimeDelta.new index,
  :delta_column => :updated_at,
  :threshold    => 1.day

Parameters:

  • index (ThinkingSphinx::Index)

    the index using this delta object

  • options (Hash) (defaults to: {})

    a hash of options for the index

Options Hash (options):

  • :delta_column (Symbol) — default: :updated_at

    The column to use for tracking when a record has changed. Default to :updated_at.

  • :threshold (Integer) — default: 1.day

    The window of time to store changes for, in seconds. Defaults to one day.



45
46
47
48
49
# File 'lib/thinking_sphinx/deltas/datetime_delta.rb', line 45

def initialize(index, options = {})
  @index      = index
  @column     = options.delete(:delta_column) || :updated_at
  @threshold  = options.delete(:threshold)    || 1.day
end

Instance Attribute Details

#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



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.

Parameters:

  • model (Class)

    The ActiveRecord model to generate the SQL condition for.

  • toggled (Boolean)

    Whether the query should request delta documents or all documents.

Returns:

  • (String, NilClass)

    The SQL condition if the toggled version is requested, otherwise nil.



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.

Parameters:

  • model (Class)

    the ActiveRecord model to index

Returns:

  • (Boolean)

    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.

Parameters:

  • model (Class)

    the ActiveRecord model to index.

  • instance (ActiveRecord::Base) (defaults to: nil)

    the instance of the given model that has changed. Optional.

Returns:

  • (Boolean)

    true

See Also:



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.

Parameters:

  • model (Class)

    The ActiveRecord model that is requesting the query

Returns:

  • (NilClass)

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

Parameters:

  • instance (ActiveRecord::Base)

    the instance to be toggled



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.

Parameters:

  • instance (ActiveRecord::Base)

    the instance to check

Returns:

  • (Boolean)

    True if within the threshold window, otherwise false.



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