void combine(FnObj &fn_obj) { intermediates_t intermediates; intermediates.resize(num_partitions_); using std::swap; swap(intermediates_, intermediates); for (auto const &intermediate : intermediates) { for (auto const &kv : intermediate) { fn_obj.start(kv.first); for (auto const &value : kv.second) fn_obj(value); fn_obj.finish(kv.first, *this); } } }
void combine(FnObj &fn_obj) { this->close_files(); for (intermediates_t::iterator it=intermediate_files_.begin(); it!=intermediate_files_.end(); ++it) { std::string infilename = it->second.first; std::string outfilename = platform::get_temporary_filename(); // sort the input file SortFn()(infilename.c_str(), outfilename.c_str(), 11); boost::filesystem::remove(infilename); std::swap(infilename, outfilename); std::string key, last_key; typename reduce_task_type::value_type value; std::ifstream infile(infilename.c_str()); while (read_record(infile, key, value)) { if (key != last_key && key.length() > 0) { if (last_key.length() > 0) fn_obj.finish(last_key, *this); if (key.length() > 0) { fn_obj.start(key); std::swap(key, last_key); } } fn_obj(value); } if (last_key.length() > 0) fn_obj.finish(last_key, *this); infile.close(); boost::filesystem::remove(infilename); } this->close_files(); }