Mercurial Hosting > luan
comparison src/goodjava/util/MapReduce.java @ 2172:75c45f1a743e default tip
MapReduce cleanup
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 22 Mar 2026 20:58:17 -0600 |
| parents | 8b77bd42864d |
| children |
comparison
equal
deleted
inserted
replaced
| 2171:8b77bd42864d | 2172:75c45f1a743e |
|---|---|
| 9 import java.util.Set; | 9 import java.util.Set; |
| 10 | 10 |
| 11 | 11 |
| 12 public class MapReduce { | 12 public class MapReduce { |
| 13 | 13 |
| 14 public interface Mapper { | 14 public interface Handler { |
| 15 public List map(Object arg); | 15 public List map(Object arg); |
| 16 } | |
| 17 | |
| 18 public interface Handler extends Mapper { | |
| 19 public List reduce(List<List> lists); | 16 public List reduce(List<List> lists); |
| 20 } | 17 } |
| 21 | 18 |
| 22 private static Map<String,MapReduce> instances = new HashMap<String,MapReduce>(); | 19 private static Map<String,MapReduce> instances = new HashMap<String,MapReduce>(); |
| 23 | 20 |
| 24 // an active handler will be arbitrarily chosen for reduce | 21 // an active handler will be arbitrarily chosen for reduce |
| 25 public static synchronized MapReduce register(String key,Mapper mapper) { | 22 public static synchronized MapReduce register(String key,Handler handler) { |
| 26 MapReduce mr = instances.get(key); | 23 MapReduce mr = instances.get(key); |
| 27 if( mr == null ) { | 24 if( mr == null ) { |
| 28 mr = new MapReduce(); | 25 mr = new MapReduce(); |
| 29 instances.put(key,mr); | 26 instances.put(key,mr); |
| 30 } | 27 } |
| 31 mr.mappers.add(mapper); | 28 mr.handlers.add(handler); |
| 32 return mr; | 29 return mr; |
| 33 } | 30 } |
| 34 | 31 |
| 35 private Set<Mapper> mappers = Collections.newSetFromMap(new WeakHashMap<Mapper, Boolean>()); | 32 private Set<Handler> handlers = Collections.newSetFromMap(new WeakHashMap<Handler, Boolean>()); |
| 36 | 33 |
| 37 public List run(Object arg) { | 34 public List run(Object arg) { |
| 38 List<List> lists = new ArrayList<List>(); | 35 List<List> lists = new ArrayList<List>(); |
| 39 Handler lastHandler = null; | 36 Handler lastHandler = null; |
| 40 Mapper[] snapshot = mappers.toArray(new Mapper[0]); | 37 Handler[] snapshot = handlers.toArray(new Handler[0]); |
| 41 for( Mapper m : snapshot ) { | 38 for( Handler h : snapshot ) { |
| 42 lists.add( m.map(arg) ); | 39 lists.add( h.map(arg) ); |
| 43 if( m instanceof Handler ) | 40 lastHandler = h; |
| 44 lastHandler = (Handler)m; | |
| 45 } | 41 } |
| 46 return lastHandler==null ? Collections.emptyList() : lastHandler.reduce(lists); | 42 return lastHandler==null ? Collections.emptyList() : lastHandler.reduce(lists); |
| 47 } | 43 } |
| 48 } | 44 } |
