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 2019/02/01 23:10:40 UTC

[GitHub] athanatos commented on a change in pull request #1931: (WIP) GetListOfEntriesOfLedger implementation

athanatos commented on a change in pull request #1931: (WIP) GetListOfEntriesOfLedger implementation
URL: https://github.com/apache/bookkeeper/pull/1931#discussion_r253228615
 
 

 ##########
 File path: bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/EntryMemTable.java
 ##########
 @@ -456,4 +459,95 @@ boolean isEmpty() {
     public void close() throws Exception {
         // no-op
     }
+
+    /*
+     * returns the Iterator of EntryKeys of a ledger available in this
+     * EntryMemTable. It would be in the ascending order and this Iterator is
+     * weakly consistent.
+     */
+    Iterator<EntryKey> getEntriesOfALedger(long ledgerId) {
+        EntryKey thisLedgerFloorEntry = new EntryKey(ledgerId, 0);
+        EntryKey thisLedgerCeilingEntry = new EntryKey(ledgerId, LONG_MAX_VALUE);
+        Iterator<EntryKey> thisLedgerEntriesInKVMap;
+        Iterator<EntryKey> thisLedgerEntriesInSnapshot;
+        this.lock.readLock().lock();
+        try {
+            /*
+             * Gets a view of the portion of this map that corresponds to
+             * entries of this ledger.
+             *
+             * Here 'kvmap' is of type 'ConcurrentSkipListMap', so its 'subMap'
+             * call would return a view of the portion of this map whose keys
+             * range from fromKey to toKey and it would be of type
+             * 'ConcurrentNavigableMap'. ConcurrentNavigableMap's 'keySet' would
+             * return NavigableSet view of the keys contained in this map. This
+             * view's iterator would be weakly consistent -
+             * https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/
+             * package-summary.html#Weakly.
+             *
+             * 'weakly consistent' would guarantee 'to traverse elements as they
+             * existed upon construction exactly once, and may (but are not
+             * guaranteed to) reflect any modifications subsequent to
+             * construction.'
+             *
+             */
+            thisLedgerEntriesInKVMap = this.kvmap.subMap(thisLedgerFloorEntry, thisLedgerCeilingEntry).keySet()
+                    .iterator();
+            thisLedgerEntriesInSnapshot = this.snapshot.subMap(thisLedgerFloorEntry, thisLedgerCeilingEntry).keySet()
+                    .iterator();
+        } finally {
+            this.lock.readLock().unlock();
+        }
+        return new Iterator<EntryKey>() {
+            private EntryKey curKVMapEntry = null;
+            private EntryKey curSnapshotEntry = null;
+            private boolean hasToPreFetch = true;
+
+            @Override
+            public boolean hasNext() {
+                if (hasToPreFetch) {
+                    if (curKVMapEntry == null) {
+                        curKVMapEntry = thisLedgerEntriesInKVMap.hasNext() ? thisLedgerEntriesInKVMap.next() : null;
+                    }
+                    if (curSnapshotEntry == null) {
+                        curSnapshotEntry = thisLedgerEntriesInSnapshot.hasNext() ? thisLedgerEntriesInSnapshot.next()
+                                : null;
+                    }
+                }
+                hasToPreFetch = false;
+                return (curKVMapEntry != null || curSnapshotEntry != null);
+            }
+
+            @Override
+            public EntryKey next() {
+                if (hasNext()) {
+                    EntryKey returnEntryKey = null;
+                    if (curKVMapEntry != null && curSnapshotEntry != null) {
 
 Review comment:
   Agreed.

----------------------------------------------------------------
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