Example usage for com.mongodb.client.model CreateCollectionOptions sizeInBytes

List of usage examples for com.mongodb.client.model CreateCollectionOptions sizeInBytes

Introduction

In this page you can find the example usage for com.mongodb.client.model CreateCollectionOptions sizeInBytes.

Prototype

long sizeInBytes

To view the source code for com.mongodb.client.model CreateCollectionOptions sizeInBytes.

Click Source Link

Usage

From source file:com.imaginea.mongodb.services.impl.CollectionServiceImpl.java

License:Apache License

/**
 * Creates a collection inside a database in mongo to which user is connected to.
 *
 * @param dbName Name of Database in which to insert a collection
 * @param newCollName Name of Collection to be added/renamed to
 * @param capped Specify if the collection is capped
 * @param size Specify the size of collection
 * @param maxDocs specify maximum no of documents in the collection
 * @return Success if Insertion is successful else throw exception
 * @throws DatabaseException throw super type of UndefinedDatabaseException
 * @throws ValidationException throw super type of
 *         EmptyDatabaseNameException,EmptyCollectionNameException
 * @throws CollectionException throw super type of
 *         DuplicateCollectionException,InsertCollectionException
 *//*from  ww  w . j a v  a2s . c  om*/
public String insertCollection(String dbName, String newCollName, boolean capped, long size, int maxDocs,
        boolean autoIndexId) throws DatabaseException, CollectionException, ValidationException {

    if (dbName == null || dbName.equals("")) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Invalid Database name");
    }
    if (newCollName == null || newCollName.equals("")) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Invalid Collection name");
    }

    try {
        // if (!databaseService.getDbList().contains(dbName)) {
        //   throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS,
        //       "Db with name [" + dbName + "] doesn't exist.");
        // }

        if (getCollList(dbName).contains(newCollName)) {
            throw new CollectionException(ErrorCodes.COLLECTION_ALREADY_EXISTS,
                    "Collection [" + newCollName + "] already exists in Database [" + dbName + "]");
        }

        // DBObject options = new BasicDBObject();
        CreateCollectionOptions options = new CreateCollectionOptions();

        options.capped(capped);
        if (capped) {
            options.maxDocuments(maxDocs);
            options.autoIndex(autoIndexId);
            options.sizeInBytes(size);
        }

        mongoInstance.getDatabase(dbName).createCollection(newCollName, options);
    } catch (MongoException m) {
        throw new CollectionException(ErrorCodes.COLLECTION_CREATION_EXCEPTION, m.getMessage());
    }
    return "Collection [" + newCollName + "] was successfully added to Database [" + dbName + "].";
}

From source file:com.imaginea.mongodb.services.impl.CollectionServiceImpl.java

License:Apache License

/**
 * Creates a collection inside a database in mongo to which user is connected to.
 *
 * @param dbName Name of Database in which to insert a collection
 * @param selectedCollectionName Collection on which the operation is performed
 * @param newCollName Name of Collection to be added/renamed to
 * @param capped Specify if the collection is capped
 * @param size Specify the size of collection
 * @param maxDocs specify maximum no of documents in the collection
 * @return Success if Insertion is successful else throw exception
 * @throws DatabaseException throw super type of UndefinedDatabaseException
 * @throws ValidationException throw super type of
 *         EmptyDatabaseNameException,EmptyCollectionNameException
 * @throws CollectionException throw super type of
 *         DuplicateCollectionException,InsertCollectionException
 *//*from w  ww.ja v a 2 s  .  c  om*/
public String updateCollection(String dbName, String selectedCollectionName, String newCollName, boolean capped,
        long size, int maxDocs, boolean autoIndexId)
        throws DatabaseException, CollectionException, ValidationException {

    if (dbName == null || dbName.equals("")) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Invalid Database name");
    }

    if (selectedCollectionName == null || newCollName == null) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Collection name should be provided");
    }
    if (selectedCollectionName.equals("") || newCollName.equals("")) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Collection name cannot be empty");
    }
    String result = "No updates were specified!";
    try {
        // if (!databaseService.getDbList().contains(dbName)) {
        //   throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS,
        //       "Db with name [" + dbName + "] doesn't exist.");
        // }

        boolean convertedToCapped = false, convertedToNormal = false, renamed = false;

        MongoDatabase db = mongoInstance.getDatabase(dbName);
        MongoCollection<Document> selectedCollection = db.getCollection(selectedCollectionName);
        CreateCollectionOptions options = new CreateCollectionOptions();
        options.capped(capped);
        if (capped) {
            options.maxDocuments(maxDocs);
            options.autoIndex(autoIndexId);
            options.sizeInBytes(size);
            createCollection(options, selectedCollection, selectedCollectionName, db);
            convertedToCapped = true;
        } else {
            createCollection(options, selectedCollection, selectedCollectionName, db);
            convertedToNormal = true;
        }

        if (!selectedCollectionName.equals(newCollName)) {
            if (getCollList(dbName).contains(newCollName)) {
                throw new CollectionException(ErrorCodes.COLLECTION_ALREADY_EXISTS,
                        "Collection [" + newCollName + "] already exists in Database [" + dbName + "]");
            }
            selectedCollection = db.getCollection(selectedCollectionName);

            MongoNamespace mongoNamespace = new MongoNamespace(dbName + "." + newCollName);

            selectedCollection.renameCollection(mongoNamespace);
            renamed = true;
        }
        if ((convertedToNormal || convertedToCapped) && renamed) {
            result = "Collection [" + selectedCollectionName + "] was successfully updated.";
        } else if (convertedToCapped) {
            result = "Collection [" + selectedCollectionName
                    + "] was successfully converted to capped collection";
        } else if (convertedToNormal) {
            result = "Capped Collection [" + selectedCollectionName
                    + "] was successfully converted to normal collection";
        } else if (renamed) {
            result = "Collection [" + selectedCollectionName + "] was successfully renamed to '" + newCollName
                    + "'";
        }
    } catch (MongoException m) {
        throw new CollectionException(ErrorCodes.COLLECTION_UPDATE_EXCEPTION, m.getMessage());
    }
    return result;
}

From source file:org.apache.eagle.alert.metadata.impl.MongoMetadataDaoImpl.java

License:Apache License

private MongoCollection<Document> getCollection(String collectionName) {
    // first check if collection exists, if not then create a new collection with cappedSize
    if (!isCollectionExists(collectionName)) {
        CreateCollectionOptions option = new CreateCollectionOptions();
        option.capped(true);//from  www .j ava  2  s . c o m
        option.maxDocuments(cappedMaxDocuments);
        option.sizeInBytes(cappedMaxSize);
        db.createCollection(collectionName, option);
    }

    return db.getCollection(collectionName);

}

From source file:org.springframework.data.mongodb.core.ReactiveMongoTemplate.java

License:Apache License

protected CreateCollectionOptions convertToCreateCollectionOptions(CollectionOptions collectionOptions) {

    CreateCollectionOptions result = new CreateCollectionOptions();
    if (collectionOptions != null) {

        if (collectionOptions.getCapped() != null) {
            result = result.capped(collectionOptions.getCapped());
        }//from ww  w .  j  a va 2s  .  c  o m

        if (collectionOptions.getSize() != null) {
            result = result.sizeInBytes(collectionOptions.getSize());
        }

        if (collectionOptions.getMaxDocuments() != null) {
            result = result.maxDocuments(collectionOptions.getMaxDocuments());
        }
    }
    return result;
}