spaceppl
spaceppl

Reputation: 43

Merge two streams containing ordered data into one stream in node.js

I'm new to node.js. The problem is reading from 2 (or more) streams which have sorted data, and producing a "sorted merge" of them. For example:

Stream A: 1 5 6 8
Stream B: 2 3 4 7
========================
Result: 1 2 3 4 5 6 7 8

In C++/Java/C# this has a pretty obvious solution, something like:

BufferedReader[] readers = new BufferedReader[2];
String[] lines = new String[2];
// fill lines with initial values from both readers
// ...
while (true) {
    int earliestIndex = -1;
    // ....
    // determine earliestIndex by looping over lines and comparing them
    if (earliestIndex < 0) break;
    String line = lines[earliestIndex];
    // do something with line
    System.out.println(line);
    // advance reader
    lines[earliestIndex] = readers[earliestIndex].readLine();
}

But in node, this seems rather difficult. Any ideas?

Upvotes: 2

Views: 3121

Answers (1)

spaceppl
spaceppl

Reputation: 43

Here's a solution I eventually came up with. I'm using node-line-reader for reading from stream line-by-line (file stream, but this can be changed easily):

var LineReader = require('node-line-reader').LineReader;

var files = ['c:\\temp\\1.txt', 'c:\\temp\\2.txt'];
var readers = [];
var lines = [];

var readWhile = function (done) {
    var earliestIndex = -1;
    var earliest = MAX_VALUE;
    for (i = 0; i < lines.length; i++) {
        var l = lines[i];
        var value = parseInt(l);
        if (value < earliest) {
            earliest = value;
            earliestIndex = i;
        }
    }
    if (earliestIndex < 0) {
        done();
        return;
    }
    var line = lines[earliestIndex];
    console.log('Read from ' + files[earliestIndex] + ': ' + line);
    readers[earliestIndex].nextLine(function (err, line) {
        if (err) throw err;
        lines[earliestIndex] = line;
        process.nextTick(function () {
            readWhile(done);
        });
    });
}

new Promise(function (success, error) {
   for (i = 0; i < files.length; i++) {
      var reader = new LineReader(files[i]);
      readers.push(reader);
      new Promise(function (success, failure) {
         reader.nextLine(function (err, line) {
            if (err) failure(err);
            lines.push(line);
            success();
         });
      }).then(function (data) {
         if (lines.length == files.length) success();
      });
   }
}).then(function (data) {
    return new Promise(function (success, failure) {
        readWhile(success);
    });
}).then(function() {
   console.log('All done');
}, function (err) {
   console.log('Error: ' + err);
});

Upvotes: 1

Related Questions