This is an automated email from the ASF dual-hosted git repository. sijie pushed a commit to branch branch-4.6 in repository https://gitbox.apache.org/repos/asf/bookkeeper.git
commit 4a8b1f13d4574b2e91b34f8cdfd3aa0b5ee9cb9c Author: Ivan Kelly <iv...@apache.org> AuthorDate: Sun Nov 26 07:56:57 2017 -0800 Make LedgerEntries an Iterable<LedgerEntry> This seems to have been overlooked in the previous changes. As the title says, this change just adds the Iterable<LedgerEntry> to the interfaces from which LedgerEntries extends. Author: Ivan Kelly <iv...@apache.org> Reviewers: Enrico Olivelli <eolive...@gmail.com>, Sijie Guo <si...@apache.org> This closes #770 from ivankelly/ledger-entries-iterable --- .../bookkeeper/client/api/LedgerEntries.java | 13 ++++--- .../bookkeeper/client/api/BookKeeperApiTest.java | 40 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/LedgerEntries.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/LedgerEntries.java index 5bcb48b..1ee193f 100644 --- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/LedgerEntries.java +++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/LedgerEntries.java @@ -20,27 +20,32 @@ package org.apache.bookkeeper.client.api; import java.util.Iterator; /** - * Interface to wrap the entries. + * Interface to wrap a sequence of entries. * * @since 4.6 */ -public interface LedgerEntries extends AutoCloseable { +public interface LedgerEntries + extends AutoCloseable, Iterable<LedgerEntry> { /** * Gets a specific LedgerEntry by entryId. * * @param entryId the LedgerEntry id - * @return the LedgerEntry, null if no LedgerEntry with such entryId. + * @return the LedgerEntry, null if no LedgerEntry with such entryId */ LedgerEntry getEntry(long entryId); /** + * Get an iterator over all the ledger entries contained in the + * LedgerEntries object. + * * Calling this method does not modify the reference count of the ByteBuf in the returned LedgerEntry objects. * The caller who calls {@link #iterator()} should make sure that they do not call ByteBuf.release() on the * LedgerEntry objects to avoid a double free. * All reference counts will be decremented when the containing LedgerEntries object is closed. * - * @return the iterator of type LedgerEntry. + * @return an iterator of LedgerEntry objects */ + @Override Iterator<LedgerEntry> iterator(); } diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java index bdb26a4..cefbe80 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java @@ -28,6 +28,7 @@ import static org.junit.Assert.assertEquals; import io.netty.buffer.Unpooled; import java.nio.ByteBuffer; import java.util.Iterator; +import java.util.concurrent.atomic.AtomicLong; import org.apache.bookkeeper.client.BKException; import org.apache.bookkeeper.client.BKException.BKDigestMatchException; import org.apache.bookkeeper.client.BKException.BKDuplicateEntryIdException; @@ -276,6 +277,45 @@ public class BookKeeperApiTest extends MockBookKeeperTestCase { result(newDeleteLedgerOp().withLedgerId(lId).execute()); } + @Test + public void testLedgerEntriesIterable() throws Exception { + long lId; + try (WriteHandle writer = newCreateLedgerOp() + .withAckQuorumSize(1) + .withWriteQuorumSize(2) + .withEnsembleSize(3) + .withPassword(password) + .execute().get()) { + lId = writer.getId(); + // write data and populate LastAddConfirmed + result(writer.append(ByteBuffer.wrap(data))); + result(writer.append(ByteBuffer.wrap(data))); + result(writer.append(ByteBuffer.wrap(data))); + } + + try (ReadHandle reader = newOpenLedgerOp() + .withPassword(password) + .withRecovery(false) + .withLedgerId(lId) + .execute().get()) { + long lac = reader.getLastAddConfirmed(); + assertEquals(2, lac); + + try (LedgerEntries entries = reader.read(0, lac).get()) { + AtomicLong i = new AtomicLong(0); + for (LedgerEntry e : entries) { + assertEquals(i.getAndIncrement(), e.getEntryId()); + assertArrayEquals(data, e.getEntryBytes()); + } + i.set(0); + entries.forEach((e) -> { + assertEquals(i.getAndIncrement(), e.getEntryId()); + assertArrayEquals(data, e.getEntryBytes()); + }); + } + } + } + private static void checkEntries(LedgerEntries entries, byte[] data) throws InterruptedException, BKException { Iterator<LedgerEntry> iterator = entries.iterator(); -- To stop receiving notification emails like this one, please contact "commits@bookkeeper.apache.org" <commits@bookkeeper.apache.org>.