diff options
Diffstat (limited to 'src/mongo/db/catalog/index_key_validate.cpp')
-rw-r--r-- | src/mongo/db/catalog/index_key_validate.cpp | 112 |
1 files changed, 55 insertions, 57 deletions
diff --git a/src/mongo/db/catalog/index_key_validate.cpp b/src/mongo/db/catalog/index_key_validate.cpp index c42c2a1921f..d29c4d93d92 100644 --- a/src/mongo/db/catalog/index_key_validate.cpp +++ b/src/mongo/db/catalog/index_key_validate.cpp @@ -35,80 +35,78 @@ namespace mongo { - using std::string; +using std::string; - Status validateKeyPattern(const BSONObj& key) { - const ErrorCodes::Error code = ErrorCodes::CannotCreateIndex; +Status validateKeyPattern(const BSONObj& key) { + const ErrorCodes::Error code = ErrorCodes::CannotCreateIndex; - if ( key.objsize() > 2048 ) - return Status(code, "Index key pattern too large."); + if (key.objsize() > 2048) + return Status(code, "Index key pattern too large."); - if ( key.isEmpty() ) - return Status(code, "Index keys cannot be empty."); + if (key.isEmpty()) + return Status(code, "Index keys cannot be empty."); - string pluginName = IndexNames::findPluginName( key ); - if ( pluginName.size() ) { - if ( !IndexNames::isKnownName( pluginName ) ) - return Status(code, - mongoutils::str::stream() << "Unknown index plugin '" - << pluginName << '\''); - } + string pluginName = IndexNames::findPluginName(key); + if (pluginName.size()) { + if (!IndexNames::isKnownName(pluginName)) + return Status( + code, mongoutils::str::stream() << "Unknown index plugin '" << pluginName << '\''); + } - BSONObjIterator it( key ); - while ( it.more() ) { - BSONElement keyElement = it.next(); + BSONObjIterator it(key); + while (it.more()) { + BSONElement keyElement = it.next(); - if( keyElement.type() == Object || keyElement.type() == Array ) - return Status(code, "Index keys cannot be Objects or Arrays."); + if (keyElement.type() == Object || keyElement.type() == Array) + return Status(code, "Index keys cannot be Objects or Arrays."); - if ( keyElement.type() == String && pluginName != keyElement.str() ) { - return Status(code, "Can't use more than one index plugin for a single index."); - } + if (keyElement.type() == String && pluginName != keyElement.str()) { + return Status(code, "Can't use more than one index plugin for a single index."); + } - // Ensure that the fields on which we are building the index are valid: a field must not - // begin with a '$' unless it is part of a DBRef or text index, and a field path cannot - // contain an empty field. If a field cannot be created or updated, it should not be - // indexable. + // Ensure that the fields on which we are building the index are valid: a field must not + // begin with a '$' unless it is part of a DBRef or text index, and a field path cannot + // contain an empty field. If a field cannot be created or updated, it should not be + // indexable. - FieldRef keyField( keyElement.fieldName() ); + FieldRef keyField(keyElement.fieldName()); - const size_t numParts = keyField.numParts(); - if ( numParts == 0 ) { - return Status(code, "Index keys cannot be an empty field."); - } + const size_t numParts = keyField.numParts(); + if (numParts == 0) { + return Status(code, "Index keys cannot be an empty field."); + } - // "$**" is acceptable for a text index. - if ( mongoutils::str::equals( keyElement.fieldName(), "$**" ) && - keyElement.valuestrsafe() == IndexNames::TEXT ) - continue; + // "$**" is acceptable for a text index. + if (mongoutils::str::equals(keyElement.fieldName(), "$**") && + keyElement.valuestrsafe() == IndexNames::TEXT) + continue; - for ( size_t i = 0; i != numParts; ++i ) { - const StringData part = keyField.getPart(i); + for (size_t i = 0; i != numParts; ++i) { + const StringData part = keyField.getPart(i); - // Check if the index key path contains an empty field. - if ( part.empty() ) { - return Status(code, "Index keys cannot contain an empty field."); - } + // Check if the index key path contains an empty field. + if (part.empty()) { + return Status(code, "Index keys cannot contain an empty field."); + } - if ( part[0] != '$' ) - continue; + if (part[0] != '$') + continue; - // Check if the '$'-prefixed field is part of a DBRef: since we don't have the - // necessary context to validate whether this is a proper DBRef, we allow index - // creation on '$'-prefixed names that match those used in a DBRef. - const bool mightBePartOfDbRef = (i != 0) && - (part == "$db" || - part == "$id" || - part == "$ref"); + // Check if the '$'-prefixed field is part of a DBRef: since we don't have the + // necessary context to validate whether this is a proper DBRef, we allow index + // creation on '$'-prefixed names that match those used in a DBRef. + const bool mightBePartOfDbRef = + (i != 0) && (part == "$db" || part == "$id" || part == "$ref"); - if ( !mightBePartOfDbRef ) { - return Status(code, "Index key contains an illegal field name: " - "field name starts with '$'."); - } + if (!mightBePartOfDbRef) { + return Status(code, + "Index key contains an illegal field name: " + "field name starts with '$'."); } } - - return Status::OK(); } -} // namespace mongo + + return Status::OK(); +} +} // namespace mongo |