File: continuum_native.rb

package info (click to toggle)
ruby-memcache-client 1.8.5-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 268 kB
  • sloc: ruby: 2,120; makefile: 6
file content (41 lines) | stat: -rw-r--r-- 1,149 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Continuum

  class << self

    # Native extension to perform the binary search within the continuum
    # space.  There's a pure ruby version in memcache.rb so this is purely
    # optional for performance and only necessary if you are using multiple
    # memcached servers.
    begin
      require 'inline'
      inline do |builder|
        builder.c <<-EOM
        int binary_search(VALUE ary, unsigned int r) {
            int upper = RARRAY_LEN(ary) - 1;
            int lower = 0;
            int idx = 0;
            ID value = rb_intern("value");

            while (lower <= upper) {
                idx = (lower + upper) / 2;

                VALUE continuumValue = rb_funcall(RARRAY_PTR(ary)[idx], value, 0);
                unsigned int l = NUM2UINT(continuumValue);
                if (l == r) {
                    return idx;
                }
                else if (l > r) {
                    upper = idx - 1;
                }
                else {
                    lower = idx + 1;
                }
            }
            return upper;
        }
        EOM
      end
    rescue Exception => e
    end
  end
end