Class: Xunch::Sharded

Inherits:
Object
  • Object
show all
Includes:
Murmurhash
Defined in:
lib/xunch/shard/sharded.rb

Overview

maintain a consistency hash ring this ring must be same as other language xunch sharded

Direct Known Subclasses

ShardRedis

Instance Method Summary collapse

Constructor Details

#initialize(regex, shard_infos) ⇒ Sharded

Returns a new instance of Sharded.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/xunch/shard/sharded.rb', line 7

def initialize(regex, shard_infos)
  @regexp = Regexp.new(regex)
  @nodes = RBTree.new
  @resources = Hash.new
  for i in 0 .. shard_infos.length - 1 do
    if (shard_infos[i].name == nil)
      for n in 0 .. 160 * shard_infos[i].weight do
        @nodes.put(Murmurhash.hash2A("SHARD-#{i}-NODE-#{n}"), shard_infos[i]);
      end
    else
      for n in 0 .. 160 * shard_infos[i].weight do
        @nodes.put(Murmurhash.hash2A("#{shard_infos[i].name}-NODE-#{n}"), shard_infos[i]);
      end
    end
    @resources.store(shard_infos[i], shard_infos[i].create_resource);
  end
end

Instance Method Details

#destroyObject



25
26
27
28
29
30
31
# File 'lib/xunch/shard/sharded.rb', line 25

def destroy
  @resources.each { |shard_info, redis_client|
    redis_client.destroy
  }
  @nodes.clear
  @resources.clear
end