Search in sources :

Example 1 with IndexModel

use of com.mongodb.client.model.IndexModel in project mongo-java-driver by mongodb.

the class MongoCollectionImpl method createIndexes.

@Override
public List<String> createIndexes(final List<IndexModel> indexes) {
    notNull("indexes", indexes);
    List<IndexRequest> indexRequests = new ArrayList<IndexRequest>(indexes.size());
    for (IndexModel model : indexes) {
        if (model == null) {
            throw new IllegalArgumentException("indexes can not contain a null value");
        }
        indexRequests.add(new IndexRequest(toBsonDocument(model.getKeys())).name(model.getOptions().getName()).background(model.getOptions().isBackground()).unique(model.getOptions().isUnique()).sparse(model.getOptions().isSparse()).expireAfter(model.getOptions().getExpireAfter(TimeUnit.SECONDS), TimeUnit.SECONDS).version(model.getOptions().getVersion()).weights(toBsonDocument(model.getOptions().getWeights())).defaultLanguage(model.getOptions().getDefaultLanguage()).languageOverride(model.getOptions().getLanguageOverride()).textVersion(model.getOptions().getTextVersion()).sphereVersion(model.getOptions().getSphereVersion()).bits(model.getOptions().getBits()).min(model.getOptions().getMin()).max(model.getOptions().getMax()).bucketSize(model.getOptions().getBucketSize()).storageEngine(toBsonDocument(model.getOptions().getStorageEngine())).partialFilterExpression(toBsonDocument(model.getOptions().getPartialFilterExpression())).collation(model.getOptions().getCollation()));
    }
    CreateIndexesOperation createIndexesOperation = new CreateIndexesOperation(getNamespace(), indexRequests, writeConcern);
    executor.execute(createIndexesOperation);
    return createIndexesOperation.getIndexNames();
}
Also used : CreateIndexesOperation(com.mongodb.operation.CreateIndexesOperation) ArrayList(java.util.ArrayList) IndexRequest(com.mongodb.bulk.IndexRequest) IndexModel(com.mongodb.client.model.IndexModel)

Example 2 with IndexModel

use of com.mongodb.client.model.IndexModel in project mongo-java-driver by mongodb.

the class MongoCollectionImpl method createIndexes.

@Override
public void createIndexes(final List<IndexModel> indexes, final SingleResultCallback<List<String>> callback) {
    notNull("indexes", indexes);
    List<IndexRequest> indexRequests = new ArrayList<IndexRequest>(indexes.size());
    for (IndexModel model : indexes) {
        if (model == null) {
            throw new IllegalArgumentException("indexes can not contain a null value");
        }
        indexRequests.add(new IndexRequest(toBsonDocument(model.getKeys())).name(model.getOptions().getName()).background(model.getOptions().isBackground()).unique(model.getOptions().isUnique()).sparse(model.getOptions().isSparse()).expireAfter(model.getOptions().getExpireAfter(TimeUnit.SECONDS), TimeUnit.SECONDS).version(model.getOptions().getVersion()).weights(toBsonDocument(model.getOptions().getWeights())).defaultLanguage(model.getOptions().getDefaultLanguage()).languageOverride(model.getOptions().getLanguageOverride()).textVersion(model.getOptions().getTextVersion()).sphereVersion(model.getOptions().getSphereVersion()).bits(model.getOptions().getBits()).min(model.getOptions().getMin()).max(model.getOptions().getMax()).bucketSize(model.getOptions().getBucketSize()).storageEngine(toBsonDocument(model.getOptions().getStorageEngine())).partialFilterExpression(toBsonDocument(model.getOptions().getPartialFilterExpression())).collation(model.getOptions().getCollation()));
    }
    final CreateIndexesOperation createIndexesOperation = new CreateIndexesOperation(getNamespace(), indexRequests, writeConcern);
    executor.execute(createIndexesOperation, new SingleResultCallback<Void>() {

        @Override
        public void onResult(final Void result, final Throwable t) {
            if (t != null) {
                callback.onResult(null, t);
            } else {
                callback.onResult(createIndexesOperation.getIndexNames(), null);
            }
        }
    });
}
Also used : CreateIndexesOperation(com.mongodb.operation.CreateIndexesOperation) ArrayList(java.util.ArrayList) IndexRequest(com.mongodb.bulk.IndexRequest) IndexModel(com.mongodb.client.model.IndexModel)

Example 3 with IndexModel

use of com.mongodb.client.model.IndexModel in project mongo-java-driver by mongodb.

the class Operations method createIndexes.

