}
List<LedgerRange> results = new ArrayList<LedgerRange>();
List<LedgerRange> lrs = ranges.getRangesList();
long startSeqId = 1L;
if (!lrs.isEmpty()) {
LedgerRange range = lrs.get(0);
if (!range.hasStartSeqIdIncluded() && range.hasEndSeqIdIncluded()) {
long ledgerId = range.getLedgerId();
try {
LedgerHandle lh = bk.openLedgerNoRecovery(ledgerId, DigestType.CRC32, passwd);
long numEntries = lh.readLastConfirmed() + 1;
long endOfLedger = range.getEndSeqIdIncluded().getLocalComponent();
startSeqId = endOfLedger - numEntries + 1;
} catch (BKException.BKNoSuchLedgerExistsException be) {
// ignore it
}
}
}
Iterator<LedgerRange> lrIter = lrs.iterator();
while (lrIter.hasNext()) {
LedgerRange range = lrIter.next();
if (range.hasEndSeqIdIncluded()) {
long endOfLedger = range.getEndSeqIdIncluded().getLocalComponent();
if (range.hasStartSeqIdIncluded()) {
startSeqId = range.getStartSeqIdIncluded();
} else {
range = buildLedgerRange(range.getLedgerId(), startSeqId, range.getEndSeqIdIncluded());
}
results.add(range);
if (startSeqId < endOfLedger + 1) {
startSeqId = endOfLedger + 1;
}
continue;
}
if (lrIter.hasNext()) {
throw new IllegalStateException("Ledger " + range.getLedgerId() + " for topic " + topic.toString()
+ " is not the last one but still does not have an end seq-id");
}
if (range.hasStartSeqIdIncluded()) {
startSeqId = range.getStartSeqIdIncluded();
}
LedgerHandle lh = bk.openLedgerNoRecovery(range.getLedgerId(), DigestType.CRC32, passwd);
long endOfLedger = startSeqId + lh.readLastConfirmed();
MessageSeqId endSeqId = MessageSeqId.newBuilder().setLocalComponent(endOfLedger).build();
results.add(buildLedgerRange(range.getLedgerId(), startSeqId, endSeqId));
}
return results;
}