Source:
private Observable<IndexOperationMessage> processBatch( final IndexOperationMessage batch ) { final Set<IndexOperation> indexOperationSet = batch.getIndexRequests(); final Set<DeIndexOperation> deIndexOperationSet = batch.getDeIndexRequests(); final int indexOperationSetSize = indexOperationSet.size(); final int deIndexOperationSetSize = deIndexOperationSet.size(); log.debug("Emitting add and remove operations", indexOperationSetSize, deIndexOperationSetSize); indexSizeCounter.dec(indexOperationSetSize); indexSizeCounter.dec(deIndexOperationSetSize); final Observable<IndexOperation> index = Observable.from(batch.getIndexRequests()); final Observable<DeIndexOperation> deIndex = Observable.from(batch.getDeIndexRequests()); final Observable<BatchOperation> batchOps = Observable.merge(index, deIndex); final Observable<BulkRequestBuilder> requests = batchOps.buffer(indexFig.getIndexBatchSize()) .flatMap(individualOps -> Observable.from(individualOps) .collect(() -> initRequest(), (bulkRequestBuilder, batchOperation) -> { log.debug("adding operation to bulkRequestBuilder {}", batchOperation, bulkRequestBuilder); batchOperation.doOperation(client, bulkRequestBuilder); })) .doOnNext(bulkRequestBuilder -> sendRequest(bulkRequestBuilder)); final Observable<IndexOperationMessage> processedIndexOperations = requests.lastOrDefault(null).flatMap(lastRequest -> { if (lastRequest != null) { return Observable.just(batch); } else { return Observable.empty(); } }); return processedIndexOperations.doOnNext(processedIndexOp -> { roundtripTimer.update(System.currentTimeMillis() - processedIndexOp.getCreationTime()); }); }

Target:
private Observable<IndexOperationMessage> processBatch( final IndexOperationMessage batch ) { final Set<IndexOperation> indexOperationSet = batch.getIndexRequests(); final Set<DeIndexOperation> deIndexOperationSet = batch.getDeIndexRequests(); final int indexOperationSetSize = indexOperationSet.size(); final int deIndexOperationSetSize = deIndexOperationSet.size(); log.debug("Emitting add and remove operations", indexOperationSetSize, deIndexOperationSetSize); indexSizeCounter.dec(indexOperationSetSize); indexSizeCounter.dec(deIndexOperationSetSize); final Observable<IndexOperation> index = Observable.from(batch.getIndexRequests()); final Observable<DeIndexOperation> deIndex = Observable.from(batch.getDeIndexRequests()); final Observable<BatchOperation> batchOps = Observable.merge(index, deIndex); final Observable<BulkRequestBuilder> requests = batchOps.buffer(indexFig.getIndexBatchSize()) .flatMap(individualOps -> Observable.from(individualOps) .collect(() -> initRequest(), (bulkRequestBuilder, batchOperation) -> { log.debug("adding operation to bulkRequestBuilder {}", batchOperation, bulkRequestBuilder); batchOperation.doOperation(client, bulkRequestBuilder); })) .doOnNext(bulkRequestBuilder -> sendRequest(bulkRequestBuilder)); final Observable<IndexOperationMessage> processedIndexOperations = requests.flatMap(lastRequest -> { if (lastRequest != null) { return Observable.just(batch); } else { return Observable.empty(); } }); return processedIndexOperations.doOnNext(processedIndexOp -> { roundtripTimer.update(System.currentTimeMillis() - processedIndexOp.getCreationTime()); }); }