class Sequel::SchemaSharding::Ring

This class is responsible for mapping IDs into shards, using Ketama consistent hashing algorithm, which makes it easier to rehash in the future, should the number of shards increase. For more information see en.wikipedia.org/wiki/Consistent_hashing This implementation is borrowed from Dali memcached library.

Constants

POINTS_PER_SERVER
PRODUCTION_SHARDS

Public Class Methods

new(shards) click to toggle source
# File lib/sequel/schema-sharding/ring.rb, line 15
def initialize(shards)
  @number_of_shards = shards.size
  if ENV['RACK_ENV'] == "production"
    raise "Expecting production shards to be #{PRODUCTION_SHARDS}, got #{@number_of_shards}" \
      if @number_of_shards != PRODUCTION_SHARDS
  end
end

Public Instance Methods

shard_for_id(id) click to toggle source
# File lib/sequel/schema-sharding/ring.rb, line 23
def shard_for_id(id)
  id = id.to_i
  raise "id is passed as zero" if id == 0
  id % @number_of_shards + 1
end