Class: LetsShard

Inherits:
Object
  • Object
show all
Defined in:
lib/lets_shard.rb,
lib/lets_shard/shard.rb,
lib/lets_shard/digest.rb,
lib/lets_shard/version.rb

Defined Under Namespace

Modules: Digest Classes: LetsShardError, Shard

Constant Summary collapse

DEFAULT_SLOTS =
16384
VERSION =
"0.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects, weights: []) ⇒ LetsShard

Returns a new instance of LetsShard.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lets_shard.rb', line 10

def initialize(objects, weights: [])
  validate_parameters!(objects, weights)

  @objects = objects
  @weights = weights.any? ? weights : Array.new(@objects.size, 1)
  @slots = [DEFAULT_SLOTS, weights.sum].max

  set_unit_weight!
  set_remainder_slots!
  generate_shards!
end

Instance Attribute Details

#shardsObject (readonly)

Returns the value of attribute shards.



6
7
8
# File 'lib/lets_shard.rb', line 6

def shards
  @shards
end

Instance Method Details

#get_object(key) ⇒ Object



30
31
32
# File 'lib/lets_shard.rb', line 30

def get_object(key)
  get_shard(key).object
end

#get_shard(key) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/lets_shard.rb', line 22

def get_shard(key)
  hkey = get_hkey(key)

  @shards.find do |shard|
    (shard.start_slot..shard.end_slot).include?(hkey)
  end
end