Class: Friendly::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly/indexer.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datastore = Friendly.datastore, translator = Translator.new) ⇒ Indexer

Returns a new instance of Indexer.



19
20
21
22
# File 'lib/friendly/indexer.rb', line 19

def initialize(datastore  = Friendly.datastore, translator = Translator.new)
  @datastore  = datastore
  @translator = translator
end

Class Attribute Details

.objects_per_iterationObject

Returns the value of attribute objects_per_iteration.



4
5
6
# File 'lib/friendly/indexer.rb', line 4

def objects_per_iteration
  @objects_per_iteration
end

Instance Attribute Details

#datastoreObject (readonly)

Returns the value of attribute datastore.



17
18
19
# File 'lib/friendly/indexer.rb', line 17

def datastore
  @datastore
end

#translatorObject (readonly)

Returns the value of attribute translator.



17
18
19
# File 'lib/friendly/indexer.rb', line 17

def translator
  @translator
end

Class Method Details

.instanceObject



10
11
12
# File 'lib/friendly/indexer.rb', line 10

def instance
  @instance ||= new
end

.populate(klass, *fields) ⇒ Object



6
7
8
# File 'lib/friendly/indexer.rb', line 6

def populate(klass, *fields)
  instance.populate(klass, klass.storage_proxy.index_for_fields(fields))
end

Instance Method Details

#populate(klass, index) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/friendly/indexer.rb', line 24

def populate(klass, index)
  count  = 0
  loop do
    rows = datastore.all(klass, Query.new(:offset! => count, 
                                          :limit!  => objects_per_iteration, 
                                          :order!  => :added_id.asc)).to_a
    rows.each do |attrs|
      begin
        index.create(translator.to_object(klass, attrs))
      rescue Sequel::DatabaseError
        # we can safely swallow this exception because if we've gotten
        # to this point, we can be pretty sure that it's a duplicate
        # key error, which just means that the object already exists
        # in the index
      end
    end
    break if rows.length < objects_per_iteration
    count += objects_per_iteration
  end
end