Skip to content

Commit

Permalink
fix cache
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed Oct 3, 2023
1 parent 909ce89 commit 40e72cc
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public void testRecordCompatible(boolean codegen) throws Throwable {
String code1 =
"import java.util.*;"
+ "public record TestRecord(int f1, String f2, List<String> f3, char f4, Map<String, Integer> f5) {}";
Class<?> cls1 = Struct.createStructClass("TestRecord", code1);
Class<?> cls1 =
Struct.createStructClass(
"TestRecord", code1, RecordSerializersTest.class + "testRecordCompatible_1");
Object record1 =
RecordUtils.getRecordConstructor(cls1)
.f1
Expand All @@ -114,7 +116,9 @@ public void testRecordCompatible(boolean codegen) throws Throwable {
String code2 =
"import java.util.*;"
+ "public record TestRecord(int f1, String f2, char f4, Map<String, Integer> f5) {}";
Class<?> cls2 = Struct.createStructClass("TestRecord", code2);
Class<?> cls2 =
Struct.createStructClass(
"TestRecord", code2, RecordSerializersTest.class + "testRecordCompatible_2");
Object record2 =
RecordUtils.getRecordConstructor(cls2).f1.invoke(1, "abc", 'a', ofHashMap("a", 1));
Fury fury2 =
Expand All @@ -134,7 +138,9 @@ public void testRecordMetaShare(boolean codegen) throws Throwable {
String code1 =
"import java.util.*;"
+ "public record TestRecord(int f1, String f2, List<String> f3, char f4, Map<String, Integer> f5) {}";
Class<?> cls1 = Struct.createStructClass("TestRecord", code1);
Class<?> cls1 =
Struct.createStructClass(
"TestRecord", code1, RecordSerializersTest.class + "testRecordMetaShare_1");
Object record1 =
RecordUtils.getRecordConstructor(cls1)
.f1
Expand All @@ -150,7 +156,9 @@ public void testRecordMetaShare(boolean codegen) throws Throwable {
String code2 =
"import java.util.*;"
+ "public record TestRecord(String f2, char f4, Map<String, Integer> f5) {}";
Class<?> cls2 = Struct.createStructClass("TestRecord", code2);
Class<?> cls2 =
Struct.createStructClass(
"TestRecord", code2, RecordSerializersTest.class + "testRecordMetaShare_2");
Object record2 =
RecordUtils.getRecordConstructor(cls2).f1.invoke("abc", 'a', ofHashMap("a", 1));
Fury fury2 =
Expand Down
12 changes: 6 additions & 6 deletions java/fury-core/src/test/java/io/fury/serializer/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static Class<?> createCompatibleClass1() {
+ " private int[][] int2DArray;\n"
+ " private int[][] int2DArray_added;\n"
+ "}";
return loadClass(BeanA.class, code);
return loadClass(BeanA.class, code, ClassUtils.class + "createCompatibleClass1");
}

public static Class<?> createCompatibleClass2() {
Expand All @@ -90,7 +90,7 @@ public static Class<?> createCompatibleClass2() {
+ " public Map<String, String> map2;\n"
+ " public SortedMap<Integer, Integer> sortedMap3;"
+ "}";
return loadClass(CollectionFields.class, code);
return loadClass(CollectionFields.class, code, ClassUtils.class + "createCompatibleClass2");
}

public static Class<?> createCompatibleClass3() {
Expand Down Expand Up @@ -121,13 +121,13 @@ public static Class<?> createCompatibleClass3() {
+ " public Map singletonMap;\n"
+ " public Map<String, Integer> singletonMap2;\n"
+ "}";
return loadClass(MapFields.class, code);
return loadClass(MapFields.class, code, ClassUtils.class + "createCompatibleClass3");
}

private static final ConcurrentHashMap<String, Class<?>> classCache = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<Object, Class<?>> classCache = new ConcurrentHashMap<>();

static Class<?> loadClass(Class<?> cls, String code) {
return classCache.computeIfAbsent(code, k -> compileClass(cls, code));
static Class<?> loadClass(Class<?> cls, String code, Object cacheKey) {
return classCache.computeIfAbsent(cacheKey, k -> compileClass(cls, code));
}

private static Class<?> compileClass(Class<?> cls, String code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ public void testWriteCompatibleCollectionBasic(boolean referenceTracking, boolea
+ " private Iterable<BeanB> beanBIterable;\n"
+ " private List<BeanB> beanBList;\n"
+ "}";
Class<?> cls1 = loadClass(BeanA.class, code);
Class<?> cls1 =
loadClass(
BeanA.class,
code,
CodegenCompatibleSerializerTest.class + "testWriteCompatibleCollectionBasic_1");
Fury fury1 =
Fury.builder()
.withLanguage(Language.JAVA)
Expand All @@ -165,7 +169,11 @@ public void testWriteCompatibleCollectionBasic(boolean referenceTracking, boolea
+ " private List<Double> doubleList;\n"
+ " private Iterable<BeanB> beanBIterable;\n"
+ "}";
Class<?> cls2 = loadClass(BeanA.class, code);
Class<?> cls2 =
loadClass(
BeanA.class,
code,
CodegenCompatibleSerializerTest.class + "testWriteCompatibleCollectionBasic_2");
Object newBeanA = cls2.newInstance();
ReflectionUtils.unsafeCopy(beanA, newBeanA);
Fury fury2 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ public static Class<?> createCompatibleClass1() {
+ " private int[][] int2DArray;\n"
+ " private int[][] int2DArray_added;\n"
+ "}";
return ClassUtils.loadClass(BeanA.class, code);
return ClassUtils.loadClass(
BeanA.class, code, CompatibleSerializerTest.class + "createCompatibleClass1");
}

@Test(dataProvider = "referenceTrackingConfig")
Expand Down Expand Up @@ -304,7 +305,8 @@ public static Class<?> createCompatibleClass2() {
+ " public Map<String, String> map2;\n"
+ " public SortedMap<Integer, Integer> sortedMap3;"
+ "}";
return ClassUtils.loadClass(CollectionFields.class, code);
return ClassUtils.loadClass(
CollectionFields.class, code, CompatibleSerializerTest.class + "createCompatibleClass2");
}

@Test(dataProvider = "referenceTrackingConfig")
Expand Down Expand Up @@ -380,7 +382,8 @@ public static Class<?> createCompatibleClass3() {
+ " public Map singletonMap;\n"
+ " public Map<String, Integer> singletonMap2;\n"
+ "}";
return ClassUtils.loadClass(MapFields.class, code);
return ClassUtils.loadClass(
MapFields.class, code, CompatibleSerializerTest.class + "createCompatibleClass3");
}

@Test(dataProvider = "compressNumber")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ public void testWriteCompatibleCollectionBasic(
+ " private Iterable<BeanB> beanBIterable;\n"
+ " private List<BeanB> beanBList;\n"
+ "}";
Class<?> cls1 = loadClass(BeanA.class, code);
Class<?> cls1 =
loadClass(
BeanA.class,
code,
MetaSharedCompatibleTest.class + "testWriteCompatibleCollectionBasic_1");
Fury fury1 =
Fury.builder()
.withLanguage(Language.JAVA)
Expand All @@ -237,7 +241,11 @@ public void testWriteCompatibleCollectionBasic(
+ " private List<Double> doubleList;\n"
+ " private Iterable<BeanB> beanBIterable;\n"
+ "}";
Class<?> cls2 = loadClass(BeanA.class, code);
Class<?> cls2 =
loadClass(
BeanA.class,
code,
MetaSharedCompatibleTest.class + "testWriteCompatibleCollectionBasic_2");
Object o2 = cls2.newInstance();
ReflectionUtils.unsafeCopy(beanA, o2);
Fury fury2 =
Expand Down
13 changes: 10 additions & 3 deletions java/fury-test-core/src/main/java/io/fury/test/bean/Struct.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static Class<?> createNumberStructClass(String classname, int repeat) {
IntStream.range(i * numFields, i * numFields + numFields).boxed().toArray()));
}
classCode.append("}");
return createStructClass(classname, classCode.toString());
return compile(classname, classCode.toString());
});
}

Expand Down Expand Up @@ -277,13 +277,20 @@ public static Class<?> createStructClass(String classname, int repeat) {
IntStream.range(i * numFields, i * numFields + numFields).boxed().toArray()));
}
classCode.append("}");
return createStructClass(classname, classCode.toString());
return compile(classname, classCode.toString());
});
}

/** Create class. */
private static Class<?> createStructClass(String classname, String classCode) {
public static Class<?> createStructClass(String classname, String classCode, Object cache) {
if (cache == null) {
return compile(classname, classCode);
}
return classCache.computeIfAbsent(cache, k -> compile(classname, classCode));
}

/** Create class. */
private static Class<?> compile(String classname, String classCode) {
Path path = Paths.get(classname + ".java");
try {
Files.deleteIfExists(path);
Expand Down

0 comments on commit 40e72cc

Please sign in to comment.