-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add index hint support for distinct command #1581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -217,13 +217,14 @@ private <TResult> FindOperation<TResult> createFindOperation(final MongoNamespac | |||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
<TResult> DistinctOperation<TResult> distinct(final String fieldName, @Nullable final Bson filter, final Class<TResult> resultClass, | ||||||||||||||||||||||||||||
final Collation collation, final BsonValue comment) { | ||||||||||||||||||||||||||||
final Collation collation, final BsonValue comment, @Nullable final Bson hint, @Nullable final String hintString) { | ||||||||||||||||||||||||||||
return new DistinctOperation<>(assertNotNull(namespace), | ||||||||||||||||||||||||||||
fieldName, codecRegistry.get(resultClass)) | ||||||||||||||||||||||||||||
.retryReads(retryReads) | ||||||||||||||||||||||||||||
.filter(filter == null ? null : filter.toBsonDocument(documentClass, codecRegistry)) | ||||||||||||||||||||||||||||
.collation(collation) | ||||||||||||||||||||||||||||
.comment(comment); | ||||||||||||||||||||||||||||
.comment(comment) | ||||||||||||||||||||||||||||
.hint(hint != null ? toBsonDocument(hint) : (hintString != null ? new BsonString(hintString) : null)); | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is fine to implement via one-liner, but I think the similar way in the above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the suggestion, will update! |
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
<TResult> AggregateOperation<TResult> aggregate(final List<? extends Bson> pipeline, final Class<TResult> resultClass, | ||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
{ | ||
"description": "distinct-hint", | ||
"schemaVersion": "1.0", | ||
"runOnRequirements": [ | ||
{ | ||
"minServerVersion": "7.1.0" | ||
} | ||
], | ||
"createEntities": [ | ||
{ | ||
"client": { | ||
"id": "client0", | ||
"observeEvents": [ | ||
"commandStartedEvent" | ||
] | ||
} | ||
}, | ||
{ | ||
"database": { | ||
"id": "database0", | ||
"client": "client0", | ||
"databaseName": "distinct-hint-tests" | ||
} | ||
}, | ||
{ | ||
"collection": { | ||
"id": "collection0", | ||
"database": "database0", | ||
"collectionName": "coll0" | ||
} | ||
} | ||
], | ||
"initialData": [ | ||
{ | ||
"collectionName": "coll0", | ||
"databaseName": "distinct-hint-tests", | ||
"documents": [ | ||
{ | ||
"_id": 1, | ||
"x": 11 | ||
}, | ||
{ | ||
"_id": 2, | ||
"x": 22 | ||
}, | ||
{ | ||
"_id": 3, | ||
"x": 33 | ||
} | ||
] | ||
} | ||
], | ||
"tests": [ | ||
{ | ||
"description": "distinct with hint string", | ||
"operations": [ | ||
{ | ||
"name": "distinct", | ||
"object": "collection0", | ||
"arguments": { | ||
"fieldName": "x", | ||
"filter": { | ||
"_id": 1 | ||
}, | ||
"hint": "_id_" | ||
}, | ||
"expectResult": [ | ||
11 | ||
] | ||
} | ||
], | ||
"expectEvents": [ | ||
{ | ||
"client": "client0", | ||
"events": [ | ||
{ | ||
"commandStartedEvent": { | ||
"command": { | ||
"distinct": "coll0", | ||
"key": "x", | ||
"query": { | ||
"_id": 1 | ||
}, | ||
"hint": "_id_" | ||
}, | ||
"commandName": "distinct", | ||
"databaseName": "distinct-hint-tests" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"description": "distinct with hint document", | ||
"operations": [ | ||
{ | ||
"name": "distinct", | ||
"object": "collection0", | ||
"arguments": { | ||
"fieldName": "x", | ||
"filter": { | ||
"_id": 1 | ||
}, | ||
"hint": { | ||
"_id": 1 | ||
} | ||
}, | ||
"expectResult": [ | ||
11 | ||
] | ||
} | ||
], | ||
"expectEvents": [ | ||
{ | ||
"client": "client0", | ||
"events": [ | ||
{ | ||
"commandStartedEvent": { | ||
"command": { | ||
"distinct": "coll0", | ||
"key": "x", | ||
"query": { | ||
"_id": 1 | ||
}, | ||
"hint": { | ||
"_id": 1 | ||
} | ||
}, | ||
"commandName": "distinct", | ||
"databaseName": "distinct-hint-tests" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import com.mongodb.annotations.Alpha | |
import com.mongodb.annotations.Reason | ||
import com.mongodb.client.cursor.TimeoutMode | ||
import com.mongodb.client.model.Collation | ||
import com.mongodb.lang.Nullable | ||
import com.mongodb.reactivestreams.client.DistinctPublisher | ||
import java.util.concurrent.TimeUnit | ||
import kotlinx.coroutines.flow.Flow | ||
|
@@ -103,5 +104,21 @@ public class DistinctFlow<T : Any>(private val wrapped: DistinctPublisher<T>) : | |
*/ | ||
public fun comment(comment: BsonValue?): DistinctFlow<T> = apply { wrapped.comment(comment) } | ||
|
||
/** | ||
* Sets the hint for which index to use. A null value means no hint is set. | ||
* | ||
* @param hint the hint | ||
* @return this | ||
*/ | ||
public fun hint(@Nullable hint: Bson?): DistinctFlow<T> = apply { wrapped.hint(hint) } | ||
|
||
/** | ||
* Sets the hint for which index to use. A null value means no hint is set. | ||
* | ||
* @param hint the name of the index which should be used for the operation | ||
* @return this | ||
*/ | ||
public fun hintString(@Nullable hint: String?): DistinctFlow<T> = apply { wrapped.hintString(hint) } | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems the doc is consistent with the counterpart of
could we add this detail here as well? |
||
public override suspend fun collect(collector: FlowCollector<T>): Unit = wrapped.asFlow().collect(collector) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import com.mongodb.annotations.Reason | |
import com.mongodb.client.DistinctIterable as JDistinctIterable | ||
import com.mongodb.client.cursor.TimeoutMode | ||
import com.mongodb.client.model.Collation | ||
import com.mongodb.lang.Nullable | ||
import java.util.concurrent.TimeUnit | ||
import org.bson.BsonValue | ||
import org.bson.conversions.Bson | ||
|
@@ -99,4 +100,20 @@ public class DistinctIterable<T : Any?>(private val wrapped: JDistinctIterable<T | |
* @return this | ||
*/ | ||
public fun comment(comment: BsonValue?): DistinctIterable<T> = apply { wrapped.comment(comment) } | ||
|
||
/** | ||
* Sets the hint for which index to use. A null value means no hint is set. | ||
* | ||
* @param hint the hint | ||
* @return this | ||
*/ | ||
public fun hint(@Nullable hint: Bson?): DistinctIterable<T> = apply { wrapped.hint(hint) } | ||
|
||
/** | ||
* Sets the hint for which index to use. A null value means no hint is set. | ||
* | ||
* @param hint the name of the index which should be used for the operation | ||
* @return this | ||
*/ | ||
public fun hintString(@Nullable hint: String?): DistinctIterable<T> = apply { wrapped.hintString(hint) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, could we add the following detail to above to be consistent with
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,18 @@ public DistinctIterable<T> comment(@Nullable final BsonValue comment) { | |
return this; | ||
} | ||
|
||
@Override | ||
public DistinctIterable<T> hint(@Nullable final Bson hint) { | ||
wrapped.hint(hint); | ||
return this; | ||
} | ||
|
||
@Override | ||
public DistinctIterable<T> hintString(final String hint) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems a |
||
wrapped.hintString(hint); | ||
return this; | ||
} | ||
|
||
@Override | ||
public DistinctIterable<T> timeoutMode(final TimeoutMode timeoutMode) { | ||
wrapped.timeoutMode(timeoutMode); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
hint
field suffices which is ofBsonValue
type that will accomodate bothBsonDocument
andBsonString
.Similar scenario applies in
FindOperation
within the same package which has ahint
field as well.