eolivelli commented on a change in pull request #1931: (WIP)
GetListOfEntriesOfLedger implementation
URL: https://github.com/apache/bookkeeper/pull/1931#discussion_r253196109
##########
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:
so you are implement some kind of merge join between the streams, do I
understand correctly ?
we can create an utility class "MergeSortIterator" and create specific tests,
having such complexity here is hard to test and to be sure that corner cases
are covered by tests
something like
https://github.com/apache/calcite/blob/4814943895d609813878763faf0d38adb624eac4/linq4j/src/main/java/org/apache/calcite/linq4j/EnumerableDefaults.java#L1364
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services