lib/picky/internals/indexing/index.rb in picky-2.1.2 vs lib/picky/internals/indexing/index.rb in picky-2.2.0
- old
+ new
@@ -4,63 +4,118 @@
module Indexing
class Index
- attr_reader :name, :source, :categories, :after_indexing
+ attr_reader :name, :categories, :after_indexing, :bundle_class, :tokenizer
# Delegators for indexing.
#
delegate :connect_backend,
:to => :source
- delegate :index,
- :cache,
- :generate_caches,
- :backup_caches,
- :restore_caches,
- :check_caches,
- :clear_caches,
- :create_directory_structure,
- :to => :categories
+ each_delegate :backup_caches,
+ :cache!,
+ :check_caches,
+ :clear_caches,
+ :create_directory_structure,
+ :generate_caches,
+ :restore_caches,
+ :to => :categories
- def initialize name, source, options = {}
- @name = name
- @source = source
-
+ def initialize name, options = {}
+ @name = name
+ @source = options[:source]
@after_indexing = options[:after_indexing]
@bundle_class = options[:indexing_bundle_class] # TODO This should actually be a fixed parameter.
+ @tokenizer = options[:tokenizer]
- @categories = Categories.new
+ @categories = []
end
# TODO Spec. Doc.
#
def define_category category_name, options = {}
- options = default_category_options.merge options
-
new_category = Category.new category_name, self, options
+ new_category = yield new_category if block_given?
categories << new_category
new_category
end
- # By default, the category uses
- # * the index's source.
- # * the index's bundle type.
+ # TODO Spec. Doc.
#
- def default_category_options
- {
- :source => @source,
- :indexing_bundle_class => @bundle_class
- }
+ def define_indexing options = {}
+ @tokenizer = Internals::Tokenizers::Index.new options
end
+ #
+ #
+ def define_source source
+ @source = source
+ end
+ def source
+ @source || raise_no_source
+ end
+ def raise_no_source
+ raise NoSourceSpecifiedException.new(<<-NO_SOURCE
+
+
+No source given for index #{name}. An index needs a source.
+Example:
+ Index::Memory.new(:with_source) do
+ source Sources::CSV.new(:title, file: 'data/books.csv')
+ category :title
+ category :author
+ end
+
+ NO_SOURCE
+)
+ end
+
+ #
+ #
+ def find category_name
+ category_name = category_name.to_sym
+
+ categories.each do |category|
+ next unless category.name == category_name
+ return category
+ end
+
+ raise %Q{Index category "#{category_name}" not found. Possible categories: "#{categories.map(&:name).join('", "')}".}
+ end
+
+ # Decides whether to use a parallel indexer or whether to
+ # delegate to each category to index themselves.
+ #
+ def index!
+ if source.respond_to?(:each)
+ warn %Q{\n\033[1mWarning\033[m, source for index "#{name}" is empty: #{source} (responds true to empty?).\n} if source.respond_to?(:empty?) && source.empty?
+ index_parallel
+ else
+ categories.each &:index!
+ end
+ end
+ # Indexes the categories in parallel.
+ #
+ # Only use where the category does not have a non-#each source defined.
+ #
+ def index_parallel
+ indexer = Indexers::Parallel.new self
+ categories.first.prepare_index_directory # TODO Unnice.
+ indexer.index
+ end
+
# Indexing.
#
+ # Note: If it is an each source we do not take a snapshot.
+ #
def take_snapshot
- source.take_snapshot self
+ source.take_snapshot self unless source.respond_to? :each
end
+ #
+ #
def to_s
<<-INDEX
Indexing(#{name}):
#{"source: #{source}".indented_to_s}
#{"Categories:\n#{categories.indented_to_s}".indented_to_s}
\ No newline at end of file