Skip to content

Commit

Permalink
Fix bug, the last line of a file can not be write correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
aleafs committed Jun 24, 2012
1 parent 3ae910a commit d9ef61a
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions index.js
Expand Up @@ -91,6 +91,46 @@ exports.create = function (flist, prefix, options) {
var _wcache = {}; /**< 缓冲对象, 每个路由值对应一个 */
var _wlines = {}; /**< 写入行数, 每个路由值对应一个 */

/* {{{ function _afterRead() */

var _tail = '';
var _afterRead = function (data) {
if (_tail.length === 0 && data.length === 0) {
return;
}
var rows = (_tail + data).split(_options.EOL);
if (data.length) {
_tail = rows.pop();
}

for (var i = 0, m = rows.length; i < m; i++) {
var row = rows[i].split(_options.EOF);
var idx = _getRoute(row);
var txt = _buildRow(row) + _options.EOL;

if (undefined === _wcache[idx]) {
_wcache[idx] = txt;
_writer[idx] = _createWriter(idx);
_wlines[idx] = 0;
} else {
_wcache[idx] += txt;
}

_wlines[idx]++;

if (_wcache[idx].length >= _options.bufferSize || _wlines[idx] >= _options.maxLines) {
_writer[idx].write(_wcache[idx]);
_wcache[idx] = '';
if (_wlines[idx] >= _options.maxLines) {
_writer[idx].end();
_writer[idx] = _createWriter(idx);
_wlines[idx] = 0;
}
}
}
};
/* }}} */

var _readfile = function (fname) {

if (!fname) {
Expand All @@ -110,41 +150,11 @@ exports.create = function (flist, prefix, options) {
reader.on('error', function (error) {
_complete(iError('StreamReadError', error.stack));
});
reader.on('data', _afterRead);
reader.on('end', function () {
_afterRead('');
_readfile(flist.shift());
});

var _tail = '';
reader.on('data', function (data) {
var rows = (_tail + data).split(_options.EOL);
_tail = rows.pop();

for (var i = 0, m = rows.length; i < m; i++) {
var row = rows[i].split(_options.EOF);
var idx = _getRoute(row);
var txt = _buildRow(row) + _options.EOL;

if (undefined === _wcache[idx]) {
_wcache[idx] = txt;
_writer[idx] = _createWriter(idx);
_wlines[idx] = 0;
} else {
_wcache[idx] += txt;
}

_wlines[idx]++;

if (_wcache[idx].length >= _options.bufferSize || _wlines[idx] >= _options.maxLines) {
_writer[idx].write(_wcache[idx]);
_wcache[idx] = '';
if (_wlines[idx] >= _options.maxLines) {
_writer[idx].end();
_writer[idx] = _createWriter(idx);
_wlines[idx] = 0;
}
}
}
});
};

return function (callback) {
Expand Down

0 comments on commit d9ef61a

Please sign in to comment.