Search in sources :

Example 1 with CreateCollectionOperation

use of com.mongodb.internal.operation.CreateCollectionOperation in project mongo-java-driver by mongodb.

the class CollectionHelper method create.

public void create(final String collectionName, final CreateCollectionOptions options, final WriteConcern writeConcern) {
    drop(namespace, writeConcern);
    CreateCollectionOperation operation = new CreateCollectionOperation(namespace.getDatabaseName(), collectionName, writeConcern).capped(options.isCapped()).sizeInBytes(options.getSizeInBytes()).maxDocuments(options.getMaxDocuments());
    IndexOptionDefaults indexOptionDefaults = options.getIndexOptionDefaults();
    if (indexOptionDefaults.getStorageEngine() != null) {
        operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(indexOptionDefaults.getStorageEngine())));
    }
    ValidationOptions validationOptions = options.getValidationOptions();
    if (validationOptions.getValidator() != null) {
        operation.validator(toBsonDocument(validationOptions.getValidator()));
    }
    if (validationOptions.getValidationLevel() != null) {
        operation.validationLevel(validationOptions.getValidationLevel());
    }
    if (validationOptions.getValidationAction() != null) {
        operation.validationAction(validationOptions.getValidationAction());
    }
    operation.execute(getBinding());
}
Also used : CreateCollectionOperation(com.mongodb.internal.operation.CreateCollectionOperation) BsonDocument(org.bson.BsonDocument) ValidationOptions(com.mongodb.client.model.ValidationOptions) IndexOptionDefaults(com.mongodb.client.model.IndexOptionDefaults)

Example 2 with CreateCollectionOperation

use of com.mongodb.internal.operation.CreateCollectionOperation in project mongo-java-driver by mongodb.

the class MongoOperationPublisher method createCollection.

Publisher<Void> createCollection(@Nullable final ClientSession clientSession, final MongoNamespace namespace, final CreateCollectionOptions options) {
    return createWriteOperationMono(() -> {
        CreateCollectionOperation operation = new CreateCollectionOperation(namespace.getDatabaseName(), namespace.getCollectionName(), getWriteConcern()).capped(options.isCapped()).sizeInBytes(options.getSizeInBytes()).maxDocuments(options.getMaxDocuments()).storageEngineOptions(toBsonDocument(options.getStorageEngineOptions())).collation(options.getCollation()).expireAfter(options.getExpireAfter(TimeUnit.SECONDS)).timeSeriesOptions(options.getTimeSeriesOptions());
        IndexOptionDefaults indexOptionDefaults = options.getIndexOptionDefaults();
        Bson storageEngine = indexOptionDefaults.getStorageEngine();
        if (storageEngine != null) {
            operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(storageEngine)));
        }
        ValidationOptions validationOptions = options.getValidationOptions();
        Bson validator = validationOptions.getValidator();
        if (validator != null) {
            operation.validator(toBsonDocument(validator));
        }
        if (validationOptions.getValidationLevel() != null) {
            operation.validationLevel(validationOptions.getValidationLevel());
        }
        if (validationOptions.getValidationAction() != null) {
            operation.validationAction(validationOptions.getValidationAction());
        }
        return operation;
    }, clientSession);
}
Also used : CreateCollectionOperation(com.mongodb.internal.operation.CreateCollectionOperation) BsonDocument(org.bson.BsonDocument) ValidationOptions(com.mongodb.client.model.ValidationOptions) IndexOptionDefaults(com.mongodb.client.model.IndexOptionDefaults) Bson(org.bson.conversions.Bson)

Example 3 with CreateCollectionOperation

use of com.mongodb.internal.operation.CreateCollectionOperation in project mongo-java-driver by mongodb.

the class DB method getCreateCollectionOperation.

