Retain batch concept and return lines in groups of 1000

This commit is contained in:
Young Hahn
2015-01-16 13:22:41 -05:00
parent ea292f20c9
commit 62831d95bb
2 changed files with 59 additions and 11 deletions

View File

@@ -12,7 +12,7 @@ function ZXYStream(source, options) {
options = options || {};
this.source = source;
this._afterGet = this._afterGet.bind(this);
this.batch = options.batch || 1000;
stream.Readable.call(this);
}
@@ -38,14 +38,29 @@ ZXYStream.prototype._read = function() {
return;
}
stream.statement.get(stream._afterGet);
};
var lines = '';
var error;
var remaining = stream.batch;
for (var i = 0; i < stream.batch; i++) stream.statement.get(afterGet);
ZXYStream.prototype._afterGet = function(err, row) {
if (err && err.code === 'SQLITE_ERROR' && /no such table/.test(err.message)) return this.push(null);
if (err) return this.emit('error', err);
if (!row) return this.push(null);
this.push(toLine(row));
function afterGet(err, row) {
if (err && err.code === 'SQLITE_ERROR' && /no such table/.test(err.message)) {
// no-op
} else if (err) {
error = err;
} else if (!row) {
// no-op
} else {
lines += toLine(row);
}
if (!--remaining) {
if (error) {
stream.emit('error', error);
} else {
stream.push(lines || null);
}
}
}
};
function toLine(row) {