hash_ring 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ require 'mkmf'
2
+
3
+ extension_name = 'hash_ring'
4
+
5
+ dir_config(extension_name)
6
+
7
+ have_library("hashring")
8
+
9
+ create_makefile("hash_ring/hash_ring")
@@ -0,0 +1,109 @@
1
+ #include "ruby.h"
2
+ #include "hash_ring.h"
3
+
4
+ VALUE cHashRing;
5
+
6
+ static VALUE hash_ring_dealloc(void * ring)
7
+ {
8
+ hash_ring_free(ring);
9
+ }
10
+
11
+ static VALUE hash_ring_alloc(VALUE klass)
12
+ {
13
+ hash_ring_t * ring;
14
+ VALUE obj = Data_Make_Struct(klass, hash_ring_t, NULL, hash_ring_dealloc, ring);
15
+ return obj;
16
+ }
17
+
18
+ static VALUE method_initialize(VALUE self, VALUE num_replicas, VALUE hash_function)
19
+ {
20
+ Check_Type(num_replicas, T_FIXNUM);
21
+ Check_Type(hash_function, T_FIXNUM);
22
+
23
+ hash_ring_t * ring;
24
+ hash_ring_t * new_ring;
25
+
26
+ int nr_int = FIX2INT(num_replicas);
27
+ int hf_int = FIX2INT(hash_function);
28
+
29
+ Data_Get_Struct(self, hash_ring_t, ring);
30
+
31
+ new_ring = hash_ring_create(nr_int, hf_int);
32
+
33
+ MEMCPY(ring, new_ring, hash_ring_t, 1);
34
+
35
+ hash_ring_free(new_ring);
36
+
37
+ return self;
38
+ }
39
+
40
+ static VALUE method_add_node(VALUE self, VALUE name)
41
+ {
42
+ Check_Type(name, T_STRING);
43
+ hash_ring_t *ring;
44
+
45
+ Data_Get_Struct(self, hash_ring_t, ring);
46
+
47
+ char * cname = StringValuePtr(name);
48
+ hash_ring_add_node(ring, cname, strlen(cname));
49
+
50
+ return self;
51
+ }
52
+
53
+ static VALUE method_remove_node(VALUE self, VALUE name)
54
+ {
55
+ Check_Type(name, T_STRING);
56
+
57
+ hash_ring_t * ring;
58
+
59
+ char * cname = StringValuePtr(name);
60
+
61
+ Data_Get_Struct(self, hash_ring_t, ring);
62
+
63
+ hash_ring_remove_node(ring, cname, strlen(cname));
64
+
65
+ return self;
66
+ }
67
+
68
+ static VALUE method_print_ring(VALUE self)
69
+ {
70
+ hash_ring_t * ring;
71
+ Data_Get_Struct(self, hash_ring_t, ring);
72
+
73
+ hash_ring_print(ring);
74
+ return self;
75
+ }
76
+
77
+ static VALUE method_find_node(VALUE self, VALUE key){
78
+ Check_Type(key, T_STRING);
79
+
80
+ char * ckey = StringValuePtr(key);
81
+
82
+ hash_ring_t * ring;
83
+ Data_Get_Struct(self, hash_ring_t, ring);
84
+
85
+ hash_ring_node_t *node = hash_ring_find_node(ring, ckey, strlen(ckey));
86
+
87
+ VALUE cs;
88
+
89
+ if(node == NULL){
90
+ rb_raise(rb_eRuntimeError, "No nodes found");
91
+ } else {
92
+ cs = rb_str_new(node->name, node->nameLen);
93
+ }
94
+
95
+ return cs;
96
+ }
97
+
98
+ void Init_hash_ring()
99
+ {
100
+ cHashRing = rb_define_class("HashRing", rb_cObject);
101
+
102
+ rb_define_alloc_func(cHashRing, hash_ring_alloc);
103
+
104
+ rb_define_method(cHashRing, "init", method_initialize, 2);
105
+ rb_define_method(cHashRing, "addNode", method_add_node, 1);
106
+ rb_define_method(cHashRing, "removeNode", method_remove_node, 1);
107
+ rb_define_method(cHashRing, "findNode", method_find_node, 1);
108
+ rb_define_method(cHashRing, "printRing", method_print_ring, 0);
109
+ }
data/lib/hash_ring.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'hash_ring/hash_ring'
2
+
3
+ class HashRing
4
+ VERSION = "0.0.1"
5
+
6
+ attr_accessor :nodes, :num_replicas, :hash_function
7
+
8
+ def initialize(num_replicas, hash_function)
9
+ @nodes = []
10
+ @num_replicas = num_replicas
11
+ @hash_function = hash_function
12
+ init(num_replicas, hash_function)
13
+ end
14
+
15
+ def add_node(name)
16
+ @nodes << name
17
+ @nodes.uniq!
18
+ self.addNode(name)
19
+ end
20
+
21
+ def remove_node(name)
22
+ @nodes.reject!{|n| n == name}
23
+ self.removeNode(name)
24
+ end
25
+
26
+ def find_node(name)
27
+ self.findNode(name)
28
+ end
29
+
30
+ def print
31
+ self.printRing
32
+ end
33
+
34
+ def to_s
35
+ "<HashRing:#{self.object_id} @num_replicas=#{@num_replicas} @hash_function=#{@hash_function} @nodes=#{@nodes}>"
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_ring
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael R. Bernstein
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-12 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Wraps https://github.com/mikeyk/hash-ring
15
+ email: michaelrbernstein@gmail.com
16
+ executables: []
17
+ extensions:
18
+ - ext/hash_ring/extconf.rb
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/hash_ring.rb
22
+ - ext/hash_ring/hash_ring.c
23
+ - ext/hash_ring/extconf.rb
24
+ homepage: http://github.com/mrb/hash_ring
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.10
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: A wrapper for a fast C hash ring library
48
+ test_files: []