view worker-map-reducer.js @ 49:a5e2db06b58d

Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
author Atul Varma <varmaa@toolness.com>
date Tue, 14 Apr 2009 15:02:19 -0700
parents
children e29662aba1b6
line wrap: on
line source

function map(func, dict) {
  var mapDict = {};
  var currDoc;

  function emit(key, value) {
    var item = mapDict[key];
    if (!item)
      item = mapDict[key] = {keys: [], values: []};
    item.keys.push(currDoc.id);
    item.values.push(value);
  }

  for (key in dict) {
    currDoc = dict[key];
    func(currDoc, emit);
  }

  return mapDict;
}

onmessage = function(event) {
  var mapFunc = eval("(" + event.data.map + ")");
  postMessage(map(mapFunc, event.data.dict));
};