Skip to content

Commit

Permalink
add type name hash check
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed Nov 24, 2024
1 parent bc15ed7 commit 3ef5e35
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.apache.fury.resolver;

import static org.apache.fury.meta.Encoders.GENERIC_ENCODER;
import static org.apache.fury.meta.Encoders.PACKAGE_DECODER;
import static org.apache.fury.meta.Encoders.TYPE_NAME_DECODER;

import org.apache.fury.collection.Tuple2;
import org.apache.fury.config.Language;
Expand Down Expand Up @@ -142,6 +144,15 @@ void setSerializer(ClassResolver resolver, Serializer<?> serializer) {
needToWriteClassDef = serializer != null && resolver.needToWriteClassDef(serializer);
}


public String decodeNamespace() {
return packageNameBytes.decode(PACKAGE_DECODER);
}

public String decodeTypeName() {
return classNameBytes.decode(TYPE_NAME_DECODER);
}

@Override
public String toString() {
return "ClassInfo{"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ public void register(Class<?> type, int typeId) {
throw new IllegalArgumentException(
String.format("Type %s has been registered with id %s", type, classInfo.xtypeId));
}
String prevNamespace = decodeNamespace(classInfo.packageNameBytes);
String prevTypeName = decodeTypeName(classInfo.classNameBytes);
String prevNamespace = classInfo.decodeNamespace();
String prevTypeName = classInfo.decodeTypeName();
if (!type.getSimpleName().equals(prevTypeName)) {
throw new IllegalArgumentException(
String.format(
Expand Down Expand Up @@ -167,8 +167,8 @@ public void register(Class<?> type, String namespace, String typeName) {
if (classInfo != null) {
serializer = classInfo.serializer;
if (classInfo.classNameBytes != null) {
String prevNamespace = decodeNamespace(classInfo.packageNameBytes);
String prevTypeName = decodeTypeName(classInfo.classNameBytes);
String prevNamespace = classInfo.decodeNamespace();
String prevTypeName = classInfo.decodeTypeName();
if (!namespace.equals(prevNamespace) || typeName.equals(prevTypeName)) {
throw new IllegalArgumentException(
String.format(
Expand Down Expand Up @@ -234,14 +234,6 @@ private ClassInfo newClassInfo(
type, fullClassNameBytes, nsBytes, classNameBytes, false, serializer, NO_CLASS_ID, xtypeId);
}

private String decodeNamespace(MetaStringBytes packageNameBytes) {
return packageNameBytes.decode(PACKAGE_DECODER);
}

private String decodeTypeName(MetaStringBytes classNameBytes) {
return classNameBytes.decode(TYPE_NAME_DECODER);
}

public <T> void registerSerializer(Class<T> type, Class<? extends Serializer> serializerClass) {
ClassInfo classInfo = checkClassRegistration(type);
classInfo.serializer = Serializers.newSerializer(fury, type, serializerClass);
Expand All @@ -257,7 +249,7 @@ private ClassInfo checkClassRegistration(Class<?> type) {
Preconditions.checkArgument(
classInfo != null
&& (classInfo.xtypeId != 0
|| !type.getSimpleName().equals(decodeTypeName(classInfo.classNameBytes))),
|| !type.getSimpleName().equals(classInfo.decodeTypeName())),
"Type %s should be registered with id or namespace+typename before register serializer",
type);
return classInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.fury.type.Descriptor;
import org.apache.fury.type.GenericType;
import org.apache.fury.type.Generics;
import org.apache.fury.type.TypeUtils;
import org.apache.fury.type.Types;
import org.apache.fury.util.ExceptionUtils;
import org.apache.fury.util.Preconditions;
Expand Down Expand Up @@ -220,7 +221,11 @@ int computeFieldHash(int hash, GenericType fieldGeneric) {
try {
ClassInfo classInfo = fury.getClassResolver().getClassInfo(fieldGeneric.getCls());
int xtypeId = classInfo.getXtypeId();
id = Math.abs(xtypeId);
if (Types.isStructType((byte) xtypeId)) {
id = TypeUtils.computeStringHash(classInfo.decodeNamespace() + classInfo.decodeTypeName());
} else {
id = Math.abs(xtypeId);
}
} catch (Exception e) {
return hash;
}
Expand Down
11 changes: 11 additions & 0 deletions java/fury-core/src/main/java/org/apache/fury/type/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,15 @@ public class Types {
public static final int FLOAT64_ARRAY = 43;
public static final int ARROW_RECORD_BATCH = 44;
public static final int ARROW_TABLE = 45;

public static boolean isStructType(int value) {
return value == STRUCT ||
value == POLYMORPHIC_STRUCT ||
value == COMPATIBLE_STRUCT ||
value == POLYMORPHIC_COMPATIBLE_STRUCT ||
value == NS_STRUCT ||
value == NS_POLYMORPHIC_STRUCT ||
value == NS_COMPATIBLE_STRUCT ||
value == NS_POLYMORPHIC_COMPATIBLE_STRUCT;
}
}

0 comments on commit 3ef5e35

Please sign in to comment.