You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2017/11/26 15:57:09 UTC

[GitHub] sijie closed pull request #770: Make LedgerEntries an Iterable

sijie closed pull request #770: Make LedgerEntries an Iterable<LedgerEntry>
URL: https://github.com/apache/bookkeeper/pull/770
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 5bcb48b4a..1ee193fa1 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 @@
 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 bdb26a4bc..cefbe804c 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 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 void testCannotDeleteLedgerTwice() throws Exception {
         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();


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services