intrusive_ptr<Document> DocumentSourceUnwind::clonePath() const { /* For this to be valid, we must already have pNoUnwindDocument set, and have set up the vector of indices for that document in fieldIndex. */ assert(pNoUnwindDocument.get()); intrusive_ptr<Document> pClone(pNoUnwindDocument->clone()); intrusive_ptr<Document> pCurrent(pClone); const size_t n = fieldIndex.size(); assert(n); for(size_t i = 0; i < n; ++i) { const size_t fi = fieldIndex[i]; Document::FieldPair fp(pCurrent->getField(fi)); if (i + 1 < n) { /* For every object in the path but the last, clone it and continue on down. */ intrusive_ptr<Document> pNext( fp.second->getDocument()->clone()); pCurrent->setField(fi, fp.first, Value::createDocument(pNext)); pCurrent = pNext; } else { /* for the last, subsitute the next unwound value */ pCurrent->setField(fi, fp.first, pUnwindValue); } } return pClone; }
UInt32 MemoryStreamBuf::written() { int written = pCurrent()-begin(); if(written<0) written=0; if(written>_written) _written = (UInt32)written; return _written; }
void MemoryStreamBuf::resize(UInt32 newSize) { _bufferSize = newSize; int pos = gCurrent()-_pBuffer; if(pos>_bufferSize) pos = _bufferSize; setg(_pBuffer,_pBuffer+pos,_pBuffer + _bufferSize); pos = pCurrent()-_pBuffer; if(pos>_bufferSize) pos = _bufferSize; setp(_pBuffer,_pBuffer + _bufferSize); pbump(pos); }
void MemoryStreamBuf::clip(Int32 offset) { if(offset==0) return; char* gpos = gCurrent(); _pBuffer += offset; _bufferSize -= offset; int ppos = pCurrent()-_pBuffer; setg(_pBuffer,gpos,_pBuffer + _bufferSize); setp(_pBuffer,_pBuffer + _bufferSize); pbump(ppos); if(_written<offset) _written=0; else _written-=offset; if(_written>_bufferSize) _written=_bufferSize; }
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; }