consistent_hash 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2bf968f60c0e1ad50c87dd3e34f83a5eba9bc49f
4
+ data.tar.gz: 7d48b9f7701ed9e5d9b16445f673dff011bedd7d
5
+ SHA512:
6
+ metadata.gz: 2d84da0164eb8e40282923c72d6a1780247a2200961d1c8402026f0084ad37129f73649779520c55175fec845fd9eeeea4ae6dc9c6578eb9a23fbb6c1b07ed30
7
+ data.tar.gz: 0b3bd29a6b69dcd407c84898feccbea2b1d41cd5fc5be1ca9c9a7012cffb32e4be6ab235df26c9f80876ee86fee087dde6bd30b27526e3a078a87a240cb3fef9
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in consistent_hash.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ulysse Carion
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # ConsistentHash
2
+
3
+ A simple Ruby library that gives `String` the method `#consistent_hash`, which
4
+ will return consistent values, even between different instances of MRI.
5
+
6
+ This entire gem is basically just a dozen lines of C. The code is written as a C
7
+ extension so that it can perform as well as `String#hash`. (See the section on
8
+ performance.)
9
+
10
+ `String#consistent_hash` and Java's `String#hashCode` methods do exactly the
11
+ same thing, and return the same results. This library is not re-inventing the
12
+ string hash function, it's just implementing an alternative to Ruby's built-in
13
+ `#hash`.
14
+
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'consistent_hash'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ ```sh
27
+ $ bundle
28
+ ```
29
+
30
+ Or install it yourself as:
31
+
32
+ ```sh
33
+ $ gem install consistent_hash
34
+ ```
35
+
36
+ ## Motivation
37
+
38
+ Every time you restart Ruby, the values returned from `String#hash` are changed:
39
+
40
+ ```sh
41
+ $ irb
42
+ 2.0.0p353 :001 > "hello".hash
43
+ => 482951767139383391
44
+ 2.0.0p353 :002 > exit
45
+
46
+ $ irb
47
+ 2.0.0p353 :001 > "hello".hash
48
+ => 3216751850140847920
49
+ 2.0.0p353 :002 > exit
50
+ ```
51
+
52
+ This means you cannot rely on the value of `String#hash` to precalculate some
53
+ data about a string and store the value of `#hash` in a database, because if you
54
+ restart your Ruby program, the values of `String#hash` will be completely
55
+ changed.
56
+
57
+ But with `#consistent_hash`, this is no longer the case:
58
+
59
+ ```sh
60
+ $ irb
61
+ 2.0.0-p353 :001 > require 'consistent_hash'
62
+ => true
63
+ 2.0.0-p353 :002 > "hello".consistent_hash
64
+ => 99162322
65
+ 2.0.0-p353 :003 > exit
66
+
67
+ $ irb
68
+ 2.0.0-p353 :001 > require 'consistent_hash'
69
+ => true
70
+ 2.0.0-p353 :002 > "hello".consistent_hash
71
+ => 99162322
72
+ 2.0.0-p353 :003 > exit
73
+ ```
74
+
75
+ ## Performance
76
+
77
+ If you're concerned about how fast your hash function is, `#consistent_hash`
78
+ will work for you. Because this library is written in C, it's almost as fast as
79
+ the built-in hash function, and it's a few orders of magnitude faster than any
80
+ Ruby code:
81
+
82
+ ```
83
+ user system total real
84
+ naive: 35.620000 0.220000 35.840000 ( 35.841524) # this is written in Ruby
85
+ ruby: 30.460000 0.180000 30.640000 ( 30.644228) # this is written in Ruby
86
+ c: 0.010000 0.000000 0.010000 ( 0.005697) # this is #consistent_hash
87
+ std hash: 0.000000 0.000000 0.000000 ( 0.003163) # this is String#hash
88
+ ```
89
+
90
+ (See [benchmark.rb](benchmark.rb))
91
+
92
+ ## Contributing
93
+
94
+ 1. Fork it
95
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
96
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
97
+ 4. Push to the branch (`git push origin my-new-feature`)
98
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rake/extensiontask'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ Rake::ExtensionTask.new('consistent_hash')
8
+
9
+ task default: %i(compile spec)
data/benchmark.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'benchmark'
2
+
3
+ def random_string
4
+ length = rand(10000).to_i
5
+ length.times.map { (('a'..'z').to_a + ('A'..'Z').to_a).sample }.join
6
+ end
7
+
8
+ strings = 1000.times.map { random_string }
9
+
10
+ def naive_hash(s)
11
+ s.chars.reduce(0) { |acc, char| acc * 31 + char.ord }
12
+ end
13
+
14
+ def ruby_hash(s)
15
+ h = 0
16
+ i = 0
17
+ while i < s.bytesize
18
+ h = h * 31 + s.getbyte(i)
19
+ i += 1
20
+ end
21
+ h
22
+ end
23
+
24
+ require 'consistent_hash'
25
+
26
+ Benchmark.bm(10) do |x|
27
+ x.report("naive:") { strings.each { |s| naive_hash(s) } }
28
+ x.report("ruby:") { strings.each { |s| ruby_hash(s) } }
29
+ x.report("c:") { strings.each { |s| s.consistent_hash } }
30
+ x.report("std hash:") { strings.each { |s| s.hash } }
31
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'consistent_hash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "consistent_hash"
8
+ spec.version = ConsistentHash::VERSION
9
+ spec.authors = ["Ulysse Carion"]
10
+ spec.email = ["ulyssecarion@gmail.com"]
11
+ spec.description = %q{A string hash function that returns the same value each time it's called.}
12
+ spec.summary = %q{A consistent hash function for strings.}
13
+ spec.homepage = "https://github.com/ucarion/consistent_hash"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.14"
24
+ spec.add_development_dependency "rake-compiler"
25
+ end
@@ -0,0 +1,16 @@
1
+ #include "ruby.h"
2
+
3
+ VALUE consistent_hash(VALUE string) {
4
+ uint32_t h = 0;
5
+
6
+ for (int i = 0; i < RSTRING_LEN(string); i++) {
7
+ uint32_t c = RSTRING_PTR(string)[i];
8
+ h = h * 31 + c;
9
+ }
10
+
11
+ return INT2NUM(h);
12
+ }
13
+
14
+ void Init_consistent_hash() {
15
+ rb_define_method(rb_cString, "consistent_hash", consistent_hash, 0);
16
+ }
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile('consistent_hash')
@@ -0,0 +1,6 @@
1
+ require "consistent_hash/version"
2
+ require 'consistent_hash.so'
3
+
4
+ module ConsistentHash
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,3 @@
1
+ module ConsistentHash
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe String do
4
+ it 'creates a String#consistent_hash method' do
5
+ expect("").to respond_to(:consistent_hash)
6
+ end
7
+
8
+ it 'works just like Java String#hashCode' do
9
+ examples = {
10
+ "" => 0,
11
+ "hello world" => 1794106052,
12
+ "this is a test" => -1879005787,
13
+ "a" * 200 => 469437568
14
+ }
15
+
16
+ examples.each { |k, v| expect(k.consistent_hash).to eq v }
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'consistent_hash'
5
+
6
+ RSpec.configure do |config|
7
+ config.color_enabled = true
8
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consistent_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ulysse Carion
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compiler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A string hash function that returns the same value each time it's called.
70
+ email:
71
+ - ulyssecarion@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - benchmark.rb
82
+ - consistent_hash.gemspec
83
+ - ext/consistent_hash/consistent_hash.c
84
+ - ext/consistent_hash/extconf.rb
85
+ - lib/consistent_hash.rb
86
+ - lib/consistent_hash/version.rb
87
+ - spec/consistent_hash_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: https://github.com/ucarion/consistent_hash
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.1.11
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A consistent hash function for strings.
113
+ test_files:
114
+ - spec/consistent_hash_spec.rb
115
+ - spec/spec_helper.rb