cmccabe commented on a change in pull request #9901:
URL: https://github.com/apache/kafka/pull/9901#discussion_r566481305



##########
File path: 
metadata/src/main/java/org/apache/kafka/timeline/timeline/TimelineHashSet.java
##########
@@ -0,0 +1,260 @@
+/*
+ * 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.Collection;
+import java.util.Iterator;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * This is a hash set which can be snapshotted.
+ *
+ * See {@SnapshottableHashTable} for more details about the implementation.
+ *
+ * This class requires external synchronization.  Null values are not 
supported.
+ *
+ * @param <T>   The value type of the set.
+ */
+public class TimelineHashSet<T>
+        extends SnapshottableHashTable<TimelineHashSet.TimelineHashSetEntry<T>>
+        implements Set<T> {
+    static class TimelineHashSetEntry<T>
+            implements SnapshottableHashTable.ElementWithStartEpoch {
+        private final T value;
+        private long startEpoch;
+
+        TimelineHashSetEntry(T value) {
+            this.value = value;
+            this.startEpoch = Long.MAX_VALUE;
+        }
+
+        public T getValue() {
+            return value;
+        }
+
+        @Override
+        public void setStartEpoch(long startEpoch) {
+            this.startEpoch = startEpoch;
+        }
+
+        @Override
+        public long startEpoch() {
+            return startEpoch;
+        }
+
+        @SuppressWarnings("unchecked")
+        @Override
+        public boolean equals(Object o) {
+            if (!(o instanceof TimelineHashSetEntry)) return false;
+            TimelineHashSetEntry<T> other = (TimelineHashSetEntry<T>) o;
+            return value.equals(other.value);
+        }
+
+        @Override
+        public int hashCode() {
+            return value.hashCode();
+        }
+    }
+
+    public TimelineHashSet(SnapshotRegistry snapshotRegistry, int 
expectedSize) {
+        super(snapshotRegistry, expectedSize);
+    }
+
+    @Override
+    public int size() {
+        return size(Long.MAX_VALUE);
+    }
+
+    public int size(long epoch) {
+        return snapshottableSize(epoch);
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return isEmpty(Long.MAX_VALUE);
+    }
+
+    public boolean isEmpty(long epoch) {
+        return snapshottableSize(epoch) == 0;
+    }
+
+    @Override
+    public boolean contains(Object key) {
+        return contains(key, Long.MAX_VALUE);
+    }
+
+    public boolean contains(Object object, long epoch) {
+        return snapshottableGet(new TimelineHashSetEntry<>(object), epoch) != 
null;
+    }
+
+    final class ValueIterator implements Iterator<T> {
+        private final Iterator<TimelineHashSetEntry<T>> iter;
+
+        ValueIterator(long epoch) {
+            this.iter = snapshottableIterator(epoch);
+        }
+
+        @Override
+        public boolean hasNext() {
+            return iter.hasNext();
+        }
+
+        @Override
+        public T next() {
+            return iter.next().value;
+        }
+
+        @Override
+        public void remove() {
+            iter.remove();
+        }
+    }
+
+    @Override
+    public Iterator<T> iterator() {
+        return iterator(Long.MAX_VALUE);
+    }
+
+    public Iterator<T> iterator(long epoch) {
+        return new ValueIterator(epoch);
+    }
+
+    @Override
+    public Object[] toArray() {
+        Object[] result = new Object[size()];
+        Iterator<T> iter = iterator();
+        int i = 0;
+        while (iter.hasNext()) {
+            result[i++] = iter.next();
+        }
+        return result;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public <R> R[] toArray(R[] a) {
+        int size = size();
+        if (size <= a.length) {
+            Iterator<T> iter = iterator();
+            int i = 0;
+            while (iter.hasNext()) {
+                a[i++] = (R) iter.next();
+            }
+            while (i < a.length) {
+                a[i++] = null;
+            }
+            return a;
+        } else {
+            return (R[]) toArray();
+        }
+    }
+
+    @Override
+    public boolean add(T newValue) {
+        Objects.requireNonNull(newValue);
+        return snapshottableAddUnlessPresent(new 
TimelineHashSetEntry<>(newValue));
+    }
+
+    @Override
+    public boolean remove(Object value) {
+        return snapshottableRemove(new TimelineHashSetEntry<>(value)) != null;
+    }
+
+    @Override
+    public boolean containsAll(Collection<?> collection) {
+        for (Object value : collection) {
+            if (!contains(value)) return false;
+        }
+        return true;
+    }
+
+    @Override
+    public boolean addAll(Collection<? extends T> collection) {
+        boolean modified = false;
+        for (T value : collection) {
+            if (add(value)) {
+                modified = true;
+            }
+        }
+        return modified;
+    }
+
+    @Override
+    public boolean retainAll(Collection<?> collection) {
+        Objects.requireNonNull(collection);
+        boolean modified = false;
+        Iterator<T> it = iterator();
+        while (it.hasNext()) {
+            if (!collection.contains(it.next())) {
+                it.remove();
+                modified = true;
+            }
+        }
+        return modified;
+    }
+
+    @Override
+    public boolean removeAll(Collection<?> collection) {
+        Objects.requireNonNull(collection);
+        boolean modified = false;
+        Iterator<?> it = iterator();
+        while (it.hasNext()) {
+            if (collection.contains(it.next())) {
+                it.remove();
+                modified = true;
+            }
+        }
+        return modified;
+    }
+
+    @Override
+    public void clear() {

Review comment:
       clear just changes the current state, it doesn't affect snapshotted state




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


Reply via email to