the index is now using the marshal format for reading and writing the
data
This commit is contained in:
parent
f0ea1944da
commit
720228945f
16
Gemfile
16
Gemfile
|
@ -1,14 +1,10 @@
|
|||
source "http://rubygems.org"
|
||||
# Add dependencies required to use your gem here.
|
||||
# Example:
|
||||
# gem "activesupport", ">= 2.3.5"
|
||||
|
||||
# Add dependencies to develop your gem here.
|
||||
# Include everything needed to run rake, tests, features, etc.
|
||||
group :development do
|
||||
gem "rspec", "~> 2.3.0"
|
||||
gem "yard", "~> 0.6.0"
|
||||
gem "bundler", "~> 1.0.0"
|
||||
gem "jeweler", "~> 1.6.0"
|
||||
gem "reek", "~> 1.2.8"
|
||||
gem "rspec"
|
||||
gem "rdoc"
|
||||
gem "yard"
|
||||
gem "bundler"
|
||||
gem "jeweler"
|
||||
gem "reek"
|
||||
end
|
||||
|
|
|
@ -9,38 +9,49 @@ class Polecat
|
|||
def initialize path
|
||||
if File.directory? path
|
||||
@path = path
|
||||
@buffer = ""
|
||||
@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 term
|
||||
@buffer += "#{term}\n"
|
||||
@buffer << term
|
||||
end
|
||||
|
||||
# read all stored documents from the index files into the index
|
||||
def read
|
||||
if (File.exists?(@path + '/index.txt'))
|
||||
@documents = Marshal.load(File.read(@path+'/index.txt'))
|
||||
end
|
||||
end
|
||||
|
||||
def flush
|
||||
File.open @path + '/index.txt', 'a' do |f|
|
||||
f.write "#{@buffer}"
|
||||
f.flush
|
||||
@documents += @buffer
|
||||
File.open @path + '/index.txt', 'w' do |f|
|
||||
f.write Marshal.dump(@documents)
|
||||
end
|
||||
end
|
||||
|
||||
def search term
|
||||
File.open @path + '/index.txt' do |f|
|
||||
linenr = 0
|
||||
matches = []
|
||||
while (line = f.gets) do
|
||||
matches << linenr if line =~ /#{term}/
|
||||
linenr += 1
|
||||
end
|
||||
matches
|
||||
matches = []
|
||||
linenr = 0
|
||||
@documents.each do |line|
|
||||
matches << linenr if line =~ /#{term}/
|
||||
linenr += 1
|
||||
end
|
||||
matches
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||
|
||||
describe "Document" do
|
||||
describe "#initialize" do
|
||||
it "creates a new Document object" do
|
||||
d = Spec::FooDocument.new
|
||||
d.is_a?(Polecat::Document).should == true
|
||||
end
|
||||
|
||||
it "takes a hash as argument" do
|
||||
d = Spec::FooDocument.new(
|
||||
:id => 1,
|
||||
:name => 'foo',
|
||||
:lastname => 'bar',
|
||||
:description => 'this is a test'
|
||||
)
|
||||
d.id.should == 1
|
||||
d.name.should == 'foo'
|
||||
d.lastname.should == 'bar'
|
||||
d.description.should == 'this is a test'
|
||||
end
|
||||
|
||||
it "sets the attributes to nil as default" do
|
||||
d = Spec::FooDocument.new
|
||||
d.id.should == nil
|
||||
end
|
||||
|
||||
it "creates a method to get an attribute value" do
|
||||
class FooDoc
|
||||
include Polecat::Document
|
||||
field :doc
|
||||
end
|
||||
FooDoc.new.respond_to?(:doc).should == true
|
||||
end
|
||||
|
||||
it "creates a method to set an attribute" do
|
||||
class FooDoc
|
||||
include Polecat::Document
|
||||
field :doc
|
||||
end
|
||||
d = FooDoc.new
|
||||
d.doc = 'foo'
|
||||
d.doc.should == 'foo'
|
||||
end
|
||||
|
||||
it "raises an error, when the key is not found" do
|
||||
lambda { Spec::FooDocument.new(:foo => :bar) }.should(
|
||||
raise_error(ArgumentError))
|
||||
end
|
||||
end
|
||||
|
||||
describe "#attributes" do
|
||||
it "returns a hash with all attributes" do
|
||||
class FooDoc
|
||||
include Polecat::Document
|
||||
|
||||
field :bar
|
||||
end
|
||||
d = FooDoc.new
|
||||
d.attributes.should == {:bar => Polecat::Document::OPTIONS}
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,25 @@
|
|||
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 "foo"
|
||||
i.flush
|
||||
i.count.should == 1
|
||||
end
|
||||
|
||||
it "does not count entries, which got not flushed" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write "foo"
|
||||
i.count.should == 0
|
||||
end
|
||||
end
|
|
@ -0,0 +1,22 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||
|
||||
describe "Document#flush" do
|
||||
before do
|
||||
@path = prepare_index_dir
|
||||
@file = @path + '/index.txt'
|
||||
end
|
||||
|
||||
it "does not write anything to the file, until the #flush was called" do
|
||||
i = Polecat::Index.new @path
|
||||
i.flush
|
||||
i.write "foo"
|
||||
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 "foo"
|
||||
i.flush
|
||||
File.read(@file).should == Marshal.dump(["foo"])
|
||||
end
|
||||
end
|
|
@ -0,0 +1,19 @@
|
|||
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 'foo'
|
||||
i.flush
|
||||
i.index_dir?.should == true
|
||||
end
|
||||
end
|
|
@ -0,0 +1,22 @@
|
|||
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
|
|
@ -0,0 +1,22 @@
|
|||
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 "foo"
|
||||
i.flush
|
||||
i = Polecat::Index.new @path
|
||||
i.read
|
||||
i.count.should == 1
|
||||
end
|
||||
end
|
|
@ -0,0 +1,32 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||
|
||||
describe "Index#search" do
|
||||
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 "foo"
|
||||
i.write "bar"
|
||||
i.write "foo"
|
||||
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 "foo bar baz"
|
||||
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 "foo"
|
||||
i.write "bar"
|
||||
i.flush
|
||||
i.search("baz").should == []
|
||||
end
|
||||
end
|
|
@ -0,0 +1,23 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||
|
||||
describe "Document#write" do
|
||||
before do
|
||||
@path = prepare_index_dir
|
||||
@file = @path + '/index.txt'
|
||||
end
|
||||
|
||||
it "writes a string to a file" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write "foobar"
|
||||
i.flush
|
||||
File.read(@file).should == Marshal.dump(["foobar"])
|
||||
end
|
||||
|
||||
it "appends new entries to the end of the file" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write "foo"
|
||||
i.write "bar"
|
||||
i.flush
|
||||
File.read(@file).should == Marshal.dump(["foo", "bar"])
|
||||
end
|
||||
end
|
|
@ -1,113 +0,0 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||
|
||||
describe "Index" do
|
||||
before :all do
|
||||
@path = prepare_index_dir
|
||||
end
|
||||
|
||||
describe "#initialize" do
|
||||
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
|
||||
|
||||
describe "#index_dir?" do
|
||||
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 'foo'
|
||||
i.flush
|
||||
i.index_dir?.should == true
|
||||
end
|
||||
end
|
||||
|
||||
describe "#write" do
|
||||
before do
|
||||
@path = prepare_index_dir
|
||||
@file = @path + '/index.txt'
|
||||
end
|
||||
|
||||
it "writes a string to a file" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write "foobar"
|
||||
i.flush
|
||||
File.read(@file).should == "foobar\n"
|
||||
end
|
||||
|
||||
it "appends new entries to the end of the file" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write "foo"
|
||||
i.write "bar"
|
||||
i.flush
|
||||
File.read(@file).should == "foo\nbar\n"
|
||||
end
|
||||
end
|
||||
|
||||
describe "#flush" do
|
||||
before do
|
||||
@path = prepare_index_dir
|
||||
@file = @path + '/index.txt'
|
||||
end
|
||||
|
||||
it "does not write anything to the file, until the #flush was called" do
|
||||
i = Polecat::Index.new @path
|
||||
i.flush
|
||||
i.write "foo"
|
||||
File.read(@file).should == ""
|
||||
end
|
||||
|
||||
it "writes the content in the buffer to the file" do
|
||||
i = Polecat::Index.new @path
|
||||
i.write "foo"
|
||||
i.flush
|
||||
File.read(@file).should == "foo\n"
|
||||
end
|
||||
end
|
||||
|
||||
describe "#search" do
|
||||
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 "foo"
|
||||
i.write "bar"
|
||||
i.write "foo"
|
||||
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 "foo bar baz"
|
||||
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 "foo"
|
||||
i.write "bar"
|
||||
i.flush
|
||||
i.search("baz").should == []
|
||||
end
|
||||
end
|
||||
end
|
|
@ -27,7 +27,7 @@ end
|
|||
|
||||
module Spec
|
||||
class FooDocument
|
||||
include Polecat::Document
|
||||
include Document
|
||||
|
||||
field :id
|
||||
field :name, :lazy => true
|
||||
|
|
Loading…
Reference in New Issue