Example usage for com.mongodb.client.model ReplaceOptions ReplaceOptions

List of usage examples for com.mongodb.client.model ReplaceOptions ReplaceOptions

Introduction

In this page you can find the example usage for com.mongodb.client.model ReplaceOptions ReplaceOptions.

Prototype

ReplaceOptions

Source Link

Usage

From source file:com.hurence.logisland.service.mongodb.MongoDBUpdater.java

License:Apache License

@Override
public void run() {
    List<Tuple<Document, Bson>> batchBuffer = new ArrayList<>();

    while (true) {
        try {//from   w w  w. ja v  a 2  s .c  o  m
            Tuple<Record, Bson> record = records.poll(flushInterval, TimeUnit.MILLISECONDS);
            if (record != null) {
                batchBuffer.add(new Tuple<>(RecordConverter.convert(record.getKey()), record.getValue()));
            }
            long currentTS = System.nanoTime();
            if (batchBuffer.size() > 0
                    && ((currentTS - lastTS) >= flushInterval * 1000000 || batchBuffer.size() >= batchSize)) {
                //use moustache operator to avoid composing strings when not needed
                logger.debug("committing {} records to Mongo after {} ns", batchBuffer.size(),
                        (currentTS - lastTS));

                if (MongoDBControllerService.BULK_MODE_UPSERT.getValue().equals(bulkMode)) {
                    ReplaceOptions replaceOptions = new ReplaceOptions().upsert(true);
                    //split batches by 500 document each max
                    for (int i = 0; i < batchBuffer.size(); i += 500) {
                        try {
                            col.bulkWrite(
                                    batchBuffer.stream().skip(i).limit(500)
                                            .map(document -> new ReplaceOneModel<>(document.getValue(),
                                                    document.getKey(), replaceOptions))
                                            .collect(Collectors.toList()));
                        } catch (MongoBulkWriteException bwe) {
                            bwe.getWriteErrors().forEach(error -> {
                                if (error.getCode() != 11000) {
                                    logger.warn("MongoDB updater got error: {}", error);
                                }
                            });
                        }
                    }
                } else {
                    col.insertMany(batchBuffer.stream().map(Tuple::getKey).collect(Collectors.toList()));
                }
                lastTS = currentTS;
                batchBuffer = new ArrayList<>();
            }

        } catch (InterruptedException e) {
            //here we should exit the loop
            logger.info("Interrupted while waiting: {}", e.getMessage());
            break;
        } catch (Exception e) {
            logger.error("Unrecoverable error from MongoDB updater. Loosing data!", e);
            batchBuffer.clear();
            lastTS = System.nanoTime();
        }
    }
}

From source file:org.mongojack.JacksonMongoCollection.java

License:Apache License

/**
 * Performs an update operation, replacing the entire document.
 *
 * @param query/*  ww  w .  ja v  a  2 s. c  om*/
 *            search query for old object to replace
 * @param object
 *            object with which to replace <tt>query</tt>
 * @param upsert
 *            if the database should create the element if it does not exist
 * @param concern
 *            the write concern
 * @return The write result
 * @throws MongoWriteException
 *             If the write failed due some other failure specific to the update command
 * @throws MongoWriteConcernException
 *             If the write failed due being unable to fulfill the write concern
 * @throws MongoException
 *             If an error occurred
 */
public UpdateResult replaceOne(DBQuery.Query query, T object, boolean upsert, WriteConcern concern)
        throws MongoException, MongoWriteException, MongoWriteConcernException {
    if (concern != null) {
        return mongoCollection.withWriteConcern(concern).replaceOne(serializeQuery(query), object,
                new ReplaceOptions().upsert(upsert));
    } else {
        return mongoCollection.replaceOne(serializeQuery(query), object, new ReplaceOptions().upsert(upsert));
    }

}

From source file:org.mongojack.JacksonMongoCollection.java

License:Apache License

/**
 * Performs an update operation, replacing the entire document.
 *
 * @param query/* w ww. j av  a2  s.  c  o m*/
 *            search query for old object to replace
 * @param object
 *            object with which to replace <tt>query</tt>
 * @param upsert
 *            if the database should create the element if it does not exist
 * @param concern
 *            the write concern
 * @return The write result
 * @throws MongoWriteException
 *             If the write failed due some other failure specific to the update command
 * @throws MongoWriteConcernException
 *             If the write failed due being unable to fulfill the write concern
 * @throws MongoException
 *             If an error occurred
 */
public UpdateResult replaceOne(Document query, T object, boolean upsert, WriteConcern concern)
        throws MongoException, MongoWriteException, MongoWriteConcernException {
    query = serializeFields(query);
    if (concern != null) {
        return mongoCollection.withWriteConcern(concern).replaceOne(query, object,
                new ReplaceOptions().upsert(upsert));
    } else {
        return mongoCollection.replaceOne(query, object, new ReplaceOptions().upsert(upsert));
    }

}