summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/expression_context.h
diff options
context:
space:
mode:
authorCharlie Swanson <charlie.swanson@mongodb.com>2016-12-13 10:15:08 -0500
committerCharlie Swanson <charlie.swanson@mongodb.com>2016-12-16 16:24:32 -0500
commit37e720678f6e468726c6cc775a5dc898d080f0f3 (patch)
tree4bd6b4932cc0ac436c0d7c949f7e37df613684d2 /src/mongo/db/pipeline/expression_context.h
parent0cd2bf29d5798a395a07e67ae79ede9a5cefd411 (diff)
downloadmongo-37e720678f6e468726c6cc775a5dc898d080f0f3.tar.gz
SERVER-25535 Remove injectExpressionContext().
These methods were formally used to propagate a new ExpressionContext to stages, accumulators, or expressions which potentially needed to comparisons. Originally, this was necessary since Pipeline parsing happened outside of the collection lock and thus could not determine if there was a default collation on the collection. This meant that the collation could change after parsing and any operators that might compare strings would need to know about it. We have since moved parsing within the lock, so the collation can be known at parse time and the ExpressionContext should not change. This patch requires an ExpressionContext at construction time, and disallows changing the collation on an ExpressionContext.
Diffstat (limited to 'src/mongo/db/pipeline/expression_context.h')
-rw-r--r--src/mongo/db/pipeline/expression_context.h52
1 files changed, 40 insertions, 12 deletions
diff --git a/src/mongo/db/pipeline/expression_context.h b/src/mongo/db/pipeline/expression_context.h
index 67114fa52b0..af6d7d199c9 100644
--- a/src/mongo/db/pipeline/expression_context.h
+++ b/src/mongo/db/pipeline/expression_context.h
@@ -45,7 +45,7 @@
namespace mongo {
-struct ExpressionContext : public IntrusiveCounterUnsigned {
+class ExpressionContext : public RefCountable {
public:
struct ResolvedNamespace {
ResolvedNamespace() = default;
@@ -55,9 +55,14 @@ public:
std::vector<BSONObj> pipeline;
};
- ExpressionContext() = default;
-
- ExpressionContext(OperationContext* opCtx, const AggregationRequest& request);
+ /**
+ * Constructs an ExpressionContext to be used for Pipeline parsing and evaluation.
+ * 'resolvedNamespaces' maps collection names (not full namespaces) to ResolvedNamespaces.
+ */
+ ExpressionContext(OperationContext* opCtx,
+ const AggregationRequest& request,
+ std::unique_ptr<CollatorInterface> collator,
+ StringMap<ExpressionContext::ResolvedNamespace> resolvedNamespaces);
/**
* Used by a pipeline to check for interrupts so that killOp() works. Throws a UserAssertion if
@@ -65,8 +70,6 @@ public:
*/
void checkForInterrupt();
- void setCollator(std::unique_ptr<CollatorInterface> coll);
-
const CollatorInterface* getCollator() const {
return _collator.get();
}
@@ -85,6 +88,16 @@ public:
*/
boost::intrusive_ptr<ExpressionContext> copyWith(NamespaceString ns) const;
+ /**
+ * Returns the ResolvedNamespace corresponding to 'nss'. It is an error to call this method on a
+ * namespace not involved in the pipeline.
+ */
+ const ResolvedNamespace& getResolvedNamespace(const NamespaceString& nss) const {
+ auto it = _resolvedNamespaces.find(nss.coll());
+ invariant(it != _resolvedNamespaces.end());
+ return it->second;
+ };
+
bool isExplain = false;
bool inShard = false;
bool inRouter = false;
@@ -100,19 +113,34 @@ public:
// collation.
BSONObj collation;
- StringMap<ResolvedNamespace> resolvedNamespaces;
-
+protected:
static const int kInterruptCheckPeriod = 128;
- int interruptCounter = kInterruptCheckPeriod; // when 0, check interruptStatus
-private:
- // Collator used to compare elements. 'collator' is initialized from 'collation', except in the
- // case where 'collation' is empty and there is a collection default collation.
+ /**
+ * Should only be used by 'ExpressionContextForTest'.
+ */
+ ExpressionContext() = default;
+
+ /**
+ * Sets '_collator' and resets '_documentComparator' and '_valueComparator'.
+ *
+ * Use with caution - it is illegal to change the collation once a Pipeline has been parsed with
+ * this ExpressionContext.
+ */
+ void setCollator(std::unique_ptr<CollatorInterface> collator);
+
+ // Collator used for comparisons.
std::unique_ptr<CollatorInterface> _collator;
// Used for all comparisons of Document/Value during execution of the aggregation operation.
+ // Must not be changed after parsing a Pipeline with this ExpressionContext.
DocumentComparator _documentComparator;
ValueComparator _valueComparator;
+
+ // A map from namespace to the resolved namespace, in case any views are involved.
+ StringMap<ResolvedNamespace> _resolvedNamespaces;
+
+ int _interruptCounter = kInterruptCheckPeriod;
};
} // namespace mongo