Class: XapianDb::IndexWriters::DirectWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/xapian_db/index_writers/direct_writer.rb

Constant Summary collapse

BATCH_SIZE =
500

Class Method Summary collapse

Class Method Details

.delete_doc_with(xapian_id, commit = true) ⇒ Object

Remove an object from the index


31
32
33
34
# File 'lib/xapian_db/index_writers/direct_writer.rb', line 31

def delete_doc_with(xapian_id, commit=true)
  XapianDb.database.delete_doc_with_unique_term xapian_id
  XapianDb.database.commit if commit
end

.index(object, commit = true, changed_attrs: []) ⇒ Object

Update an object in the index


21
22
23
24
25
26
27
# File 'lib/xapian_db/index_writers/direct_writer.rb', line 21

def index(object, commit=true, changed_attrs: [])
  blueprint = XapianDb::DocumentBlueprint.blueprint_for(object.class.name)
  indexer   = XapianDb::Indexer.new(XapianDb.database, blueprint)
  doc       = indexer.build_document_for(object)
  XapianDb.database.store_doc(doc)
  XapianDb.database.commit if commit
end

.reindex(object, commit = true, changed_attrs: []) ⇒ Object

Update or delete a xapian document belonging to an object depending on the ignore_if logic(if present)


38
39
40
41
42
43
44
45
# File 'lib/xapian_db/index_writers/direct_writer.rb', line 38

def reindex(object, commit=true, changed_attrs: [])
  blueprint = XapianDb::DocumentBlueprint.blueprint_for object.class.name
  if blueprint.should_index?(object)
    index object, commit, changed_attrs: changed_attrs
  else
    delete_doc_with object.xapian_id, commit
  end
end

.reindex_class(klass, options = {}) ⇒ Object

Reindex all objects of a given class

Options Hash (options):

  • :verbose (Boolean) — default: false

    Should the reindexing give status informations?


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/xapian_db/index_writers/direct_writer.rb', line 51

def reindex_class(klass, options={})
  opts = {:verbose => false}.merge(options)
  blueprint = XapianDb::DocumentBlueprint.blueprint_for klass.name
  primary_key = blueprint._adapter.primary_key_for(klass)
  XapianDb.database.delete_docs_of_class(klass)
  indexer    = XapianDb::Indexer.new(XapianDb.database, blueprint)
  if blueprint.lazy_base_query
    base_query = blueprint.lazy_base_query.call
  else
    base_query = klass
  end
  show_progressbar = false
  obj_count = base_query.count
  if opts[:verbose]
    show_progressbar = defined?(ProgressBar)
    puts "reindexing #{obj_count} objects of #{klass}..."
    pbar = ProgressBar.create(:title => "Status", :total => obj_count, :format => ' %t %e %B %p%%') if show_progressbar
  end

  # Process the objects in batches to reduce the memory footprint
  nr_of_batches = (obj_count / BATCH_SIZE) + 1
  nr_of_batches.times do |batch|
    base_query.offset(batch * BATCH_SIZE).limit(BATCH_SIZE).order(klass.order_condition(primary_key)).each do |obj|
      reindex obj, false
      pbar.increment if show_progressbar
    end
  end
  XapianDb.database.commit
  true
end