Class: ActsAsXapian::ReadableIndex

Inherits:
Index
  • Object
show all
Defined in:
lib/acts_as_xapian/readable_index.rb

Constant Summary collapse

@@available_indicies =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Index

init, prepare_environment

Constructor Details

#initialize(models) ⇒ ReadableIndex

Opens the db for reading and builds the query parser



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/acts_as_xapian/readable_index.rb', line 26

def initialize(models)
  raise NoXapianRubyBindingsError.new("Xapian Ruby bindings not installed") unless ActsAsXapian.bindings_available
  raise "acts_as_xapian hasn't been called in any models" if @@init_values.empty?

  self.class.prepare_environment

  # basic Xapian objects
  begin
    @db = Xapian::Database.new(@@db_path)
    @enquire = Xapian::Enquire.new(@db)
  rescue IOError
    raise "Xapian database not opened; have you built it with rake xapian:rebuild_index ?"
  end

  init_query_parser(models)
end

Instance Attribute Details

#enquireObject (readonly)

Returns the value of attribute enquire.



5
6
7
# File 'lib/acts_as_xapian/readable_index.rb', line 5

def enquire
  @enquire
end

#query_parserObject (readonly)

Returns the value of attribute query_parser.



5
6
7
# File 'lib/acts_as_xapian/readable_index.rb', line 5

def query_parser
  @query_parser
end

#values_by_prefixObject (readonly)

Returns the value of attribute values_by_prefix.



5
6
7
# File 'lib/acts_as_xapian/readable_index.rb', line 5

def values_by_prefix
  @values_by_prefix
end

Class Method Details

.index_for(models) ⇒ Object

Takes an array of model classes and returns an index object to be used for searching across the given models

Prevents query parser interaction across multiple models unless performing a multi model search



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/acts_as_xapian/readable_index.rb', line 12

def self.index_for(models)
  index_key = models.map {|m| m.to_s }.sort.join('---')
  if @@available_indicies.key?(index_key)
    index = @@available_indicies[index_key]
    index.reset_enquire!
    index
  else
    index = self.new(models)
    @@available_indicies[index_key] = index
    index
  end
end

Instance Method Details

#reset_enquire!Object

Creates a new search session



44
45
46
47
48
49
# File 'lib/acts_as_xapian/readable_index.rb', line 44

def reset_enquire!
  @db.reopen # This grabs the latest db updates
  @enquire = Xapian::Enquire.new(@db)
rescue IOError
  raise "Xapian database not opened; have you built it with rake xapian:rebuild_index ?"
end