Module: SimpleRecord::Sharding::ClassMethods

Included in:
Base
Defined in:
lib/simple_record/sharding.rb

Instance Method Summary collapse

Instance Method Details

#find_sharded(*params) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/simple_record/sharding.rb', line 25

def find_sharded(*params)
  puts 'find_sharded ' + params.inspect

  options = params.size > 1 ? params[1] : {}

  if options[:shard] # User specified shard.
    shard = options[:shard]
    domains = shard.is_a?(Array) ? (shard.collect { |x| prefix_shard_name(x) }) : [prefix_shard_name(shard)]
  else
    domains = sharded_domains
  end
#                puts "sharded_domains=" + domains.inspect

  single = false
  by_ids = false
  case params.first
    when nil then
      raise "Invalid parameters passed to find: nil."
    when :all, :first, :count
      # nada
    else # single id
      by_ids = true
      unless params.first.is_a?(Array)
        single = true
      end
  end
  puts 'single? ' + single.inspect
  puts 'by_ids? ' + by_ids.inspect

  # todo: should have a global executor
  executor = options[:concurrent] ? Concur::Executor.new_multi_threaded_executor : Concur::Executor.new_single_threaded_executor
  results = nil
  if by_ids
    results = []
  else
    results = ShardedResults.new(params)
  end
  futures = []
  domains.each do |d|
    p2 = params.dup
    op2 = options.dup
    op2[:from] = d
    op2[:shard_find] = true
    p2[1] = op2

    futures << executor.execute do
      puts 'executing=' + p2.inspect
      # todo: catch RecordNotFound errors and throw later if there really isn't any record found.
      rs = find(*p2)
      puts 'rs=' + rs.inspect
      rs
    end
  end
  futures.each do |f|
    puts 'getting future ' + f.inspect
    if params.first == :first || single
      puts 'f.get=' + f.get.inspect
      return f.get if f.get
    elsif by_ids
      results << f.get if f.get
    else
      results.add_results f.get
    end
  end
  executor.shutdown
#        puts 'results=' + results.inspect
  if params.first == :first || single
    # Then we found nothing by this point so return nil
    return nil
  elsif params.first == :count
    return results.sum_count
  end
  results

end

#is_sharded?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/simple_record/sharding.rb', line 21

def is_sharded?
  @sharding_options
end

#prefix_shard_name(s) ⇒ Object



105
106
107
# File 'lib/simple_record/sharding.rb', line 105

def prefix_shard_name(s)
  "#{domain}_#{s}"
end

#shard(options = nil) ⇒ Object



13
14
15
# File 'lib/simple_record/sharding.rb', line 13

def shard(options=nil)
  @sharding_options = options
end

#sharded_domainsObject



110
111
112
113
114
115
116
117
# File 'lib/simple_record/sharding.rb', line 110

def sharded_domains
  sharded_domains = []
  shard_names = shards
  shard_names.each do |s|
    sharded_domains << prefix_shard_name(s)
  end
  sharded_domains
end

#sharding_optionsObject



17
18
19
# File 'lib/simple_record/sharding.rb', line 17

def sharding_options
  @sharding_options
end

#shardsObject



101
102
103
# File 'lib/simple_record/sharding.rb', line 101

def shards
  send(sharding_options[:shards])
end