Example usage for com.mongodb DBCollection setWriteConcern

List of usage examples for com.mongodb DBCollection setWriteConcern

Introduction

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

Prototype

public void setWriteConcern(final WriteConcern writeConcern) 

Source Link

Document

Set the WriteConcern for this collection.

Usage

From source file:org.alfresco.cacheserver.dao.mongo.MongoWebSocketDAO.java

License:Open Source License

protected DBCollection getCappedCollection(DB db, String collectionName, Integer maxCollectionSize,
        Integer maxDocuments, WriteConcern writeConcern) {
    if (!db.collectionExists(collectionName)) {
        BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();

        builder.add("capped", true);

        if (maxCollectionSize != null) {
            builder.add("size", maxCollectionSize);
        }//w  w w  .  j  ava  2 s  . co  m

        if (maxDocuments != null) {
            builder.add("max", maxDocuments);
        }

        DBObject options = builder.get();
        db.createCollection(collectionName, options);
    }
    DBCollection collection = db.getCollection(collectionName);
    collection.setWriteConcern(writeConcern);

    return collection;
}

From source file:org.alfresco.repo.domain.node.mongo.MongoNodeDAO.java

License:Open Source License

private DBCollection getCollection(DB db, String collectionName, WriteConcern writeConcern) {
    DBCollection collection = db.getCollection(collectionName);
    collection.setWriteConcern(writeConcern);
    return collection;
}

From source file:org.exoplatform.mongo.service.impl.MongoRestServiceImpl.java

License:Open Source License

@POST
@Path("/databases/{dbName}/collections")
@Override//from   www  . ja v a2  s  .  c  o m
public Response createCollection(@PathParam("dbName") String dbName,
        org.exoplatform.mongo.entity.request.Collection collection, @Context HttpHeaders headers,
        @Context UriInfo uriInfo, @Context SecurityContext securityContext) {
    if (shutdown) {
        return Response.status(ServerError.SERVICE_UNAVAILABLE.code())
                .entity(ServerError.SERVICE_UNAVAILABLE.message()).build();
    }
    Response response = null;
    String user = null;
    try {
        Credentials credentials = authenticateAndAuthorize(headers, uriInfo, securityContext);
        user = credentials.getUserName();
        String dbNamespace = constructDbNamespace(credentials.getUserName(), dbName);
        if (mongo.getDatabaseNames().contains(dbNamespace)) {
            DB db = mongo.getDB(dbNamespace);
            authServiceAgainstMongo(db);
            DBObject options = new BasicDBObject();
            options.put("max", configuration.getMaxDocsPerCollection());
            DBCollection dbCollection = db.createCollection(collection.getName(), options);
            if (collection.getWriteConcern() != null) {
                dbCollection.setWriteConcern(collection.getWriteConcern().getMongoWriteConcern());
            }

            URI statusSubResource = uriInfo.getBaseUriBuilder().path(MongoRestServiceImpl.class)
                    .path("/databases/" + dbName + "/collections/" + collection.getName()).build();
            response = Response.created(statusSubResource).build();
        } else {
            response = Response.status(ClientError.NOT_FOUND.code()).entity(dbName + " does not exist").build();
        }
    } catch (Exception exception) {
        response = lobException(exception, headers, uriInfo);
    } finally {
        updateStats(user, "createCollection");
    }

    return response;
}

From source file:org.exoplatform.mongo.service.impl.MongoRestServiceImpl.java

License:Open Source License

@PUT
@Path("/databases/{dbName}/collections/{collName}")
@Override//  w  w  w.  j  a  va 2  s . c  om
public Response updateCollection(@PathParam("dbName") String dbName, @PathParam("collName") String collName,
        org.exoplatform.mongo.entity.request.Collection collection, @Context HttpHeaders headers,
        @Context UriInfo uriInfo, @Context SecurityContext securityContext) {
    if (shutdown) {
        return Response.status(ServerError.SERVICE_UNAVAILABLE.code())
                .entity(ServerError.SERVICE_UNAVAILABLE.message()).build();
    }
    Response response = null;
    String user = null;
    try {
        Credentials credentials = authenticateAndAuthorize(headers, uriInfo, securityContext);
        user = credentials.getUserName();

        String dbNamespace = constructDbNamespace(credentials.getUserName(), dbName);
        DB db = mongo.getDB(dbNamespace);
        authServiceAgainstMongo(db);
        DBCollection dbCollection = null;
        if (mongo.getDatabaseNames().contains(dbNamespace)) {
            URI statusSubResource = uriInfo.getBaseUriBuilder().path(MongoRestServiceImpl.class)
                    .path("/databases/" + dbName + "/collections/" + collection.getName()).build();
            if (db.getCollectionNames().contains(collection.getName())) {
                dbCollection = db.getCollection(collection.getName());
                if (collection.getWriteConcern() != null) {
                    dbCollection.setWriteConcern(collection.getWriteConcern().getMongoWriteConcern());
                }
                response = Response.ok(statusSubResource).build();
            } else {
                DBObject options = new BasicDBObject();
                options.put("max", configuration.getMaxDocsPerCollection());
                dbCollection = db.createCollection(collection.getName(), options);
                if (collection.getWriteConcern() != null) {
                    dbCollection.setWriteConcern(collection.getWriteConcern().getMongoWriteConcern());
                }
                response = Response.created(statusSubResource).build();
            }
        } else {
            response = Response.status(ClientError.NOT_FOUND.code()).entity(dbName + " does not exist").build();
        }
    } catch (Exception exception) {
        response = lobException(exception, headers, uriInfo);
    } finally {
        updateStats(user, "updateCollection");
    }

    return response;
}

