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



75
76
77
78
79
# File 'ext/fast_hash_ring.c', line 75

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'ext/fast_hash_ring.c', line 112

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/fast_hash_ring.c', line 93

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 = NUM2INT(cFastHashRing_bisect(vself, vsorted_keys, vkey));

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

#iterate_nodes(vstring_key) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'ext/fast_hash_ring.c', line 127

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



182
183
184
# File 'ext/fast_hash_ring.c', line 182

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

#sorted_keysObject



178
179
180
# File 'ext/fast_hash_ring.c', line 178

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