Skip to content

Commit 20d3d28

Browse files
committed
Hidden indexes
JAVA-3717
1 parent 4d90099 commit 20d3d28

File tree

9 files changed

+89
-2
lines changed

9 files changed

+89
-2
lines changed

driver-core/src/main/com/mongodb/client/model/IndexOptions.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class IndexOptions {
4747
private Bson partialFilterExpression;
4848
private Collation collation;
4949
private Bson wildcardProjection;
50+
private boolean hidden;
5051

5152
/**
5253
* Create the index in the background
@@ -491,6 +492,30 @@ public IndexOptions wildcardProjection(final Bson wildcardProjection) {
491492
return this;
492493
}
493494

495+
/**
496+
* Gets whether the index should not be used by the query planner when executing operations.
497+
*
498+
* @return true if the index should not be used by the query planner when executing operations.
499+
* @mongodb.server.release 4.4
500+
* @since 4.1
501+
*/
502+
public boolean isHidden() {
503+
return hidden;
504+
}
505+
506+
/**
507+
* Should the index not be used by the query planner when executing operations.
508+
*
509+
* @param hidden true if the index should be hidden
510+
* @return this
511+
* @mongodb.server.release 4.4
512+
* @since 4.1
513+
*/
514+
public IndexOptions hidden(final boolean hidden) {
515+
this.hidden = hidden;
516+
return this;
517+
}
518+
494519
@Override
495520
public String toString() {
496521
return "IndexOptions{"
@@ -513,6 +538,7 @@ public String toString() {
513538
+ ", partialFilterExpression=" + partialFilterExpression
514539
+ ", collation=" + collation
515540
+ ", wildcardProjection=" + wildcardProjection
541+
+ ", hidden=" + hidden
516542
+ '}';
517543
}
518544
}

driver-core/src/main/com/mongodb/internal/bulk/IndexRequest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class IndexRequest {
5656
private BsonDocument partialFilterExpression;
5757
private Collation collation;
5858
private BsonDocument wildcardProjection;
59+
private boolean hidden;
5960

6061
/**
6162
* Construct a new instance with the given keys
@@ -536,4 +537,26 @@ public IndexRequest wildcardProjection(final BsonDocument wildcardProjection) {
536537
this.wildcardProjection = wildcardProjection;
537538
return this;
538539
}
540+
541+
/**
542+
* Gets if the index should exist on the target collection but should not be used by the query
543+
* planner when executing operations.
544+
*
545+
* @return true if the index should not be used by the query planner when executing operations.
546+
*/
547+
public boolean isHidden() {
548+
return hidden;
549+
}
550+
551+
/**
552+
* Should the index exist on the target collection but should not be used by the query
553+
* planner when executing operations.
554+
*
555+
* @param hidden true if the index should be hidden
556+
* @return this
557+
*/
558+
public IndexRequest hidden(final boolean hidden) {
559+
this.hidden = hidden;
560+
return this;
561+
}
539562
}

driver-core/src/main/com/mongodb/internal/operation/CreateIndexesOperation.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ private BsonDocument getIndex(final IndexRequest request) {
302302
if (request.getWildcardProjection() != null) {
303303
index.append("wildcardProjection", request.getWildcardProjection());
304304
}
305+
if (request.isHidden()) {
306+
index.append("hidden", BsonBoolean.TRUE);
307+
}
305308
return index;
306309
}
307310

driver-core/src/main/com/mongodb/internal/operation/Operations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ CreateIndexesOperation createIndexes(final List<IndexModel> indexes, final Creat
479479
.partialFilterExpression(toBsonDocument(model.getOptions().getPartialFilterExpression()))
480480
.collation(model.getOptions().getCollation())
481481
.wildcardProjection(toBsonDocument(model.getOptions().getWildcardProjection()))
482+
.hidden(model.getOptions().isHidden())
482483
);
483484
}
484485
return new CreateIndexesOperation(namespace, indexRequests, writeConcern)

driver-core/src/test/functional/com/mongodb/internal/operation/CreateIndexesOperationSpecification.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,30 @@ class CreateIndexesOperationSpecification extends OperationFunctionalSpecificati
560560
async << [true, false]
561561
}
562562