private CreateCollectionOperation getCreateCollectionOperation(final String collectionName, final DBObject options) {
    if (options.get("size") != null && !(options.get("size") instanceof Number)) {
        throw new IllegalArgumentException("'size' should be Number");
    }
    if (options.get("max") != null && !(options.get("max") instanceof Number)) {
        throw new IllegalArgumentException("'max' should be Number");
    }
    if (options.get("capped") != null && !(options.get("capped") instanceof Boolean)) {
        throw new IllegalArgumentException("'capped' should be Boolean");
    }
    if (options.get("autoIndexId") != null && !(options.get("autoIndexId") instanceof Boolean)) {
        throw new IllegalArgumentException("'autoIndexId' should be Boolean");
    }
    if (options.get("storageEngine") != null && !(options.get("storageEngine") instanceof DBObject)) {
        throw new IllegalArgumentException("'storageEngine' should be DBObject");
    }
    if (options.get("indexOptionDefaults") != null && !(options.get("indexOptionDefaults") instanceof DBObject)) {
        throw new IllegalArgumentException("'indexOptionDefaults' should be DBObject");
    }
    if (options.get("validator") != null && !(options.get("validator") instanceof DBObject)) {
        throw new IllegalArgumentException("'validator' should be DBObject");
    }
    if (options.get("validationLevel") != null && !(options.get("validationLevel") instanceof String)) {
        throw new IllegalArgumentException("'validationLevel' should be String");
    }
    if (options.get("validationAction") != null && !(options.get("validationAction") instanceof String)) {
        throw new IllegalArgumentException("'validationAction' should be String");
    }
    boolean capped = false;
    boolean autoIndex = true;
    long sizeInBytes = 0;
    long maxDocuments = 0;
    BsonDocument storageEngineOptions = null;
    BsonDocument indexOptionDefaults = null;
    BsonDocument validator = null;
    ValidationLevel validationLevel = null;
    ValidationAction validationAction = null;
    if (options.get("capped") != null) {
        capped = (Boolean) options.get("capped");
    }
    if (options.get("size") != null) {
        sizeInBytes = ((Number) options.get("size")).longValue();
    }
    if (options.get("autoIndexId") != null) {
        autoIndex = (Boolean) options.get("autoIndexId");
    }
    if (options.get("max") != null) {
        maxDocuments = ((Number) options.get("max")).longValue();
    }
    if (options.get("storageEngine") != null) {
        storageEngineOptions = wrap((DBObject) options.get("storageEngine"));
    }
    if (options.get("indexOptionDefaults") != null) {
        indexOptionDefaults = wrap((DBObject) options.get("indexOptionDefaults"));
    }
    if (options.get("validator") != null) {
        validator = wrap((DBObject) options.get("validator"));
    }
    if (options.get("validationLevel") != null) {
        validationLevel = ValidationLevel.fromString((String) options.get("validationLevel"));
    }
    if (options.get("validationAction") != null) {
        validationAction = ValidationAction.fromString((String) options.get("validationAction"));
    }
    Collation collation = DBObjectCollationHelper.createCollationFromOptions(options);
    return new CreateCollectionOperation(getName(), collectionName, getWriteConcern()).capped(capped).collation(collation).sizeInBytes(sizeInBytes).autoIndex(autoIndex).maxDocuments(maxDocuments).storageEngineOptions(storageEngineOptions).indexOptionDefaults(indexOptionDefaults).validator(validator).validationLevel(validationLevel).validationAction(validationAction);
}
Also used : CreateCollectionOperation(com.mongodb.internal.operation.CreateCollectionOperation) BsonDocument(org.bson.BsonDocument) ValidationAction(com.mongodb.client.model.ValidationAction) ValidationLevel(com.mongodb.client.model.ValidationLevel) Collation(com.mongodb.client.model.Collation)

Example 4 with CreateCollectionOperation

use of com.mongodb.internal.operation.CreateCollectionOperation in project mongo-java-driver by mongodb.

the class MongoDatabaseImpl method executeCreateCollection.

private void executeCreateCollection(@Nullable final ClientSession clientSession, final String collectionName, final CreateCollectionOptions createCollectionOptions) {
    CreateCollectionOperation operation = new CreateCollectionOperation(name, collectionName, writeConcern).collation(createCollectionOptions.getCollation()).capped(createCollectionOptions.isCapped()).sizeInBytes(createCollectionOptions.getSizeInBytes()).maxDocuments(createCollectionOptions.getMaxDocuments()).storageEngineOptions(toBsonDocument(createCollectionOptions.getStorageEngineOptions())).expireAfter(createCollectionOptions.getExpireAfter(TimeUnit.SECONDS)).timeSeriesOptions(createCollectionOptions.getTimeSeriesOptions());
    IndexOptionDefaults indexOptionDefaults = createCollectionOptions.getIndexOptionDefaults();
    Bson storageEngine = indexOptionDefaults.getStorageEngine();
    if (storageEngine != null) {
        operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(storageEngine)));
    }
    ValidationOptions validationOptions = createCollectionOptions.getValidationOptions();
    Bson validator = validationOptions.getValidator();
    if (validator != null) {
        operation.validator(toBsonDocument(validator));
    }
    if (validationOptions.getValidationLevel() != null) {
        operation.validationLevel(validationOptions.getValidationLevel());
    }
    if (validationOptions.getValidationAction() != null) {
        operation.validationAction(validationOptions.getValidationAction());
    }
    executor.execute(operation, readConcern, clientSession);
}
Also used : CreateCollectionOperation(com.mongodb.internal.operation.CreateCollectionOperation) BsonDocument(org.bson.BsonDocument) ValidationOptions(com.mongodb.client.model.ValidationOptions) IndexOptionDefaults(com.mongodb.client.model.IndexOptionDefaults) Bson(org.bson.conversions.Bson)

Aggregations

CreateCollectionOperation (com.mongodb.internal.operation.CreateCollectionOperation)4 BsonDocument (org.bson.BsonDocument)4 IndexOptionDefaults (com.mongodb.client.model.IndexOptionDefaults)3 ValidationOptions (com.mongodb.client.model.ValidationOptions)3 Bson (org.bson.conversions.Bson)2 Collation (com.mongodb.client.model.Collation)1 ValidationAction (com.mongodb.client.model.ValidationAction)1 ValidationLevel (com.mongodb.client.model.ValidationLevel)1