@Override public String toString() { return String.format("Version = %d, Epoch = %d, LedgerCount = %d, Truncate = (%d-%d)", this.updateVersion.get(), this.epoch, this.ledgers.size(), this.truncationAddress.getLedgerId(), this.truncationAddress.getEntryId()); }
/** * Creates a new instance of the LogMetadata class which contains all the ledgers after (and including) the given address. * * @param upToAddress The address to truncate to. * @return A new instance of the LogMetadata class. */ LogMetadata truncate(LedgerAddress upToAddress) { Preconditions.checkState(this.enabled, "Log is not enabled. Cannot perform any modifications on it."); // Exclude all those Ledgers that have a LedgerId less than the one we are given. An optimization to this would // involve trimming out the ledger which has a matching ledger id and the entry is is the last one, but that would // involve opening the Ledger in BookKeeper and inspecting it, which would take too long. val newLedgers = this.ledgers.stream().filter(lm -> lm.getLedgerId() >= upToAddress.getLedgerId()).collect(Collectors.toList()); return new LogMetadata(this.epoch, this.enabled, Collections.unmodifiableList(newLedgers), upToAddress, this.updateVersion.get()); }
} else if (address.getEntryId() < lastEntryId) { result = new LedgerAddress(address.getLedgerSequence(), address.getLedgerId(), address.getEntryId() + 1); } else { int index = getLedgerMetadataIndex(address.getLedgerId()) + 1; if (index > 0) { if (lm.getLedgerId() > address.getLedgerId()) { ledgerMetadata = lm; break;
private void write00(LogMetadata m, RevisionDataOutput output) throws IOException { output.writeBoolean(m.isEnabled()); output.writeCompactLong(m.getEpoch()); output.writeCompactLong(m.truncationAddress.getSequence()); output.writeCompactLong(m.truncationAddress.getLedgerId()); output.writeCollection(m.ledgers, this::writeLedger00); }
private void assertEquals(LedgerAddress a, int ledgerSeq, long ledgerId, long entryId) { Assert.assertEquals("Unexpected ledger sequence.", ledgerSeq, a.getLedgerSequence()); Assert.assertEquals("Unexpected ledger id.", ledgerId, a.getLedgerId()); Assert.assertEquals("Unexpected entry id.", entryId, a.getEntryId()); } }
LedgerMetadata metadata = this.metadata.getLedger(address.getLedgerId()); assert metadata != null : "no LedgerMetadata could be found with valid LedgerAddress " + address; val allMetadatas = this.metadata.getLedgers();
/** * Tests serialization/deserialization. */ @Test(timeout = 5000) public void testSerialization() throws Exception { Supplier<Long> nextLedgerId = new AtomicLong()::incrementAndGet; LogMetadata m1 = null; val lacs = new HashMap<Long, Long>(); for (int i = 0; i < LEDGER_COUNT; i++) { long ledgerId = nextLedgerId.get() * 2; if (m1 == null) { m1 = new LogMetadata(ledgerId).withUpdateVersion(i); } else { m1 = m1.addLedger(ledgerId).withUpdateVersion(i); } if (i % 2 == 0) { // Every other Ledger, update the LastAddConfirmed. lacs.put((long) i, (long) i + 1); } } m1 = m1.updateLedgerStatus(lacs); val serialization = LogMetadata.SERIALIZER.serialize(m1); val m2 = LogMetadata.SERIALIZER.deserialize(serialization); Assert.assertEquals("Unexpected epoch.", m1.getEpoch(), m2.getEpoch()); Assert.assertEquals("Unexpected TruncationAddress.", m1.getTruncationAddress().getSequence(), m2.getTruncationAddress().getSequence()); Assert.assertEquals("Unexpected TruncationAddress.", m1.getTruncationAddress().getLedgerId(), m2.getTruncationAddress().getLedgerId()); AssertExtensions.assertListEquals("Unexpected ledgers.", m1.getLedgers(), m2.getLedgers(), (l1, l2) -> l1.getSequence() == l2.getSequence() && l1.getLedgerId() == l2.getLedgerId() && l1.getStatus() == l2.getStatus()); }
val firstLedgerSeq = m.getLedgers().get(0).getSequence(); LedgerAddress a = m.getNextAddress(new LedgerAddress(firstLedgerSeq - 1, firstLedgerId - 1, 1234), Long.MAX_VALUE); Assert.assertEquals("Unexpected ledger id when input address less than first ledger.", firstLedgerId, a.getLedgerId()); Assert.assertEquals("Unexpected entry id when input address less than first ledger.", 0, a.getEntryId()); Assert.assertEquals("Unexpected ledger id when result should be in the same ledger.", firstLedgerId, a.getLedgerId()); Assert.assertEquals("Unexpected entry id when result should be in the same ledger.", 1, a.getEntryId()); } else { val nextLm = m.getLedgers().get(i + 1); Assert.assertEquals("Unexpected ledger id when result should be in the next ledger.", nextLm.getLedgerId(), a.getLedgerId()); Assert.assertEquals("Unexpected entry id when result should be in the next ledger.", 0, a.getEntryId()); Assert.assertEquals("Unexpected ledger id when result should be in the next ledger.", secondLedgerId, a.getLedgerId()); Assert.assertEquals("Unexpected entry id when result should be in the next ledger.", 0, a.getEntryId());