From 402ef4e491b3a956774436cf3ba1fe9185122d23 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Thu, 26 May 2011 21:42:18 +0200 Subject: [PATCH] * small doc change in document.rb * added IndexWriter to let the checks pass --- lib/polecat/document.rb | 2 +- lib/polecat/index_writer.rb | 89 +++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 lib/polecat/index_writer.rb diff --git a/lib/polecat/document.rb b/lib/polecat/document.rb index ffc62eb..54ade2b 100644 --- a/lib/polecat/document.rb +++ b/lib/polecat/document.rb @@ -8,7 +8,7 @@ class Polecat :value => nil } - def self.included klass # :nodoc: + def self.included klass klass.extend(DocumentResource) klass.instance_variable_set :@attributes, {} end diff --git a/lib/polecat/index_writer.rb b/lib/polecat/index_writer.rb new file mode 100644 index 0000000..b4c6273 --- /dev/null +++ b/lib/polecat/index_writer.rb @@ -0,0 +1,89 @@ +class Polecat + # 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. + class IndexWriter + + # create a new IndexWriter + # + # This creates a new IndexWriter set to the given path. + # @param [String] path the path to the index directory + 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 + + # returns the path of the IndexWriter + # + # @return [String] the path of the IndexWriter + def path + @path + end + + # 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 + def count + @documents.count + end + + # 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 + def add doc + if doc.kind_of? Polecat::Document + @documents << doc + else + raise ArgumentError, 'not a document' + end + end + + # 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 + def write + lock_path = @path + '/index.lock' + if File.exists?(lock_path) + return false + else + FileUtils.touch(lock_path) + 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.open file_name, 'w' do |file| + file.write Marshal.dump(@documents) + end + + @documents = [] + FileUtils.rm lock_path + return true + end + end + end +end