Sha256: 1fe4003a0502ac0ffab51162333ed651e7dc1c781e759b0e9ed081b4721dadc5

Contents?: true

Size: 1.2 KB

Versions: 10

Compression:

Stored size: 1.2 KB

Contents

module ActiveRecord
  # Support parallel execution with each shard and deal with AR connection
  # management in parallel execution.
  module ShardFor
    class AllShardsInParallel
      # @param [Array<Class>] An array of shard model class
      def initialize(shards)
        @shards = shards
      end

      # @yield [Class] A shard model class
      # @return [Array] A result
      # @example
      #   User.all_shards_in_parallel.map(&:count).reduce(&:+)
      def map
        return [] unless block_given?

        commands = @shards.map do |m|
          Expeditor::Command.new { m.connection_pool.with_connection { yield m } }
        end
        commands.each(&:start)
        commands.map(&:get)
      end

      # @yield [Class] A shard model class
      # @return [Array] A result
      # @example
      #   User.all_shards_in_parallel.flat_map {|m| m.where(age: 1) }
      def flat_map(&block)
        map(&block).flatten
      end

      # @yield [Class] A shard model class
      # @return [ActiveRecord::ShardFor::AllShardsInParallel]
      # @example
      #   User.all_shards_in_parallel.each {|m| puts m.count }
      def each(&block)
        map(&block) if block_given?
        self
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
activerecord-shard_for-0.6.0 lib/activerecord/shard_for/all_shards_in_parallel.rb
activerecord-shard_for-0.5.0 lib/activerecord/shard_for/all_shards_in_parallel.rb
activerecord-shard_for-0.4.1 lib/activerecord/shard_for/all_shards_in_parallel.rb
activerecord-shard_for-0.4.0 lib/activerecord/shard_for/all_shards_in_parallel.rb
activerecord-shard_for-0.3.0 lib/activerecord/shard_for/all_shards_in_parallel.rb
activerecord-shard_for-0.2.1 lib/activerecord/shard_for/all_shards_in_parallel.rb
activerecord-shard_for-0.2.0 lib/activerecord/shard_for/all_shards_in_parallel.rb
activerecord-shard_for-0.1.2 lib/activerecord/shard_for/all_shards_in_parallel.rb
activerecord-shard_for-0.1.1 lib/activerecord/shard_for/all_shards_in_parallel.rb
activerecord-shard_for-0.1.0 lib/activerecord/shard_for/all_shards_in_parallel.rb