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



##########
File path: 
metadata/src/main/java/org/apache/kafka/timeline/timeline/TimelineHashMap.java
##########
@@ -0,0 +1,414 @@
+/*
+ * 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.AbstractCollection;
+import java.util.AbstractSet;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * This is a hash map which can be snapshotted.
+ *
+ * See {@SnapshottableHashTable} for more details about the implementation.
+ *
+ * This class requires external synchronization.  Null keys and values are not 
supported.
+ *
+ * @param <K>   The key type of the set.
+ * @param <V>   The value type of the set.
+ */
+public class TimelineHashMap<K, V>
+        extends SnapshottableHashTable<TimelineHashMap.TimelineHashMapEntry<K, 
V>>
+        implements Map<K, V> {
+    static class TimelineHashMapEntry<K, V>
+            implements SnapshottableHashTable.ElementWithStartEpoch, 
Map.Entry<K, V> {
+        private final K key;
+        private final V value;
+        private long startEpoch;
+
+        TimelineHashMapEntry(K key, V value) {
+            this.key = key;
+            this.value = value;
+            this.startEpoch = Long.MAX_VALUE;
+        }
+
+        @Override
+        public K getKey() {
+            return key;
+        }
+
+        @Override
+        public V getValue() {
+            return value;
+        }
+
+        @Override
+        public V setValue(V value) {
+            // This would be inefficient to support since we'd need a 
back-reference
+            // to the enclosing map in each Entry object.  There would also be
+            // complications if this entry object was sourced from a 
historical iterator;
+            // we don't support modifying the past.  Since we don't really 
need this API,
+            // let's just not support it.
+            throw new UnsupportedOperationException();
+        }
+
+        @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 TimelineHashMapEntry)) return false;
+            TimelineHashMapEntry<K, V> other = (TimelineHashMapEntry<K, V>) o;
+            return key.equals(other.key);
+        }
+
+        @Override
+        public int hashCode() {
+            return key.hashCode();
+        }
+    }
+
+    public TimelineHashMap(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 containsKey(Object key) {
+        return containsKey(key, Long.MAX_VALUE);
+    }
+
+    public boolean containsKey(Object key, long epoch) {
+        return snapshottableGet(new TimelineHashMapEntry<>(key, null), epoch) 
!= null;
+    }
+
+    @Override
+    public boolean containsValue(Object value) {
+        Iterator<Entry<K, V>> iter = entrySet().iterator();
+        while (iter.hasNext()) {
+            Entry<K, V> e = iter.next();
+            if (value.equals(e.getValue())) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public V get(Object key) {
+        return get(key, Long.MAX_VALUE);
+    }
+
+    public V get(Object key, long epoch) {
+        Entry<K, V> entry =
+            snapshottableGet(new TimelineHashMapEntry<>(key, null), epoch);
+        if (entry == null) {
+            return null;
+        }
+        return entry.getValue();
+    }
+
+    @Override
+    public V put(K key, V value) {
+        Objects.requireNonNull(key);
+        Objects.requireNonNull(value);
+        TimelineHashMapEntry<K, V> entry = new TimelineHashMapEntry<>(key, 
value);
+        TimelineHashMapEntry<K, V> prev = snapshottableAddOrReplace(entry);
+        if (prev == null) {
+            return null;
+        }
+        return prev.getValue();
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public V remove(Object key) {
+        return (V) snapshottableRemove(new TimelineHashMapEntry<>(key, null));
+    }
+
+    @Override
+    public void putAll(Map<? extends K, ? extends V> map) {
+        for (Map.Entry<? extends K, ? extends V> e : map.entrySet()) {
+            put(e.getKey(), e.getValue());
+        }
+    }
+
+    @Override
+    public void clear() {

Review comment:
       Hmm, could we document that? Also, do we have a use case for that? Once 
we clear the top tier, the snapshot may no longer be complete. So, not sure how 
useful it is.




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