Retain batch concept and return lines in groups of 1000
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user