cmccabe commented on a change in pull request #9901: URL: https://github.com/apache/kafka/pull/9901#discussion_r567032323
########## File path: metadata/src/main/java/org/apache/kafka/timeline/SnapshotRegistry.java ########## @@ -0,0 +1,249 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kafka.timeline; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.NoSuchElementException; +import java.util.stream.Collectors; + +import org.apache.kafka.common.utils.LogContext; +import org.slf4j.Logger; + + +/** + * A registry containing snapshots of timeline data structures. + * We generally expect a small number of snapshots-- perhaps 1 or 2 at a time. + * Therefore, we use ArrayLists here rather than a data structure with higher overhead. + */ +public class SnapshotRegistry { + class SnapshotIterator implements ListIterator<Snapshot> { + private Snapshot cur; + private Snapshot lastResult = null; + + SnapshotIterator(Snapshot startAfter) { + this.cur = startAfter; + } + + @Override + public boolean hasNext() { + return cur.next() != head; + } + + @Override + public Snapshot next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + cur = cur.next(); + lastResult = cur; + return cur; + } + + @Override + public boolean hasPrevious() { + return cur != head; + } + + @Override + public Snapshot previous() { + if (!hasPrevious()) { + throw new NoSuchElementException(); + } + Snapshot result = cur; + cur = cur.prev(); + lastResult = result; Review comment: ListIterator#remove is kind of annoying to implement because it can delete things that were returned by next() OR by previous(). Imagine that we have the list 1, 2, 3 and the user called next() and got back 2. Then the user calls remove() and removes 2. A subsequent call to next must return 3. That means cur needs to end up as 1 after remove() is called. Then imagine a different scenario, where the user called previous() and got back 2. Then the user calls remove() and removes 2. A subsequent call to previous must return 1. That means cur needs to end up as 3 after remove() is called. So it is not sufficient to just look at cur when implementing remove() and always do it the same way. That's why lastResult exists. (Another reason lastResult exists is to enforce the invariant that next or previous must be called before any delete) ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org