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



23
24
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
# File 'lib/simple_record/sharding.rb', line 23

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
    case params.first
        when nil then
            raise "Invalid parameters passed to find: nil."
        when :all, :first, :count
            # nada
        else # single id
            unless params.first.is_a?(Array)
                single = true
            end
    end

    results = ShardedResults.new(params)
    domains.each do |d|
        p2               = params.dup
        op2              = options.dup
        op2[:from]       = d
        op2[:shard_find] = true
        p2[1]            = op2
        rs               = find(*p2)
        if params.first == :first || single
            return rs if rs
        else
            results.add_results rs
        end
    end
    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)


19
20
21
# File 'lib/simple_record/sharding.rb', line 19

def is_sharded?
    @sharding_options
end

#prefix_shard_name(s) ⇒ Object



77
78
79
# File 'lib/simple_record/sharding.rb', line 77

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

#shard(options = nil) ⇒ Object



11
12
13
# File 'lib/simple_record/sharding.rb', line 11

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

#sharded_domainsObject



82
83
84
85
86
87
88
89
# File 'lib/simple_record/sharding.rb', line 82

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



15
16
17
# File 'lib/simple_record/sharding.rb', line 15

def sharding_options
    @sharding_options
end

#shardsObject



73
74
75
# File 'lib/simple_record/sharding.rb', line 73

def shards
    send(sharding_options[:shards])
end