/** * Indicates that this write completed successfully. This will set the final result on the externalCompletion future. */ Timer complete() { Preconditions.checkState(this.entryId.get() >= 0, "entryId not set; cannot complete Write."); this.failureCause.set(null); this.result.complete(new LedgerAddress(this.writeLedger.get().metadata, this.entryId.get())); return endAttempt(); }
@Override protected LogAddress createLogAddress(long seqNo) { return new LedgerAddress(seqNo, seqNo); }
@SneakyThrows(IOException.class) ReadItem(LedgerEntry entry, LedgerMetadata ledgerMetadata) { this.address = new LedgerAddress(ledgerMetadata, entry.getEntryId()); this.payload = entry.getEntryInputStream(); this.length = this.payload.available(); }
if (address.getLedgerSequence() < firstLedger.getSequence()) { result = new LedgerAddress(firstLedger, 0); } else if (address.getEntryId() < lastEntryId) { result = new LedgerAddress(address.getLedgerSequence(), address.getLedgerId(), address.getEntryId() + 1); } else { result = new LedgerAddress(ledgerMetadata, 0);
/** * 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); } } }
private void read00(RevisionDataInput input, LogMetadata.LogMetadataBuilder builder) throws IOException { builder.enabled(input.readBoolean()); builder.epoch(input.readCompactLong()); builder.truncationAddress(new LedgerAddress(input.readCompactLong(), input.readCompactLong())); List<LedgerMetadata> ledgers = input.readCollection(this::readLedger00, ArrayList::new); builder.ledgers(Collections.unmodifiableList(ledgers)); builder.updateVersion(INITIAL_VERSION); }
@Override public DurableDataLog.ReadItem getNext() throws DurableDataLogException { Exceptions.checkNotClosed(this.closed.get(), this); if (this.currentLedger == null) { // First time we call this. Locate the first ledger based on the metadata truncation address. We don't know // how many entries are in that first ledger, so open it anyway so we can figure out. openNextLedger(this.metadata.getNextAddress(this.metadata.getTruncationAddress(), Long.MAX_VALUE)); } while (this.currentLedger != null && (!this.currentLedger.canRead())) { // We have reached the end of the current ledger. Find next one, and skip over empty ledgers). val lastAddress = new LedgerAddress(this.currentLedger.metadata, this.currentLedger.handle.getLastAddConfirmed()); Ledgers.close(this.currentLedger.handle); openNextLedger(this.metadata.getNextAddress(lastAddress, this.currentLedger.handle.getLastAddConfirmed())); } // Try to read from the current reader. if (this.currentLedger == null || this.currentLedger.reader == null) { return null; } return new LogReader.ReadItem(this.currentLedger.reader.nextElement(), this.currentLedger.metadata); }
/** * Tests the Compare method. */ @Test(timeout = 5000) public void testCompare() { val addresses = new ArrayList<LedgerAddress>(); for (int ledgerSeq = 0; ledgerSeq < LEDGER_COUNT; ledgerSeq++) { long ledgerId = (ledgerSeq + 1) * 31; for (long entryId = 0; entryId < ENTRY_COUNT; entryId++) { addresses.add(new LedgerAddress(ledgerSeq, ledgerId, entryId)); } } for (int i = 0; i < addresses.size() / 2; i++) { val a1 = addresses.get(i); val a2 = addresses.get(addresses.size() - i - 1); val result1 = a1.compareTo(a2); val result2 = a2.compareTo(a1); AssertExtensions.assertLessThan("Unexpected when comparing smaller to larger.", 0, result1); AssertExtensions.assertGreaterThan("Unexpected when comparing larger to smaller.", 0, result2); Assert.assertEquals("Unexpected when comparing to itself.", 0, a1.compareTo(a1)); } }
val truncatedMetadata = metadata.truncate(new LedgerAddress(0, ledgerId, 123));
public void testGetNextAddress() { val empty = new LogMetadata(1).truncate(new LedgerAddress(2, 2, 2)); Assert.assertNull("Unexpected result from empty metadata", empty.getNextAddress(new LedgerAddress(1, 1, 0), Long.MAX_VALUE)); 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()); LedgerAddress truncationAddress = new LedgerAddress(secondLedgerSeq, secondLedgerId, 1); val m2 = m.truncate(truncationAddress); a = m2.getNextAddress(new LedgerAddress(firstLedgerSeq, firstLedgerId, 0), Long.MAX_VALUE); Assert.assertEquals("Unexpected result when input address less than truncation address.", 0, truncationAddress.compareTo(a)); a = m.getNextAddress(new LedgerAddress(firstLedgerSeq, firstLedgerId, 0), 2); 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()); a = m.getNextAddress(new LedgerAddress(lm.getSequence(), lm.getLedgerId(), 3), 3); if (i == m.getLedgers().size() - 1) { Assert.assertNull("Unexpected result when reached the end of the log.", a); a = m.getNextAddress(new LedgerAddress(firstLedgerSeq + 1, firstLedgerId + 1, 3), 3); 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());