Sha256: 9c8ed9552d759c3b92ff0f21d132acafdd28be856e5417e342bb1d689554126b

Contents?: true

Size: 1.88 KB

Versions: 8

Compression:

Stored size: 1.88 KB

Contents

module Picky

  class Bundle

    # Removes the given id from the indexes.
    #
    def remove id
      # Is it anywhere?
      #
      syms = @realtime_mapping[id]
      return unless syms

      syms.each do |sym|
        ids = @inverted[sym]
        ids.delete id

        encoded = self.similarity_strategy.encoded sym

        if ids.empty?
          @inverted.delete   sym
          @weights.delete    sym
          # Since no element uses this sym anymore, we can delete the similarity for it.
          # TODO Not really. Since multiple syms can point to the same encoded.
          @similarity.delete encoded
        else
          @weights[sym] = self.weights_strategy.weight_for ids.size
        end
      end

      @realtime_mapping.delete id
    end

    # Returns a reference to the array where the id has been added.
    #
    def add id, sym
      ary = @inverted[sym]

      syms = @realtime_mapping[id]
      syms = (@realtime_mapping[id] = []) unless syms # TODO Nicefy.

      # Inverted.
      #
      ids = if syms.include? sym
        ids = @inverted[sym]
        ids.delete  id # Move id
        ids.unshift id # to front
      else
        syms << sym
        ids = @inverted[sym] ||= []
        ids.unshift id
      end

      # Weights.
      #
      @weights[sym] = self.weights_strategy.weight_for ids.size

      # Similarity.
      #
      if encoded = self.similarity_strategy.encoded(sym)
        similarity = @similarity[encoded] ||= []
        if similarity.include? sym
          similarity.delete sym  # Not completely correct, as others will also be affected, but meh.
          similarity.unshift sym #
        else
          similarity.unshift sym
        end
      end
    end

    # Partializes the text and then adds each.
    #
    def add_partialized id, text
      self.partial_strategy.each_partial text do |partial_text|
        add id, partial_text
      end
    end

  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
picky-3.4.3 lib/picky/bundle_realtime.rb
picky-3.4.2 lib/picky/bundle_realtime.rb
picky-3.4.1 lib/picky/bundle_realtime.rb
picky-3.4.0 lib/picky/bundle_realtime.rb
picky-3.3.3 lib/picky/bundle_realtime.rb
picky-3.3.2 lib/picky/bundle_realtime.rb
picky-3.3.1 lib/picky/bundle_realtime.rb
picky-3.3.0 lib/picky/bundle_realtime.rb