-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add support for managing Atlas search indexes. #1158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
bb610dd
Add support for managing Atlas search indexes.
vbabanin 123c8cd
Fix formatting, spotbugs issues.
vbabanin 1dd433d
Fix formatting.
vbabanin 8ce344a
Add javadoc.
vbabanin e89553c
Fix tests.
vbabanin f977401
Change javadoc.
vbabanin ef13ea8
Change assignment ordering.
vbabanin 795d351
Remove wildcard imports.
vbabanin 891d386
Change documentation.
vbabanin ab7d67d
Change javadoc.
vbabanin 95b6810
Remove maxAwaitTime option for ListSearchIndexes iterable/publisher i…
vbabanin 78dcd04
Update driver-core/src/main/com/mongodb/internal/operation/SearchInde…
vbabanin d88b6a8
Update driver-core/src/main/com/mongodb/client/model/SearchIndexModel…
vbabanin 36c6bc8
Update driver-core/src/main/com/mongodb/internal/operation/AbstractWr…
vbabanin d7208ca
Update driver-core/src/main/com/mongodb/internal/operation/AbstractWr…
vbabanin d04f161
Update driver-core/src/main/com/mongodb/internal/operation/AbstractWr…
vbabanin a7298ad
Update driver-core/src/main/com/mongodb/internal/operation/CreateSear…
vbabanin f8879d1
Update driver-core/src/main/com/mongodb/internal/operation/CreateSear…
vbabanin eb25379
Apply suggestions from code review
vbabanin f83e870
Add retryable functionality.
vbabanin 1554663
Change javadoc.
vbabanin ba83e2e
Change Java doc of the createSearchIndexes methods.
vbabanin da5bed0
Change verb from "remove" to "drop" in javadoc.
vbabanin 7bae99b
Fix spotBugs.
vbabanin 4d8365c
Move test files.
vbabanin a24d795
Remove whitespaces.
vbabanin 7163044
Revert retryable writes.
vbabanin dc04204
Remove mentioning of nullubility from ListSearchIterable and Publishe…
vbabanin 684c455
Change javadoc.
vbabanin 2235541
Update driver-sync/src/main/com/mongodb/client/MongoCollection.java
vbabanin e7b8192
Fix spotless warnings.
vbabanin f5f6230
Make class internal.
vbabanin f63ff7b
Add null-check.
vbabanin 5cbee87
Add null-check.
vbabanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
driver-core/src/main/com/mongodb/client/model/SearchIndexModel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.client.model; | ||
|
||
import com.mongodb.lang.Nullable; | ||
import org.bson.conversions.Bson; | ||
|
||
import static com.mongodb.assertions.Assertions.notNull; | ||
|
||
/** | ||
* A model describing the creation of a single Atlas Search index. | ||
* | ||
* @since 4.11 | ||
* @mongodb.server.release 7.0 | ||
*/ | ||
public final class SearchIndexModel { | ||
@Nullable | ||
private final String name; | ||
private final Bson definition; | ||
|
||
/** | ||
* Construct an instance with the given Atlas Search index mapping definition. | ||
* | ||
* <p>After calling this constructor, the {@code name} field will be {@code null}. In that case, when passing this | ||
* {@code SearchIndexModel} to the {@code createSearchIndexes} method, the default search index name 'default' | ||
* will be used to create the search index.</p> | ||
* | ||
* @param definition the search index mapping definition. | ||
*/ | ||
public SearchIndexModel(final Bson definition) { | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.definition = notNull("definition", definition); | ||
this.name = null; | ||
} | ||
|
||
/** | ||
* Construct an instance with the given Atlas Search name and index definition. | ||
* | ||
* @param name the search index name. | ||
* @param definition the search index mapping definition. | ||
*/ | ||
public SearchIndexModel(final String name, final Bson definition) { | ||
this.definition = notNull("definition", definition); | ||
this.name = notNull("name", name); | ||
} | ||
|
||
/** | ||
* Get the Atlas Search index mapping definition. | ||
* | ||
* @return the index definition. | ||
*/ | ||
public Bson getDefinition() { | ||
return definition; | ||
} | ||
|
||
/** | ||
* Get the Atlas Search index name. | ||
* | ||
* @return the search index name. | ||
*/ | ||
@Nullable | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SearchIndexModel{" | ||
+ "name=" + name | ||
+ ", definition=" + definition | ||
+ '}'; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
driver-core/src/main/com/mongodb/internal/operation/AbstractWriteSearchIndexOperation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.internal.operation; | ||
|
||
|
||
import com.mongodb.MongoCommandException; | ||
import com.mongodb.MongoNamespace; | ||
import com.mongodb.WriteConcern; | ||
import com.mongodb.internal.async.SingleResultCallback; | ||
import com.mongodb.internal.binding.AsyncWriteBinding; | ||
import com.mongodb.internal.binding.WriteBinding; | ||
import com.mongodb.lang.Nullable; | ||
import org.bson.BsonDocument; | ||
|
||
import static com.mongodb.internal.operation.CommandOperationHelper.executeCommand; | ||
import static com.mongodb.internal.operation.CommandOperationHelper.executeCommandAsync; | ||
import static com.mongodb.internal.operation.CommandOperationHelper.writeConcernErrorTransformer; | ||
import static com.mongodb.internal.operation.CommandOperationHelper.writeConcernErrorWriteTransformer; | ||
import static com.mongodb.internal.operation.OperationHelper.withAsyncSourceAndConnection; | ||
import static com.mongodb.internal.operation.OperationHelper.withConnection; | ||
|
||
/** | ||
* An abstract class for defining operations for managing Atlas Search indexes. | ||
* | ||
* <p>This class is not part of the public API and may be removed or changed at any time</p> | ||
*/ | ||
abstract class AbstractWriteSearchIndexOperation implements AsyncWriteOperation<Void>, WriteOperation<Void> { | ||
private final MongoNamespace namespace; | ||
private final WriteConcern writeConcern; | ||
vbabanin marked this conversation as resolved.
Show resolved
Hide resolved
vbabanin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
AbstractWriteSearchIndexOperation(final MongoNamespace mongoNamespace, | ||
final WriteConcern writeConcern) { | ||
this.namespace = mongoNamespace; | ||
this.writeConcern = writeConcern; | ||
} | ||
|
||
@Override | ||
public Void execute(final WriteBinding binding) { | ||
return withConnection(binding, connection -> { | ||
try { | ||
executeCommand(binding, namespace.getDatabaseName(), buildCommand(), connection, writeConcernErrorTransformer()); | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (MongoCommandException mongoCommandException) { | ||
swallowOrThrow(mongoCommandException); | ||
} | ||
return null; | ||
}); | ||
} | ||
|
||
@Override | ||
public void executeAsync(final AsyncWriteBinding binding, final SingleResultCallback<Void> callback) { | ||
withAsyncSourceAndConnection(binding::getWriteConnectionSource, false, callback, | ||
(connectionSource, connection, cb) -> | ||
executeCommandAsync(binding, namespace.getDatabaseName(), buildCommand(), connection, | ||
writeConcernErrorWriteTransformer(), (result, commandExecutionError) -> { | ||
try { | ||
swallowOrThrow(commandExecutionError); | ||
callback.onResult(result, null); | ||
} catch (Throwable mongoCommandException) { | ||
callback.onResult(null, mongoCommandException); | ||
} | ||
} | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Handles the provided execution exception by either throwing it or ignoring it. This method is meant to be overridden | ||
* by subclasses that need to handle exceptions differently based on their specific requirements. | ||
* | ||
* <p> | ||
* <strong>Note:</strong> While the method declaration allows throwing a checked exception to enhance readability, the implementation | ||
* of this method must not throw a checked exception. | ||
* </p> | ||
* | ||
* @param <E> The type of the execution exception. | ||
* @param mongoExecutionException The execution exception to handle. If not null, it may be thrown or ignored. | ||
* @throws E The execution exception, if it is not null (implementation-specific). | ||
*/ | ||
<E extends Throwable> void swallowOrThrow(@Nullable final E mongoExecutionException) throws E { | ||
if (mongoExecutionException != null) { | ||
throw mongoExecutionException; | ||
} | ||
} | ||
|
||
abstract BsonDocument buildCommand(); | ||
|
||
MongoNamespace getNamespace() { | ||
return namespace; | ||
} | ||
|
||
WriteConcern getWriteConcern() { | ||
return writeConcern; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
driver-core/src/main/com/mongodb/internal/operation/CreateSearchIndexesOperation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.internal.operation; | ||
|
||
import com.mongodb.MongoNamespace; | ||
import com.mongodb.WriteConcern; | ||
import org.bson.BsonArray; | ||
import org.bson.BsonDocument; | ||
import org.bson.BsonString; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.mongodb.assertions.Assertions.assertNotNull; | ||
import static com.mongodb.internal.operation.WriteConcernHelper.appendWriteConcernToCommand; | ||
|
||
/** | ||
* An operation that creates one or more Atlas Search indexes. | ||
* | ||
* <p>This class is not part of the public API and may be removed or changed at any time</p> | ||
*/ | ||
final class CreateSearchIndexesOperation extends AbstractWriteSearchIndexOperation { | ||
private static final String COMMAND_NAME = "createSearchIndexes"; | ||
private final List<SearchIndexRequest> indexRequests; | ||
|
||
CreateSearchIndexesOperation(final MongoNamespace namespace, final List<SearchIndexRequest> indexRequests, | ||
final WriteConcern writeConcern) { | ||
super(namespace, writeConcern); | ||
this.indexRequests = assertNotNull(indexRequests); | ||
} | ||
|
||
private static BsonArray convert(final List<SearchIndexRequest> requests) { | ||
return requests.stream() | ||
.map(CreateSearchIndexesOperation::convert) | ||
.collect(Collectors.toCollection(BsonArray::new)); | ||
} | ||
|
||
private static BsonDocument convert(final SearchIndexRequest request) { | ||
BsonDocument bsonIndexRequest = new BsonDocument(); | ||
String searchIndexName = request.getIndexName(); | ||
if (searchIndexName != null) { | ||
bsonIndexRequest.append("name", new BsonString(searchIndexName)); | ||
} | ||
bsonIndexRequest.append("definition", request.getDefinition()); | ||
return bsonIndexRequest; | ||
} | ||
|
||
@Override | ||
BsonDocument buildCommand() { | ||
BsonDocument command = new BsonDocument(COMMAND_NAME, new BsonString(getNamespace().getCollectionName())) | ||
.append("indexes", convert(indexRequests)); | ||
appendWriteConcernToCommand(getWriteConcern(), command); | ||
return command; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.