Class: DbCharmer::Sharding::Method::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/db_charmer/sharding/method/range.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Range

Returns a new instance of Range.



7
8
9
# File 'lib/db_charmer/sharding/method/range.rb', line 7

def initialize(config)
  @ranges = config[:ranges] ? config[:ranges].clone : raise(ArgumentError, "No :ranges defined!")
end

Instance Attribute Details

#rangesObject

Returns the value of attribute ranges.



5
6
7
# File 'lib/db_charmer/sharding/method/range.rb', line 5

def ranges
  @ranges
end

Instance Method Details

#shard_connectionsObject



27
28
29
# File 'lib/db_charmer/sharding/method/range.rb', line 27

def shard_connections
  ranges.values.uniq
end

#shard_for_key(key) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/db_charmer/sharding/method/range.rb', line 11

def shard_for_key(key)
  return ranges[:default] if key == :default

  ranges.each do |range, shard|
    next if range == :default
    return shard if range.member?(key.to_i)
  end

  return ranges[:default] if ranges[:default]
  raise ArgumentError, "Invalid key value, no shards found for this key!"
end

#support_default_shard?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/db_charmer/sharding/method/range.rb', line 23

def support_default_shard?
  ranges.has_key?(:default)
end