Class: XapianFu::XapianDocumentsAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/xapian_fu/xapian_documents_accessor.rb

Overview

A XapianDocumentsAccessor is used to provide the XapianDb#documents interface. It is usually set up by a XapianDb so you shouldn't need to set up your own.

Instance Method Summary collapse

Constructor Details

#initialize(xdb) ⇒ XapianDocumentsAccessor

:nodoc:



6
7
8
# File 'lib/xapian_fu/xapian_documents_accessor.rb', line 6

def initialize(xdb) #:nodoc:
  @xdb = xdb
end

Instance Method Details

#[](doc_id) ⇒ Object

Return the document with the given id from the database or nil if it doesn't exist



49
50
51
52
53
# File 'lib/xapian_fu/xapian_documents_accessor.rb', line 49

def [](doc_id)
  find(doc_id)
rescue XapianFu::DocNotFound
  nil
end

#add(doc) ⇒ Object Also known as: <<

Add a document to the index. A document can be just a hash, the keys representing field names and their values the data to be indexed. Or it can be a XapianDoc, or any object with a to_s method.

If the document has an :id field, it is used as the primary key in the Xapian database.

If the document object reponds to the method :data, whatever it returns is marshalled and stored in the Xapian database. Any arbitrary data up to Xmeg can be stored here.

Currently, all fields are stored in the database. This will change to store only those fields requested to be stored.



29
30
31
32
33
34
# File 'lib/xapian_fu/xapian_documents_accessor.rb', line 29

def add(doc)
  doc = XapianDoc.new(doc) unless doc.is_a? XapianDoc
  doc.db = @xdb
  doc.save
  doc
end

#delete(doc) ⇒ Object

Delete the given document from the database and return the document id, or nil if it doesn't exist



57
58
59
60
61
62
63
64
# File 'lib/xapian_fu/xapian_documents_accessor.rb', line 57

def delete(doc)
  if doc.respond_to?(:to_i)
    @xdb.rw.delete_document(doc.to_i)
    doc.to_i
  end
rescue RuntimeError => e
  raise e unless e.to_s =~ /^DocNotFoundError/
end

#find(doc_id) ⇒ Object

Return the document with the given id from the database. Raises a XapianFu::DocNotFoundError exception if it doesn't exist.



40
41
42
43
44
45
# File 'lib/xapian_fu/xapian_documents_accessor.rb', line 40

def find(doc_id)
  xdoc = @xdb.ro.document(doc_id)
  XapianDoc.new(xdoc, :xapian_db => @xdb)
rescue RuntimeError => e
  raise e.to_s =~ /^DocNotFoundError/ ? XapianFu::DocNotFound : e
end

#max(key = :id) ⇒ Object

Return the document with the highest value in the specified field or nil if it doesn't exist



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/xapian_fu/xapian_documents_accessor.rb', line 67

def max(key = :id)
  if key == :id
    # for :id we can use lastdocid
    find(@xdb.ro.lastdocid) rescue nil
  else
    # for other values, we do a search ordered by that key in descening order
    query = Xapian::Query.new(Xapian::Query::OP_VALUE_GE, XapianDocValueAccessor.value_key(key), "0")
    e = Xapian::Enquire.new(@xdb.ro)
    e.query = query
    e.sort_by_value!(XapianDocValueAccessor.value_key(key))
    r = e.mset(0, 1).matches.first
    find(r.docid) rescue nil
  end
end

#new(doc = nil, options = { }) ⇒ Object

Build a new XapianDoc for this database



11
12
13
14
# File 'lib/xapian_fu/xapian_documents_accessor.rb', line 11

def new(doc = nil, options = { })
  options = options.merge({ :xapian_db => @xdb })
  XapianDoc.new(doc, options)
end