summaryrefslogtreecommitdiff
path: root/src/mongo/db/update/path_support.h
diff options
context:
space:
mode:
authorTed Tuckman <ted.tuckman@mongodb.com>2020-10-06 10:50:17 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-10-15 14:14:00 +0000
commit16b59bd6236a90ad3d807a66ec2fe529e09cf0ec (patch)
treec7e2025f51fde1118f9dd997fcfa81769ecd6683 /src/mongo/db/update/path_support.h
parent122e9e8fe5db8f4b1bfcc2f30e2f574045d19547 (diff)
downloadmongo-16b59bd6236a90ad3d807a66ec2fe529e09cf0ec.tar.gz
SERVER-50778 Compare array indexes numerically for updates
Diffstat (limited to 'src/mongo/db/update/path_support.h')
-rw-r--r--src/mongo/db/update/path_support.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/mongo/db/update/path_support.h b/src/mongo/db/update/path_support.h
index 808f2df5dff..c6df4afe3c8 100644
--- a/src/mongo/db/update/path_support.h
+++ b/src/mongo/db/update/path_support.h
@@ -32,6 +32,7 @@
#include <cstdint>
#include <string>
+#include "mongo/base/parse_number.h"
#include "mongo/base/status.h"
#include "mongo/bson/mutable/element.h"
#include "mongo/db/field_ref.h"
@@ -50,6 +51,27 @@ static const size_t kMaxPaddingAllowed = 1500000;
// Convenience type to hold equality matches at particular paths from a MatchExpression
typedef std::map<StringData, const EqualityMatchExpression*> EqualityMatches;
+struct cmpPathsAndArrayIndexes {
+ // Assumes paths are valid.
+ bool operator()(const std::string& a, const std::string& b) const {
+ NumberParser parser;
+ long numA;
+ long numB;
+ auto status = parser(a, &numA);
+ if (!status.isOK())
+ return a < b;
+ status = parser(b, &numB);
+ if (!status.isOK())
+ return a < b;
+ // If the numbers are the same, and the strings are the same, 'numA' < 'numB' is equivalent
+ // to 'a' < 'b'. However, the strings could still be different (for example, "1" and "01")
+ // and we need to treat them differently.
+ if (numA == numB)
+ return a < b;
+ return numA < numB;
+ }
+};
+
/**
* Finds the longest portion of 'prefix' that exists in document rooted at 'root' and is
* "viable." A viable path is one that, if fully created on a given doc, would not