Class: XapianDb::PersistentDatabase

Inherits:
Database
  • Object
show all
Defined in:
lib/xapian_db/database.rb

Overview

Persistent database on disk

Instance Method Summary collapse

Methods inherited from Database

#delete_doc_with_unique_term, #delete_docs_of_class, #facets, #find_similar_to, #search, #size, #store_doc

Methods included from Utilities

#assert_valid_keys, #camelize, #constantize

Constructor Details

#initialize(options) ⇒ PersistentDatabase

Constructor

Examples:

Force the creation of a database. Will overwrite an existing database

db = XapianDb::PersistentDatabase.new(:path => "/tmp/mydb", :create => true)

Open an existing database. The database must exist

db = XapianDb::PersistentDatabase.new(:path => "/tmp/mydb", :create => false)

Parameters:

  • options (Hash)

    Options for the persistent database

Options Hash (options):

  • :path (String)

    A path to the file system

  • :create (Boolean)

    Should the database be created? Will overwrite an existing database if true!



214
215
216
217
218
219
220
221
222
223
# File 'lib/xapian_db/database.rb', line 214

def initialize(options)
  @path    = options[:path]
  @db_flag = options[:create] ? Xapian::DB_CREATE_OR_OVERWRITE : Xapian::DB_OPEN
  if options[:create]
    # make sure the path exists; Xapian will not create the necessary directories
    FileUtils.makedirs @path
    @writer = Xapian::WritableDatabase.new(@path, @db_flag)
  end
  @reader = Xapian::Database.new(@path)
end

Instance Method Details

#commitObject

Commit all pending changes



240
241
242
# File 'lib/xapian_db/database.rb', line 240

def commit
  writer.commit
end

#readerXapian::Database

Get a readable instance of the database

Returns:



227
228
229
# File 'lib/xapian_db/database.rb', line 227

def reader
  Xapian::Database.new(@path)
end

#writerXapian::WritableDatabase

The writer is instantiated layzily to avoid a permanent write lock on the database. Please note that you will get locking exceptions if you open the same database multiple times and access the writer in more than one instance!

Returns:



235
236
237
# File 'lib/xapian_db/database.rb', line 235

def writer
  @writer ||= Xapian::WritableDatabase.new(@path, @db_flag)
end