class Polecat::IndexWriter

handles the writing of new documents to the index.

This class is responsible for writing the documents to the index. It takes a path on creation and checks, if it is an empty or a valid index directory.

When the documents are getting written to the filesystem, a ‘index.lock’ file is written as an extra lock. It then writes a new file into the directory, which has all documents.

Attributes

path[R]

Public Class Methods

new(path) click to toggle source

create a new IndexWriter

This creates a new IndexWriter set to the given path. @param [String] path the path to the index directory

# File lib/polecat/index_writer.rb, line 17
def initialize path
  if !File.directory? path
    raise ArgumentError, 'not a directory'
  elsif File.exists? path + '/index.lock'
    raise IOError, 'index is locked'
  else
    @path = path
    @documents = []
  end
end

Public Instance Methods

add(doc) click to toggle source

add a new document to the writer

This adds a Document to the temporary storage. Call write to write them to the filesystem. @param [Document] doc the document to store

# File lib/polecat/index_writer.rb, line 42
def add doc
  if doc.respond_to? :attributes
    @documents << doc
  else
    raise ArgumentError, 'missing method attributes'
  end
end
count() click to toggle source

returns the count of elements not flushed

This method returns the count of all elements stored in the Writer, but not yet flushed to a file. @return [Fixnum] count of files

# File lib/polecat/index_writer.rb, line 33
def count
  @documents.count
end
create_reader() click to toggle source

creates an index reader with the writers path

@returns [Polecat::IndexReader] an IndexReader with the same path

# File lib/polecat/index_writer.rb, line 69
def create_reader
  Polecat::IndexReader.new @path
end
write() click to toggle source

write all documents to the disc

Write all stored documents to the disc and clear the buffer. @return [Boolean] true when the write was a success

# File lib/polecat/index_writer.rb, line 54
def write
  return false unless set_lock
  file_name = generate_filename
          
  File.open file_name, 'w' do |file|
    file.write Marshal.dump(@documents)
  end

  @documents = []
  release_lock
end

Private Instance Methods

generate_filename() click to toggle source

generates a new file name for an index file @private

# File lib/polecat/index_writer.rb, line 106
def generate_filename 
  last_file = Dir[@path + '/*.ind'].sort.last
  if last_file.nil?
    file_name = @path + '/ind0.ind'
  else
    number = File.basename(last_file).match(/[0-9]+/)[0].to_i
    # we have to match the complete name, because there can be
    # numbers before the file too
    file_name = last_file.gsub(
      /ind#{number}\.ind/,
      "ind#{(number + 1)}.ind"
    )
  end
  file_name
end
lock_file_name() click to toggle source

get the full path of the lock file @private

# File lib/polecat/index_writer.rb, line 99
def lock_file_name
  @path + '/index.lock'
end
release_lock() click to toggle source

release the index lock @private

# File lib/polecat/index_writer.rb, line 87
def release_lock
  if File.exists? lock_file_name
    FileUtils.rm lock_file_name
    true
  else
    false
  end
end
set_lock() click to toggle source

set the lock on the index @private

# File lib/polecat/index_writer.rb, line 75
def set_lock
  if File.exists? lock_file_name
    false
  else
    FileUtils.touch lock_file_name
    true
  end
end