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/07/23 09:20:27 UTC

[GitHub] [iceberg] openinx commented on a change in pull request #1234: avro: Extract DecoderResolver to provide cached ResolvingDecoder for resolving avro decoder

openinx commented on a change in pull request #1234:
URL: https://github.com/apache/iceberg/pull/1234#discussion_r459315261



##########
File path: core/src/main/java/org/apache/iceberg/avro/GenericAvroReader.java
##########
@@ -66,28 +61,12 @@ public void setClassLoader(ClassLoader newClassLoader) {
 
   @Override
   public T read(T reuse, Decoder decoder) throws IOException {
-    ResolvingDecoder resolver = resolve(decoder);
+    ResolvingDecoder resolver = DecoderResolver.resolve(decoder, readSchema, fileSchema);
     T value = reader.read(resolver, reuse);
     resolver.drain();
     return value;
   }
 
-  private ResolvingDecoder resolve(Decoder decoder) throws IOException {
-    Map<Schema, Map<Schema, ResolvingDecoder>> cache = DECODER_CACHES.get();
-    Map<Schema, ResolvingDecoder> fileSchemaToResolver = cache
-        .computeIfAbsent(readSchema, k -> new HashMap<>());
-
-    ResolvingDecoder resolver = fileSchemaToResolver.get(fileSchema);
-    if (resolver == null) {
-      resolver = newResolver();
-      fileSchemaToResolver.put(fileSchema, resolver);
-    }
-
-    resolver.configure(decoder);
-
-    return resolver;
-  }
-
   private ResolvingDecoder newResolver() {

Review comment:
       This `newResolver` could also be removed ? 

##########
File path: core/src/main/java/org/apache/iceberg/data/avro/DecoderResolver.java
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.data.avro;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.avro.Schema;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.ResolvingDecoder;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.relocated.com.google.common.collect.MapMaker;
+
+/**
+ * Resolver to resolve {@link Decoder} to a {@link ResolvingDecoder}.
+ * This class uses a {@link ThreadLocal} for caching {@link ResolvingDecoder}.
+ */
+public class DecoderResolver {
+
+  private DecoderResolver() {}
+
+  private static final ThreadLocal<Map<Schema, Map<Schema, ResolvingDecoder>>> DECODER_CACHES =
+      ThreadLocal.withInitial(() -> new MapMaker().weakKeys().makeMap());
+
+  public static ResolvingDecoder resolve(Decoder decoder, Schema readSchema, Schema fileSchema) throws IOException {
+    Map<Schema, Map<Schema, ResolvingDecoder>> cache = DECODER_CACHES.get();
+    Map<Schema, ResolvingDecoder> fileSchemaToResolver = cache
+        .computeIfAbsent(readSchema, k -> new HashMap<>());
+
+    ResolvingDecoder resolver = fileSchemaToResolver.get(fileSchema);
+    if (resolver == null) {
+      resolver = newResolver(readSchema, fileSchema);
+      fileSchemaToResolver.put(fileSchema, resolver);
+    }

Review comment:
       Could simplify those lines as `fileSchemaToResolver.computeIfAbsent`  ?

##########
File path: core/src/main/java/org/apache/iceberg/data/avro/DecoderResolver.java
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.data.avro;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.avro.Schema;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.ResolvingDecoder;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.relocated.com.google.common.collect.MapMaker;
+
+/**
+ * Resolver to resolve {@link Decoder} to a {@link ResolvingDecoder}.
+ * This class uses a {@link ThreadLocal} for caching {@link ResolvingDecoder}.
+ */
+public class DecoderResolver {
+
+  private DecoderResolver() {}
+
+  private static final ThreadLocal<Map<Schema, Map<Schema, ResolvingDecoder>>> DECODER_CACHES =

Review comment:
       Q: Will it have problem when the  GenericAvroReader , DataReader, SparkAvroReader  share the same  cache ?  




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