@SuppressWarnings("deprecation")
public CreateIndexesOperation createIndexes(final List<IndexModel> indexes, final CreateIndexOptions createIndexOptions) {
    notNull("indexes", indexes);
    notNull("createIndexOptions", createIndexOptions);
    List<IndexRequest> indexRequests = new ArrayList<>(indexes.size());
    for (IndexModel model : indexes) {
        if (model == null) {
            throw new IllegalArgumentException("indexes can not contain a null value");
        }
        indexRequests.add(new IndexRequest(toBsonDocument(model.getKeys())).name(model.getOptions().getName()).background(model.getOptions().isBackground()).unique(model.getOptions().isUnique()).sparse(model.getOptions().isSparse()).expireAfter(model.getOptions().getExpireAfter(TimeUnit.SECONDS), TimeUnit.SECONDS).version(model.getOptions().getVersion()).weights(toBsonDocument(model.getOptions().getWeights())).defaultLanguage(model.getOptions().getDefaultLanguage()).languageOverride(model.getOptions().getLanguageOverride()).textVersion(model.getOptions().getTextVersion()).sphereVersion(model.getOptions().getSphereVersion()).bits(model.getOptions().getBits()).min(model.getOptions().getMin()).max(model.getOptions().getMax()).bucketSize(model.getOptions().getBucketSize()).storageEngine(toBsonDocument(model.getOptions().getStorageEngine())).partialFilterExpression(toBsonDocument(model.getOptions().getPartialFilterExpression())).collation(model.getOptions().getCollation()).wildcardProjection(toBsonDocument(model.getOptions().getWildcardProjection())).hidden(model.getOptions().isHidden()));
    }
    return new CreateIndexesOperation(namespace, indexRequests, writeConcern).maxTime(createIndexOptions.getMaxTime(MILLISECONDS), MILLISECONDS).commitQuorum(createIndexOptions.getCommitQuorum());
}
Also used : ArrayList(java.util.ArrayList) IndexRequest(com.mongodb.internal.bulk.IndexRequest) IndexModel(com.mongodb.client.model.IndexModel)

Example 4 with IndexModel

use of com.mongodb.client.model.IndexModel in project mongo-java-driver by mongodb.

the class MongoCollectionImplTest method testCreateIndexes.

@Test
public void testCreateIndexes() {
    Bson key = BsonDocument.parse("{key: 1}");
    CreateIndexOptions createIndexOptions = new CreateIndexOptions();
    CreateIndexOptions customCreateIndexOptions = new CreateIndexOptions().commitQuorum(CreateIndexCommitQuorum.VOTING_MEMBERS);
    List<IndexModel> indexes = singletonList(new IndexModel(key, new IndexOptions().background(true).bits(9)));
    assertAll("createIndexes", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.createIndexes(null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.createIndexes(indexes, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.createIndexes(clientSession, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.createIndexes(clientSession, indexes, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.createIndexes(null, indexes)), () -> assertThrows(IllegalArgumentException.class, () -> collection.createIndexes(null, indexes, createIndexOptions))), () -> {
        Publisher<String> expected = mongoOperationPublisher.createIndexes(null, indexes, createIndexOptions);
        assertPublisherIsTheSameAs(expected, collection.createIndexes(indexes), "Default");
    }, () -> {
        Publisher<String> expected = mongoOperationPublisher.createIndexes(null, indexes, customCreateIndexOptions);
        assertPublisherIsTheSameAs(expected, collection.createIndexes(indexes, customCreateIndexOptions), "With custom options");
    }, () -> {
        Publisher<String> expected = mongoOperationPublisher.createIndexes(clientSession, indexes, createIndexOptions);
        assertPublisherIsTheSameAs(expected, collection.createIndexes(clientSession, indexes), "With client session");
    }, () -> {
        Publisher<String> expected = mongoOperationPublisher.createIndexes(clientSession, indexes, customCreateIndexOptions);
        assertPublisherIsTheSameAs(expected, collection.createIndexes(clientSession, indexes, customCreateIndexOptions), "With client session, & custom options");
    });
}
Also used : CreateIndexOptions(com.mongodb.client.model.CreateIndexOptions) DropIndexOptions(com.mongodb.client.model.DropIndexOptions) IndexOptions(com.mongodb.client.model.IndexOptions) CreateIndexOptions(com.mongodb.client.model.CreateIndexOptions) IndexModel(com.mongodb.client.model.IndexModel) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Aggregations

IndexModel (com.mongodb.client.model.IndexModel)4 ArrayList (java.util.ArrayList)3 IndexRequest (com.mongodb.bulk.IndexRequest)2 CreateIndexesOperation (com.mongodb.operation.CreateIndexesOperation)2 CreateIndexOptions (com.mongodb.client.model.CreateIndexOptions)1 DropIndexOptions (com.mongodb.client.model.DropIndexOptions)1 IndexOptions (com.mongodb.client.model.IndexOptions)1 IndexRequest (com.mongodb.internal.bulk.IndexRequest)1 Bson (org.bson.conversions.Bson)1 Test (org.junit.jupiter.api.Test)1