Android Open Source - Profiterole Reducer






From Project

Back to project page Profiterole.

License

The source code is released under:

Apache License

If you think the Android project Profiterole listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package profiterole.mapreduce;
/*from w ww.  ja v  a 2  s  .c  om*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import profiterole.api.Waffle;
import profiterole.waffle.WaffleImpl;

public class Reducer {

  // seems major bottle neck here specially if the map is huge
  // thus need to look for problems with small maps
  // such as search/grep or word count
  // can add heuristics for hash table size

  // Reducer(when we pass to it identity func) is not reducer
  // Do limits for reducer, sort of hash must be less than 50,

  private Reducer() {
    // no need for constructor 1 object/function per running JVM instance
  }

  public static Waffle<Integer> reduce(List<HashMap<String, Integer>> maps) {
    HashMap<String, Integer> accumMap  = new HashMap<String, Integer>();

    // library code check parameters for validity
    if (maps == null) {

      // TODO do empty waffle
      return null;
    }

    // TODO current problem works only for ints use generic reduce version
    for (HashMap<String, Integer> map : maps) {

      for (String word : map.keySet()) {
        if (accumMap.containsKey(word)) {
          AtomicInteger value = new AtomicInteger(map.get(word).intValue()
                            + accumMap.get(word).intValue());
          accumMap.put(word, value.get());
        } else {
          accumMap.put(word, map.get(word).intValue());
        }
      }
    }

    return new WaffleImpl<Integer>(accumMap);
  }

  // FUTURE DIRECTION
  // List-based generic reduction
  static <E> E reduce(List<E> list, Function<E> f, E initVal) {
    List<E> snapshot;
    synchronized (list) {
      snapshot = new ArrayList<E>(list);
    }
    E result = initVal;
    for (E e : snapshot)
      result = f.apply(result, e);
    return result;
  }

  interface Function<M> {
    M apply(M arg1, M arg2);
  }
}




Java Source Code List

profiterole.android.DictionaryScreen.java
profiterole.android.SearchScreen.java
profiterole.api.MapReduce.java
profiterole.api.OnUpdateStatusCallback.java
profiterole.api.Waffle.java
profiterole.mapreduce.MapCallback.java
profiterole.mapreduce.MapReduceService.java
profiterole.mapreduce.Reducer.java
profiterole.mapreduce.Splitter.java
profiterole.samples.Driver.java
profiterole.samples.PrintPromptListener.java
profiterole.samples.REPL.java
profiterole.waffle.InvertedIndex.java
profiterole.waffle.WaffleBackend.java
profiterole.waffle.WaffleImpl.java
profiterole.waffle.WaffleUtils.java