Anonymous avatar Anonymous committed 2aa0ef4

Removing Riak.emit from built-ins;Making sure map and reduce functions Do The Right Thing with return values

Comments (0)

Files changed (1)

apps/riak/priv/mapred_builtins.js

 var Riak = function() {
-  var _mapResults = [];
 
   return {
-    emit: function(data) { _mapResults[_mapResults.length] = data; return true; },
-    getResults: function() { var retval = _mapResults; _mapResults = []; return retval; },
     mapIdentity: function(values, key_data, arg) { return values; },
     mapSelectField: function(values, key_data, fieldName) { if (values instanceof Array) {
 	if (values.length == 1 && (values[0] instanceof Array && values[0].length > 0)) {
 	  values = values[0];
         }
-	values.forEach(function(value) { Riak.emit(value[fieldName]); });
-	return true;
+	return values.map(function(value) { value[fieldName]; });
       }
     },
     reduceUnion: function(values, arg) {
 	  if (retval.indexOf(value) == -1) {
 	    retval[retval.length] = value;
 	  } });
-      return retval;
+      return [retval];
     },
     reduceSum: function(values, arg) {
-      return values.reduce(function(prev, curr, index, array) { return prev + curr; });
+      return [values.reduce(function(prev, curr, index, array) { return prev + curr; })];
     },
     reduceMin: function(values, arg) {
       values.sort();
-      return values[0];
+      return [values[0]];
     },
     reduceMax: function(values, arg) {
       values.sort().reverse();
-      return values[0];
+      return [values[0]];
     },
     reduceAverage: function(values, arg) {
       var total = Riak.reduceSum(values, arg);
-      return total / values.length;
+      return [total / values.length];
     },
     reduceRandom: function(values, arg) {
       var index = Math.floor(Math.random() * values.length - 1);
-      return values[index];
+      return [values[index]];
     }
   }
 }();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.