Class: RiakRecord::Finder::Basic
- Inherits:
-
Object
- Object
- RiakRecord::Finder::Basic
- Includes:
- Enumerable
- Defined in:
- lib/riak_record/finder/basic.rb
Direct Known Subclasses
Instance Method Summary collapse
- #all ⇒ Object (also: #to_ary)
- #any?(&block) ⇒ Boolean
- #count ⇒ Object
- #count_by(attribute) ⇒ Object
- #count_by_map_reduce(attribute, timeout = nil) ⇒ Object
- #count_map_reduce(timeout = nil) ⇒ Object
- #each ⇒ Object
- #empty?(&block) ⇒ Boolean (also: #none?)
- #find(ifnone = nil, &block) ⇒ Object (also: #detect)
- #first(n = nil) ⇒ Object
-
#initialize(finder_class, conditions) ⇒ Basic
constructor
A new instance of Basic.
- #keys ⇒ Object
- #page(continuation = nil, page_size = 100) ⇒ Object
- #pluck_by_map_reduce(attribute, timeout = nil) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(finder_class, conditions) ⇒ Basic
Returns a new instance of Basic.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/riak_record/finder/basic.rb', line 6 def initialize(finder_class, conditions) unless finder_class <= RiakRecord::Base raise ArgumentError, "RiakRecord::Finder requires a RiakRecord::Base class" end unless conditions.is_a?(Hash) && conditions.size == 1 raise ArgumentError, "RiakRecord::Finder requires exactly one condition specified as a hash" end @finder_class = finder_class @bucket = finder_class.bucket @index = finder_class.index_names[conditions.keys.first.to_sym] @value = conditions.values.first @load_complete = false @page_size = 100 @loaded_objects = [] end |
Instance Method Details
#all ⇒ Object Also known as: to_ary
22 23 24 25 26 27 |
# File 'lib/riak_record/finder/basic.rb', line 22 def all until @load_complete do load_next_page end @loaded_objects end |
#any?(&block) ⇒ Boolean
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/riak_record/finder/basic.rb', line 80 def any?(&block) if block_given? return true if @loaded_objects.any? &block until @load_complete return true if load_next_page.any? &block end false else !empty? end end |
#count ⇒ Object
42 43 44 |
# File 'lib/riak_record/finder/basic.rb', line 42 def count @load_complete ? @loaded_objects.count : count_map_reduce end |
#count_by(attribute) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/riak_record/finder/basic.rb', line 92 def count_by(attribute) return count_by_map_reduce(attribute) unless @load_complete results = {} @loaded_objects.each{|o| k = o.send(attribute).to_s; results[k] ||= 0; results[k] += 1 } results end |
#count_by_map_reduce(attribute, timeout = nil) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/riak_record/finder/basic.rb', line 115 def count_by_map_reduce(attribute, timeout = nil) count_by_index = @finder_class.index_names[attribute.to_sym].present? parsed_attribute = count_by_index ? "v.values[0].metadata.index.#{@finder_class.index_names[attribute.to_sym]}" : "JSON.parse(v.values[0].data).#{attribute}" mr = new_map_reduce.map("function(v){ var h = {}; h[#{parsed_attribute}] = 1; return [h] }", :keep => false). reduce("function(values) { var result = {}; for (var value in values) { for (var key in values[value]) { if (key in result) { result[key] += values[value][key]; } else { result[key] = values[value][key]; }}} return [result]; }", :keep => true) mr.timeout = timeout if timeout.present? mr.run.first end |
#count_map_reduce(timeout = nil) ⇒ Object
124 125 126 127 128 129 |
# File 'lib/riak_record/finder/basic.rb', line 124 def count_map_reduce(timeout = nil) mr = new_map_reduce.map("function(v){ return [1] }", :keep => false). reduce(['riak_kv_mapreduce','reduce_sum'], :keep => true) mr.timeout = timeout if timeout.present? mr.run.first end |
#each ⇒ Object
38 39 40 |
# File 'lib/riak_record/finder/basic.rb', line 38 def each all.each{|o| yield o} end |
#empty?(&block) ⇒ Boolean Also known as: none?
69 70 71 72 73 74 75 76 77 |
# File 'lib/riak_record/finder/basic.rb', line 69 def empty?(&block) if block_given? !any?(&block) elsif load_started? @loaded_objects.count.zero? else Riak::SecondaryIndex.new(@bucket, @index, @value, :max_results => 1).keys.empty? end end |
#find(ifnone = nil, &block) ⇒ Object Also known as: detect
58 59 60 61 62 63 64 65 66 |
# File 'lib/riak_record/finder/basic.rb', line 58 def find(ifnone = nil, &block) found = @loaded_objects.find(&block) until found || @load_complete found = load_next_page.find(&block) end return found if found return ifnone.call if ifnone nil end |
#first(n = nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/riak_record/finder/basic.rb', line 46 def first(n=nil) if n unless @load_complete || @loaded_objects.count >= n load_next_page(n-@loaded_objects.count) end @loaded_objects.first(n) else load_next_page(1) unless load_started? @loaded_objects.first end end |
#keys ⇒ Object
34 35 36 |
# File 'lib/riak_record/finder/basic.rb', line 34 def keys @keys ||= Riak::SecondaryIndex.new(@bucket, @index, @value).keys end |
#page(continuation = nil, page_size = 100) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/riak_record/finder/basic.rb', line 99 def page(continuation = nil, page_size = 100) = { :max_results => page_size } [:continuation] = continuation if continuation.present? querier = Riak::SecondaryIndex.new(@bucket, @index, @value, ) results = querier.values.compact.map{ |robject| @finder_class.new(robject) } return results, querier.keys.continuation end |
#pluck_by_map_reduce(attribute, timeout = nil) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/riak_record/finder/basic.rb', line 107 def pluck_by_map_reduce(attribute, timeout = nil) pluck_by_index = @finder_class.index_names[attribute.to_sym].present? parsed_attribute = pluck_by_index ? "v.values[0].metadata.index.#{@finder_class.index_names[attribute.to_sym]}" : "JSON.parse(v.values[0].data).#{attribute}" mr = new_map_reduce.map("function(v){ return [#{parsed_attribute}] }", :keep => true) mr.timeout = timeout if timeout.present? mr.run end |
#to_a ⇒ Object
30 31 32 |
# File 'lib/riak_record/finder/basic.rb', line 30 def to_a all.dup # new array end |