Module: Bramble::Reduce

Extended by:
Keys
Defined in:
lib/bramble/reduce.rb

Class Method Summary collapse

Methods included from Keys

data_key, finished_at_key, job_id_key, keys_key, map_finished_count_key, map_total_count_key, namespace, reduce_finished_count_key, reduce_total_count_key, result_key, status_key

Class Method Details

.perform(handle, implementation) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/bramble/reduce.rb', line 7

def perform(handle, implementation)
  Bramble::State.running?(handle) do
    all_raw_keys = storage.map_keys_get(keys_key(handle))
    storage.set(reduce_total_count_key(handle), all_raw_keys.length)
    all_raw_keys.each do |raw_key|
      Bramble::ReduceJob.perform_later(handle, implementation.name, raw_key)
    end
  end
end

.perform_reduce(handle, implementation, raw_key) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bramble/reduce.rb', line 17

def perform_reduce(handle, implementation, raw_key)
  if Bramble::State.running?(handle)
    raw_values = storage.map_result_get(data_key(handle, raw_key))
    values = Bramble::Serialize.load(raw_values)
    key = Bramble::Serialize.load(raw_key)
    reduced_value = implementation.reduce(key, values)
    Bramble::State.running?(handle) do
      storage.reduce_result_set(result_key(handle), raw_key, Bramble::Serialize.dump(reduced_value))
      storage.increment(reduce_finished_count_key(handle))
      if Bramble::State.percent_reduced(handle) >= 1
        storage.set(finished_at_key(handle), Time.now.to_i)
      end
    end
  else
    Bramble::State.clear_reduce(handle)
  end
end