square/rangeclient

View on GitHub
bin/range_split

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

# given a range and N buckets, slice the range
# into N buckets, attempting even bucket distribution
# based on hostname

require 'rangeclient'

range_arg = ARGV.shift;
num_buckets = ARGV.shift;
num_buckets = num_buckets.to_i

r = Range::Client.new
hosts = r.expand(range_arg)
hosts.sort!

buckets = []

# if we asked for more buckets than exist for hosts
# reduce the number of buckets to be 1:1 with number of hosts
num_buckets = hosts.length if num_buckets > hosts.length

num_buckets.times do |ii|
  buckets << []
end

current_bucket = 0
hosts.each do |host|
  buckets[current_bucket] << host
  current_bucket += 1
  current_bucket = current_bucket % num_buckets
end

buckets.each do |hosts|
  puts r.compress(hosts)
end