Skip to content

Commit

Permalink
fix: ensure enough data for skip and limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Shepheard committed May 22, 2014
1 parent a1ce605 commit 30db0f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/index.js
Expand Up @@ -29,7 +29,13 @@ function createMergedReadStream(db, options) {

var sources = options.ranges.map(function (range) {
// Force keys for comparator
return db.createReadStream(extend(options, range, { keys: true }));
var overrides = { keys: true };
// Increase per-stream limit to guarantee enough results for merged limit to
// be reached
if (options.limit && options.skip) {
overrides.limit = options.limit + options.skip;
}
return db.createReadStream(extend(options, range, overrides));
});
var stream = merge(makeComparator(options), sources);
if (options.skip) {
Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Expand Up @@ -246,6 +246,25 @@ describe('level-merged-stream', function () {
});
});
});

it('should provide enough source stream results to meet skip & limit', function (done) {
var results = [];
db.mergedReadStream({
ranges: ranges,
comparator: comparator,
skip: 3,
limit: 1
})
.on('data', function (data) {
results.push(data.value);
})
.on('end', function () {
// If createReadStream limit was set to 1 and not 3 + 1, we'd get []
// and not ['3'] because the 'b' stream would already have ended
expect(results).to.deep.equal(['3']);
done();
});
});
});

describe('createMergedKeyStream', function () {
Expand Down

0 comments on commit 30db0f7

Please sign in to comment.