Sha256: 8f9af24cb2f5da71d396fb2c6ef9848fabab6e9df7f4fc0ecf096f03dda0737e

Contents?: true

Size: 1.28 KB

Versions: 23

Compression:

Stored size: 1.28 KB

Contents

# -*- coding: utf-8 -*-
require 'bsearch'
module ActiveRecord::Turntable::Algorithm
  class RangeBsearchAlgorithm < Base
    def initialize(config)
      @config = config
      @config["shards"].sort_by! {|a| a["less_than"]}
    end

    def calculate(key)
      idx = calculate_idx(key)
      @config["shards"][idx]["connection"]
    rescue
      raise ActiveRecord::Turntable::CannotSpecifyShardError, "cannot specify shard for key:#{key}"
    end

    def calculate_idx(key)
      @config["shards"].bsearch_upper_boundary { |h|
        h["less_than"] <=> key
      }
    end

    # { connection_name => weight, ... }
    def calculate_used_shards_with_weight(sequence_value)
      idx = calculate_idx(sequence_value)
      last_connection = calculate(sequence_value)
      shards = @config["shards"][0..idx]
      weighted_hash = Hash.new {|h,k| h[k]=0}
      prev_max = 0
      shards.each_with_index do |h,idx|
        weighted_hash[h["connection"]] += if idx < shards.size - 1
                                            h["less_than"] - prev_max - 1
                                          else
                                            sequence_value - prev_max
                                          end
        prev_max = h["less_than"] - 1
      end
      return weighted_hash
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
activerecord-turntable-2.5.0 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.4.0 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.3.3 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.3.2 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.3.1 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.3.0 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.2.2 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.2.1 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.2.0 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.1.1 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.1.0 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.1.0.rc2 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.1.0.rc1 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.1.0.beta2 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.1.0.beta1 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.0.6 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.0.5 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.0.4 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.0.3 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb
activerecord-turntable-2.0.2 lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb