Skip to content
This repository
tag: v3.4.1
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 175 lines (161 sloc) 4.739 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
module Picky

  # A Bundle is a number of indexes
  # per [index, category] combination.
  #
  # At most, there are three indexes:
  # * *core* index (always used)
  # * *weights* index (always used)
  # * *similarity* index (used with similarity)
  #
  # In Picky, indexing is separated from the index
  # handling itself through a parallel structure.
  #
  # Both use methods provided by this base class, but
  # have very different goals:
  #
  # * *Indexing*::*Bundle* is just concerned with creating index files
  # and providing helper functions to e.g. check the indexes.
  #
  # * *Index*::*Bundle* is concerned with loading these index files into
  # memory and looking up search data as fast as possible.
  #
  # This is the indexing bundle.
  #
  # It does all menial tasks that have nothing to do
  # with the actual index running etc.
  # (Find these in Indexed::Bundle)
  #
  class Bundle

    attr_reader :backend,
                :prepared

    # When indexing, clear only clears the inverted index.
    #
    delegate :clear, :to => :inverted

    # Sets up a piece of the index for the given token.
    #
    def initialize_inverted_index_for token
      self.inverted[token] ||= []
    end

    # Generation
    #

    # This method
    # * Loads the base index from the "prepared..." file.
    # * Generates derived indexes.
    # * Dumps all the indexes into files.
    #
    def generate_caches_from_source
      load_from_prepared_index_file
      generate_caches_from_memory
    end
    # Generates derived indexes from the index and dumps.
    #
    # Note: assumes that there is something in the index
    #
    def generate_caches_from_memory
      cache_from_memory_generation_message
      generate_derived
    end
    def cache_from_memory_generation_message
      timed_exclaim %Q{"#{identifier}": Caching from intermediate in-memory index.}
    end

    # Generates the weights and similarity from the main index.
    #
    def generate_derived
      generate_weights
      generate_similarity
    end

    # "Empties" the index(es) by getting a new empty
    # internal backend instance.
    #
    def empty
      empty_inverted
      empty_configuration
    end
    def empty_inverted
      @inverted = @backend_inverted.empty
    end
    def empty_configuration
      @configuration = @backend_configuration.empty
    end

    # Load the data from the db.
    #
    def load_from_prepared_index_file
      load_from_prepared_index_generation_message
      retrieve
    end
    def load_from_prepared_index_generation_message
      timed_exclaim %Q{"#{identifier}": Loading prepared data into memory.}
    end
    # Retrieves the prepared index data into the index.
    #
    # This is in preparation for generating
    # derived indexes (like weights, similarity)
    # and later dumping the optimized index.
    #
    # TODO Move this out to the category?
    #
    def retrieve
      format = key_format || :to_i
      empty_inverted
      prepared.retrieve do |id, token|
        initialize_inverted_index_for token
        self.inverted[token] << id.send(format)
      end
    end

    # Generate a partial index from the given exact inverted index.
    #
    def generate_partial_from exact_inverted_index
      timed_exclaim %Q{"#{identifier}": Generating partial index for index.}
      self.inverted = exact_inverted_index
      self.generate_partial
      self
    end

    # Generates a new index (writes its index) using the
    # partial caching strategy of this bundle.
    #
    def generate_partial
      self.inverted = partial_strategy.generate_from self.inverted
    end
    # Generates a new weights index (writes its index) using the
    # given weight caching strategy.
    #
    def generate_weights
      self.weights = weights_strategy.generate_from self.inverted
    end
    # Generates a new similarity index (writes its index) using the
    # given similarity caching strategy.
    #
    def generate_similarity
      self.similarity = similarity_strategy.generate_from self.inverted
    end

    # Saves the indexes in a dump file.
    #
    def dump
      timed_exclaim %Q{"#{identifier}": Dumping data.}
      dump_inverted
      dump_similarity
      dump_weights
      dump_configuration
    end
    # Dumps the core index.
    #
    def dump_inverted
      @backend_inverted.dump self.inverted
    end
    # Dumps the weights index.
    #
    def dump_weights
      @backend_weights.dump self.weights
    end
    # Dumps the similarity index.
    #
    def dump_similarity
      @backend_similarity.dump self.similarity
    end
    # Dumps the similarity index.
    #
    def dump_configuration
      @backend_configuration.dump self.configuration
    end

  end

end
Something went wrong with that request. Please try again.