hash_ring 0.0.1 → 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.
@@ -4,6 +4,6 @@ extension_name = 'hash_ring'
4
4
 
5
5
  dir_config(extension_name)
6
6
 
7
- have_library("hashring")
7
+ abort "libhashring is required" unless have_library("hashring")
8
8
 
9
9
  create_makefile("hash_ring/hash_ring")
@@ -1,18 +1,21 @@
1
1
  #include "ruby.h"
2
2
  #include "hash_ring.h"
3
3
 
4
+ #define CHECK_RING(data) \
5
+ if (!data) \
6
+ rb_raise(rb_eRuntimeError, "uninitialized data");
7
+
4
8
  VALUE cHashRing;
5
9
 
6
10
  static VALUE hash_ring_dealloc(void * ring)
7
11
  {
8
- hash_ring_free(ring);
12
+ if (ring)
13
+ hash_ring_free(ring);
9
14
  }
10
15
 
11
16
  static VALUE hash_ring_alloc(VALUE klass)
12
17
  {
13
- hash_ring_t * ring;
14
- VALUE obj = Data_Make_Struct(klass, hash_ring_t, NULL, hash_ring_dealloc, ring);
15
- return obj;
18
+ return Data_Wrap_Struct(klass, NULL, hash_ring_dealloc, NULL);
16
19
  }
17
20
 
18
21
  static VALUE method_initialize(VALUE self, VALUE num_replicas, VALUE hash_function)
@@ -21,18 +24,24 @@ static VALUE method_initialize(VALUE self, VALUE num_replicas, VALUE hash_functi
21
24
  Check_Type(hash_function, T_FIXNUM);
22
25
 
23
26
  hash_ring_t * ring;
24
- hash_ring_t * new_ring;
27
+ Data_Get_Struct(self, hash_ring_t, ring);
28
+
29
+ /* If this method is called twice, free the old ring. */
30
+ if (ring)
31
+ hash_ring_free(ring);
25
32
 
26
33
  int nr_int = FIX2INT(num_replicas);
27
34
  int hf_int = FIX2INT(hash_function);
28
35
 
29
- Data_Get_Struct(self, hash_ring_t, ring);
36
+ ring = hash_ring_create(nr_int, hf_int);
30
37
 
31
- new_ring = hash_ring_create(nr_int, hf_int);
38
+ VALUE ary = rb_ary_new();
32
39
 
33
- MEMCPY(ring, new_ring, hash_ring_t, 1);
40
+ rb_iv_set(self, "@num_replicas", num_replicas);
41
+ rb_iv_set(self, "@hash_function", hash_function);
42
+ rb_iv_set(self, "@nodes", ary);
34
43
 
35
- hash_ring_free(new_ring);
44
+ DATA_PTR(self) = ring;
36
45
 
37
46
  return self;
38
47
  }
@@ -43,9 +52,17 @@ static VALUE method_add_node(VALUE self, VALUE name)
43
52
  hash_ring_t *ring;
44
53
 
45
54
  Data_Get_Struct(self, hash_ring_t, ring);
55
+ CHECK_RING(ring);
46
56
 
47
57
  char * cname = StringValuePtr(name);
48
- hash_ring_add_node(ring, cname, strlen(cname));
58
+
59
+ if(hash_ring_add_node(ring, cname, strlen(cname)) == 0){
60
+ VALUE ary = rb_iv_get(self, "@nodes");
61
+ rb_ary_push(ary, name);
62
+ rb_iv_set(self, "@nodes", ary);
63
+ } else {
64
+ return Qnil;
65
+ }
49
66
 
50
67
  return self;
51
68
  }
@@ -59,8 +76,15 @@ static VALUE method_remove_node(VALUE self, VALUE name)
59
76
  char * cname = StringValuePtr(name);
60
77
 
61
78
  Data_Get_Struct(self, hash_ring_t, ring);
79
+ CHECK_RING(ring);
62
80
 
63
- hash_ring_remove_node(ring, cname, strlen(cname));
81
+ if(hash_ring_remove_node(ring, cname, strlen(cname)) == 0){
82
+ VALUE ary = rb_iv_get(self, "@nodes");
83
+ rb_ary_delete(ary, name);
84
+ rb_iv_set(self, "@nodes", ary);
85
+ } else {
86
+ return Qnil;
87
+ }
64
88
 
65
89
  return self;
66
90
  }
@@ -69,6 +93,7 @@ static VALUE method_print_ring(VALUE self)
69
93
  {
70
94
  hash_ring_t * ring;
71
95
  Data_Get_Struct(self, hash_ring_t, ring);
96
+ CHECK_RING(ring);
72
97
 
73
98
  hash_ring_print(ring);
74
99
  return self;
@@ -81,6 +106,7 @@ static VALUE method_find_node(VALUE self, VALUE key){
81
106
 
82
107
  hash_ring_t * ring;
83
108
  Data_Get_Struct(self, hash_ring_t, ring);
109
+ CHECK_RING(ring);
84
110
 
85
111
  hash_ring_node_t *node = hash_ring_find_node(ring, ckey, strlen(ckey));
86
112
 
@@ -101,9 +127,9 @@ void Init_hash_ring()
101
127
 
102
128
  rb_define_alloc_func(cHashRing, hash_ring_alloc);
103
129
 
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);
130
+ rb_define_method(cHashRing, "initialize", method_initialize, 2);
131
+ rb_define_method(cHashRing, "add_node", method_add_node, 1);
132
+ rb_define_method(cHashRing, "remove_node", method_remove_node, 1);
133
+ rb_define_method(cHashRing, "find_node", method_find_node, 1);
134
+ rb_define_method(cHashRing, "print_ring", method_print_ring, 0);
109
135
  }
data/lib/hash_ring.rb CHANGED
@@ -1,37 +1,13 @@
1
- require 'hash_ring/hash_ring'
2
-
3
1
  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
2
+ VERSION = "0.0.2"
29
3
 
30
4
  def print
31
- self.printRing
5
+ self.print_ring
32
6
  end
33
7
 
34
8
  def to_s
35
9
  "<HashRing:#{self.object_id} @num_replicas=#{@num_replicas} @hash_function=#{@hash_function} @nodes=#{@nodes}>"
36
10
  end
37
11
  end
12
+
13
+ require 'hash_ring/hash_ring'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_ring
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-12 00:00:00.000000000Z
12
+ date: 2012-02-13 00:00:00.000000000Z
13
13
  dependencies: []
14
- description: Wraps https://github.com/mikeyk/hash-ring
14
+ description: Wraps https://github.com/chrismoos/hash-ring
15
15
  email: michaelrbernstein@gmail.com
16
16
  executables: []
17
17
  extensions: