public V createOrUpdate(final V value, final long maxTime, final TimeUnit timeUnit) { final Document doc = encode(value); collectionWithWriteTimeout(maxTime, timeUnit) .replaceOne(byId(keyOf(value)), doc, new UpdateOptions().upsert(true)); return decode(doc); }
public void update(final V value) { final K key = keyOf(value); collection().replaceOne(byId(key), encode(value)); }
public void updateIfMatch(final V value, final String eTag) { Bson query = and(eq(AbstractMongoRepository.ID, keyOf(value)), eq(ETAG, eTag)); Document updatedETaggable = collection().findOneAndReplace(query, encode(value), new FindOneAndReplaceOptions().returnDocument(AFTER)); if (isNull(updatedETaggable)) { Optional<V> findById = findOne(keyOf(value)); if (findById.isPresent()) { throw new ConcurrentModificationException("Entity concurrently modified: " + keyOf(value)); } throw new NotFoundException("Entity does not exist: " + keyOf(value)); } }
public V create(final V value, final long maxTime, final TimeUnit timeUnit) { final K key = keyOf(value); if (key != null) { final Document doc = encode(value); collectionWithWriteTimeout(maxTime, timeUnit).insertOne(doc); return decode(doc); } else { throw new NullPointerException("Key must not be null"); } }
public V create(final V value) { Document doc = encode(value); collection().insertOne(doc); return decode(doc); }
final long maxTime, final TimeUnit timeUnit) { final K key = keyOf(value); if (key != null) { final Bson query = and(eq(AbstractMongoRepository.ID, key), eq(ETAG, eTag)); final Document updatedETaggable = collectionWithWriteTimeout(maxTime, timeUnit).findOneAndReplace(query, encode(value), new FindOneAndReplaceOptions().returnDocument(AFTER)); if (isNull(updatedETaggable)) { final boolean documentExists = collection() .count(eq(AbstractMongoRepository.ID, key), new CountOptions().maxTime(maxTime, timeUnit)) != 0; if (documentExists) {
public void delete(final K key) { collection().deleteOne(byId(key)); }
public void createOrUpdateBulk(final Collection<V> values, final long maxTime, final TimeUnit timeUnit) { if (values.isEmpty()) { return; } final List<ReplaceOneModel<Document>> bulkOperations = values.stream() .map(value -> new ReplaceOneModel<>( eq(ID, keyOf(value)), encode(value), BULK_UPSERT_OPERATION)) .collect(toList()); collectionWithWriteTimeout(maxTime, timeUnit) .bulkWrite(bulkOperations, BULK_WRITE_OPTIONS); }
public long size() { return collection().count(); }
public void deleteAll() { collection().deleteMany(matchAll()); }
/** * Deletes the document identified by key. * * @param key the identifier of the deleted document * @param maxTime max time for the operation * @param timeUnit the time unit for the maxTime value */ public void delete(final K key, final long maxTime, final TimeUnit timeUnit) { collectionWithWriteTimeout(maxTime, timeUnit).deleteOne(byId(key)); }
/** * Deletes all documents from this repository. * * @param maxTime max time for the operation * @param timeUnit the time unit for the maxTime value */ public void deleteAll(final long maxTime, final TimeUnit timeUnit) { collectionWithWriteTimeout(maxTime, timeUnit).deleteMany(matchAll()); }
public Stream<V> findAllAsStream(final long maxTime, final TimeUnit timeUnit) { return toStream( collection().find() .maxTime(maxTime, timeUnit)) .map(this::decode); }
/** * Find a single value with the specified key, if existing. * * @param key the key to search for * @return an Optional containing the requested value, or {@code Optional.empty()} if no value with this key exists */ public Optional<V> findOne(final K key) { return findOne(key, mongoProperties.getDefaultReadTimeout(), TimeUnit.MILLISECONDS); }
public V create(final V value) { return create(value, mongoProperties.getDefaultWriteTimeout(), TimeUnit.MILLISECONDS); }
public V createOrUpdate(final V value) { return createOrUpdate(value, mongoProperties.getDefaultWriteTimeout(), TimeUnit.MILLISECONDS); }
final long maxTime, final TimeUnit timeUnit) { final K key = keyOf(value); if (key != null) { final Bson query = and(eq(AbstractMongoRepository.ID, key), eq(ETAG, eTag)); final Document updatedETaggable = collectionWithWriteTimeout(maxTime, timeUnit).findOneAndReplace(query, encode(value), new FindOneAndReplaceOptions().returnDocument(AFTER)); if (isNull(updatedETaggable)) { final boolean documentExists = collection() .countDocuments(eq(AbstractMongoRepository.ID, key), new CountOptions().maxTime(maxTime, timeUnit)) != 0; if (documentExists) {
public Optional<V> findOne(final K key) { return ofNullable(collection() .find(byId(key)) .map(this::decode) .first()); }
public void createOrUpdateBulk(final Collection<V> values, final long maxTime, final TimeUnit timeUnit) { if (values.isEmpty()) { return; } final List<ReplaceOneModel<Document>> bulkOperations = values.stream() .map(value -> new ReplaceOneModel<>( eq(ID, keyOf(value)), encode(value), BULK_UPSERT_OPERATION)) .collect(toList()); collectionWithWriteTimeout(maxTime, timeUnit) .bulkWrite(bulkOperations, BULK_WRITE_OPTIONS); }
protected MongoCollection<Document> collectionWithWriteTimeout(long maxTime, TimeUnit timeUnit) { return collection() .withWriteConcern(collection().getWriteConcern().withWTimeout(maxTime, timeUnit)); }