本文整理汇总了C++中mutablebson::Element::getDocument方法的典型用法代码示例。如果您正苦于以下问题:C++ Element::getDocument方法的具体用法?C++ Element::getDocument怎么用?C++ Element::getDocument使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mutablebson::Element
的用法示例。
在下文中一共展示了Element::getDocument方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: prepare
Status ModifierPush::prepare(mutablebson::Element root,
const StringData& matchedField,
ExecInfo* execInfo) {
_preparedState.reset(new PreparedState(&root.getDocument()));
// If we have a $-positional field, it is time to bind it to an actual field part.
if (_posDollar) {
if (matchedField.empty()) {
return Status(ErrorCodes::BadValue,
str::stream() << "The positional operator did not find the match "
"needed from the query. Unexpanded update: "
<< _fieldRef.dottedField());
}
_fieldRef.setPart(_posDollar, matchedField);
}
// Locate the field name in 'root'. Note that we may not have all the parts in the path
// in the doc -- which is fine. Our goal now is merely to reason about whether this mod
// apply is a noOp or whether is can be in place. The remainin path, if missing, will
// be created during the apply.
Status status = pathsupport::findLongestPrefix(_fieldRef,
root,
&_preparedState->idxFound,
&_preparedState->elemFound);
// FindLongestPrefix may say the path does not exist at all, which is fine here, or
// that the path was not viable or otherwise wrong, in which case, the mod cannot
// proceed.
if (status.code() == ErrorCodes::NonExistentPath) {
_preparedState->elemFound = root.getDocument().end();
}
else if (status.isOK()) {
const bool destExists = (_preparedState->idxFound == (_fieldRef.numParts()-1));
// If the path exists, we require the target field to be already an
// array.
if (destExists && _preparedState->elemFound.getType() != Array) {
mb::Element idElem = mb::findFirstChildNamed(root, "_id");
return Status(ErrorCodes::BadValue,
str::stream() << "The field '" << _fieldRef.dottedField() << "'"
<< " must be an array but is of type "
<< typeName(_preparedState->elemFound.getType())
<< " in document {" << idElem.toString() << "}");
}
}
else {
return status;
}
// We register interest in the field name. The driver needs this info to sort out if
// there is any conflict among mods.
execInfo->fieldRef[0] = &_fieldRef;
return Status::OK();
}
示例2: prepare
Status ModifierObjectReplace::prepare(mutablebson::Element root,
StringData matchedField,
ExecInfo* execInfo) {
_preparedState.reset(new PreparedState(&root.getDocument()));
// objectSize checked by binaryEqual (optimization)
BSONObj objOld = root.getDocument().getObject();
if (objOld.binaryEqual(_val)) {
_preparedState->noOp = true;
execInfo->noOp = true;
}
return Status::OK();
}
示例3: prepare
Status ModifierObjectReplace::prepare(mutablebson::Element root,
const StringData& matchedField,
ExecInfo* execInfo) {
_preparedState.reset(new PreparedState(&root.getDocument()));
return Status::OK();
}
示例4: PreparedState
PreparedState(mutablebson::Element root)
: doc(root.getDocument())
, fromElemFound(doc.end())
, toIdxFound(0)
, toElemFound(doc.end())
, applyCalled(false){
}
示例5: log
Status ModifierInc::log(mutablebson::Element logRoot) const {
// We'd like to create an entry such as {$set: {<fieldname>: <value>}} under 'logRoot'.
// We start by creating the {$set: ...} Element.
mutablebson::Document& doc = logRoot.getDocument();
mutablebson::Element setElement = doc.makeElementObject("$set");
if (!setElement.ok()) {
return Status(ErrorCodes::InternalError, "cannot append log entry for $set mod");
}
// Then we create the {<fieldname>: <value>} Element.
mutablebson::Element logElement = doc.makeElementSafeNum(
_fieldRef.dottedField(),
_preparedState->newValue);
if (!logElement.ok()) {
return Status(ErrorCodes::InternalError, "cannot append details for $set mod");
}
// Now, we attach the {<fieldname>: <value>} Element under the {$set: ...} one.
Status status = setElement.pushBack(logElement);
if (!status.isOK()) {
return status;
}
// And attach the result under the 'logRoot' Element provided.
return logRoot.pushBack(setElement);
}
示例6: log
Status ModifierUnset::log(mutablebson::Element logRoot) const {
// We'd like to create an entry such as {$unset: {<fieldname>: 1}} under 'logRoot'.
// We start by creating the {$unset: ...} Element.
mutablebson::Document& doc = logRoot.getDocument();
mutablebson::Element unsetElement = doc.makeElementObject("$unset");
if (!unsetElement.ok()) {
return Status(ErrorCodes::InternalError, "cannot create log entry for $unset mod");
}
// Then we create the {<fieldname>: <value>} Element. Note that <fieldname> must be a
// dotted field, and not only the last part of that field. The rationale here is that
// somoene picking up this log entry -- e.g., a secondary -- must be capable of doing
// the same path find/creation that was done in the previous calls here.
mutablebson::Element logElement = doc.makeElementInt(_fieldRef.dottedField(), 1);
if (!logElement.ok()) {
return Status(ErrorCodes::InternalError, "cannot create log details for $unset mod");
}
// Now, we attach the {<fieldname>: `} Element under the {$unset: ...} one.
Status status = unsetElement.pushBack(logElement);
if (!status.isOK()) {
return status;
}
// And attach the result under the 'logRoot' Element provided.
return logRoot.pushBack(unsetElement);
}
示例7: log
Status ModifierSet::log(mutablebson::Element logRoot) const {
// We'd like to create an entry such as {$set: {<fieldname>: <value>}} under 'logRoot'.
// We start by creating the {$set: ...} Element.
mutablebson::Document& doc = logRoot.getDocument();
mutablebson::Element setElement = doc.makeElementObject("$set");
if (!setElement.ok()) {
return Status(ErrorCodes::InternalError, "cannot create log entry for $set mod");
}
// Then we create the {<fieldname>: <value>} Element. Note that we log the mod with a
// dotted field, if it was applied over a dotted field. The rationale is that the
// secondary may be in a different state than the primary and thus make different
// decisions about creating the intermediate path in _fieldRef or not.
mutablebson::Element logElement = doc.makeElementWithNewFieldName(_fieldRef.dottedField(),
_val);
if (!logElement.ok()) {
return Status(ErrorCodes::InternalError, "cannot create details for $set mod");
}
// Now, we attach the {<fieldname>: <value>} Element under the {$set: ...} one.
Status status = setElement.pushBack(logElement);
if (!status.isOK()) {
return status;
}
// And attach the result under the 'logRoot' Element provided.
return logRoot.pushBack(setElement);
}
示例8: prepare
Status ModifierCurrentDate::prepare(mutablebson::Element root,
StringData matchedField,
ExecInfo* execInfo) {
_preparedState.reset(new PreparedState(root.getDocument()));
// If we have a $-positional field, it is time to bind it to an actual field part.
if (_pathReplacementPosition) {
if (matchedField.empty()) {
return Status(ErrorCodes::BadValue,
str::stream() << "The positional operator did not find the match "
"needed from the query. Unexpanded update: "
<< _updatePath.dottedField());
}
_updatePath.setPart(_pathReplacementPosition, matchedField);
}
// Locate the field name in 'root'. Note that we may not have all the parts in the path
// in the doc -- which is fine. Our goal now is merely to reason about whether this mod
// apply is a noOp or whether is can be in place. The remaining path, if missing, will
// be created during the apply.
Status status = pathsupport::findLongestPrefix(_updatePath,
root,
&_preparedState->idxFound,
&_preparedState->elemFound);
// FindLongestPrefix may say the path does not exist at all, which is fine here, or
// that the path was not viable or otherwise wrong, in which case, the mod cannot
// proceed.
if (status.code() == ErrorCodes::NonExistentPath) {
_preparedState->elemFound = root.getDocument().end();
}
else if (!status.isOK()) {
return status;
}
// We register interest in the field name. The driver needs this info to sort out if
// there is any conflict among mods.
execInfo->fieldRef[0] = &_updatePath;
return Status::OK();
}
示例9: log
Status ModifierAddToSet::log(mb::Element logRoot) const {
// TODO: This is copied more or less identically from $push. As a result, it copies the
// behavior in $push that relies on 'apply' having been called unless this is a no-op.
// TODO We can log just a positional set in several cases. For now, let's just log the
// full resulting array.
// We'd like to create an entry such as {$set: {<fieldname>: [<resulting aray>]}} under
// 'logRoot'. We start by creating the {$set: ...} Element.
mb::Document& doc = logRoot.getDocument();
mb::Element setElement = doc.makeElementObject("$set");
if (!setElement.ok()) {
return Status(ErrorCodes::InternalError, "cannot create log entry for $addToSet mod");
}
// Then we create the {<fieldname>:[]} Element, that is, an empty array.
mb::Element logElement = doc.makeElementArray(_fieldRef.dottedField());
if (!logElement.ok()) {
return Status(ErrorCodes::InternalError, "cannot create details for $addToSet mod");
}
// Fill up the empty array.
mb::Element curr = _preparedState->elemFound.leftChild();
while (curr.ok()) {
dassert(curr.hasValue());
// We need to copy each array entry from the resulting document to the log
// document.
mb::Element currCopy = doc.makeElementWithNewFieldName(
StringData(),
curr.getValue());
if (!currCopy.ok()) {
return Status(ErrorCodes::InternalError, "could create copy element");
}
Status status = logElement.pushBack(currCopy);
if (!status.isOK()) {
return Status(ErrorCodes::BadValue, "could not append entry for $addToSet log");
}
curr = curr.rightSibling();
}
// Now, we attach the {<fieldname>: [<filled array>]} Element under the {$set: ...}
// one.
Status status = setElement.pushBack(logElement);
if (!status.isOK()) {
return status;
}
// And attach the result under the 'logRoot' Element provided.
return logRoot.pushBack(setElement);
}
示例10: prepare
Status ModifierUnset::prepare(mutablebson::Element root,
const StringData& matchedField,
ExecInfo* execInfo) {
_preparedState.reset(new PreparedState(&root.getDocument()));
// If we have a $-positional field, it is time to bind it to an actual field part.
if (_posDollar) {
if (matchedField.empty()) {
return Status(ErrorCodes::BadValue,
str::stream() << "The positional operator did not find the match "
"needed from the query. Unexpanded update: "
<< _fieldRef.dottedField());
}
_preparedState->boundDollar = matchedField.toString();
_fieldRef.setPart(_posDollar, _preparedState->boundDollar);
}
// Locate the field name in 'root'. Note that if we don't have the full path in the
// doc, there isn't anything to unset, really.
Status status = pathsupport::findLongestPrefix(_fieldRef,
root,
&_preparedState->idxFound,
&_preparedState->elemFound);
if (!status.isOK() ||
_preparedState->idxFound != (_fieldRef.numParts() -1)) {
execInfo->noOp = _preparedState->noOp = true;
execInfo->fieldRef[0] = &_fieldRef;
return Status::OK();
}
// If there is indeed something to unset, we register so, along with the interest in
// the field name. The driver needs this info to sort out if there is any conflict
// among mods.
execInfo->fieldRef[0] = &_fieldRef;
// The only way for an $unset to be inplace is for its target field to be the last one
// of the object. That is, it is always the right child on its paths. The current
// rationale is that there should be no holes in a BSONObj and, to be in place, no
// field boundaries must change.
//
// TODO:
// mutablebson::Element curr = _preparedState->elemFound;
// while (curr.ok()) {
// if (curr.rightSibling().ok()) {
// }
// curr = curr.parent();
// }
return Status::OK();
}
示例11: getBSONForRole
Status AuthorizationManager::getBSONForRole(RoleGraph* graph,
const RoleName& roleName,
mutablebson::Element result) {
if (!graph->roleExists(roleName)) {
return Status(ErrorCodes::RoleNotFound,
mongoutils::str::stream() << roleName.getFullName() <<
"does not name an existing role");
}
std::string id = mongoutils::str::stream() << roleName.getDB() << "." << roleName.getRole();
result.appendString("_id", id);
result.appendString(ROLE_NAME_FIELD_NAME, roleName.getRole());
result.appendString(ROLE_SOURCE_FIELD_NAME, roleName.getDB());
// Build privileges array
mutablebson::Element privilegesArrayElement =
result.getDocument().makeElementArray("privileges");
result.pushBack(privilegesArrayElement);
const PrivilegeVector& privileges = graph->getDirectPrivileges(roleName);
Status status = getBSONForPrivileges(privileges, privilegesArrayElement);
if (!status.isOK()) {
return status;
}
// Build roles array
mutablebson::Element rolesArrayElement = result.getDocument().makeElementArray("roles");
result.pushBack(rolesArrayElement);
for (RoleNameIterator roles = graph->getDirectSubordinates(roleName);
roles.more();
roles.next()) {
const RoleName& subRole = roles.get();
mutablebson::Element roleObj = result.getDocument().makeElementObject("");
roleObj.appendString(ROLE_NAME_FIELD_NAME, subRole.getRole());
roleObj.appendString(ROLE_SOURCE_FIELD_NAME, subRole.getDB());
rolesArrayElement.pushBack(roleObj);
}
return Status::OK();
}
示例12: log
Status ModifierObjectReplace::log(mutablebson::Element logRoot) const {
// We'd like to create an entry such as {<object replacement>} under 'logRoot'.
mutablebson::Document& doc = logRoot.getDocument();
BSONObjIterator it(_val);
while (it.more()) {
BSONElement elem = it.next();
Status status = doc.root().appendElement(elem);
if (!status.isOK()) {
return status;
}
}
return Status::OK();
}
示例13: prepare
Status ModifierAddToSet::prepare(mb::Element root,
const StringData& matchedField,
ExecInfo* execInfo) {
_preparedState.reset(new PreparedState(root.getDocument()));
// If we have a $-positional field, it is time to bind it to an actual field part.
if (_posDollar) {
if (matchedField.empty()) {
return Status(ErrorCodes::BadValue, "matched field not provided");
}
_preparedState->boundDollar = matchedField.toString();
_fieldRef.setPart(_posDollar, _preparedState->boundDollar);
}
// Locate the field name in 'root'.
Status status = pathsupport::findLongestPrefix(_fieldRef,
root,
&_preparedState->idxFound,
&_preparedState->elemFound);
// FindLongestPrefix may say the path does not exist at all, which is fine here, or
// that the path was not viable or otherwise wrong, in which case, the mod cannot
// proceed.
if (status.code() == ErrorCodes::NonExistentPath) {
_preparedState->elemFound = root.getDocument().end();
} else if (!status.isOK()) {
return status;
}
// We register interest in the field name. The driver needs this info to sort out if
// there is any conflict among mods.
execInfo->fieldRef[0] = &_fieldRef;
//
// in-place and no-op logic
//
// If the field path is not fully present, then this mod cannot be in place, nor is a
// noOp.
if (!_preparedState->elemFound.ok() ||
_preparedState->idxFound < static_cast<int32_t>(_fieldRef.numParts() - 1)) {
// If no target element exists, we will simply be creating a new array.
_preparedState->addAll = true;
return Status::OK();
}
// This operation only applies to arrays
if (_preparedState->elemFound.getType() != mongo::Array)
return Status(
ErrorCodes::BadValue,
"Cannot apply $addToSet to a non-array value");
// If the array is empty, then we don't need to check anything: all of the values are
// going to be added.
if (!_preparedState->elemFound.hasChildren()) {
_preparedState->addAll = true;
return Status::OK();
}
// For each value in the $each clause, compare it against the values in the array. If
// the element is not present, record it as one to add.
mb::Element eachIter = _val.leftChild();
while (eachIter.ok()) {
mb::Element where = mb::findElement(
_preparedState->elemFound.leftChild(),
mb::woEqualTo(eachIter, false));
if (!where.ok()) {
// The element was not found. Record the element from $each as one to be added.
_preparedState->elementsToAdd.push_back(eachIter);
}
eachIter = eachIter.rightSibling();
}
// If we didn't find any elements to add, then this is a no-op, and therefore in place.
if (_preparedState->elementsToAdd.empty()) {
_preparedState->noOp = execInfo->noOp = true;
execInfo->inPlace = true;
}
return Status::OK();
}
示例14: prepare
Status ModifierPullAll::prepare(mutablebson::Element root,
StringData matchedField,
ExecInfo* execInfo) {
_preparedState.reset(new PreparedState(&root.getDocument()));
// If we have a $-positional field, it is time to bind it to an actual field part.
if (_positionalPathIndex) {
if (matchedField.empty()) {
return Status(ErrorCodes::BadValue,
str::stream() << "The positional operator did not find the match "
"needed from the query. Unexpanded update: "
<< _fieldRef.dottedField());
}
_fieldRef.setPart(_positionalPathIndex, matchedField);
}
// Locate the field name in 'root'. Note that if we don't have the full path in the
// doc, there isn't anything to unset, really.
Status status = pathsupport::findLongestPrefix(
_fieldRef, root, &_preparedState->pathFoundIndex, &_preparedState->pathFoundElement);
// Check if we didn't find the full path
if (status.isOK()) {
const bool destExists = (_preparedState->pathFoundIndex == (_fieldRef.numParts() - 1));
if (!destExists) {
execInfo->noOp = true;
} else {
// If the path exists, we require the target field to be already an
// array.
if (_preparedState->pathFoundElement.getType() != Array) {
mb::Element idElem = mb::findElementNamed(root.leftChild(), "_id");
return Status(
ErrorCodes::BadValue,
str::stream() << "Can only apply $pullAll to an array. " << idElem.toString()
<< " has the field "
<< _preparedState->pathFoundElement.getFieldName()
<< " of non-array type "
<< typeName(_preparedState->pathFoundElement.getType()));
}
// No children, nothing to do -- not an error state
if (!_preparedState->pathFoundElement.hasChildren()) {
execInfo->noOp = true;
} else {
mutablebson::Element elem = _preparedState->pathFoundElement.leftChild();
while (elem.ok()) {
if (std::find_if(_elementsToFind.begin(),
_elementsToFind.end(),
mutableElementEqualsBSONElement(elem, _collator)) !=
_elementsToFind.end()) {
_preparedState->elementsToRemove.push_back(elem);
}
elem = elem.rightSibling();
}
// Nothing to remove so it is a noOp.
if (_preparedState->elementsToRemove.empty())
execInfo->noOp = true;
}
}
} else {
// Let the caller know we can't do anything given the mod, _fieldRef, and doc.
execInfo->noOp = true;
// okay if path not found
if (status.code() == ErrorCodes::NonExistentPath)
status = Status::OK();
}
// Let the caller know what field we care about
execInfo->fieldRef[0] = &_fieldRef;
return status;
}
示例15: prepare
Status ModifierInc::prepare(mutablebson::Element root,
const StringData& matchedField,
ExecInfo* execInfo) {
_preparedState.reset(new PreparedState(root.getDocument()));
// If we have a $-positional field, it is time to bind it to an actual field part.
if (_posDollar) {
if (matchedField.empty()) {
return Status(ErrorCodes::BadValue, "matched field not provided");
}
_preparedState->boundDollar = matchedField.toString();
_fieldRef.setPart(_posDollar, _preparedState->boundDollar);
}
// Locate the field name in 'root'. Note that we may not have all the parts in the path
// in the doc -- which is fine. Our goal now is merely to reason about whether this mod
// apply is a noOp or whether is can be in place. The remaining path, if missing, will
// be created during the apply.
Status status = pathsupport::findLongestPrefix(_fieldRef,
root,
&_preparedState->idxFound,
&_preparedState->elemFound);
// FindLongestPrefix may say the path does not exist at all, which is fine here, or
// that the path was not viable or otherwise wrong, in which case, the mod cannot
// proceed.
if (status.code() == ErrorCodes::NonExistentPath) {
_preparedState->elemFound = root.getDocument().end();
}
else if (!status.isOK()) {
return status;
}
// We register interest in the field name. The driver needs this info to sort out if
// there is any conflict among mods.
execInfo->fieldRef[0] = &_fieldRef;
// Capture the value we are going to write. At this point, there may not be a value
// against which to operate, so the result will be simply _val.
_preparedState->newValue = _val;
//
// in-place and no-op logic
//
// If the field path is not fully present, then this mod cannot be in place, nor is a
// noOp.
if (!_preparedState->elemFound.ok() ||
_preparedState->idxFound < static_cast<int32_t>(_fieldRef.numParts() - 1)) {
return Status::OK();
}
// If the value being $inc'ed is the same as the one already in the doc, than this is a
// noOp.
if (!_preparedState->elemFound.isNumeric())
return Status(ErrorCodes::BadValue,
"invalid attempt to increment a non-numeric field");
const SafeNum currentValue = _preparedState->elemFound.getValueSafeNum();
// Update newValue w.r.t to the current value of the found element.
_preparedState->newValue += currentValue;
// If the result of the addition is invalid, we must return an error.
if (!_preparedState->newValue.isValid())
return Status(ErrorCodes::BadValue,
"Failed to increment current value");
// If the values are identical (same type, same value), then this is a no-op, and
// therefore in-place as well.
if (_preparedState->newValue.isIdentical(currentValue)) {
_preparedState->noOp = execInfo->noOp = true;
_preparedState->inPlace = execInfo->inPlace = true;
return Status::OK();
}
// If the types are the same, this can be done in place.
//
// TODO: Potentially, cases where $inc results in a mixed type of the same size could
// be in-place as well, but we don't currently handle them.
if (_preparedState->newValue.type() == currentValue.type()) {
_preparedState->inPlace = execInfo->inPlace = true;
}
return Status::OK();
}