Search in sources :

Example 1 with MongoChangeStreamException

use of com.mongodb.MongoChangeStreamException in project mongo-java-driver by mongodb.

the class ChangeStreamBatchCursor method convertAndProduceLastId.

/**
 * @param lastIdConsumer Is {@linkplain Consumer#accept(Object) called} iff {@code rawDocuments} is successfully converted
 *                       and the returned {@link List} is neither {@code null} nor {@linkplain List#isEmpty() empty}.
 */
@Nullable
static <T> List<T> convertAndProduceLastId(@Nullable final List<RawBsonDocument> rawDocuments, final Decoder<T> decoder, final Consumer<BsonDocument> lastIdConsumer) {
    List<T> results = null;
    if (rawDocuments != null) {
        results = new ArrayList<T>();
        for (RawBsonDocument rawDocument : rawDocuments) {
            if (!rawDocument.containsKey("_id")) {
                throw new MongoChangeStreamException("Cannot provide resume functionality when the resume token is missing.");
            }
            results.add(rawDocument.decode(decoder));
        }
        lastIdConsumer.accept(rawDocuments.get(rawDocuments.size() - 1).getDocument("_id"));
    }
    return results;
}
Also used : RawBsonDocument(org.bson.RawBsonDocument) MongoChangeStreamException(com.mongodb.MongoChangeStreamException) Nullable(com.mongodb.lang.Nullable)

Aggregations

MongoChangeStreamException (com.mongodb.MongoChangeStreamException)1 Nullable (com.mongodb.lang.Nullable)1 RawBsonDocument (org.bson.RawBsonDocument)1