/** * Gets a Sequence number identifying the Ledger inside the log. This is different from getSequence (which identifies * a particular write inside the entire log. It is also different from LedgerId, which is a BookKeeper assigned id. * * @return The result. */ int getLedgerSequence() { return (int) (getSequence() >>> 32); }
/** * Gets a value representing the BookKeeper-assigned Entry id of this address. This entry id is unique per ledger, but * is likely duplicated across ledgers (since it grows sequentially from 0 in each ledger). * * @return The result. */ long getEntryId() { return getSequence() & INT_MASK; }
@Override public int compareTo(LedgerAddress address) { return Long.compare(getSequence(), address.getSequence()); }
@Override public int hashCode() { return Long.hashCode(getSequence()); }
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); }
/** * Tests the various properties as well as translation from the encoded sequence and back. */ @Test(timeout = 5000) public void testSequence() { for (int ledgerSeq = 0; ledgerSeq < LEDGER_COUNT; ledgerSeq++) { long ledgerId = (ledgerSeq + 1) * 31; for (long entryId = 0; entryId < ENTRY_COUNT; entryId++) { val a = new LedgerAddress(ledgerSeq, ledgerId, entryId); assertEquals(a, ledgerSeq, ledgerId, entryId); val a2 = new LedgerAddress(a.getSequence(), ledgerId); assertEquals(a2, ledgerSeq, ledgerId, entryId); } } }
/** * 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()); }