Mercurial Hosting > luan
changeset 2172:75c45f1a743e default tip
MapReduce cleanup
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 22 Mar 2026 20:58:17 -0600 |
| parents | 8b77bd42864d |
| children | |
| files | src/goodjava/util/MapReduce.java src/luan/modules/ThreadLuan.java |
| diffstat | 2 files changed, 31 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- a/src/goodjava/util/MapReduce.java Sun Mar 22 19:24:52 2026 -0600 +++ b/src/goodjava/util/MapReduce.java Sun Mar 22 20:58:17 2026 -0600 @@ -11,37 +11,33 @@ public class MapReduce { - public interface Mapper { + public interface Handler { public List map(Object arg); - } - - public interface Handler extends Mapper { public List reduce(List<List> lists); } private static Map<String,MapReduce> instances = new HashMap<String,MapReduce>(); // an active handler will be arbitrarily chosen for reduce - public static synchronized MapReduce register(String key,Mapper mapper) { + public static synchronized MapReduce register(String key,Handler handler) { MapReduce mr = instances.get(key); if( mr == null ) { mr = new MapReduce(); instances.put(key,mr); } - mr.mappers.add(mapper); + mr.handlers.add(handler); return mr; } - private Set<Mapper> mappers = Collections.newSetFromMap(new WeakHashMap<Mapper, Boolean>()); + private Set<Handler> handlers = Collections.newSetFromMap(new WeakHashMap<Handler, Boolean>()); public List run(Object arg) { List<List> lists = new ArrayList<List>(); Handler lastHandler = null; - Mapper[] snapshot = mappers.toArray(new Mapper[0]); - for( Mapper m : snapshot ) { - lists.add( m.map(arg) ); - if( m instanceof Handler ) - lastHandler = (Handler)m; + Handler[] snapshot = handlers.toArray(new Handler[0]); + for( Handler h : snapshot ) { + lists.add( h.map(arg) ); + lastHandler = h; } return lastHandler==null ? Collections.emptyList() : lastHandler.reduce(lists); }
--- a/src/luan/modules/ThreadLuan.java Sun Mar 22 19:24:52 2026 -0600 +++ b/src/luan/modules/ThreadLuan.java Sun Mar 22 20:58:17 2026 -0600 @@ -290,51 +290,34 @@ } - public static MapReduce registerMapReduce(Luan luan,String key,LuanFunction map,LuanFunction reduce) - throws LuanException - { - MapReduce.Mapper m; + public static MapReduce registerMapReduce(Luan luan,String key,LuanFunction map,LuanFunction reduce) { final Luan newLuan = new Luan(luan); LuanMutable.makeImmutable(map); - if( reduce == null ) { - m = new MapReduce.Mapper() { - @Override public List map(Object arg) { - try { - LuanTable t = (LuanTable)map.call(newLuan,arg); - return t.asList(); - } catch(LuanException e) { - throw new LuanRuntimeException(e); - } + LuanMutable.makeImmutable(reduce); + MapReduce.Handler h = new MapReduce.Handler() { + @Override public List map(Object arg) { + try { + LuanTable t = (LuanTable)map.call(newLuan,arg); + return t.asList(); + } catch(LuanException e) { + throw new LuanRuntimeException(e); } - }; - } else { - Luan.checkSecurity(luan,"java"); - LuanMutable.makeImmutable(reduce); - m = new MapReduce.Handler() { - @Override public List map(Object arg) { - try { - LuanTable t = (LuanTable)map.call(newLuan,arg); - return t.asList(); - } catch(LuanException e) { - throw new LuanRuntimeException(e); + } + @Override public List reduce(List<List> lists) { + try { + LuanTable tLists = new LuanTable(); + for( List list : lists ) { + tLists.rawAdd( new LuanTable(list) ); } + LuanTable t = (LuanTable)reduce.call(newLuan,tLists); + return t.asList(); + } catch(LuanException e) { + throw new LuanRuntimeException(e); } - @Override public List reduce(List<List> lists) { - try { - LuanTable tLists = new LuanTable(); - for( List list : lists ) { - tLists.rawAdd( new LuanTable(list) ); - } - LuanTable t = (LuanTable)reduce.call(newLuan,tLists); - return t.asList(); - } catch(LuanException e) { - throw new LuanRuntimeException(e); - } - } - }; - } - luan.registry.put("MapReduce_"+key,m); - return MapReduce.register(key,m); + } + }; + luan.registry.put("MapReduce_"+key,h); + return MapReduce.register(key,h); } }