563+
@IgnoreIf({ !serverVersionAtLeast(4, 4) })
564+
def 'should be able to set hidden index'() {
565+
given:
566+
def operation = new CreateIndexesOperation(getNamespace(), [new IndexRequest(new BsonDocument('field', new BsonInt32(1)))])
567+
568+
when:
569+
execute(operation, async)
570+
571+
then:
572+
getUserCreatedIndexes('hidden').size() == 0
573+
574+
when:
575+
getCollectionHelper().drop(getNamespace())
576+
operation = new CreateIndexesOperation(getNamespace(),
577+
[new IndexRequest(new BsonDocument('field', new BsonInt32(1))).hidden(true)])
578+
execute(operation, async)
579+
580+
then:
581+
getUserCreatedIndexes('hidden').size() == 1
582+
583+
where:
584+
async << [true, false]
585+
}
586+
563587
Document getIndex(final String indexName) {
564588
getIndexes().find {
565589
it -> it.getString('name') == indexName

driver-core/src/test/unit/com/mongodb/IndexRequestSpecification.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class IndexRequestSpecification extends Specification {
5656
request.getPartialFilterExpression() == null
5757
request.getCollation() == null
5858
request.getWildcardProjection() == null
59+
!request.isHidden()
5960

6061
when:
6162
def keys = BsonDocument.parse('{ a: 1 }')
@@ -94,6 +95,7 @@ class IndexRequestSpecification extends Specification {
9495
.partialFilterExpression(partialFilterExpression)
9596
.collation(collation)
9697
.wildcardProjection(wildcardProjection)
98+
.hidden(true)
9799

98100
then:
99101
request2.getKeys() == keys
@@ -117,6 +119,7 @@ class IndexRequestSpecification extends Specification {
117119
request2.getPartialFilterExpression() == partialFilterExpression
118120
request2.getCollation() == collation
119121
request2.getWildcardProjection() == wildcardProjection
122+
request2.isHidden()
120123
}
121124

122125

driver-core/src/test/unit/com/mongodb/client/model/IndexOptionsSpecification.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class IndexOptionsSpecification extends Specification {
4747
options.getPartialFilterExpression() == null
4848
options.getCollation() == null
4949
options.getWildcardProjection() == null
50+
!options.isHidden()
5051
def wildcardProjection = BsonDocument.parse('{a : 1}')
5152

5253
when:
@@ -73,6 +74,7 @@ class IndexOptionsSpecification extends Specification {
7374
.partialFilterExpression(partialFilterExpression)
7475
.collation(collation)
7576
.wildcardProjection(wildcardProjection)
77+
.hidden(true)
7678

7779
then:
7880
options.isBackground()
@@ -94,6 +96,7 @@ class IndexOptionsSpecification extends Specification {
9496
options.getPartialFilterExpression() == partialFilterExpression
9597
options.getCollation() == collation
9698
options.getWildcardProjection() == wildcardProjection
99+
options.isHidden()
97100
}
98101

99102
def 'should convert expireAfter'() {

driver-core/src/test/unit/com/mongodb/internal/async/client/AsyncMongoCollectionSpecification.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,7 @@ class AsyncMongoCollectionSpecification extends Specification {
12001200
.partialFilterExpression(BsonDocument.parse('{status: "active"}'))
12011201
.collation(collation)
12021202
.wildcardProjection(new BsonDocument('a', new BsonInt32(1)))
1203+
.hidden(true)
12031204
], ACKNOWLEDGED)
12041205
indexName = execute(createIndexMethod, session, new Document('key', 1), new IndexOptions()
12051206
.background(true)
@@ -1220,7 +1221,8 @@ class AsyncMongoCollectionSpecification extends Specification {
12201221
.storageEngine(BsonDocument.parse('{wiredTiger: {configString: "block_compressor=zlib"}}'))
12211222
.partialFilterExpression(BsonDocument.parse('{status: "active"}'))
12221223
.collation(collation)
1223-
.wildcardProjection(new BsonDocument('a', new BsonInt32(1))))
1224+
.wildcardProjection(new BsonDocument('a', new BsonInt32(1)))
1225+
.hidden(true))
12241226
operation = executor.getWriteOperation() as CreateIndexesOperation
12251227

12261228
then:

driver-sync/src/test/unit/com/mongodb/client/internal/MongoCollectionSpecification.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,7 @@ class MongoCollectionSpecification extends Specification {
12131213
.partialFilterExpression(BsonDocument.parse('{status: "active"}'))
12141214
.collation(collation)
12151215
.wildcardProjection(new BsonDocument('a', new BsonInt32(1)))
1216+
.hidden(true)
12161217
], ACKNOWLEDGED)
12171218
indexName = execute(createIndexMethod, session, new Document('key', 1), new IndexOptions()
12181219
.background(true)
@@ -1233,7 +1234,8 @@ class MongoCollectionSpecification extends Specification {
12331234
.storageEngine(BsonDocument.parse('{wiredTiger: {configString: "block_compressor=zlib"}}'))
12341235
.partialFilterExpression(BsonDocument.parse('{status: "active"}'))
12351236
.collation(collation)
1236-
.wildcardProjection(new BsonDocument('a', new BsonInt32(1))))
1237+
.wildcardProjection(new BsonDocument('a', new BsonInt32(1)))
1238+
.hidden(true))
12371239
operation = executor.getWriteOperation() as CreateIndexesOperation
12381240

12391241
then:

0 commit comments

Comments
 (0)