module MongoidXapian::Indexer

Public Class Methods

add(doc) click to toggle source
# File lib/mongoid-xapian/indexer.rb, line 3
def self.add(doc)
  on_db(doc) do
    xapian_doc = documents.add(doc.to_xapian)
    doc.set(:xapian_id, xapian_doc.id)
  end
end
remove(doc) click to toggle source
# File lib/mongoid-xapian/indexer.rb, line 21
def self.remove(doc)
  if doc.xapian_id
    on_db(doc) do
      documents.delete(doc.xapian_id)
    end
  end
end
update(doc) click to toggle source
# File lib/mongoid-xapian/indexer.rb, line 10
def self.update(doc)
  if doc.xapian_id
    on_db(doc) do
      xapian_doc = XapianFu::XapianDoc.new(doc.to_xapian.merge(:id => doc.xapian_id), :xapian_db => self)
      xapian_doc.save
    end
  else
    add(doc)
  end
end

Private Class Methods

on_db(doc, &block) click to toggle source
# File lib/mongoid-xapian/indexer.rb, line 30
def self.on_db(doc, &block)
  10.times do
    begin
      doc.class.xapian_db.instance_exec(&block)
      break
    rescue IOError
    end
  end

  Thread.start do
    ok = false
    10.times do
      begin
        doc.class.xapian_db.flush
        ok = true
        break
      rescue IOError
      end
      sleep 0.1
    end

    puts "CANNOT UNLOCK DB" if !ok
  end
end