You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2020/12/01 00:56:29 UTC

[GitHub] [iceberg] rdblue commented on a change in pull request #1853: Core: Add utility StructLikeMap.

rdblue commented on a change in pull request #1853:
URL: https://github.com/apache/iceberg/pull/1853#discussion_r533002236



##########
File path: core/src/main/java/org/apache/iceberg/util/StructLikeMap.java
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.iceberg.util;
+
+import java.util.AbstractMap;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.types.Types;
+
+public class StructLikeMap<T> extends AbstractMap<StructLike, T> implements Map<StructLike, T> {
+
+  public static <T> StructLikeMap<T> create(Types.StructType type) {
+    return new StructLikeMap<>(type);
+  }
+
+  private final Types.StructType type;
+  private final Map<StructLikeWrapper, T> wrapperMap;
+  private final ThreadLocal<StructLikeWrapper> wrappers;
+
+  private StructLikeMap(Types.StructType type) {
+    this.type = type;
+    this.wrapperMap = Maps.newHashMap();
+    this.wrappers = ThreadLocal.withInitial(() -> StructLikeWrapper.forType(type));
+  }
+
+  @Override
+  public int size() {
+    return wrapperMap.size();
+  }
+
+  @Override
+  public boolean isEmpty() {
+    return wrapperMap.isEmpty();
+  }
+
+  @Override
+  public boolean containsKey(Object key) {
+    if (key instanceof StructLike) {
+      StructLikeWrapper wrapper = wrappers.get();
+      boolean result = wrapperMap.containsKey(wrapper.set((StructLike) key));
+      wrapper.set(null);
+      return result;
+    }
+    return false;
+  }
+
+  @Override
+  public boolean containsValue(Object value) {
+    return wrapperMap.containsValue(value);
+  }
+
+  @Override
+  public T get(Object key) {
+    if (key instanceof StructLike) {
+      StructLikeWrapper wrapper = wrappers.get();
+      T value = wrapperMap.get(wrapper.set((StructLike) key));
+      wrapper.set(null);
+      return value;
+    }
+    return null;
+  }
+
+  @Override
+  public T put(StructLike key, T value) {
+    return wrapperMap.put(StructLikeWrapper.forType(type).set(key), value);
+  }
+
+  @Override
+  public T remove(Object key) {
+    if (key instanceof StructLike) {
+      StructLikeWrapper wrapper = wrappers.get();
+      T value = wrapperMap.remove(wrapper.set((StructLike) key));
+      wrapper.set(null); // don't hold a reference to the value.
+      return value;
+    }
+    return null;
+  }
+
+  @Override
+  public void putAll(Map<? extends StructLike, ? extends T> keyValues) {

Review comment:
       This can be delegated to the implementation in `AbstractMap`, since `put` will automatically wrap the key.

##########
File path: core/src/main/java/org/apache/iceberg/util/StructLikeMap.java
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.iceberg.util;
+
+import java.util.AbstractMap;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.types.Types;
+
+public class StructLikeMap<T> extends AbstractMap<StructLike, T> implements Map<StructLike, T> {
+
+  public static <T> StructLikeMap<T> create(Types.StructType type) {
+    return new StructLikeMap<>(type);
+  }
+
+  private final Types.StructType type;
+  private final Map<StructLikeWrapper, T> wrapperMap;
+  private final ThreadLocal<StructLikeWrapper> wrappers;
+
+  private StructLikeMap(Types.StructType type) {
+    this.type = type;
+    this.wrapperMap = Maps.newHashMap();
+    this.wrappers = ThreadLocal.withInitial(() -> StructLikeWrapper.forType(type));
+  }
+
+  @Override
+  public int size() {
+    return wrapperMap.size();
+  }
+
+  @Override
+  public boolean isEmpty() {
+    return wrapperMap.isEmpty();
+  }
+
+  @Override
+  public boolean containsKey(Object key) {
+    if (key instanceof StructLike) {
+      StructLikeWrapper wrapper = wrappers.get();
+      boolean result = wrapperMap.containsKey(wrapper.set((StructLike) key));
+      wrapper.set(null);
+      return result;
+    }
+    return false;
+  }
+
+  @Override
+  public boolean containsValue(Object value) {
+    return wrapperMap.containsValue(value);
+  }
+
+  @Override
+  public T get(Object key) {
+    if (key instanceof StructLike) {
+      StructLikeWrapper wrapper = wrappers.get();
+      T value = wrapperMap.get(wrapper.set((StructLike) key));
+      wrapper.set(null);
+      return value;
+    }
+    return null;
+  }
+
+  @Override
+  public T put(StructLike key, T value) {
+    return wrapperMap.put(StructLikeWrapper.forType(type).set(key), value);
+  }
+
+  @Override
+  public T remove(Object key) {
+    if (key instanceof StructLike) {
+      StructLikeWrapper wrapper = wrappers.get();
+      T value = wrapperMap.remove(wrapper.set((StructLike) key));
+      wrapper.set(null); // don't hold a reference to the value.
+      return value;
+    }
+    return null;
+  }
+
+  @Override
+  public void putAll(Map<? extends StructLike, ? extends T> keyValues) {
+    if (keyValues.size() > 0) {

Review comment:
       I don't think there is much value in this size check. If it is 0, then the `entrySet` will be empty and the loop will run 0 times.

##########
File path: core/src/main/java/org/apache/iceberg/util/StructLikeMap.java
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.iceberg.util;
+
+import java.util.AbstractMap;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.types.Types;
+
+public class StructLikeMap<T> extends AbstractMap<StructLike, T> implements Map<StructLike, T> {
+
+  public static <T> StructLikeMap<T> create(Types.StructType type) {
+    return new StructLikeMap<>(type);
+  }
+
+  private final Types.StructType type;
+  private final Map<StructLikeWrapper, T> wrapperMap;
+  private final ThreadLocal<StructLikeWrapper> wrappers;
+
+  private StructLikeMap(Types.StructType type) {
+    this.type = type;
+    this.wrapperMap = Maps.newHashMap();
+    this.wrappers = ThreadLocal.withInitial(() -> StructLikeWrapper.forType(type));
+  }
+
+  @Override
+  public int size() {
+    return wrapperMap.size();
+  }
+
+  @Override
+  public boolean isEmpty() {
+    return wrapperMap.isEmpty();
+  }
+
+  @Override
+  public boolean containsKey(Object key) {
+    if (key instanceof StructLike) {
+      StructLikeWrapper wrapper = wrappers.get();
+      boolean result = wrapperMap.containsKey(wrapper.set((StructLike) key));
+      wrapper.set(null);
+      return result;
+    }
+    return false;
+  }
+
+  @Override
+  public boolean containsValue(Object value) {
+    return wrapperMap.containsValue(value);
+  }
+
+  @Override
+  public T get(Object key) {
+    if (key instanceof StructLike) {
+      StructLikeWrapper wrapper = wrappers.get();
+      T value = wrapperMap.get(wrapper.set((StructLike) key));
+      wrapper.set(null);
+      return value;
+    }
+    return null;
+  }
+
+  @Override
+  public T put(StructLike key, T value) {
+    return wrapperMap.put(StructLikeWrapper.forType(type).set(key), value);
+  }
+
+  @Override
+  public T remove(Object key) {
+    if (key instanceof StructLike) {
+      StructLikeWrapper wrapper = wrappers.get();
+      T value = wrapperMap.remove(wrapper.set((StructLike) key));
+      wrapper.set(null); // don't hold a reference to the value.
+      return value;
+    }
+    return null;
+  }
+
+  @Override
+  public void putAll(Map<? extends StructLike, ? extends T> keyValues) {
+    if (keyValues.size() > 0) {
+      for (Map.Entry<? extends StructLike, ? extends T> e : keyValues.entrySet()) {
+        wrapperMap.put(StructLikeWrapper.forType(type).set(e.getKey()), e.getValue());

Review comment:
       I think it would be better to call `put` each time through the loop so this logic isn't duplicated here and in `put`.




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org