Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add the indexer

  • Loading branch information...
commit 4fab55d72a14804173e3716c07d031ee521c1d52 1 parent b53085b
James Golick authored
Showing with 51 additions and 0 deletions.
  1. +1 −0  lib/friendly.rb
  2. +50 −0 lib/friendly/indexer.rb
1  lib/friendly.rb
View
@@ -7,6 +7,7 @@
require 'friendly/document'
require 'friendly/document_table'
require 'friendly/index'
+require 'friendly/indexer'
require 'friendly/memcached'
require 'friendly/query'
require 'friendly/sequel_monkey_patches'
50 lib/friendly/indexer.rb
View
@@ -0,0 +1,50 @@
+module Friendly
+ class Indexer
+ class << self
+ attr_accessor :objects_per_iteration
+
+ def populate(klass, *fields)
+ instance.populate(klass, klass.storage_proxy.index_for_fields(fields))
+ end
+
+ def instance
+ @instance ||= new
+ end
+ end
+
+ self.objects_per_iteration = 100
+
+ attr_reader :datastore, :translator
+
+ def initialize(datastore = Friendly.datastore, translator = Translator.new)
+ @datastore = datastore
+ @translator = translator
+ end
+
+ def populate(klass, index)
+ loop do
+ count = 0
+ rows = datastore.all(klass, Query.new(:offset! => count,
+ :limit! => objects_per_iteration,
+ :order! => :added_id.asc))
+ 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
+
+ protected
+ def objects_per_iteration
+ self.class.objects_per_iteration
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.