fast_hash_ring 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -20,7 +20,7 @@ CLEAN.include('pkg', 'tmp')
20
20
 
21
21
  gemspec = Gem::Specification.new do |s|
22
22
  s.name = 'fast_hash_ring'
23
- s.version = '0.1'
23
+ s.version = '0.1.1'
24
24
  s.authors = [ 'Flinn' ]
25
25
  s.email = 'flinn@actsasflinn.com'
26
26
  s.homepage = 'http://github.com/actsasflinn/fast_hash_ring/'
@@ -51,7 +51,7 @@ Rake::GemPackageTask.new(gemspec) do |pkg|
51
51
  pkg.need_tar = true
52
52
  end
53
53
 
54
- Rake::PackageTask.new('fast_hash_ring', '0.1') do |pkg|
54
+ Rake::PackageTask.new('fast_hash_ring', '0.1.1') do |pkg|
55
55
  pkg.need_zip = true
56
56
  pkg.package_files = FileList[
57
57
  'COPYING',
@@ -1,3 +1,7 @@
1
+ # user system total real
2
+ # Hash Ring 44.970000 0.110000 45.080000 ( 45.194632)
3
+ # Fash Hash Ring 1.820000 0.000000 1.820000 ( 1.824138)
4
+
1
5
  require 'benchmark'
2
6
  require 'rubygems'
3
7
  require 'faker'
@@ -15,7 +19,7 @@ weights = {}
15
19
  end
16
20
 
17
21
  keys = []
18
- 10_000.times do |i|
22
+ 100_000.times do |i|
19
23
  keys << Faker::Name.name
20
24
  end
21
25
 
data/ext/fast_hash_ring.c CHANGED
@@ -18,6 +18,13 @@ unsigned int hash_val(char *key, int x){
18
18
  digest[0 + x]);
19
19
  }
20
20
 
21
+ static VALUE cFastHashRing_hash_val(VALUE vself, VALUE vkey, VALUE vx){
22
+ VALUE vhash;
23
+
24
+ vhash = INT2NUM(hash_val(RSTRING_PTR(vkey), NUM2INT(vx)));
25
+ return vhash;
26
+ }
27
+
21
28
  static VALUE cFastHashRing_generate_circle(VALUE vself){
22
29
  VALUE vnodes, vweights, vring, vsorted_keys;
23
30
  int i, j, n, vnodes_len;
@@ -71,16 +78,16 @@ static VALUE cFastHashRing_gen_key(VALUE vself, VALUE vstring_key){
71
78
  return INT2NUM(key);
72
79
  }
73
80
 
74
- int bisect(VALUE vsorted_keys, VALUE vkey){
81
+ static VALUE cFastHashRing_bisect(VALUE vself, VALUE vsorted_keys, VALUE vkey){
75
82
  int i, vsorted_keys_len;
76
83
 
77
84
  vsorted_keys_len = RARRAY_LEN(vsorted_keys);
78
85
  for(i=0;i<vsorted_keys_len;i++){
79
86
  VALUE vsorted_key = rb_ary_entry(vsorted_keys, i);
80
- if (NUM2LONG(vkey) < NUM2LONG(vsorted_key)) return i;
87
+ if (NUM2LONG(vkey) < NUM2LONG(vsorted_key)) return INT2NUM(i);
81
88
  }
82
89
 
83
- return vsorted_keys_len;
90
+ return INT2NUM(vsorted_keys_len);
84
91
  }
85
92
 
86
93
  static VALUE cFastHashRing_get_node_pos(VALUE vself, VALUE vstring_key){
@@ -94,7 +101,7 @@ static VALUE cFastHashRing_get_node_pos(VALUE vself, VALUE vstring_key){
94
101
  vsorted_keys = rb_iv_get(vself, "@sorted_keys");
95
102
 
96
103
  vkey = cFastHashRing_gen_key(vself, vstring_key);
97
- pos = bisect(vsorted_keys, vkey);
104
+ pos = NUM2INT(cFastHashRing_bisect(vself, vsorted_keys, vkey));
98
105
 
99
106
  if(pos == RARRAY_LEN(vsorted_keys))
100
107
  return INT2NUM(0);
@@ -189,4 +196,8 @@ void Init_fast_hash_ring(){
189
196
  rb_define_method(cFastHashRing, "gen_key", cFastHashRing_gen_key, 1);
190
197
  rb_define_method(cFastHashRing, "sorted_keys", cFastHashRing_sorted_keys, 0);
191
198
  rb_define_method(cFastHashRing, "nodes", cFastHashRing_nodes, 0);
199
+
200
+ // Make specs work
201
+ rb_define_protected_method(cFastHashRing, "hash_val", cFastHashRing_hash_val, 2);
202
+ rb_define_protected_method(cFastHashRing, "bisect", cFastHashRing_bisect, 2);
192
203
  }
@@ -22,15 +22,15 @@ describe FastHashRing do
22
22
  end
23
23
 
24
24
  it "should return 0 if it less than the first element" do
25
- @ring.bisect(@test_array, 5).should eql(0)
25
+ @ring.send(:bisect, @test_array, 5).should eql(0)
26
26
  end
27
27
 
28
28
  it "should return the index it should go into to maintain order" do
29
- @ring.bisect(@test_array, 15).should eql(1)
29
+ @ring.send(:bisect, @test_array, 15).should eql(1)
30
30
  end
31
31
 
32
32
  it "should return the final index if greater than all items" do
33
- @ring.bisect(@test_array, 40).should eql(3)
33
+ @ring.send(:bisect, @test_array, 40).should eql(3)
34
34
  end
35
35
  end
36
36
 
@@ -149,19 +149,10 @@ describe FastHashRing do
149
149
  @ring = FastHashRing.new(['a'])
150
150
  end
151
151
 
152
- it "should return the raw digest for _hash_digest" do
153
- random_string = 'some random string'
154
-
155
- m = Digest::MD5.new
156
- m.update(random_string)
157
-
158
- @ring._hash_digest(random_string).should eql(m.digest)
159
- end
160
-
161
- it "should match the python output for _hash_val" do
152
+ it "should match the python output for hash_val" do
162
153
  # This output was taken directly from the python library
163
154
  py_output = 2830561728
164
- ruby_output = @ring._hash_val(@ring._hash_digest('a')) { |x| x+4 }
155
+ ruby_output = @ring.send(:hash_val, 'a', 4)
165
156
 
166
157
  ruby_output.should eql(py_output)
167
158
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_hash_ring
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flinn
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-15 00:00:00 -05:00
12
+ date: 2009-11-21 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15