List of usage examples for com.mongodb.client.model CreateCollectionOptions capped
boolean capped
To view the source code for com.mongodb.client.model CreateCollectionOptions capped.
Click Source Link
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 *//* ww w . j a va2 s. co m*/ 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 . j av a2s .c o m 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); option.maxDocuments(cappedMaxDocuments); option.sizeInBytes(cappedMaxSize); db.createCollection(collectionName, option); }/*ww w .j ava2s . c o m*/ return db.getCollection(collectionName); }
From source file:org.bananaforscale.cormac.dao.collection.CollectionDataServiceImpl.java
License:Apache License
/** * Creates a new collection explicitly. Because MongoDB creates a collection * implicitly when the collection is first referenced in a command, this * method is not required for usage of said collection. * * @param databaseName the database/*from w w w .j ava2s .co m*/ * @param collectionName the collection to create * @return the result of the operation * @throws DatasourceException * @throws ExistsException * @throws NotFoundException */ @Override public boolean addCollection(String databaseName, String collectionName) throws DatasourceException, ExistsException, NotFoundException { try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } if (collectionExists(databaseName, collectionName)) { throw new ExistsException("The collection already exists in the datasource"); } MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName); CreateCollectionOptions options = new CreateCollectionOptions(); options.capped(false); mongoDatabase.createCollection(collectionName, options); return true; } catch (MongoException ex) { logger.error("An error occured while adding the collection", ex); throw new DatasourceException("An error occured while adding the collection"); } }
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 w w w.j a v a 2s .c o m if (collectionOptions.getSize() != null) { result = result.sizeInBytes(collectionOptions.getSize()); } if (collectionOptions.getMaxDocuments() != null) { result = result.maxDocuments(collectionOptions.getMaxDocuments()); } } return result; }