Skip to content
This repository
tag: v4.6.4
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 96 lines (85 sloc) 2.709 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
module Picky

  module Backends

    #
    #
    class Backend
      
      # This is the default behaviour and should be overridden
      # for different backends.
      #
      
      # Returns an object that on #initial, #load returns
      # an object that responds to:
      # [:token] # => [id, id, id, id, id] (an array of ids)
      #
      def create_inverted bundle
        json bundle.index_path(:inverted)
      end
      # Returns an object that on #initial, #load returns
      # an object that responds to:
      # [:token] # => 1.23 (a weight)
      #
      def create_weights bundle
        json bundle.index_path(:weights)
      end
      # # Returns an object that on #initial, #load returns
      # # an object that responds to:
      # # [:encoded] # => [:original, :original] (an array of original symbols this similarity encoded thing maps to)
      # #
      # def create_similarity bundle
      # Marshal.new bundle.index_path(:similarity)
      # end
      # Returns an object that on #initial, #load returns
      # an object that responds to:
      # [:key] # => value (a value for this config key)
      #
      def create_configuration bundle
        json bundle.index_path(:configuration)
      end
      # Returns an object that on #initial, #load returns
      # an object that responds to:
      # [id] # => [:sym1, :sym2]
      #
      def create_realtime bundle
        json bundle.index_path(:realtime)
      end

      # Returns the total score of the combinations.
      #
      # Default implementation. Override to speed up.
      #
      def weight combinations
        combinations.score
      end

      # Returns the result ids for the allocation.
      #
      # Sorts the ids by size and & through them in the following order (sizes):
      # 0. [100_000, 400, 30, 2]
      # 1. [2, 30, 400, 100_000]
      # 2. (100_000 & (400 & (30 & 2))) # => result
      #
      # Note: Uses a C-optimized intersection routine (in performant.c)
      # for speed and memory efficiency.
      #
      # Note: In the memory based version we ignore the amount and
      # offset hints.
      # We cannot use the information to speed up the algorithm,
      # unfortunately.
      #
      def ids combinations, _, _
        # Get the ids for each combination.
        #
        id_arrays = combinations.inject([]) do |total, combination|
          total << combination.ids
        end

        # Call the optimized C algorithm.
        #
        # Note: It orders the passed arrays by size.
        #
        Performant::Array.memory_efficient_intersect id_arrays
      end

      #
      #
      def to_s
        self.class.name
      end

    end

  end

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