diff options
author | Mathias Stearn <mathias@10gen.com> | 2013-08-02 19:34:34 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2013-08-09 13:38:27 -0400 |
commit | 311aafaf4d17470f9d5b1ae2090acf38fb4d00a3 (patch) | |
tree | b0cfbc348f84e1ec647d4e84a052f138a5530642 /src/mongo/db/pipeline/document_source_skip.cpp | |
parent | cceff51b744ace213d1bc2adae37ecfaf35439d4 (diff) | |
download | mongo-311aafaf4d17470f9d5b1ae2090acf38fb4d00a3.tar.gz |
Simplify aggregation DocumentSource API
Diffstat (limited to 'src/mongo/db/pipeline/document_source_skip.cpp')
-rw-r--r-- | src/mongo/db/pipeline/document_source_skip.cpp | 53 |
1 files changed, 15 insertions, 38 deletions
diff --git a/src/mongo/db/pipeline/document_source_skip.cpp b/src/mongo/db/pipeline/document_source_skip.cpp index 3141ecaf9cc..bbb0358b291 100644 --- a/src/mongo/db/pipeline/document_source_skip.cpp +++ b/src/mongo/db/pipeline/document_source_skip.cpp @@ -29,8 +29,8 @@ namespace mongo { DocumentSourceSkip::DocumentSourceSkip(const intrusive_ptr<ExpressionContext> &pExpCtx): SplittableDocumentSource(pExpCtx), - skip(0), - count(0) { + _skip(0), + _needToSkip(true) { } DocumentSourceSkip::~DocumentSourceSkip() { @@ -50,50 +50,27 @@ namespace mongo { return false; /* we need to skip over the sum of the two consecutive $skips */ - skip += pSkip->skip; + _skip += pSkip->_skip; return true; } - void DocumentSourceSkip::skipper() { - if (count == 0) { - while (!pSource->eof() && count++ < skip) { - pSource->advance(); - } - } - - if (pSource->eof()) { - pCurrent.reset(); - return; - } + boost::optional<Document> DocumentSourceSkip::getNext() { + pExpCtx->checkForInterrupt(); - pCurrent = pSource->getCurrent(); - } - - bool DocumentSourceSkip::eof() { - skipper(); - return pSource->eof(); - } - - bool DocumentSourceSkip::advance() { - DocumentSource::advance(); // check for interrupts - - if (eof()) { - pCurrent.reset(); - return false; + if (_needToSkip) { + _needToSkip = false; + for (long long i=0; i < _skip; i++) { + if (!pSource->getNext()) + return boost::none; + } } - pCurrent = pSource->getCurrent(); - return pSource->advance(); - } - - Document DocumentSourceSkip::getCurrent() { - skipper(); - return pCurrent; + return pSource->getNext(); } void DocumentSourceSkip::sourceToBson( BSONObjBuilder *pBuilder, bool explain) const { - pBuilder->append("$skip", skip); + pBuilder->append("$skip", _skip); } intrusive_ptr<DocumentSourceSkip> DocumentSourceSkip::create( @@ -113,10 +90,10 @@ namespace mongo { intrusive_ptr<DocumentSourceSkip> pSkip( DocumentSourceSkip::create(pExpCtx)); - pSkip->skip = pBsonElement->numberLong(); + pSkip->_skip = pBsonElement->numberLong(); uassert(15956, str::stream() << DocumentSourceSkip::skipName << ": the number to skip cannot be negative", - pSkip->skip >= 0); + pSkip->_skip >= 0); return pSkip; } |