Class: XapianDb::Config

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/xapian_db/config.rb

Overview

Global configuration for XapianDb

Examples:

A typical configuration might look like this:

XapianDb::Config.setup do |config|
  config.adapter  :active_record
  config.writer   :direct
  config.database "db/xapian_db"
end

Author:

  • Gernot Kogler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#assert_valid_keys, #camelize, #constantize

Instance Attribute Details

#_adapterObject (readonly)


DSL methods




77
78
79
# File 'lib/xapian_db/config.rb', line 77

def _adapter
  @_adapter
end

#_beanstalk_daemonObject (readonly)


DSL methods




77
78
79
# File 'lib/xapian_db/config.rb', line 77

def _beanstalk_daemon
  @_beanstalk_daemon
end

#_databaseObject (readonly)


DSL methods




77
78
79
# File 'lib/xapian_db/config.rb', line 77

def _database
  @_database
end

#_enabled_query_flagsObject (readonly)


DSL methods




77
78
79
# File 'lib/xapian_db/config.rb', line 77

def _enabled_query_flags
  @_enabled_query_flags
end

#_resque_queueObject (readonly)


DSL methods




77
78
79
# File 'lib/xapian_db/config.rb', line 77

def _resque_queue
  @_resque_queue
end

#_sidekiq_queueObject (readonly)


DSL methods




77
78
79
# File 'lib/xapian_db/config.rb', line 77

def _sidekiq_queue
  @_sidekiq_queue
end

#_stemmerObject (readonly)


DSL methods




77
78
79
# File 'lib/xapian_db/config.rb', line 77

def _stemmer
  @_stemmer
end

#_stopperObject (readonly)


DSL methods




77
78
79
# File 'lib/xapian_db/config.rb', line 77

def _stopper
  @_stopper
end

#_term_min_lengthObject (readonly)


DSL methods




77
78
79
# File 'lib/xapian_db/config.rb', line 77

def _term_min_length
  @_term_min_length
end

#_term_splitter_countObject (readonly)


DSL methods




77
78
79
# File 'lib/xapian_db/config.rb', line 77

def _term_splitter_count
  @_term_splitter_count
end

#_writerObject (readonly)


DSL methods




77
78
79
# File 'lib/xapian_db/config.rb', line 77

def _writer
  @_writer
end

Class Method Details

.query_flagsObject



64
65
66
67
68
69
70
# File 'lib/xapian_db/config.rb', line 64

def query_flags
  @config.instance_variable_get("@_enabled_query_flags") || [ Xapian::QueryParser::FLAG_WILDCARD,
                                                              Xapian::QueryParser::FLAG_BOOLEAN,
                                                              Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE,
                                                              Xapian::QueryParser::FLAG_SPELLING_CORRECTION
                                                            ]
end

.resque_queueObject



48
49
50
# File 'lib/xapian_db/config.rb', line 48

def resque_queue
  @config.instance_variable_get("@_resque_queue") || 'xapian_db'
end

.setup {|@config| ... } ⇒ Object

Configure global options for XapianDb. Availabe options:

In a Rails app, you can configure XapianDb using a config file. See the README for the details

Yields:

  • (@config)


29
30
31
32
# File 'lib/xapian_db/config.rb', line 29

def setup(&block)
  @config ||= Config.new
  yield @config if block_given?
end

.sidekiq_queueObject



52
53
54
# File 'lib/xapian_db/config.rb', line 52

def sidekiq_queue
  @config.instance_variable_get("@_sidekiq_queue") || 'xapian_db'
end

.term_min_lengthObject



56
57
58
# File 'lib/xapian_db/config.rb', line 56

def term_min_length
  @config.instance_variable_get("@_term_min_length") || 1
end

.term_splitter_countObject



60
61
62
# File 'lib/xapian_db/config.rb', line 60

def term_splitter_count
  @config.instance_variable_get("@_term_splitter_count") || 0
end

Instance Method Details

#adapter(type) ⇒ Object

Set the adapter

Parameters:



108
109
110
111
112
113
114
115
# File 'lib/xapian_db/config.rb', line 108

def adapter(type)
  begin
    @_adapter = XapianDb::Adapters.const_get("#{camelize(type.to_s)}Adapter")
  rescue NameError
    require File.dirname(__FILE__) + "/adapters/#{type}_adapter"
    @_adapter = XapianDb::Adapters.const_get("#{camelize(type.to_s)}Adapter")
  end
