我是node.js的新手。问题是从2个(或更多)流中读取已排序数据的流,并产生它们的“排序合并”。 例如:
Stream A: 1 5 6 8
Stream B: 2 3 4 7
========================
Result: 1 2 3 4 5 6 7 8
在C ++ / Java / C#中,这有一个非常明显的解决方案,例如:
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();
}
但在节点中,这似乎相当困难。有任何想法吗?
答案 0 :(得分:1)
这是我最终提出的解决方案。我正在使用node-line-reader逐行读取流(文件流,但这可以轻松更改):
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);
});
答案 1 :(得分:-1)
您可以观看要播放到您的信息流中的内容,然后将其取消管道并将其传输到您感兴趣的信息流中:
`
var PassThrough = require('stream').PassThrough;
var stream3 = new PassThrough();
// When a source stream is piped to us, undo that pipe, and save
// off the source stream piped into our internally managed streams.
stream3.on('pipe', function(source) {
source.unpipe(this);
this.transformStream = source.pipe(stream1).pipe(stream2);
});
// When we're piped to another stream, instead pipe our internal
// transform stream to that destination.
stream3.pipe = function(destination, options) {
return this.transformStream.pipe(destination, options);
};
stdin.pipe(stream3).pipe(stdout);`
您可以将此功能提取到您自己的可构造流类中:
var util = require('util');
var PassThrough = require('stream').PassThrough;
var StreamCombiner = function() {
this.streams = Array.prototype.slice.apply(arguments);
this.on('pipe', function(source) {
source.unpipe(this);
for(i in this.streams) {
source = source.pipe(this.streams[i]);
}
this.transformStream = source;
});
};
util.inherits(StreamCombiner, PassThrough);
StreamCombiner.prototype.pipe = function(dest, options) {
return this.transformStream.pipe(dest, options);
};
var stream3 = new StreamCombiner(stream1, stream2);
stdin.pipe(stream3).pipe(stdout);