Module: XapianDb
- Defined in:
- lib/xapian_db/database.rb,
lib/xapian_db.rb,
lib/type_codec.rb,
lib/xapian_db/config.rb,
lib/xapian_db/indexer.rb,
lib/xapian_db/railtie.rb,
lib/xapian_db/resultset.rb,
lib/xapian_db/utilities.rb,
lib/xapian_db/query_parser.rb,
lib/generators/install_generator.rb,
lib/xapian_db/document_blueprint.rb,
lib/xapian_db/repositories/stemmer.rb,
lib/xapian_db/repositories/stopper.rb,
lib/xapian_db/adapters/base_adapter.rb,
lib/xapian_db/adapters/generic_adapter.rb,
lib/xapian_db/index_writers/no_op_writer.rb,
lib/xapian_db/adapters/datamapper_adapter.rb,
lib/xapian_db/index_writers/direct_writer.rb,
lib/xapian_db/index_writers/resque_worker.rb,
lib/xapian_db/index_writers/resque_writer.rb,
lib/xapian_db/index_writers/sidekiq_worker.rb,
lib/xapian_db/index_writers/sidekiq_writer.rb,
lib/xapian_db/model_extenders/active_record.rb,
lib/xapian_db/adapters/active_record_adapter.rb,
lib/xapian_db/index_writers/beanstalk_worker.rb,
lib/xapian_db/index_writers/beanstalk_writer.rb,
lib/xapian_db/index_writers/transactional_writer.rb
Overview
This writer collects index change requests but does not submit them immediately to the database. The index changes are applied when the commit method is called. This writer is intended for internal use only. Do not use it in a xapian configuration!
Defined Under Namespace
Modules: Adapters, Generators, IndexWriters, ModelExtenders, Repositories, Utilities Classes: Config, Database, DocumentBlueprint, InMemoryDatabase, Indexer, PersistentDatabase, QueryParser, Railtie, Resultset, TypeCodec
Constant Summary collapse
- LANGUAGE_MAP =
Supported languages
{:da => :danish, :nl => :dutch, :en => :english, :fi => :finnish, :fr => :french, :de => :german2, # Normalises umlauts and ß :hu => :hungarian, :it => :italian, :nb => :norwegian, :nn => :norwegian, :no => :norwegian, :pt => :portuguese, :ro => :romanian, :ru => :russian, :es => :spanish, :sv => :swedish, :tr => :turkish}
Class Method Summary collapse
-
.auto_indexing_disabled(&block) ⇒ Object
Execute a block and do not update the index.
-
.create_db(options = {}) ⇒ XapianDb::Database
Create a database.
-
.database ⇒ XapianDb::Database
Access the configured database.
-
.delete_doc_with(xapian_id, commit = true) ⇒ Object
Remove a document from the index.
-
.execute_block(opts, &block) ⇒ Object
execute a block of code with a given writer and handle errors.
-
.facets(attribute, expression) ⇒ Hash<Class, Integer>
Get facets from the configured database.
-
.index(obj, commit = true, changed_attrs: []) ⇒ Object
Update an object in the index.
-
.open_db(options = {}) ⇒ XapianDb::Database
Open a database.
-
.rebuild_xapian_index(options = {}) ⇒ Boolean
Rebuild the xapian index for all configured blueprints.
-
.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).
-
.reindex_class(klass, options = {}) ⇒ Object
Reindex all objects of a given class.
-
.search(expression, options = {}) ⇒ XapianDb::Resultset
Query the configured database.
-
.setup(&block) ⇒ Object
Global configuration for XapianDb.
-
.transaction(&block) ⇒ Object
Execute a block as a transaction.
Class Method Details
.auto_indexing_disabled(&block) ⇒ Object
Execute a block and do not update the index
165 166 167 168 169 170 |
# File 'lib/xapian_db.rb', line 165 def self.auto_indexing_disabled(&block) execute_block :writer => XapianDb::IndexWriters::NoOpWriter do block.call end end |
.create_db(options = {}) ⇒ XapianDb::Database
Create a database
56 57 58 59 60 61 62 |
# File 'lib/xapian_db.rb', line 56 def self.create_db( = {}) if [:path] PersistentDatabase.new(:path => [:path], :create => true) else InMemoryDatabase.new end end |
.database ⇒ XapianDb::Database
Access the configured database. See XapianDb::Config.setup for instructions on how to configure a database
81 82 83 |
# File 'lib/xapian_db.rb', line 81 def self.database XapianDb::Config.database end |
.delete_doc_with(xapian_id, commit = true) ⇒ Object
Remove a document from the index
115 116 117 118 |
# File 'lib/xapian_db.rb', line 115 def self.delete_doc_with(xapian_id, commit=true) writer = Thread.current[:xapian_db_block_writer] || XapianDb::Config.writer writer.delete_doc_with xapian_id, commit end |
.execute_block(opts, &block) ⇒ Object
execute a block of code with a given writer and handle errors
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/xapian_db.rb', line 176 def self.execute_block(opts, &block) Thread.current[:xapian_db_block_writer] = opts[:writer] begin block.call rescue Exception => ex if opts[:error_message] if defined?(Rails) Rails.logger.error opts[:error_message] else puts opts[:error_message] end end raise ensure # release the block writer Thread.current[:xapian_db_block_writer] = nil end end |
.facets(attribute, expression) ⇒ Hash<Class, Integer>
Get facets from the configured database. See XapianDb::Database#facets for options
102 103 104 |
# File 'lib/xapian_db.rb', line 102 def self.facets(attribute, expression) XapianDb::Config.database.facets attribute, expression end |
.index(obj, commit = true, changed_attrs: []) ⇒ Object
Update an object in the index
108 109 110 111 |
# File 'lib/xapian_db.rb', line 108 def self.index(obj, commit=true, changed_attrs: []) writer = Thread.current[:xapian_db_block_writer] || XapianDb::Config.writer writer.index obj, commit, changed_attrs: changed_attrs end |
.open_db(options = {}) ⇒ XapianDb::Database
Open a database
70 71 72 73 74 75 76 |
# File 'lib/xapian_db.rb', line 70 def self.open_db( = {}) if [:path] PersistentDatabase.new(:path => [:path], :create => false) else InMemoryDatabase.new end end |
.rebuild_xapian_index(options = {}) ⇒ Boolean
Rebuild the xapian index for all configured blueprints
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/xapian_db.rb', line 144 def self.rebuild_xapian_index(={}) configured_classes = XapianDb::DocumentBlueprint.configured_classes return false unless configured_classes.size > 0 configured_classes.each do |klass| if klass.respond_to?(:rebuild_xapian_index) XapianDb::Config.writer.reindex_class(klass, ) end end true 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)
122 123 124 125 126 127 128 129 130 |
# File 'lib/xapian_db.rb', line 122 def self.reindex(object, commit=true, changed_attrs: []) writer = Thread.current[:xapian_db_block_writer] || XapianDb::Config.writer blueprint = XapianDb::DocumentBlueprint.blueprint_for object.class.name if blueprint.should_index?(object) writer.index object, commit, changed_attrs: changed_attrs else writer.delete_doc_with object.xapian_id, commit end end |
.reindex_class(klass, options = {}) ⇒ Object
Reindex all objects of a given class
136 137 138 |
# File 'lib/xapian_db.rb', line 136 def self.reindex_class(klass, ={}) XapianDb::Config.writer.reindex_class klass, end |
.search(expression, options = {}) ⇒ XapianDb::Resultset
Query the configured database. See XapianDb::Database#search for options
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/xapian_db.rb', line 88 def self.search(expression, ={}) order = .delete :order if order attr_names = [order].flatten undefined_attrs = attr_names - XapianDb::DocumentBlueprint.attributes raise ArgumentError.new "invalid order clause: attributes #{undefined_attrs.inspect} are not defined" unless undefined_attrs.empty? [:sort_indices] = attr_names.map {|attr_name| XapianDb::DocumentBlueprint.value_number_for(attr_name) } end XapianDb::Config.database.search(expression, ) end |
.setup(&block) ⇒ Object
Global configuration for XapianDb. See XapianDb::Config.setup for available options
46 47 48 49 |
# File 'lib/xapian_db.rb', line 46 def self.setup(&block) XapianDb::Config.setup(&block) @writer = XapianDb::Config.writer end |
.transaction(&block) ⇒ Object
Execute a block as a transaction
156 157 158 159 160 161 162 |
# File 'lib/xapian_db.rb', line 156 def self.transaction(&block) writer = XapianDb::IndexWriters::TransactionalWriter.new execute_block :writer => writer, :error_message => "error in XapianDb transaction block, transaction aborted" do block.call writer.commit_using XapianDb::Config.writer end end |