Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(java): simplify generated codec name #1850

Merged
merged 3 commits into from
Sep 24, 2024
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 @@ -27,6 +27,7 @@
import java.util.Collection;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.fury.codegen.CodeGenerator;
import org.apache.fury.codegen.CodegenContext;
import org.apache.fury.codegen.CompileUnit;
Expand All @@ -53,11 +54,18 @@ public class AccessorHelper {
private static final String OBJ_NAME = "obj";
private static final String FIELD_VALUE = "fieldValue";

// Must be static to be shared across the whole process life.
private static final Map<String, Integer> idGenerator = new ConcurrentHashMap<>();

public static String accessorClassName(Class<?> beanClass) {
String name =
ReflectionUtils.getClassNameWithoutPackage(beanClass)
+ "FuryAccessor_"
+ CodeGenerator.getClassUniqueId(beanClass);
String key = CodeGenerator.getClassUniqueId(beanClass);
Integer id = idGenerator.get(key);
if (id == null) {
synchronized (idGenerator) {
id = idGenerator.computeIfAbsent(key, k -> idGenerator.size());
}
}
String name = ReflectionUtils.getClassNameWithoutPackage(beanClass) + "FuryAccessor_" + id;
return name.replace("$", "_");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.fury.Fury;
Expand Down Expand Up @@ -157,6 +158,9 @@ public BaseObjectCodecBuilder(TypeRef<?> beanType, Fury fury, Class<?> parentSer
jitCallbackUpdateFields = new HashMap<>();
}

// Must be static to be shared across the whole process life.
private static final Map<String, Map<String, Integer>> idGenerator = new ConcurrentHashMap<>();

public String codecClassName(Class<?> beanClass) {
String name = ReflectionUtils.getClassNameWithoutPackage(beanClass).replace("$", "_");
StringBuilder nameBuilder = new StringBuilder(name);
Expand All @@ -167,12 +171,17 @@ public String codecClassName(Class<?> beanClass) {
} else {
nameBuilder.append("Fury");
}
nameBuilder.append(codecSuffix()).append("Codec");
nameBuilder.append('_').append(fury.getConfig().getConfigHash());
String classUniqueId = CodeGenerator.getClassUniqueId(beanClass);
if (StringUtils.isNotBlank(classUniqueId)) {
nameBuilder.append('_').append(classUniqueId);
nameBuilder.append("Codec").append(codecSuffix());
Map<String, Integer> subGenerator =
idGenerator.computeIfAbsent(nameBuilder.toString(), k -> new ConcurrentHashMap<>());
String key = fury.getConfig().getConfigHash() + "_" + CodeGenerator.getClassUniqueId(beanClass);
Integer id = subGenerator.get(key);
if (id == null) {
synchronized (subGenerator) {
id = subGenerator.computeIfAbsent(key, k -> subGenerator.size());
}
}
nameBuilder.append('_').append(id);
return nameBuilder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import static org.apache.fury.builder.Generated.GeneratedMetaSharedSerializer.SERIALIZER_FIELD_NAME;

import java.util.Collection;
import java.util.Map;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.fury.Fury;
import org.apache.fury.builder.Generated.GeneratedMetaSharedSerializer;
import org.apache.fury.codegen.CodeGenerator;
Expand Down Expand Up @@ -86,11 +88,20 @@ public MetaSharedCodecBuilder(TypeRef<?> beanType, Fury fury, ClassDef classDef)
new ObjectCodecOptimizer(beanClass, grouper, !fury.isBasicTypesRefIgnored(), ctx);
}

// Must be static to be shared across the whole process life.
private static final Map<Long, Integer> idGenerator = new ConcurrentHashMap<>();

@Override
protected String codecSuffix() {
// For every class def sent from different peer, if the class def are different, then
// a new serializer needs being generated.
return "MetaShared" + classDef.getId();
Integer id = idGenerator.get(classDef.getId());
if (id == null) {
synchronized (idGenerator) {
id = idGenerator.computeIfAbsent(classDef.getId(), k -> idGenerator.size());
}
}
return "MetaShared" + id;
}

@Override
Expand Down
Loading