Search in sources :

Example 1 with IndexRequest

use of com.mongodb.bulk.IndexRequest in project mongo-java-driver by mongodb.

the class CreateIndexesOperation method getCommand.

private BsonDocument getCommand(final ConnectionDescription description) {
    BsonDocument command = new BsonDocument("createIndexes", new BsonString(namespace.getCollectionName()));
    List<BsonDocument> values = new ArrayList<BsonDocument>();
    for (IndexRequest request : requests) {
        values.add(getIndex(request));
    }
    command.put("indexes", new BsonArray(values));
    appendWriteConcernToCommand(writeConcern, command, description);
    return command;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) IndexRequest(com.mongodb.bulk.IndexRequest)

Example 2 with IndexRequest

use of com.mongodb.bulk.IndexRequest 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 3 with IndexRequest

use of com.mongodb.bulk.IndexRequest 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 4 with IndexRequest

use of com.mongodb.bulk.IndexRequest in project mongo-java-driver by mongodb.

the class OperationHelper method validateIndexRequestCollations.

static void validateIndexRequestCollations(final AsyncConnection connection, final List<IndexRequest> requests, final AsyncCallableWithConnection callable) {
    boolean calledTheCallable = false;
    for (IndexRequest request : requests) {
        if (request.getCollation() != null) {
            calledTheCallable = true;
            validateCollation(connection, request.getCollation(), new AsyncCallableWithConnection() {

                @Override
                public void call(final AsyncConnection connection, final Throwable t) {
                    callable.call(connection, t);
                }
            });
            break;
        }
    }
    if (!calledTheCallable) {
        callable.call(connection, null);
    }
}
Also used : AsyncConnection(com.mongodb.connection.AsyncConnection) IndexRequest(com.mongodb.bulk.IndexRequest)

Aggregations

IndexRequest (com.mongodb.bulk.IndexRequest)4 ArrayList (java.util.ArrayList)3 IndexModel (com.mongodb.client.model.IndexModel)2 CreateIndexesOperation (com.mongodb.operation.CreateIndexesOperation)2 AsyncConnection (com.mongodb.connection.AsyncConnection)1 BsonArray (org.bson.BsonArray)1 BsonDocument (org.bson.BsonDocument)1 BsonString (org.bson.BsonString)1