lib/picky/index/base_indexing.rb in picky-2.6.0 vs lib/picky/index/base_indexing.rb in picky-2.7.0

- old
+ new

@@ -1,48 +1,90 @@ module Index # # class Base - + attr_reader :after_indexing, - :bundle_class, - :tokenizer - + :bundle_class + # Delegators for indexing. # - delegate :backup_caches, - :cache, - :check_caches, - :clear_caches, - :create_directory_structure, - :generate_caches, - :restore_caches, + delegate :cache, + :check, + :clear, + :backup, + :restore, :to => :categories - - delegate :connect_backend, - :to => :source - - # Calling index on an index will - # * prepare (the data) - # * cache (the data) + + # Calling index on an index will call index # on every category. # + # Decides whether to use a parallel indexer or whether to + # delegate to each category to index themselves. + # def index - prepare - cache + if source.respond_to?(:each) + check_source_empty + index_in_parallel + else + with_data_snapshot do + categories.each &:index + end + end end + # Check if the given enumerable source is empty. + # + # Note: Checking as early as possible to tell the + # user as early as possible. + # + def check_source_empty + 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? + end + + # Note: Duplicated in category_indexing.rb. + # + # Take a data snapshot if the source offers it. + # + def with_data_snapshot + if source.respond_to? :with_snapshot + source.with_snapshot(self) do + yield + end + else + yield + end + end + + # Indexes the categories in parallel. + # + # Only use where the category does have a #each source defined. + # + def index_in_parallel + indexer = Indexers::Parallel.new self + indexer.index categories + categories.each &:cache + end + # Define an index tokenizer on the index. # # Parameters are the exact same as for indexing. # def indexing options = {} @tokenizer = Tokenizers::Index.new options end alias define_indexing indexing - + + # Returns the installed tokenizer or the default. + # + # TODO Spec. + # + def tokenizer + @tokenizer || Indexes.tokenizer + end + # Define a source on the index. # # Parameter is a source, either one of the standard sources or # anything responding to #each and returning objects that # respond to id and the category names (or the category from option). @@ -66,54 +108,20 @@ end NO_SOURCE ) end - + # Define a key_format on the index. # # Parameter is a method name to use on the key (e.g. :to_i, :to_s, :strip). # def key_format format = nil format ? define_key_format(format) : (@key_format || :to_i) end def define_key_format key_format @key_format = key_format end - - # Decides whether to use a parallel indexer or whether to - # delegate to each category to index themselves. - # - # TODO Rename to prepare. - # - def prepare - # TODO Duplicated in category.rb def indexer. - # - 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 &:prepare - 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 unless source.respond_to? :each - end - end - + end \ No newline at end of file