class XapianDb::IndexWriters::DirectWriter

Constants

BATCH_SIZE

Public Class Methods

delete_doc_with(xapian_id, commit=true) click to toggle source

Remove an object from the index @param [String] xapian_id The document id of an object

   # File lib/xapian_db/index_writers/direct_writer.rb
31 def delete_doc_with(xapian_id, commit=true)
32   XapianDb.database.delete_doc_with_unique_term xapian_id
33   XapianDb.database.commit if commit
34 end
index(object, commit=true, changed_attrs: []) click to toggle source

Update an object in the index @param [Object] object An instance of a class with a blueprint configuration

   # File lib/xapian_db/index_writers/direct_writer.rb
21 def index(object, commit=true, changed_attrs: [])
22   blueprint = XapianDb::DocumentBlueprint.blueprint_for(object.class.name)
23   indexer   = XapianDb::Indexer.new(XapianDb.database, blueprint)
24   doc       = indexer.build_document_for(object)
25   XapianDb.database.store_doc(doc)
26   XapianDb.database.commit if commit
27 end
reindex(object, commit=true, changed_attrs: []) click to toggle source

Update or delete a xapian document belonging to an object depending on the ignore_if logic(if present) @param [Object] object An instance of a class with a blueprint configuration

   # File lib/xapian_db/index_writers/direct_writer.rb
38 def reindex(object, commit=true, changed_attrs: [])
39   blueprint = XapianDb::DocumentBlueprint.blueprint_for object.class.name
40   if blueprint.should_index?(object)
41     index object, commit, changed_attrs: changed_attrs
42   else
43     delete_doc_with object.xapian_id, commit
44   end
45 end
reindex_class(klass, options={}) click to toggle source

Reindex all objects of a given class @param [Class] klass The class to reindex @param [Hash] options Options for reindexing @option options [Boolean] :verbose (false) Should the reindexing give status informations?

   # File lib/xapian_db/index_writers/direct_writer.rb
51 def reindex_class(klass, options={})
52   opts = {:verbose => false}.merge(options)
53   blueprint = XapianDb::DocumentBlueprint.blueprint_for klass.name
54   primary_key = blueprint._adapter.primary_key_for(klass)
55   XapianDb.database.delete_docs_of_class(klass)
56   indexer    = XapianDb::Indexer.new(XapianDb.database, blueprint)
57   if blueprint.lazy_base_query
58     base_query = blueprint.lazy_base_query.call
59   else
60     base_query = klass
61   end
62   show_progressbar = false
63   obj_count = base_query.count
64   if opts[:verbose]
65     show_progressbar = defined?(ProgressBar)
66     puts "reindexing #{obj_count} objects of #{klass}..."
67     pbar = ProgressBar.create(:title => "Status", :total => obj_count, :format => ' %t %e %B %p%%') if show_progressbar
68   end
69 
70   # Process the objects in batches to reduce the memory footprint
71   nr_of_batches = (obj_count / BATCH_SIZE) + 1
72   nr_of_batches.times do |batch|
73     base_query.offset(batch * BATCH_SIZE).limit(BATCH_SIZE).order(klass.order_condition(primary_key)).each do |obj|
74       reindex obj, false
75       pbar.increment if show_progressbar
76     end
77   end
78   XapianDb.database.commit
79   true
80 end