Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.avro.specific.FixedSize;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.util.ClassUtils;
import org.apache.avro.util.MapUtil;

import java.io.IOException;
import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -63,6 +64,7 @@
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/** Utilities to use existing Java classes and interfaces via reflection. */
public class ReflectData extends SpecificData {
Expand Down Expand Up @@ -826,11 +828,11 @@ public static Schema makeNullable(Schema schema) {
}
}

private static final Map<Class<?>, Field[]> FIELDS_CACHE = new ConcurrentHashMap<>();
private static final ConcurrentMap<Class<?>, Field[]> FIELDS_CACHE = new ConcurrentHashMap<>();

// Return of this class and its superclasses to serialize.
private static Field[] getCachedFields(Class<?> recordClass) {
return FIELDS_CACHE.computeIfAbsent(recordClass, rc -> getFields(rc, true));
return MapUtil.computeIfAbsent(FIELDS_CACHE, recordClass, rc -> getFields(rc, true));
}

private static Field[] getFields(Class<?> recordClass, boolean excludeJava) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.util.ClassUtils;
import org.apache.avro.util.MapUtil;
import org.apache.avro.util.internal.ClassValueCache;

import java.io.ObjectInput;
Expand All @@ -48,6 +49,7 @@
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;

/** Utilities for generated Java classes and interfaces. */
Expand Down Expand Up @@ -228,7 +230,7 @@ protected Schema getEnumSchema(Object datum) {
return (datum instanceof Enum) ? getSchema(datum.getClass()) : super.getEnumSchema(datum);
}

private Map<String, Class> classCache = new ConcurrentHashMap<>();
private final ConcurrentMap<String, Class> classCache = new ConcurrentHashMap<>();

private static final Class NO_CLASS = new Object() {
}.getClass();
Expand All @@ -251,7 +253,7 @@ public Class getClass(Schema schema) {
String name = schema.getFullName();
if (name == null)
return null;
Class<?> c = classCache.computeIfAbsent(name, n -> {
Class<?> c = MapUtil.computeIfAbsent(classCache, name, n -> {
try {
return ClassUtils.forName(getClassLoader(), getClassName(schema));
} catch (ClassNotFoundException e) {
Expand Down
45 changes: 45 additions & 0 deletions lang/java/avro/src/main/java/org/apache/avro/util/MapUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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
*
* https://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.avro.util;

import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;

public class MapUtil {

private MapUtil() {
super();
}

/**
* A temporary workaround for Java 8 specific performance issue JDK-8161372
* .<br>
* This class should be removed once we drop Java 8 support.
*
* @see <a href=
* "https://bugs.openjdk.java.net/browse/JDK-8161372">JDK-8161372</a>
*/
public static <K, V> V computeIfAbsent(ConcurrentMap<K, V> map, K key, Function<K, V> mappingFunction) {
V value = map.get(key);
if (value != null) {
return value;
}
return map.computeIfAbsent(key, mappingFunction::apply);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.concurrent.ConcurrentMap;

import io.grpc.MethodDescriptor;
import org.apache.avro.util.MapUtil;

import static io.grpc.MethodDescriptor.generateFullMethodName;

Expand All @@ -49,7 +50,7 @@ private ServiceDescriptor(Class iface, String serviceName) {
*/
public static ServiceDescriptor create(Class iface) {
String serviceName = AvroGrpcUtils.getServiceName(iface);
return SERVICE_DESCRIPTORS.computeIfAbsent(serviceName, key -> new ServiceDescriptor(iface, serviceName));
return MapUtil.computeIfAbsent(SERVICE_DESCRIPTORS, serviceName, key -> new ServiceDescriptor(iface, serviceName));
}

/**
Expand All @@ -67,7 +68,7 @@ public String getServiceName() {
* @return a {@link MethodDescriptor}
*/
public MethodDescriptor<Object[], Object> getMethod(String methodName, MethodDescriptor.MethodType methodType) {
return methods.computeIfAbsent(methodName,
return MapUtil.computeIfAbsent(methods, methodName,
key -> MethodDescriptor.<Object[], Object>newBuilder()
.setFullMethodName(generateFullMethodName(serviceName, methodName)).setType(methodType)
.setRequestMarshaller(new AvroRequestMarshaller(protocol.getMessages().get(methodName)))
Expand Down