Class: FastHashRing

Inherits:
Object
  • Object
show all
Defined in:
ext/fast_hash_ring.c

Instance Method Summary collapse

Instance Method Details

#gen_key(vstring_key) ⇒ Object



68
69
70
71
72
# File 'ext/fast_hash_ring.c', line 68

static VALUE cFastHashRing_gen_key(VALUE vself, VALUE vstring_key){
  unsigned int key = hash_val(RSTRING_PTR(vstring_key), 0);

  return INT2NUM(key);
}

#get_node(vstring_key) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'ext/fast_hash_ring.c', line 105

static VALUE cFastHashRing_get_node(VALUE vself, VALUE vstring_key){
  VALUE vpos, vring, vsorted_keys, vkey, vnode;
  vpos = cFastHashRing_get_node_pos(vself, vstring_key);

  if(vpos == Qnil) return Qnil;

  vring = rb_iv_get(vself, "@ring");
  vsorted_keys = rb_iv_get(vself, "@sorted_keys");

  vkey = rb_ary_entry(vsorted_keys, NUM2INT(vpos));
  vnode = rb_hash_aref(vring, vkey);

  return vnode;
}

#get_node_pos(vstring_key) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/fast_hash_ring.c', line 86

static VALUE cFastHashRing_get_node_pos(VALUE vself, VALUE vstring_key){
  VALUE vring, vsorted_keys, vkey;
  int pos;

  vring = rb_iv_get(vself, "@ring");

  if(rb_funcall(vring, rb_intern("empty?"), 0)) return Qnil;

  vsorted_keys = rb_iv_get(vself, "@sorted_keys");

  vkey = cFastHashRing_gen_key(vself, vstring_key);
  pos = bisect(vsorted_keys, vkey);

  if(pos == RARRAY_LEN(vsorted_keys))
    return INT2NUM(0);
  else
    return INT2NUM(pos);
}

#iterate_nodes(vstring_key) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'ext/fast_hash_ring.c', line 120

static VALUE cFastHashRing_iterate_nodes(VALUE vself, VALUE vstring_key){
  VALUE vreturned_values, vpos, vring, vsorted_keys;
  int i;

  vreturned_values = rb_ary_new();
  vpos = cFastHashRing_get_node_pos(vself, vstring_key);

  vring = rb_iv_get(vself, "@ring");
  vsorted_keys = rb_iv_get(vself, "@sorted_keys");

  for(i=0;i<RARRAY_LEN(vsorted_keys);i++){
    VALUE vkey = rb_ary_entry(vsorted_keys, i);
    VALUE vvalue = rb_hash_aref(vring, vkey);
    if(!(rb_ary_includes(vreturned_values, vvalue)))
      rb_ary_push(vreturned_values, vvalue);
  }

  for(i=0;i<RARRAY_LEN(vsorted_keys);i++){
    if(i >= NUM2INT(vpos)) break;

    VALUE vkey = rb_ary_entry(vsorted_keys, i);
    VALUE vvalue = rb_hash_aref(vring, vkey);

    if(rb_ary_includes(vreturned_values, vvalue)) continue;

    rb_ary_push(vreturned_values, vvalue);
  }

  return vreturned_values;
}

#nodesObject



175
176
177
# File 'ext/fast_hash_ring.c', line 175

static VALUE cFastHashRing_nodes(VALUE vself){
  return rb_iv_get(vself, "@nodes");
}

#sorted_keysObject



171
172
173
# File 'ext/fast_hash_ring.c', line 171

static VALUE cFastHashRing_sorted_keys(VALUE vself){
  return rb_iv_get(vself, "@sorted_keys");
}