end

#beanstalk_daemon_url(url) ⇒ Object

Set the url and port of the beanstalk daemon

Parameters:

  • url (Symbol)

    The url of the beanstalk daemon; defaults to localhost:11300



136
137
138
# File 'lib/xapian_db/config.rb', line 136

def beanstalk_daemon_url(url)
  @_beanstalk_daemon_url = url
end

#database(path) ⇒ Object

Set the global database to use

Parameters:

  • path (String)

    The path to the database. Either apply a file sytem path or :memory for an in memory database



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/xapian_db/config.rb', line 83

def database(path)

  # If the current database is a persistent database, we must release the
  # database and run the garbage collector to remove the write lock
  if @_database.is_a?(XapianDb::PersistentDatabase)
    @_database = nil
    GC.start
  end

  if path.to_sym == :memory
    @_database = XapianDb.create_db
  else
    begin
      @_database = XapianDb.open_db :path => path
    rescue IOError
      @_database = XapianDb.create_db :path => path
    end
  end
end

#disable_query_flag(flag) ⇒ Object



181
182
183
184
# File 'lib/xapian_db/config.rb', line 181

def disable_query_flag(flag)
  @_enabled_query_flags ||= []
  @_enabled_query_flags.delete flag
end

#enable_query_flag(flag) ⇒ Object



175
176
177
178
179
# File 'lib/xapian_db/config.rb', line 175

def enable_query_flag(flag)
  @_enabled_query_flags ||= []
  @_enabled_query_flags << flag
  @_enabled_query_flags.uniq!
end

#indexer_preprocess_callback(method) ⇒ Object

Set the indexer preprocess callback.

Examples:

class Util
  def self.strip_accents(terms)
    terms.gsub(/[éèêëÉÈÊË]/, "e")
  end
end

XapianDb::Config.setup do |config|
  config.indexer_preprocess_callback Util.method(:strip_accents)
end

Parameters:

  • method (Method)

    a class method; needs to take one parameter and return a string.



198
199
200
# File 'lib/xapian_db/config.rb', line 198

def indexer_preprocess_callback(method)
  @_preprocess_terms = method
end

#language(lang) ⇒ Object

Set the language. see LANGUAGE_MAP for supported languages

Examples:

XapianDb::Config.setup do |config|
  config.language :de
end

Parameters:

  • lang (Symbol)

    The language; apply the two letter ISO639 code for the language



159
160
161
162
163
# File 'lib/xapian_db/config.rb', line 159

def language(lang)
  lang ||= :none
  @_stemmer = XapianDb::Repositories::Stemmer.stemmer_for lang
  @_stopper = lang == :none ? nil : XapianDb::Repositories::Stopper.stopper_for(lang)
end

#resque_queue(name) ⇒ Object

Set the name of the resque queue

Parameters:

  • name (String)

    The name of the resque queue



142
143
144
# File 'lib/xapian_db/config.rb', line 142

def resque_queue(name)
  @_resque_queue = name
end

#sidekiq_queue(name) ⇒ Object

Set the name of the sidekiq queue

Parameters:

  • name (String)

    The name of the sidekiq queue



148
149
150
# File 'lib/xapian_db/config.rb', line 148

def sidekiq_queue(name)
  @_sidekiq_queue = name
end

#term_min_length(length) ⇒ Object

Set minimum length a term must have to get indexed; 2 is a good value to start

Parameters:

  • length (Integer)

    The minimum length



167
168
169
# File 'lib/xapian_db/config.rb', line 167

def term_min_length(length)
  @_term_min_length = length
end

#term_splitter_count(count) ⇒ Object



171
172
173
# File 'lib/xapian_db/config.rb', line 171

def term_splitter_count(count)
  @_term_splitter_count = count
end

#writer(type) ⇒ Object

Set the index writer

Parameters:



123
124
125
126
127
128
129
130
131
132
# File 'lib/xapian_db/config.rb', line 123

def writer(type)
  # We try to guess the writer name
  begin
    require File.dirname(__FILE__) + "/index_writers/#{type}_writer"
    @_writer = XapianDb::IndexWriters.const_get("#{camelize(type.to_s)}Writer")
  rescue LoadError
    puts "XapianDb: cannot load #{type} writer; see README for supported writers and how to install neccessary queue infrastructure"
    raise
  end
end