Esempio n. 1
0
    intrusive_ptr<Document> DocumentSourceProject::getCurrent() {
        intrusive_ptr<Document> pInDocument(pSource->getCurrent());

        /* create the result document */
        const size_t sizeHint =
            pEO->getSizeHint(pInDocument) + (excludeId ? 0 : 1);
        intrusive_ptr<Document> pResultDocument(Document::create(sizeHint));

        if (!excludeId) {
            intrusive_ptr<const Value> pId(
                pInDocument->getField(Document::idName));

            /*
              Previous projections could have removed _id, (or declined to
              generate it) so it might already not exist.  Only attempt to add
              if we found it.
            */
            if (pId.get())
                pResultDocument->addField(Document::idName, pId);
        }

        /*
          Use the ExpressionObject to create the base result.

          If we're excluding fields at the top level, leave out the _id if
          it is found, because we took care of it above.
        */
        pEO->addToDocument(pResultDocument, pInDocument, true);

        return pResultDocument;
    }
Esempio n. 2
0
    intrusive_ptr<Document> DocumentSourceProject::getCurrent() {
	intrusive_ptr<Document> pInDocument(pSource->getCurrent());

	/* create the result document */
	const size_t sizeHint =
	    pEO->getSizeHint(pInDocument) + (excludeId ? 0 : 1);
	intrusive_ptr<Document> pResultDocument(Document::create(sizeHint));

	if (!excludeId) {
	    intrusive_ptr<const Value> pId(
		pInDocument->getField(Document::idName));
	    pResultDocument->addField(Document::idName, pId);
	}

	/* use the ExpressionObject to create the base result */
	pEO->addToDocument(pResultDocument, pInDocument);

        return pResultDocument;
    }
Esempio n. 3
0
    intrusive_ptr<Document> DocumentSourceProject::getCurrent() {
        intrusive_ptr<Document> pInDocument(pSource->getCurrent());

        /* create the result document */
        const size_t sizeHint = pEO->getSizeHint();
        intrusive_ptr<Document> pResultDocument(Document::create(sizeHint));

        /*
          Use the ExpressionObject to create the base result.

          If we're excluding fields at the top level, leave out the _id if
          it is found, because we took care of it above.
        */
        pEO->addToDocument(pResultDocument, pInDocument, /*root=*/pInDocument);

#if defined(_DEBUG)
        if (!_simpleProjection.getSpec().isEmpty()) {
            // Make sure we return the same results as Projection class

            BSONObjBuilder inputBuilder;
            pSource->getCurrent()->toBson(&inputBuilder);
            BSONObj input = inputBuilder.done();

            BSONObjBuilder outputBuilder;
            pResultDocument->toBson(&outputBuilder);
            BSONObj output = outputBuilder.done();

            BSONObj projected = _simpleProjection.transform(input);

            if (projected != output) {
                log() << "$project applied incorrectly: " << getRaw() << endl;
                log() << "input:  " << input << endl;
                log() << "out: " << output << endl;
                log() << "projected: " << projected << endl;
                verify(false); // exits in _DEBUG builds
            }
        }
#endif

        return pResultDocument;
    }
Esempio n. 4
0
    intrusive_ptr<Document> DocumentSourceUnwind::getCurrent() {
        if (!pNoUnwindDocument.get()) {
            intrusive_ptr<Document> pInDocument(pSource->getCurrent());

            /* create the result document */
            pNoUnwindDocument = pInDocument;
            fieldIndex.clear();

            /*
              First we'll look to see if the path is there.  If it isn't,
              we'll pass this document through.  If it is, we record the
              indexes of the fields down the field path so that we can
              quickly replace them as we clone the documents along the
              field path.

              We have to clone all the documents along the field path so
              that we don't share the end value across documents that have
              come out of this pipeline operator.
             */
            intrusive_ptr<Document> pCurrent(pInDocument);
            const size_t pathLength = unwindPath.getPathLength();
            for(size_t i = 0; i < pathLength; ++i) {
                size_t idx = pCurrent->getFieldIndex(
                    unwindPath.getFieldName(i));
                if (idx == pCurrent->getFieldCount() ) {
                    /* this document doesn't contain the target field */
                    resetArray();
                    return pInDocument;
                    break;
                }

                fieldIndex.push_back(idx);
                Document::FieldPair fp(pCurrent->getField(idx));
                intrusive_ptr<const Value> pPathValue(fp.second);
                if (i < pathLength - 1) {
                    if (pPathValue->getType() != Object) {
                        /* can't walk down the field path */
                        resetArray();
                        uassert(15977, str::stream() << unwindName <<
                                ":  cannot traverse field path past scalar value for \"" <<
                                fp.first << "\"", false);
                        break;
                    }

                    /* move down the object tree */
                    pCurrent = pPathValue->getDocument();
                }
                else /* (i == pathLength - 1) */ {
                    if (pPathValue->getType() != Array) {
                        /* last item on path must be an array to unwind */
                        resetArray();
                        uassert(15978, str::stream() << unwindName <<
                                ":  value at end of field path must be an array",
                                false);
                        break;
                    }

                    /* keep track of the array we're unwinding */
                    pUnwindArray = pPathValue;
                    if (pUnwindArray->getArrayLength() == 0) {
                        /*
                          The $unwind of an empty array is a NULL value.  If we
                          encounter this, use the non-unwind path, but replace
                          pOutField with a null.

                          Make sure unwind value is clear so the array is
                          removed.
                        */
                        pUnwindValue.reset();
                        intrusive_ptr<Document> pClone(clonePath());
                        resetArray();
                        return pClone;
                    }

                    /* get the iterator we'll use to unwind the array */
                    pUnwinder = pUnwindArray->getArray();
                    assert(pUnwinder->more()); // we just checked above...
                    pUnwindValue = pUnwinder->next();
                }
            }
        }

        /*
          If we're unwinding a field, create an alternate document.  In the
          alternate (clone), replace the unwound array field with the element
          at the appropriate index.
         */
        if (pUnwindArray.get()) {
            /* clone the document with an array we're unwinding */
            intrusive_ptr<Document> pUnwindDocument(clonePath());

            return pUnwindDocument;
        }

        return pNoUnwindDocument;
    }