Java Code Examples for com.mongodb.client.MongoCollection#withWriteConcern()

The following examples show how to use com.mongodb.client.MongoCollection#withWriteConcern() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Mapper.java    From morphia with Apache License 2.0 6 votes vote down vote up
/**
 * @param type the type look up
 * @param <T>  the class type
 * @return the collection mapped for this class
 */
public <T> MongoCollection<T> getCollection(final Class<T> type) {
    MappedClass mappedClass = getMappedClass(type);
    if (mappedClass == null) {
        throw new MappingException(Sofia.notMappable(type.getName()));
    }
    if (mappedClass.getCollectionName() == null) {
        throw new MappingException(Sofia.noMappedCollection(type.getName()));
    }

    MongoCollection<T> collection = datastore.getDatabase().getCollection(mappedClass.getCollectionName(), type);

    Entity annotation = mappedClass.getEntityAnnotation();
    if (annotation != null && WriteConcern.valueOf(annotation.concern()) != null) {
        collection = collection.withWriteConcern(WriteConcern.valueOf(annotation.concern()));
    }
    return collection;
}
 
Example 2
Source File: DatastoreImpl.java    From morphia with Apache License 2.0 6 votes vote down vote up
protected <T> void saveDocument(final T entity, final MongoCollection<T> collection, final InsertOneOptions options) {
    Object id = mapper.getMappedClass(entity.getClass()).getIdField().getFieldValue(entity);
    ClientSession clientSession = findSession(options);

    if (id == null) {
        if (clientSession == null) {
            options.prepare(collection).insertOne(entity, options.getOptions());
        } else {
            options.prepare(collection).insertOne(clientSession, entity, options.getOptions());
        }
    } else {
        ReplaceOptions updateOptions = new ReplaceOptions()
                                           .bypassDocumentValidation(options.getBypassDocumentValidation())
                                           .upsert(true);
        MongoCollection<T> updated = collection;
        if (options.writeConcern() != null) {
            updated = collection.withWriteConcern(options.writeConcern());
        }
        if (clientSession == null) {
            updated.replaceOne(new Document("_id", id), entity, updateOptions);
        } else {
            updated.replaceOne(clientSession, new Document("_id", id), entity, updateOptions);
        }
    }
}
 
Example 3
Source File: MongoJobRepository.java    From edison-microservice with Apache License 2.0 5 votes vote down vote up
public MongoJobRepository(final MongoDatabase mongoDatabase,
                          final String jobInfoCollectionName,
                          final MongoProperties mongoProperties) {
    super(mongoProperties);
    MongoCollection<Document> tmpCollection = mongoDatabase.getCollection(jobInfoCollectionName).withReadPreference(primaryPreferred());
    this.jobInfoCollection = tmpCollection.withWriteConcern(tmpCollection.getWriteConcern().withWTimeout(mongoProperties.getDefaultWriteTimeout(), TimeUnit.MILLISECONDS));
    this.clock = systemDefaultZone();
}
 
Example 4
Source File: MongoJobMetaRepository.java    From edison-microservice with Apache License 2.0 5 votes vote down vote up
public MongoJobMetaRepository(final MongoDatabase mongoDatabase,
                              final String jobMetaCollectionName,
                              final MongoProperties mongoProperties) {
    this.mongoProperties = mongoProperties;
    MongoCollection<Document> tmpCollection = mongoDatabase.getCollection(jobMetaCollectionName);
    collection = tmpCollection.withWriteConcern(tmpCollection
            .getWriteConcern()
            .withWTimeout(mongoProperties.getDefaultWriteTimeout(), TimeUnit.MILLISECONDS));
}
 
Example 5
Source File: AggregationOptions.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public <C> MongoCollection<C> prepare(final MongoCollection<C> collection) {
    MongoCollection<C> updated = collection;
    if (writeConcern() != null) {
        updated = updated.withWriteConcern(writeConcern());
    }
    if (getReadConcern() != null) {
        updated = updated.withReadConcern(getReadConcern());
    }
    if (getReadPreference() != null) {
        updated = updated.withReadPreference(getReadPreference());
    }

    return updated;
}
 
Example 6
Source File: Mapper.java    From morphia with Apache License 2.0 3 votes vote down vote up
/**
 * Updates a collection to use a specific WriteConcern
 *
 * @param collection the collection to update
 * @param type       the entity type
 * @return the updated collection
 */
public MongoCollection enforceWriteConcern(final MongoCollection collection, final Class type) {
    WriteConcern applied = getWriteConcern(type);
    return applied != null
           ? collection.withWriteConcern(applied)
           : collection;
}
 
Example 7
Source File: WriteConfigurable.java    From morphia with Apache License 2.0 2 votes vote down vote up
/**
 * Applies the options to the collection
 *
 * @param collection the collection to prepare
 * @param <C>        the collection type
 * @return either the passed collection or the updated collection
 * @since 2.0
 */
default <C> MongoCollection<C> prepare(MongoCollection<C> collection) {
    return writeConcern() == null
           ? collection
           : collection.withWriteConcern(writeConcern());
}