Module: Flex::LiveReindex

Extended by:
LiveReindex
Included in:
LiveReindex
Defined in:
lib/flex/live_reindex.rb

Overview

private module

Defined Under Namespace

Modules: Redis Classes: ExtraIndexError, LiveReindexInProgressError, MissingAppIdError, MissingEnsureIndicesError, MissingOnReindexBlockError, MissingRedisError, MissingStopIndexingProcError, MultipleReindexError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#each_changeObject (readonly)

Returns the value of attribute each_change.



56
57
58
# File 'lib/flex/live_reindex.rb', line 56

def each_change
  @each_change
end

Instance Method Details

#on_each_change(&block) ⇒ Object



53
54
55
# File 'lib/flex/live_reindex.rb', line 53

def on_each_change(&block)
  @each_change = block
end

#on_reindex(&block) ⇒ Object



49
50
51
# File 'lib/flex/live_reindex.rb', line 49

def on_reindex(&block)
  @reindex = block
end

#on_stop_indexing(&block) ⇒ Object



58
59
60
# File 'lib/flex/live_reindex.rb', line 58

def on_stop_indexing(&block)
  @stop_indexing = block
end

#prefix_index(index) ⇒ Object

Raises:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/flex/live_reindex.rb', line 101

def prefix_index(index)
  base = unprefix_index(index)
  # raise if base is not included in @ensure_indices
  raise ExtraIndexError, "The index #{base} is missing from the :ensure_indices option. Reindexing aborted." \
        if @ensure_indices && !@ensure_indices.include?(base)
  prefixed = @timestamp + base
  unless @indices.include?(base)
    unless Flex.exist?(:index => prefixed)
      config_hash[base] = {} unless config_hash.has_key?(base)
      Flex.POST "/#{prefixed}", config_hash[base]
    end
    @indices |= [base]
  end
  prefixed
end

#reindex(opts = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



62
63
64
65
# File 'lib/flex/live_reindex.rb', line 62

def reindex(opts={})
  yield self
  perform(opts)
end

#reindex_indices(opts = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flex/live_reindex.rb', line 67

def reindex_indices(opts={})
  yield self if block_given?

  opts[:verbose] = true unless opts.has_key?(:verbose)
  opts[:index] ||= opts.delete(:indices) || config_hash.keys

  # we override the on_reindex eventually set
  on_reindex do
    migrate_indices(opts)
  end

  perform(opts)
end

#should_prefix_index?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/flex/live_reindex.rb', line 81

def should_prefix_index?
  Redis.get(:pid) == $$.to_s
end

#should_track_change?Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/flex/live_reindex.rb', line 85

def should_track_change?
  pid = Redis.get(:pid)
  !!pid && !(pid == $$.to_s)
end

#track_change(action, document) ⇒ Object



90
91
92
# File 'lib/flex/live_reindex.rb', line 90

def track_change(action, document)
  Redis.rpush(:changes, MultiJson.encode([action, document]))
end

#track_external_change(app_id, action, document) ⇒ Object

use this method when you are tracking the change of another app you must pass the app_id of the app being affected by the change



96
97
98
99
# File 'lib/flex/live_reindex.rb', line 96

def track_external_change(app_id, action, document)
  return unless Conf.redis
  Conf.redis.rpush("#{KEYS[:changes]}-#{app_id}", MultiJson.encode([action, document]))
end

#unprefix_index(index) ⇒ Object

remove the (eventual) prefix



118
119
120
# File 'lib/flex/live_reindex.rb', line 118

def unprefix_index(index)
  index.sub(/^\d{14}_/, '')
end