summaryrefslogtreecommitdiff
path: root/jstests/core/wildcard_index_filter.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/core/wildcard_index_filter.js')
-rw-r--r--jstests/core/wildcard_index_filter.js145
1 files changed, 76 insertions, 69 deletions
diff --git a/jstests/core/wildcard_index_filter.js b/jstests/core/wildcard_index_filter.js
index 74c81edf462..fc1f1efdc6f 100644
--- a/jstests/core/wildcard_index_filter.js
+++ b/jstests/core/wildcard_index_filter.js
@@ -6,88 +6,95 @@
* @tags: [does_not_support_stepdowns]
*/
(function() {
- "use strict";
-
- load("jstests/libs/analyze_plan.js");
-
- const coll = db.wildcard_index_filter;
-
- // Utility function to list index filters.
- function getFilters() {
- const res = assert.commandWorked(coll.runCommand('planCacheListFilters'));
- assert(res.hasOwnProperty('filters'), 'filters missing from planCacheListFilters result');
- return res.filters;
+"use strict";
+
+load("jstests/libs/analyze_plan.js");
+
+const coll = db.wildcard_index_filter;
+
+// Utility function to list index filters.
+function getFilters() {
+ const res = assert.commandWorked(coll.runCommand('planCacheListFilters'));
+ assert(res.hasOwnProperty('filters'), 'filters missing from planCacheListFilters result');
+ return res.filters;
+}
+
+// Sets an index filter given a query shape then confirms that the expected index was used to
+// answer a query.
+function assertExpectedIndexAnswersQueryWithFilter(
+ filterQuery, filterIndexes, query, expectedIndexName, hint) {
+ // Clear existing cache filters.
+ assert.commandWorked(coll.runCommand('planCacheClearFilters'), 'planCacheClearFilters failed');
+
+ // Make sure that the filter is set correctly.
+ assert.commandWorked(
+ coll.runCommand('planCacheSetFilter', {query: filterQuery, indexes: filterIndexes}));
+ assert.eq(1,
+ getFilters().length,
+ 'no change in query settings after successfully setting index filters');
+
+ // Check that expectedIndex index was used over another index.
+ let explain;
+ if (hint == undefined) {
+ explain = assert.commandWorked(coll.explain("executionStats").find(query).finish());
+ } else {
+ explain =
+ assert.commandWorked(coll.explain("executionStats").find(query).hint(hint).finish());
}
- // Sets an index filter given a query shape then confirms that the expected index was used to
- // answer a query.
- function assertExpectedIndexAnswersQueryWithFilter(
- filterQuery, filterIndexes, query, expectedIndexName, hint) {
- // Clear existing cache filters.
- assert.commandWorked(coll.runCommand('planCacheClearFilters'),
- 'planCacheClearFilters failed');
-
- // Make sure that the filter is set correctly.
- assert.commandWorked(
- coll.runCommand('planCacheSetFilter', {query: filterQuery, indexes: filterIndexes}));
- assert.eq(1,
- getFilters().length,
- 'no change in query settings after successfully setting index filters');
-
- // Check that expectedIndex index was used over another index.
- let explain;
- if (hint == undefined) {
- explain = assert.commandWorked(coll.explain("executionStats").find(query).finish());
- } else {
- explain = assert.commandWorked(
- coll.explain("executionStats").find(query).hint(hint).finish());
- }
-
- const executionStages = getExecutionStages(explain).shift();
- let planStage = getPlanStage(executionStages, 'IXSCAN');
- assert.neq(null, planStage);
- assert.eq(planStage.indexName, expectedIndexName, tojson(planStage));
- }
+ const executionStages = getExecutionStages(explain).shift();
+ let planStage = getPlanStage(executionStages, 'IXSCAN');
+ assert.neq(null, planStage);
+ assert.eq(planStage.indexName, expectedIndexName, tojson(planStage));
+}
- const indexWildcard = {"$**": 1};
- const indexA = {"a": 1};
- assert.commandWorked(coll.createIndex(indexWildcard));
- assert.commandWorked(coll.createIndex(indexA));
+const indexWildcard = {
+ "$**": 1
+};
+const indexA = {
+ "a": 1
+};
+assert.commandWorked(coll.createIndex(indexWildcard));
+assert.commandWorked(coll.createIndex(indexA));
- assert.commandWorked(coll.insert({a: "a"}));
+assert.commandWorked(coll.insert({a: "a"}));
- // Filtering on $** index. $** index is used over another index.
- assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexWildcard], {a: "a"}, "$**_1");
+// Filtering on $** index. $** index is used over another index.
+assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexWildcard], {a: "a"}, "$**_1");
- // Filtering on regular index. $** index is not used over another index.
- assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexA], {a: "a"}, "a_1");
+// Filtering on regular index. $** index is not used over another index.
+assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexA], {a: "a"}, "a_1");
- assert.commandWorked(coll.insert({a: "a", b: "b"}));
+assert.commandWorked(coll.insert({a: "a", b: "b"}));
- const indexAB = {"a": 1, "b": 1};
- assert.commandWorked(coll.createIndex(indexAB));
+const indexAB = {
+ "a": 1,
+ "b": 1
+};
+assert.commandWorked(coll.createIndex(indexAB));
- // Filtering on $** index. $** index is used over another index for compound query.
- assertExpectedIndexAnswersQueryWithFilter(
- {a: "a", b: "b"}, [indexWildcard], {a: "a", b: "b"}, "$**_1");
+// Filtering on $** index. $** index is used over another index for compound query.
+assertExpectedIndexAnswersQueryWithFilter(
+ {a: "a", b: "b"}, [indexWildcard], {a: "a", b: "b"}, "$**_1");
- // Filtering on regular compound index. Check that $** index is not used over another index
- // for compound query.
- assertExpectedIndexAnswersQueryWithFilter(
- {a: "a", b: "b"}, [indexAB], {a: "a", b: "b"}, "a_1_b_1");
+// Filtering on regular compound index. Check that $** index is not used over another index
+// for compound query.
+assertExpectedIndexAnswersQueryWithFilter({a: "a", b: "b"}, [indexAB], {a: "a", b: "b"}, "a_1_b_1");
- // Filtering on $** index while hinting on another index. Index filter is prioritized.
- assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexWildcard], {a: "a"}, "$**_1", indexA);
+// Filtering on $** index while hinting on another index. Index filter is prioritized.
+assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexWildcard], {a: "a"}, "$**_1", indexA);
- // Filtering on regular index while hinting on $** index. Index filter is prioritized.
- assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexA], {a: "a"}, "a_1", indexWildcard);
+// Filtering on regular index while hinting on $** index. Index filter is prioritized.
+assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexA], {a: "a"}, "a_1", indexWildcard);
- // Index filter for $** index does not apply when query does not match filter query shape.
- assertExpectedIndexAnswersQueryWithFilter({b: "b"}, [indexWildcard], {a: "a"}, "a_1", indexA);
+// Index filter for $** index does not apply when query does not match filter query shape.
+assertExpectedIndexAnswersQueryWithFilter({b: "b"}, [indexWildcard], {a: "a"}, "a_1", indexA);
- const indexAWildcard = {"a.$**": 1};
- assert.commandWorked(coll.createIndex(indexAWildcard));
+const indexAWildcard = {
+ "a.$**": 1
+};
+assert.commandWorked(coll.createIndex(indexAWildcard));
- // Filtering on a path specified $** index. Check that the $** is used over other indices.
- assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexAWildcard], {a: "a"}, "a.$**_1");
+// Filtering on a path specified $** index. Check that the $** is used over other indices.
+assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexAWildcard], {a: "a"}, "a.$**_1");
})(); \ No newline at end of file