Sha256: 8444f977b27d74535ae63d7b3ccb01fe656eceb22879d2b25527bb6bebc9c925

Contents?: true

Size: 1.85 KB

Versions: 11

Compression:

Stored size: 1.85 KB

Contents

module Picky

  module Backends

    class Memory < Backend

      # Returns an object that responds to:
      #   [:token] # => [id, id, id, id, id] (an array of ids)
      #
      def create_inverted bundle
        JSON.new bundle.index_path(:inverted)
      end
      # Returns an object that responds to:
      #   [:token] # => 1.23 (a weight)
      #
      def create_weights bundle
        JSON.new bundle.index_path(:weights)
      end
      # 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 responds to:
      #   [:key] # => value (a value for this config key)
      #
      def create_configuration bundle
        JSON.new bundle.index_path(:configuration)
      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

    end

  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
picky-3.1.11 lib/picky/backends/memory.rb
picky-3.1.10 lib/picky/backends/memory.rb
picky-3.1.9 lib/picky/backends/memory.rb
picky-3.1.8 lib/picky/backends/memory.rb
picky-3.1.7 lib/picky/backends/memory.rb
picky-3.1.6 lib/picky/backends/memory.rb
picky-3.1.5 lib/picky/backends/memory.rb
picky-3.1.4 lib/picky/backends/memory.rb
picky-3.1.3 lib/picky/backends/memory.rb
picky-3.1.2 lib/picky/backends/memory.rb
picky-3.1.1 lib/picky/backends/memory.rb