delete the old index, because it's obselete
This commit is contained in:
parent
aaff085b47
commit
80151adab2
|
@ -1,5 +1,4 @@
|
|||
class Polecat
|
||||
require 'polecat/index'
|
||||
require 'polecat/index_writer'
|
||||
require 'polecat/index_reader'
|
||||
require 'polecat/index_searcher'
|
||||
|
|
|
@ -1,68 +0,0 @@
|
|||
class Polecat
|
||||
# storage of documents
|
||||
#
|
||||
# This is the core of the search platform, the index. It stores the documents,
|
||||
# stores and reads them and make them searchable.
|
||||
class Index
|
||||
attr_reader :path
|
||||
|
||||
# initialises an index object on the given path
|
||||
#
|
||||
# This method initialises an index on the path. The Path has to be a
|
||||
# directory.
|
||||
def initialize path
|
||||
if File.directory? path
|
||||
@path = path
|
||||
@buffer = []
|
||||
@documents = []
|
||||
else
|
||||
raise ArgumentError, "Argument no valid directory"
|
||||
end
|
||||
end
|
||||
|
||||
# returns the document count currently loaded
|
||||
def count
|
||||
@documents.count
|
||||
end
|
||||
|
||||
# returns true, if it has an index
|
||||
def index_dir?
|
||||
File.exists?(@path + '/index.txt')
|
||||
end
|
||||
|
||||
def write doc
|
||||
if (doc.kind_of? Document)
|
||||
@buffer << doc
|
||||
else
|
||||
raise ArgumentError, "not a document"
|
||||
end
|
||||
end
|
||||
|
||||
# read all stored documents from the index files into the index
|
||||
def read
|
||||
index_file = @path + '/index.txt'
|
||||
if (File.exists? index_file)
|
||||
@documents = Marshal.load(File.read(index_file))
|
||||
end
|
||||
end
|
||||
|
||||
def flush
|
||||
@documents += @buffer
|
||||
File.open @path + '/index.txt', 'w' do |file|
|
||||
file.write Marshal.dump(@documents)
|
||||
end
|
||||
end
|
||||
|
||||
def search term
|
||||
matches = []
|
||||
linenr = 0
|
||||
@documents.each do |doc|
|
||||
doc.attributes.each do |key, value|
|
||||
matches << linenr if value[:value] =~ /#{term}/
|
||||
end
|
||||
linenr += 1
|
||||
end
|
||||
matches
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,25 +0,0 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||
|
||||
describe "Index#count" do
|
||||
before :all do
|
||||
@path = prepare_index_dir
|
||||
end
|
||||
|
||||
it "returns 0 on a fresh index" do
|
||||
i = Polecat::Index.new @path
|
||||
i.count.should == 0
|
||||
end
|
||||
|
||||
it "returns 1 when 1 entry is in the index" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write Spec::FooDocument.new(:id => 1)
|
||||
i.flush
|
||||
i.count.should == 1
|
||||
end
|
||||
|
||||
it "does not count entries, which got not flushed" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write Spec::FooDocument.new(:id => 1)
|
||||
i.count.should == 0
|
||||
end
|
||||
end
|
|
@ -1,23 +0,0 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||
|
||||
describe "Document#flush" do
|
||||
before do
|
||||
@path = prepare_index_dir
|
||||
@file = @path + '/index.txt'
|
||||
@doc = Spec::FooDocument.new(:id => 1)
|
||||
end
|
||||
|
||||
it "does not write anything to the file, until the #flush was called" do
|
||||
i = Polecat::Index.new @path
|
||||
i.flush
|
||||
i.write @doc
|
||||
File.read(@file).should == Marshal.dump([])
|
||||
end
|
||||
|
||||
it "writes the content in the buffer to the file" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write @doc
|
||||
i.flush
|
||||
File.read(@file).should == Marshal.dump([@doc])
|
||||
end
|
||||
end
|
|
@ -1,19 +0,0 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||
|
||||
describe "Document#index_dir?" do
|
||||
before :all do
|
||||
@path = prepare_index_dir
|
||||
end
|
||||
|
||||
it "returns false, if the directory does not contain an index" do
|
||||
i = Polecat::Index.new @path
|
||||
i.index_dir?.should == false
|
||||
end
|
||||
|
||||
it "returns true, if the directory contains an index" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write Spec::FooDocument.new(:id => 1)
|
||||
i.flush
|
||||
i.index_dir?.should == true
|
||||
end
|
||||
end
|
|
@ -1,22 +0,0 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||
|
||||
describe "Index#new" do
|
||||
before :all do
|
||||
@path = prepare_index_dir
|
||||
end
|
||||
|
||||
it "takes a path as an argument" do
|
||||
i = Polecat::Index.new @path
|
||||
i.path.should == @path
|
||||
end
|
||||
|
||||
it "raises an ArgumentError, when no path is given" do
|
||||
lambda { Polecat::Index.new }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "raises an ArgumentError, when the path is no directory" do
|
||||
lambda {
|
||||
Polecat::Index.new "/dev/null"
|
||||
}.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
|
@ -1,22 +0,0 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||
|
||||
describe "Index#read" do
|
||||
before :all do
|
||||
@path = prepare_index_dir
|
||||
end
|
||||
|
||||
it "does not read any entries, when no files are in the directory" do
|
||||
i = Polecat::Index.new @path
|
||||
i.read
|
||||
i.count.should == 0
|
||||
end
|
||||
|
||||
it "loads all documents in the index directory" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write Spec::FooDocument.new(:id => 1)
|
||||
i.flush
|
||||
i = Polecat::Index.new @path
|
||||
i.read
|
||||
i.count.should == 1
|
||||
end
|
||||
end
|
|
@ -1,38 +0,0 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||
|
||||
describe "Index#search" do
|
||||
before :all do
|
||||
@doc1 = Spec::FooDocument.new :id => 1, :description => 'foo'
|
||||
@doc2 = Spec::FooDocument.new :id => 2, :description => 'bar'
|
||||
@doc3 = Spec::FooDocument.new :id => 3, :description => 'foo bar baz'
|
||||
end
|
||||
|
||||
before do
|
||||
@path = prepare_index_dir
|
||||
@file = @path + '/index.txt'
|
||||
end
|
||||
|
||||
it "returns an array of lines, where the match was found" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write @doc1
|
||||
i.write @doc2
|
||||
i.write @doc1.clone
|
||||
i.flush
|
||||
i.search("foo").should == [0, 2]
|
||||
end
|
||||
|
||||
it "returns an array of lines, where the match is somewhere in it" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write @doc3
|
||||
i.flush
|
||||
i.search("baz").should == [0]
|
||||
end
|
||||
|
||||
it "returns an empty array, when no match was found" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write @doc1
|
||||
i.write @doc2
|
||||
i.flush
|
||||
i.search("baz").should == []
|
||||
end
|
||||
end
|
|
@ -1,33 +0,0 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||
|
||||
describe "Document#write" do
|
||||
before :all do
|
||||
@doc1 = Spec::FooDocument.new(:id => 1)
|
||||
@doc2 = Spec::FooDocument.new(:id => 2)
|
||||
end
|
||||
|
||||
before do
|
||||
@path = prepare_index_dir
|
||||
@file = @path + '/index.txt'
|
||||
end
|
||||
|
||||
it "writes a document to a file" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write @doc1
|
||||
i.flush
|
||||
File.read(@file).should == Marshal.dump([@doc1])
|
||||
end
|
||||
|
||||
it "appends new entries to the end of the file" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write @doc1
|
||||
i.write @doc2
|
||||
i.flush
|
||||
File.read(@file).should == Marshal.dump([@doc1, @doc2])
|
||||
end
|
||||
|
||||
it "raises an error, if the object is not a Polecat::Document" do
|
||||
lambda { Polecat::Index.new(@path).write "foo" }.should(
|
||||
raise_error(ArgumentError))
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue