lets_shard 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e1242830aacecf1dfdb28c0253b37728055c0cb8b7c622b298e96c5ee743cc4
4
- data.tar.gz: a02dfb193dd088631e62ba56b9e896a254097189630ce0a0f86f7fac0f70a17b
3
+ metadata.gz: d7ad5ad3bc12b55db4816cd5b729704a502f6365ee135f0bc8b789d9e0aed18b
4
+ data.tar.gz: 7d08482f4ee12152db3ae2d09ac33400fd748844a34a44fc8aae088e13d63a41
5
5
  SHA512:
6
- metadata.gz: 8c768ca8d7cddec5be799fff150780fb945b16139344788ce70b933d7573b69fa36d77f1d1354135625ed8db72ec7f55e13d6ed29c97ebe6dc489506e8fce599
7
- data.tar.gz: 1f380c18b3d65cbfd8315272e81fa11a29d96b57ba6ba76f116ebbdc0b5ce9a21d029a65d06e36d1b1f3ed4bfd877811645e13c64013033df8bbd69954078b46
6
+ metadata.gz: 1b15660bfda65b7d4637c7c1a27f4cdf706318a1228e8693c1fc93c770b5ab84728835c86286e8a645be061eea147120482e87ce777ad9b021d60bdf23112242
7
+ data.tar.gz: 214e8d1f9b116f9b0fa7305e398ceb384db071692d6da170242fb2278b4735697075f762f870c6909c9473fcc7bdec30f5851c8b164ab5fb03c6b3c9f4b254d8
data/README.md CHANGED
@@ -65,12 +65,12 @@ redis_shards = LetsShard.new([redis1, redis2])
65
65
  redis_shards.get_object('key').set('key', 'value')
66
66
  redis_shards.get_object('key').get('key')
67
67
 
68
- # redis_shards.get_object('key')... any redis command!
68
+ # redis_shards.get_object('key').any_redis_command
69
69
  ```
70
70
 
71
71
  ### LetsShard let you distribute objects usage better!
72
72
 
73
- If you are use 3 redis instances in 3 servers, but one of these servers has more
73
+ If you are using 3 redis instances in 3 servers, but one of these servers has more
74
74
  powerful specifications than the others and you want to utilize its power.
75
75
 
76
76
  **LetsShard** can solve it for you, easily!
@@ -86,7 +86,7 @@ redis3 = Redis.new(host: "10.0.0.3", port: 6379)
86
86
  redises = [redis1, redis2, redis3]
87
87
  weights = [2, 1, 1] # By default, the weight of each object is 1
88
88
 
89
- redis_shards = LetsShard.new(redises, weights)
89
+ redis_shards = LetsShard.new(redises, weights: weights)
90
90
 
91
91
  # So now LetsShard will give redis1 more slots to utilize it more!
92
92
  ```
@@ -99,3 +99,8 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/oshbou
99
99
  ## Code of Conduct
100
100
 
101
101
  Everyone interacting in the LetsShard project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lets_shard/blob/master/CODE_OF_CONDUCT.md).
102
+
103
+
104
+ ## License
105
+
106
+ LetsShard is released under the [MIT License](https://opensource.org/licenses/MIT).
data/lets_shard.gemspec CHANGED
@@ -5,6 +5,7 @@ Gem::Specification.new do |spec|
5
5
  spec.version = LetsShard::VERSION
6
6
  spec.authors = ['oshboul']
7
7
  spec.email = ['omar.abdula@gmail.com']
8
+ spec.licenses = ['MIT']
8
9
 
9
10
  spec.summary = spec.description = 'An easy way to shard anything!'
10
11
  spec.homepage = 'https://github.com/oshboul/lets_shard'
@@ -1,3 +1,3 @@
1
1
  class LetsShard
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/lets_shard.rb CHANGED
@@ -5,15 +5,17 @@ require 'lets_shard/shard'
5
5
  class LetsShard
6
6
  attr_reader :shards
7
7
 
8
- SLOTS = 16384
8
+ DEFAULT_SLOTS = 16384
9
9
 
10
- def initialize(objects, weights = [])
10
+ def initialize(objects, weights: [])
11
11
  validate_parameters!(objects, weights)
12
12
 
13
13
  @objects = objects
14
14
  @weights = weights.any? ? weights : Array.new(@objects.size, 1)
15
+ @slots = @objects.size < DEFAULT_SLOTS ? DEFAULT_SLOTS : @objects.size
15
16
 
16
17
  set_unit_weight!
18
+ set_remainder_slots!
17
19
  generate_shards!
18
20
  end
19
21
 
@@ -36,7 +38,12 @@ class LetsShard
36
38
  start_slot = 0
37
39
 
38
40
  @objects.each_with_index do |object, index|
39
- end_slot = [start_slot + (@weights[index] * @unit_weight), SLOTS - 1].min
41
+ end_slot = start_slot + (@weights[index] * @unit_weight) - 1
42
+
43
+ if @remainder_slots > 0
44
+ end_slot += 1
45
+ @remainder_slots -= 1
46
+ end
40
47
 
41
48
  @shards << Shard.new(object, start_slot, end_slot)
42
49
 
@@ -45,11 +52,15 @@ class LetsShard
45
52
  end
46
53
 
47
54
  def set_unit_weight!
48
- @unit_weight = (SLOTS.to_f / @weights.inject(0, :+)).ceil
55
+ @unit_weight = @slots / @weights.inject(0, :+)
56
+ end
57
+
58
+ def set_remainder_slots!
59
+ @remainder_slots = @slots - (@unit_weight * @objects.size)
49
60
  end
50
61
 
51
62
  def get_hkey(key)
52
- Digest.crc16(key.to_s) % SLOTS
63
+ Digest.crc16(key.to_s) % @slots
53
64
  end
54
65
 
55
66
  def validate_parameters!(objects, weights)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lets_shard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - oshboul
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-01 00:00:00.000000000 Z
11
+ date: 2020-04-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An easy way to shard anything!
14
14
  email:
@@ -31,7 +31,8 @@ files:
31
31
  - lib/lets_shard/shard.rb
32
32
  - lib/lets_shard/version.rb
33
33
  homepage: https://github.com/oshboul/lets_shard
34
- licenses: []
34
+ licenses:
35
+ - MIT
35
36
  metadata: {}
36
37
  post_install_message:
37
38
  rdoc_options: []