Example usage for com.mongodb DBCollection isCapped

List of usage examples for com.mongodb DBCollection isCapped

Introduction

In this page you can find the example usage for com.mongodb DBCollection isCapped.

Prototype

public boolean isCapped() 

Source Link

Document

Checks whether this collection is capped

Usage

From source file:com.imaginea.mongodb.services.DocumentServiceImpl.java

License:Apache License

/**
 * Deletes a document inside a collection in a database in mongo to which
 * user is connected to.// w  w w  .  j  a v a2  s  .c o  m
 * 
 * @param dbName
 *            Name of Database
 * @param collectionName
 *            Name of Collection from which to get all Documents
 * @param id
 *            Id of Document to be updated
 * @return Deletion status
 * @exception EmptyDatabaseNameException
 *                If database name is null
 * @exception EmptyCollectionNameException
 *                If Collection name is null
 * @exception EmptyDocumentDataException
 *                If Document data is null
 * @exception UndefinedDatabaseException
 *                If database is not present
 * @exception UndefinedCollectionException
 *                If Collection is not present
 * @exception DeleteDocumentException
 *                Any exception while deleting document
 * @exception DatabaseException
 *                throw super type of UndefinedDatabaseException
 * @exception ValidationException
 *                throw super type of
 *                EmptyDatabaseNameException,EmptyCollectionNameException
 *                ,EmptyDocumentDataException
 * @exception CollectionException
 *                throw super type of UndefinedCollectionException
 * @exception DocumentException
 *                throw super type of DeleteDocumentException
 * 
 */

public String deleteDocument(String dbName, String collectionName, ObjectId id)
        throws DatabaseException, CollectionException, DocumentException, ValidationException {
    mongoInstance = mongoInstanceProvider.getMongoInstance();
    if (dbName == null) {
        throw new EmptyDatabaseNameException("Database name is null");

    }
    if (dbName.equals("")) {
        throw new EmptyDatabaseNameException("Database Name Empty");
    }

    if (collectionName == null) {
        throw new EmptyCollectionNameException("Collection name is null");
    }
    if (collectionName.equals("")) {
        throw new EmptyCollectionNameException("Collection Name Empty");
    }

    String result = null;
    DBObject documentData = null;
    try {
        if (!mongoInstance.getDatabaseNames().contains(dbName)) {
            throw new UndefinedDatabaseException("DB [" + dbName + "] DOES NOT EXIST");
        }

        if (!mongoInstance.getDB(dbName).getCollectionNames().contains(collectionName)) {
            throw new UndefinedCollectionException(
                    "COLLECTION [ " + collectionName + "] _DOES_NOT_EXIST in Db [ " + dbName + "]");
        }
        DBCollection coll = mongoInstance.getDB(dbName).getCollection(collectionName);
        if (coll.isCapped()) {
            throw new DocumentException(ErrorCodes.DELETING_FROM_CAPPED_COLLECTION,
                    "Cannot Delete From a Capped Collection");
        }
        if (id == null) {
            throw new EmptyDocumentDataException("Document is empty");
        }

        DBObject query = new BasicDBObject();
        query.put("_id", id);
        DBCollection collection = this.mongoInstance.getDB(dbName).getCollection(collectionName);
        documentData = collection.findOne(query);

        if (documentData == null) {
            throw new UndefinedDocumentException("DOCUMENT_DOES_NOT_EXIST");
        }

        mongoInstance.getDB(dbName).getCollection(collectionName).remove(documentData);

    } catch (MongoException e) {
        throw new DeleteDocumentException("DOCUMENT_DELETION_EXCEPTION");
    }
    result = "Deleted Document with Data : [" + documentData + "]";
    return result;
}

From source file:de.otto.mongodb.profiler.collection.DefaultCollectionProfiler.java

License:Apache License

@Override
public DefaultCollectionProfile addProfile(String collectionName) throws CollectionDoesNotExistException {
    synchronized (collectionStockMonitor) {

        final DBCollection collection = findCollection(collectionName);

        if (collection == null) {
            throw new CollectionDoesNotExistException(collectionName);
        }// ww  w.  j a  v  a 2 s  . c  o  m

        return getOrCreateProfile(collectionName, collection.isCapped());
    }
}

From source file:org.graylog2.events.ClusterEventPeriodical.java

License:Open Source License

@VisibleForTesting
static DBCollection prepareCollection(final MongoConnection mongoConnection) {
    final DB db = mongoConnection.getDatabase();

    DBCollection coll = db.getCollection(COLLECTION_NAME);

    if (coll.isCapped()) {
        LOG.warn(//  w  ww  .j  av  a  2  s  .com
                "The \"{}\" collection in MongoDB is capped which will cause problems. Please drop the collection.",
                COLLECTION_NAME);
    }

    coll.createIndex(DBSort.asc("timestamp").asc("producer").asc("consumers"));

    coll.setWriteConcern(WriteConcern.FSYNCED);

    return coll;
}