Class: Polecat::IndexReader

Inherits:
Object
  • Object
show all
Defined in:
lib/polecat/index_reader.rb

Overview

reads an index directory

This class reads the content of an index directory and builds the necessary structures for the index type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ IndexReader

initialize a new reader

Create a new reader for the given path. If the directory is empty, you will get an empty index, else all documents stored in that directory.

Parameters:

  • path (String)

    the path to the index directory

Raises:

  • (ArgumentError)


14
15
16
17
# File 'lib/polecat/index_reader.rb', line 14

def initialize path
  @path = path
  raise ArgumentError, 'no valid directory' unless File.directory? @path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/polecat/index_reader.rb', line 7

def path
  @path
end

Instance Method Details

#locked?Boolean

checks whether the directory is locked or not

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/polecat/index_reader.rb', line 39

def locked?
  if File.exists? @path + '/index.lock'
    true
  else
    false
  end
end

#readPolecat::Index

read the content of the directory

Read all files of the directory and return an index object.

Returns:

  • (Polecat::Index)

    the index with all documents

Raises:

  • (IOError)

    raised when the directory is locked



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/polecat/index_reader.rb', line 24

def read
  raise IOError, 'index is locked' if locked?
  files = Dir[@path + '/*']
  if files.count > 0
    documents = []
    files.each do |file|
      documents += Marshal.load(File.read(file))
    end
    documents
  else
    []
  end
end