use of org.apache.pulsar.client.api.RawMessage in project incubator-pulsar by apache.
the class TwoPhaseCompactor method phaseOneLoop.
private void phaseOneLoop(RawReader reader, Optional<MessageId> firstMessageId, MessageId lastMessageId, Map<String, MessageId> latestForKey, CompletableFuture<PhaseOneResult> loopPromise) {
if (loopPromise.isDone()) {
return;
}
CompletableFuture<RawMessage> future = reader.readNextAsync();
scheduleTimeout(future);
future.whenCompleteAsync((m, exception) -> {
try {
if (exception != null) {
loopPromise.completeExceptionally(exception);
return;
}
MessageId id = m.getMessageId();
if (RawBatchConverter.isBatch(m)) {
try {
RawBatchConverter.extractIdsAndKeys(m).forEach(e -> latestForKey.put(e.getRight(), e.getLeft()));
} catch (IOException ioe) {
log.info("Error decoding batch for message {}. Whole batch will be included in output", id, ioe);
}
} else {
String key = extractKey(m);
latestForKey.put(key, id);
}
if (id.compareTo(lastMessageId) == 0) {
loopPromise.complete(new PhaseOneResult(firstMessageId.orElse(id), id, latestForKey));
} else {
phaseOneLoop(reader, Optional.of(firstMessageId.orElse(id)), lastMessageId, latestForKey, loopPromise);
}
} finally {
m.close();
}
}, scheduler);
}
Aggregations