Permalink
Browse files

Added a sort example

  • Loading branch information...
1 parent 066a846 commit c0f2ed8a53ae2976e628206caa30b90ab1c050ea @chriso chriso committed Dec 8, 2010
Showing with 34 additions and 0 deletions.
  1. +34 −0 examples/sort.js
View
@@ -0,0 +1,34 @@
+var Job = require('node.io').Job;
+
+var options = {
+ take: 10000
+};
+
+var temp_files = [];
+
+var methods = {
+
+ run: function(lines) {
+ lines.sort();
+ this.emit(lines);
+ },
+
+ reduce: function (lines) {
+ var self = this, temp_file = '/tmp/' + Math.floor(10000000 * Math.random());
+ this.write(temp_file, lines, function () {
+ temp_files.push(temp_file);
+ self.skip();
+ });
+ },
+
+ complete: function (callback) {
+ var self = this;
+ this.spawn('sort -m ' + temp_files.join(' '), function (err, stdout, stderr) {
+ self.output(stdout.substr(0, stdout.length-1));
+ self.exec('rm -f ' + temp_files.join(' '), callback);
+ });
+ }
+};
+
+//Export the job
+exports.job = new Job(options, methods);

0 comments on commit c0f2ed8

Please sign in to comment.