MixedBulkWriteOperation replaceOne(final Bson filter, final TDocument replacement, final ReplaceOptions options) { return bulkWrite(singletonList(new ReplaceOneModel<TDocument>(filter, replacement, options)), new BulkWriteOptions().bypassDocumentValidation(options.getBypassDocumentValidation())); }
public BulkWriteResult replace(List<? extends Bson> queries, List<? extends Bson> updates, boolean upsert) { if (queries.size() != updates.size()) { throw wrongQueryUpdateSize(queries, updates); } Iterator<? extends Bson> queryIterator = queries.iterator(); Iterator<? extends Bson> updateIterator = updates.iterator(); List<WriteModel<Document>> actions = new ArrayList<>(queries.size()); UpdateOptions updateOptions = new UpdateOptions().upsert(upsert); while (queryIterator.hasNext()) { Bson query = queryIterator.next(); Bson update = updateIterator.next(); actions.add(new ReplaceOneModel<>(query, (Document) update, updateOptions)); } return dbCollection.bulkWrite(actions, new BulkWriteOptions().ordered(false)); }
public void saveTags(@Nonnull Iterable<Tag> tags) { writeLock.lock(); try { checkNotNull(tags); Spliterator<Tag> spliterator = tags.spliterator(); List<WriteModel<Document>> updates = stream(spliterator, false) .map(tag -> new ReplaceOneModel<>( toFilter(tag), toDocument(tag), upsert() )) .collect(toList()); BulkWriteResult bulkWriteResult = getCollection().bulkWrite(updates); } finally { writeLock.unlock(); } }
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 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); }
MixedBulkWriteOperation replaceOne(final Bson filter, final TDocument replacement, final ReplaceOptions options) { return bulkWrite(singletonList(new ReplaceOneModel<TDocument>(filter, replacement, options)), new BulkWriteOptions().bypassDocumentValidation(options.getBypassDocumentValidation())); }
public void Save(Document doc) { if (!doc.containsKey("_id")) { Create(doc); return; } Document find = new Document("_id", doc.get("_id")); UpdateOptions uo = new UpdateOptions(); uo.upsert(true); ops.add(new ReplaceOneModel<Document>(find, doc, uo)); FlushOpsIfFull(); }
@Override public WriteModel<BsonDocument> createWriteModel(SinkDocument document) { BsonDocument vd = document.getValueDoc().orElseThrow( () -> new DataException("error: cannot build the WriteModel since" + " the value document was missing unexpectedly") ); return new ReplaceOneModel<>( new BsonDocument(DBCollection.ID_FIELD_NAME, vd.get(DBCollection.ID_FIELD_NAME)), vd, UPDATE_OPTIONS); } }
@Override public WriteModel<BsonDocument> createWriteModel(SinkDocument document) { BsonDocument vd = document.getValueDoc().orElseThrow( () -> new DataException("error: cannot build the WriteModel since" + " the value document was missing unexpectedly") ); return new ReplaceOneModel<>( new BsonDocument(DBCollection.ID_FIELD_NAME, vd.get(DBCollection.ID_FIELD_NAME)), vd, UPDATE_OPTIONS); } }
@Override public WriteModel<BsonDocument> createWriteModel(SinkDocument document) { BsonDocument vd = document.getValueDoc().orElseThrow( () -> new DataException("error: cannot build the WriteModel since" + " the value document was missing unexpectedly") ); BsonValue businessKey = vd.get(DBCollection.ID_FIELD_NAME); if(businessKey == null || !(businessKey instanceof BsonDocument)) { throw new DataException("error: cannot build the WriteModel since" + " the value document does not contain an _id field of type BsonDocument" + " which holds the business key fields"); } vd.remove(DBCollection.ID_FIELD_NAME); return new ReplaceOneModel<>((BsonDocument)businessKey, vd, UPDATE_OPTIONS); } }
@Override public WriteModel<BsonDocument> createWriteModel(SinkDocument document) { BsonDocument vd = document.getValueDoc().orElseThrow( () -> new DataException("error: cannot build the WriteModel since" + " the value document was missing unexpectedly") ); BsonValue businessKey = vd.get(DBCollection.ID_FIELD_NAME); if(businessKey == null || !(businessKey instanceof BsonDocument)) { throw new DataException("error: cannot build the WriteModel since" + " the value document does not contain an _id field of type BsonDocument" + " which holds the business key fields"); } vd.remove(DBCollection.ID_FIELD_NAME); return new ReplaceOneModel<>((BsonDocument)businessKey, vd, UPDATE_OPTIONS); } }
@Override public WriteModel<BsonDocument> perform(SinkDocument doc) { BsonDocument keyDoc = doc.getKeyDoc().orElseThrow( () -> new DataException("error: key doc must not be missing for insert operation") ); BsonDocument valueDoc = doc.getValueDoc().orElseThrow( () -> new DataException("error: value doc must not be missing for insert operation") ); try { BsonDocument filterDoc = RdbmsHandler.generateFilterDoc(keyDoc, valueDoc, OperationType.CREATE); BsonDocument upsertDoc = RdbmsHandler.generateUpsertOrReplaceDoc(keyDoc, valueDoc, filterDoc); return new ReplaceOneModel<>(filterDoc, upsertDoc, UPDATE_OPTIONS); } catch (Exception exc) { throw new DataException(exc); } }
@Override public WriteModel<BsonDocument> perform(SinkDocument doc) { BsonDocument keyDoc = doc.getKeyDoc().orElseThrow( () -> new DataException("error: key doc must not be missing for insert operation") ); BsonDocument valueDoc = doc.getValueDoc().orElseThrow( () -> new DataException("error: value doc must not be missing for insert operation") ); try { BsonDocument filterDoc = RdbmsHandler.generateFilterDoc(keyDoc, valueDoc, OperationType.CREATE); BsonDocument upsertDoc = RdbmsHandler.generateUpsertOrReplaceDoc(keyDoc, valueDoc, filterDoc); return new ReplaceOneModel<>(filterDoc, upsertDoc, UPDATE_OPTIONS); } catch (Exception exc) { throw new DataException(exc); } }
@Override public WriteModel<BsonDocument> perform(SinkDocument doc) { BsonDocument keyDoc = doc.getKeyDoc().orElseThrow( () -> new DataException("error: key doc must not be missing for update operation") ); BsonDocument valueDoc = doc.getValueDoc().orElseThrow( () -> new DataException("error: value doc must not be missing for update operation") ); try { BsonDocument filterDoc = RdbmsHandler.generateFilterDoc(keyDoc, valueDoc, OperationType.UPDATE); BsonDocument replaceDoc = RdbmsHandler.generateUpsertOrReplaceDoc(keyDoc, valueDoc, filterDoc); return new ReplaceOneModel<>(filterDoc, replaceDoc, UPDATE_OPTIONS); } catch (Exception exc) { throw new DataException(exc); } }
@Override public WriteModel<BsonDocument> perform(SinkDocument doc) { BsonDocument keyDoc = doc.getKeyDoc().orElseThrow( () -> new DataException("error: key doc must not be missing for update operation") ); BsonDocument valueDoc = doc.getValueDoc().orElseThrow( () -> new DataException("error: value doc must not be missing for update operation") ); try { BsonDocument filterDoc = RdbmsHandler.generateFilterDoc(keyDoc, valueDoc, OperationType.UPDATE); BsonDocument replaceDoc = RdbmsHandler.generateUpsertOrReplaceDoc(keyDoc, valueDoc, filterDoc); return new ReplaceOneModel<>(filterDoc, replaceDoc, UPDATE_OPTIONS); } catch (Exception exc) { throw new DataException(exc); } }
@Override public WriteModel<BsonDocument> perform(SinkDocument doc) { BsonDocument valueDoc = doc.getValueDoc().orElseThrow( () -> new DataException("error: value doc must not be missing for insert operation") ); try { BsonDocument insertDoc = BsonDocument.parse( valueDoc.get(JSON_DOC_FIELD_PATH).asString().getValue() ); return new ReplaceOneModel<>( new BsonDocument(DBCollection.ID_FIELD_NAME, insertDoc.get(DBCollection.ID_FIELD_NAME)), insertDoc, UPDATE_OPTIONS ); } catch(Exception exc) { throw new DataException(exc); } }
@Override public WriteModel<BsonDocument> perform(SinkDocument doc) { BsonDocument valueDoc = doc.getValueDoc().orElseThrow( () -> new DataException("error: value doc must not be missing for insert operation") ); try { BsonDocument insertDoc = BsonDocument.parse( valueDoc.get(JSON_DOC_FIELD_PATH).asString().getValue() ); return new ReplaceOneModel<>( new BsonDocument(DBCollection.ID_FIELD_NAME, insertDoc.get(DBCollection.ID_FIELD_NAME)), insertDoc, UPDATE_OPTIONS ); } catch(Exception exc) { throw new DataException(exc); } }
new BsonDocument(DBCollection.ID_FIELD_NAME, updateDoc.get(DBCollection.ID_FIELD_NAME)); return new ReplaceOneModel<>(filterDoc, updateDoc, UPDATE_OPTIONS);
new BsonDocument(DBCollection.ID_FIELD_NAME, updateDoc.get(DBCollection.ID_FIELD_NAME)); return new ReplaceOneModel<>(filterDoc, updateDoc, UPDATE_OPTIONS);
@Test public void testBulkUpsert() throws Exception { List<ReplaceOneModel<Document>> models = Arrays.asList( new ReplaceOneModel<>(Filters.eq("_id", 1), json("_id: 1, a: 1"), new ReplaceOptions().upsert(true)), new ReplaceOneModel<>(Filters.eq("_id", 2), json("_id: 2, a: 1"), new ReplaceOptions().upsert(true)) ); BulkWriteResult result = collection.bulkWrite(models, new BulkWriteOptions().ordered(false)); assertThat(result.getUpserts()) .extracting(BulkWriteUpsert::getId) .containsExactly(new BsonInt32(1), new BsonInt32(2)); assertThat(toArray(collection.find())) .containsExactlyInAnyOrder(json("_id: 1, a: 1"), json("_id: 2, a: 1")); models = Arrays.asList( new ReplaceOneModel<>(Filters.eq("_id", 1), json("_id: 1, a: 2"), new ReplaceOptions().upsert(true)), new ReplaceOneModel<>(Filters.eq("_id", 3), json("_id: 3, a: 2"), new ReplaceOptions().upsert(true)), new ReplaceOneModel<>(Filters.eq("_id", 2), json("_id: 2, a: 2"), new ReplaceOptions().upsert(true)) ); result = collection.bulkWrite(models, new BulkWriteOptions().ordered(false)); assertThat(result.getUpserts()) .extracting(BulkWriteUpsert::getId) .containsExactly(new BsonInt32(3)); assertThat(toArray(collection.find())) .containsExactlyInAnyOrder( json("_id: 1, a: 2"), json("_id: 2, a: 2"), json("_id: 3, a: 2") ); }