summaryrefslogtreecommitdiff
path: root/src/mongo/db/keypattern.cpp
diff options
context:
space:
mode:
authorJames Wahlin <james.wahlin@10gen.com>2016-12-19 14:02:02 -0500
committerJames Wahlin <james.wahlin@10gen.com>2016-12-27 16:22:21 -0500
commitac7b0469bdb57a3593dc8b97e5a7045db0efbb24 (patch)
treec759fe0f6c4dff381ac08944f603851db6492c16 /src/mongo/db/keypattern.cpp
parent72112a72ec3ee48cb883fc02b2904079d11f4954 (diff)
downloadmongo-ac7b0469bdb57a3593dc8b97e5a7045db0efbb24.tar.gz
SERVER-27175 Improve performance of planSummary string generation
Diffstat (limited to 'src/mongo/db/keypattern.cpp')
-rw-r--r--src/mongo/db/keypattern.cpp30
1 files changed, 29 insertions, 1 deletions
diff --git a/src/mongo/db/keypattern.cpp b/src/mongo/db/keypattern.cpp
index 2c1bf09497d..ffc2a1f8e71 100644
--- a/src/mongo/db/keypattern.cpp
+++ b/src/mongo/db/keypattern.cpp
@@ -31,7 +31,6 @@
#include "mongo/db/keypattern.h"
#include "mongo/db/index_names.h"
-#include "mongo/util/mongoutils/str.h"
namespace mongo {
@@ -55,6 +54,35 @@ bool KeyPattern::isHashedKeyPattern(const BSONObj& pattern) {
return IndexNames::HASHED == IndexNames::findPluginName(pattern);
}
+StringBuilder& operator<<(StringBuilder& sb, const KeyPattern& keyPattern) {
+ // Rather than return BSONObj::toString() we construct a keyPattern string manually. This allows
+ // us to avoid the cost of writing numeric direction to the str::stream which will then undergo
+ // expensive number to string conversion.
+ sb << "{ ";
+
+ bool first = true;
+ for (auto&& elem : keyPattern._pattern) {
+ if (first) {
+ first = false;
+ } else {
+ sb << ", ";
+ }
+
+ if (mongo::String == elem.type()) {
+ sb << elem;
+ } else if (elem.number() >= 0) {
+ // The canonical check as to whether a key pattern element is "ascending" or
+ // "descending" is (elem.number() >= 0). This is defined by the Ordering class.
+ sb << elem.fieldNameStringData() << ": 1";
+ } else {
+ sb << elem.fieldNameStringData() << ": -1";
+ }
+ }
+
+ sb << " }";
+ return sb;
+}
+
BSONObj KeyPattern::extendRangeBound(const BSONObj& bound, bool makeUpperInclusive) const {
BSONObjBuilder newBound(bound.objsize());