Class: HashRing
- Inherits:
-
Object
- Object
- HashRing
- Defined in:
- lib/hash_ring.rb,
ext/hash_ring/hash_ring.c
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
- #add_node(name) ⇒ Object
- #find_node(key) ⇒ Object
- #initialize(num_replicas, hash_function) ⇒ Object constructor
- #print ⇒ Object
- #print_ring ⇒ Object
- #remove_node(name) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(num_replicas, hash_function) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'ext/hash_ring/hash_ring.c', line 21
static VALUE method_initialize(VALUE self, VALUE num_replicas, VALUE hash_function)
{
Check_Type(num_replicas, T_FIXNUM);
Check_Type(hash_function, T_FIXNUM);
hash_ring_t * ring;
Data_Get_Struct(self, hash_ring_t, ring);
/* If this method is called twice, free the old ring. */
if (ring)
hash_ring_free(ring);
int nr_int = FIX2INT(num_replicas);
int hf_int = FIX2INT(hash_function);
ring = hash_ring_create(nr_int, hf_int);
VALUE ary = rb_ary_new();
rb_iv_set(self, "@num_replicas", num_replicas);
rb_iv_set(self, "@hash_function", hash_function);
rb_iv_set(self, "@nodes", ary);
DATA_PTR(self) = ring;
return self;
}
|
Instance Method Details
#add_node(name) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'ext/hash_ring/hash_ring.c', line 49
static VALUE method_add_node(VALUE self, VALUE name)
{
Check_Type(name, T_STRING);
hash_ring_t *ring;
Data_Get_Struct(self, hash_ring_t, ring);
CHECK_RING(ring);
char * cname = StringValuePtr(name);
if(hash_ring_add_node(ring, cname, strlen(cname)) == 0){
VALUE ary = rb_iv_get(self, "@nodes");
rb_ary_push(ary, name);
rb_iv_set(self, "@nodes", ary);
} else {
return Qnil;
}
return self;
}
|
#find_node(key) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'ext/hash_ring/hash_ring.c', line 102
static VALUE method_find_node(VALUE self, VALUE key){
Check_Type(key, T_STRING);
char * ckey = StringValuePtr(key);
hash_ring_t * ring;
Data_Get_Struct(self, hash_ring_t, ring);
CHECK_RING(ring);
hash_ring_node_t *node = hash_ring_find_node(ring, ckey, strlen(ckey));
VALUE cs;
if(node == NULL){
rb_raise(rb_eRuntimeError, "No nodes found");
} else {
cs = rb_str_new(node->name, node->nameLen);
}
return cs;
}
|
#print ⇒ Object
4 5 6 |
# File 'lib/hash_ring.rb', line 4 def print self.print_ring end |
#print_ring ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'ext/hash_ring/hash_ring.c', line 92
static VALUE method_print_ring(VALUE self)
{
hash_ring_t * ring;
Data_Get_Struct(self, hash_ring_t, ring);
CHECK_RING(ring);
hash_ring_print(ring);
return self;
}
|
#remove_node(name) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'ext/hash_ring/hash_ring.c', line 70
static VALUE method_remove_node(VALUE self, VALUE name)
{
Check_Type(name, T_STRING);
hash_ring_t * ring;
char * cname = StringValuePtr(name);
Data_Get_Struct(self, hash_ring_t, ring);
CHECK_RING(ring);
if(hash_ring_remove_node(ring, cname, strlen(cname)) == 0){
VALUE ary = rb_iv_get(self, "@nodes");
rb_ary_delete(ary, name);
rb_iv_set(self, "@nodes", ary);
} else {
return Qnil;
}
return self;
}
|
#to_s ⇒ Object
8 9 10 |
# File 'lib/hash_ring.rb', line 8 def to_s "<HashRing:#{self.object_id} @num_replicas=#{@num_replicas} @hash_function=#{@hash_function} @nodes=#{@nodes}>" end |