From source file:org.grails.datastore.mapping.mongo.MongoDatastore.java

License:Apache License

protected void createMongoTemplate(PersistentEntity entity, Mongo mongoInstance) {
    DocumentMappingContext dc = (DocumentMappingContext) getMappingContext();
    String collectionName = entity.getDecapitalizedName();
    String databaseName = dc.getDefaultDatabaseName();
    @SuppressWarnings("unchecked")
    ClassMapping<MongoCollection> mapping = entity.getMapping();
    final MongoCollection mongoCollection = mapping.getMappedForm() != null ? mapping.getMappedForm() : null;

    if (mongoCollection != null) {
        if (mongoCollection.getCollection() != null) {
            collectionName = mongoCollection.getCollection();
        }/*from  w  w  w .  j a v  a2s  .c o  m*/
        if (mongoCollection.getDatabase() != null) {
            databaseName = mongoCollection.getDatabase();
        }
    }

    final SimpleMongoDbFactory dbf;

    String username = read(String.class, USERNAME, connectionDetails, null);
    String password = read(String.class, PASSWORD, connectionDetails, null);

    if (username != null && password != null) {
        UserCredentials uc = new UserCredentials(username, password);
        dbf = new SimpleMongoDbFactory(mongoInstance, databaseName, uc);
    } else {
        dbf = new SimpleMongoDbFactory(mongoInstance, databaseName);
    }

    final MongoTemplate mt = new MongoTemplate(dbf);

    if (mongoCollection != null) {
        final WriteConcern writeConcern = mongoCollection.getWriteConcern();
        if (writeConcern != null) {
            final String collectionNameToUse = collectionName;
            mt.executeInSession(new DbCallback<Object>() {
                public Object doInDB(DB db) throws MongoException, DataAccessException {
                    if (writeConcern != null) {
                        DBCollection collection = db.getCollection(collectionNameToUse);
                        collection.setWriteConcern(writeConcern);
                    }
                    return null;
                }
            });
        }
    }

    mongoTemplates.put(entity, mt);
    mongoCollections.put(entity, collectionName);

    initializeIndices(entity, mt);
}

From source file:org.graylog2.cluster.ClusterConfigServiceImpl.java

License:Open Source License

@VisibleForTesting
static DBCollection prepareCollection(final MongoConnection mongoConnection) {
    DBCollection coll = mongoConnection.getDatabase().getCollection(COLLECTION_NAME);
    coll.createIndex(DBSort.asc("type"), "unique_type", true);
    coll.setWriteConcern(WriteConcern.FSYNCED);

    return coll;//from   w  ww. j av a2  s .co m
}

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(/* www. j  a  v  a 2  s .  c o  m*/
                "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;
}

From source file:org.springframework.datastore.mapping.mongo.MongoDatastore.java

License:Apache License

protected void createMongoTemplate(PersistentEntity entity, Mongo mongoInstance) {
    DocumentMappingContext dc = (DocumentMappingContext) getMappingContext();
    String collectionName = entity.getDecapitalizedName();
    String databaseName = dc.getDefaultDatabaseName();
    ClassMapping<MongoCollection> mapping = entity.getMapping();
    final MongoCollection mongoCollection = mapping.getMappedForm() != null ? mapping.getMappedForm() : null;

    if (mongoCollection != null) {
        if (mongoCollection.getCollection() != null)
            collectionName = mongoCollection.getCollection();
        if (mongoCollection.getDatabase() != null)
            databaseName = mongoCollection.getDatabase();

    }//from  ww  w  .ja v a 2 s .c om
    final MongoTemplate mt = new MongoTemplate(mongoInstance, databaseName, collectionName);

    String username = read(String.class, USERNAME, connectionDetails, null);
    String password = read(String.class, PASSWORD, connectionDetails, null);

    if (username != null && password != null) {
        mt.setUsername(username);
        mt.setPassword(password);
    }

    if (mongoCollection != null) {
        final WriteConcern writeConcern = mongoCollection.getWriteConcern();
        if (writeConcern != null) {
            mt.executeInSession(new DbCallback<Object>() {
                @Override
                public Object doInDB(DB db) throws MongoException, DataAccessException {

                    if (writeConcern != null) {
                        DBCollection collection = db.getCollection(mt.getDefaultCollectionName());
                        collection.setWriteConcern(writeConcern);
                    }
                    return null;
                }
            });
        }

    }

    try {
        mt.afterPropertiesSet();
    } catch (Exception e) {
        throw new DatastoreConfigurationException(
                "Failed to configure Mongo template for entity [" + entity + "]: " + e.getMessage(), e);
    }

    mongoTemplates.put(entity, mt);

    initializeIndices(entity, mt);
}