0
0
Fork 0

* new basis for the save and load system of the index

* the old index will disappear and IndexSearcher will be the interface
  for searching through the index
This commit is contained in:
Gibheer 2011-05-30 18:36:51 +02:00
parent fcfc48c3ec
commit d5057a5a26
9 changed files with 206 additions and 0 deletions

View File

@ -1,4 +1,7 @@
class Polecat
require 'polecat/index'
require 'polecat/index_writer'
require 'polecat/index_reader'
require 'polecat/index_searcher'
require 'polecat/document'
end

View File

@ -0,0 +1,46 @@
class Polecat
# reads an index directory
#
# This class reads the content of an index directory and builds the
# necessary structures for the index type.
class IndexReader
attr_reader :path
# 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.
# @param [String] path the path to the index directory
def initialize path
@path = path
raise ArgumentError, 'no valid directory' unless File.directory? @path
end
# read the content of the directory
#
# Read all files of the directory and return an index object.
# @raise [IOError] raised when the directory is locked
# @return [Polecat::Index] the index with all documents
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
else
{}
end
end
# checks whether the directory is locked or not
def locked?
if File.exists? @path + '/index.lock'
true
else
false
end
end
end
end

View File

@ -0,0 +1,32 @@
class Polecat
# interface for searching an index
#
# Build on top of an Polecat::IndexReader, this class let's you search through
# all documents stored in an index.
class IndexSearcher
attr_reader :reader
# creates a new Polecat::IndexSearcher
#
# Create a new Polecat::IndexSearcher to search documents. Either a path
# to a directory or a Polecat::IndexReader has to be given, to make this
# searcher work.
# @example
# # the following has the same meaning
# IndexSearcher.new 'index_dir'
# IndexSearcher.new(IndexReader.new 'index_dir')
def initialize *args
if args[0].class == Polecat::IndexReader
@reader = args[0]
elsif args[0].class == String
@reader = Polecat::IndexReader.new args[0]
end
end
# returns the path of the index directory
# @return [String] path of the index directory
def path
@reader.path
end
end
end

View File

@ -79,5 +79,12 @@ class Polecat
return true
end
end
# creates an index reader with the writers path
#
# @returns [Polecat::IndexReader] an IndexReader with the same path
def create_reader
Polecat::IndexReader.new @path
end
end
end

View File

@ -0,0 +1,18 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "IndexReader#locked?" do
before do
@path = prepare_index_dir
end
it "returns false when the directory is not locked" do
r = Polecat::IndexReader.new @path
r.locked?.should == false
end
it "returns true when the directory is locked" do
FileUtils.touch @path + '/index.lock'
r = Polecat::IndexReader.new @path
r.locked?.should == true
end
end

View File

@ -0,0 +1,21 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "IndexReader#new" do
before do
@path = prepare_index_dir
end
it "takes a directory path as an argument" do
r = Polecat::IndexReader.new @path
r.path.should == @path
end
it "raises an error when no path is given" do
lambda { Polecat::IndexReader.new }.should raise_error(ArgumentError)
end
it "raises an error when the path is not a directory" do
lambda { Polecat::IndexReader.new '/dev/null' }.should(
raise_error(ArgumentError))
end
end

View File

@ -0,0 +1,40 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "IndexReader#read" do
before do
@path = prepare_index_dir
end
it "returns a hash with all documents" do
r = Polecat::IndexReader.new @path
r.read.class.should == Hash
end
it "returns an empty hash for a empty directory" do
r = Polecat::IndexReader.new @path
r.read.count.should == 0
end
it "returns the document count found in the index directory" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new(:id => 23)
w.write
r = Polecat::IndexReader.new @path
r.read.count.should == 1
end
it "merges all documents from different files together" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new(:id => 23)
w.write
w.add Spec::FooDocument.new(:id => 24)
w.write
w.create_reader.read.count.should == 2
end
it "raises an error when the directory is locked" do
FileUtils.touch @path + '/index.lock'
r = Polecat::IndexReader.new @path
lambda { r.read }.should raise_error(IOError)
end
end

View File

@ -0,0 +1,17 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "IndexSearcher#new" do
before do
@path = prepare_index_dir
end
it "takes a path as an arugment" do
s = Polecat::IndexSearcher.new @path
s.path.should == @path
end
it "takes an IndexReader as an argument" do
s = Polecat::IndexSearcher.new(Polecat::IndexReader.new @path)
s.path.should == @path
end
end

View File

@ -0,0 +1,22 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "IndexReader#read" do
before do
@path = prepare_index_dir
end
it "returns a IndexReader" do
w = Polecat::IndexWriter.new @path
w.create_reader.class.should == Polecat::IndexReader
end
it "returns a different object everytime it is called" do
w = Polecat::IndexWriter.new @path
w.create_reader.should_not == w.create_reader
end
it "returns an IndexReader with the same path" do
w = Polecat::IndexWriter.new @path
w.create_reader.path.should == w.path
end
end