diff --git a/go/fury/type.go b/go/fury/type.go index 48cd276f85..f1d507a508 100644 --- a/go/fury/type.go +++ b/go/fury/type.go @@ -403,7 +403,7 @@ func (r *typeResolver) writeType(buffer *ByteBuffer, type_ reflect.Type) error { r.typeToTypeInfo[type_] = encodeType } } - if err := r.writeEnumString(buffer, typeInfo); err != nil { + if err := r.writeMetaString(buffer, typeInfo); err != nil { return err } else { return nil @@ -411,17 +411,17 @@ func (r *typeResolver) writeType(buffer *ByteBuffer, type_ reflect.Type) error { } func (r *typeResolver) readType(buffer *ByteBuffer) (reflect.Type, error) { - enumString, err := r.readEnumString(buffer) + metaString, err := r.readMetaString(buffer) if err != nil { return nil, err } - type_, ok := r.typeInfoToType[enumString] + type_, ok := r.typeInfoToType[metaString] if !ok { - type_, _, err = r.decodeType(enumString) + type_, _, err = r.decodeType(metaString) if err != nil { return nil, err } else { - r.typeInfoToType[enumString] = type_ + r.typeInfoToType[metaString] = type_ } } return type_, nil @@ -519,7 +519,7 @@ func (r *typeResolver) decodeType(typeStr string) (reflect.Type, string, error) } func (r *typeResolver) writeTypeTag(buffer *ByteBuffer, typeTag string) error { - if err := r.writeEnumString(buffer, typeTag); err != nil { + if err := r.writeMetaString(buffer, typeTag); err != nil { return err } else { return nil @@ -527,15 +527,15 @@ func (r *typeResolver) writeTypeTag(buffer *ByteBuffer, typeTag string) error { } func (r *typeResolver) readTypeByReadTag(buffer *ByteBuffer) (reflect.Type, error) { - enumString, err := r.readEnumString(buffer) + metaString, err := r.readMetaString(buffer) if err != nil { return nil, err } - return r.typeTagToSerializers[enumString].(*ptrToStructSerializer).type_, err + return r.typeTagToSerializers[metaString].(*ptrToStructSerializer).type_, err } func (r *typeResolver) readTypeInfo(buffer *ByteBuffer) (string, error) { - return r.readEnumString(buffer) + return r.readMetaString(buffer) } func (r *typeResolver) getTypeById(id int16) (reflect.Type, error) { @@ -546,7 +546,7 @@ func (r *typeResolver) getTypeById(id int16) (reflect.Type, error) { return type_, nil } -func (r *typeResolver) writeEnumString(buffer *ByteBuffer, str string) error { +func (r *typeResolver) writeMetaString(buffer *ByteBuffer, str string) error { if id, ok := r.dynamicStringToId[str]; !ok { dynamicStringId := r.dynamicStringId r.dynamicStringId += 1 @@ -571,7 +571,7 @@ func (r *typeResolver) writeEnumString(buffer *ByteBuffer, str string) error { return nil } -func (r *typeResolver) readEnumString(buffer *ByteBuffer) (string, error) { +func (r *typeResolver) readMetaString(buffer *ByteBuffer) (string, error) { if buffer.ReadByte_() == useStringValue { // TODO support use computed hash buffer.ReadInt64() diff --git a/java/fury-core/src/main/java/org/apache/fury/Fury.java b/java/fury-core/src/main/java/org/apache/fury/Fury.java index c848faefef..ae992ce5cb 100644 --- a/java/fury-core/src/main/java/org/apache/fury/Fury.java +++ b/java/fury-core/src/main/java/org/apache/fury/Fury.java @@ -46,8 +46,8 @@ import org.apache.fury.resolver.ClassInfo; import org.apache.fury.resolver.ClassInfoHolder; import org.apache.fury.resolver.ClassResolver; -import org.apache.fury.resolver.EnumStringResolver; import org.apache.fury.resolver.MapRefResolver; +import org.apache.fury.resolver.MetaStringResolver; import org.apache.fury.resolver.NoRefResolver; import org.apache.fury.resolver.RefResolver; import org.apache.fury.resolver.SerializationContext; @@ -101,7 +101,7 @@ public final class Fury implements BaseFury { private final boolean refTracking; private final RefResolver refResolver; private final ClassResolver classResolver; - private final EnumStringResolver enumStringResolver; + private final MetaStringResolver metaStringResolver; private final SerializationContext serializationContext; private final ClassLoader classLoader; private final JITContext jitContext; @@ -132,7 +132,7 @@ public Fury(FuryBuilder builder, ClassLoader classLoader) { this.refResolver = new NoRefResolver(); } jitContext = new JITContext(this); - enumStringResolver = new EnumStringResolver(); + metaStringResolver = new MetaStringResolver(); classResolver = new ClassResolver(this); classResolver.initialize(); serializationContext = new SerializationContext(); @@ -325,7 +325,7 @@ private void xserializeInternal(MemoryBuffer buffer, Object obj) { refResolver.resetWrite(); // fury write opaque object classname which cause later write of classname only write an id. classResolver.resetWrite(); - enumStringResolver.resetWrite(); + metaStringResolver.resetWrite(); for (Object nativeObject : nativeObjects) { writeRef(buffer, nativeObject); } @@ -807,7 +807,7 @@ private Object xdeserializeInternal(MemoryBuffer buffer) { buffer.readerIndex(readerIndex); refResolver.resetRead(); classResolver.resetRead(); - enumStringResolver.resetRead(); + metaStringResolver.resetRead(); } obj = xreadRef(buffer); buffer.readerIndex(endReaderIndex); @@ -1241,7 +1241,7 @@ private void serializeToStream(OutputStream outputStream, Consumer public void reset() { refResolver.reset(); classResolver.reset(); - enumStringResolver.reset(); + metaStringResolver.reset(); serializationContext.reset(); nativeObjects.clear(); peerOutOfBandEnabled = false; @@ -1252,7 +1252,7 @@ public void reset() { public void resetWrite() { refResolver.resetWrite(); classResolver.resetWrite(); - enumStringResolver.resetWrite(); + metaStringResolver.resetWrite(); serializationContext.reset(); nativeObjects.clear(); bufferCallback = null; @@ -1262,7 +1262,7 @@ public void resetWrite() { public void resetRead() { refResolver.resetRead(); classResolver.resetRead(); - enumStringResolver.resetRead(); + metaStringResolver.resetRead(); serializationContext.reset(); nativeObjects.clear(); peerOutOfBandEnabled = false; @@ -1305,8 +1305,8 @@ public ClassResolver getClassResolver() { return classResolver; } - public EnumStringResolver getEnumStringResolver() { - return enumStringResolver; + public MetaStringResolver getMetaStringResolver() { + return metaStringResolver; } public SerializationContext getSerializationContext() { diff --git a/java/fury-core/src/main/java/org/apache/fury/resolver/ClassInfo.java b/java/fury-core/src/main/java/org/apache/fury/resolver/ClassInfo.java index 712c456a5a..07261396e8 100644 --- a/java/fury-core/src/main/java/org/apache/fury/resolver/ClassInfo.java +++ b/java/fury-core/src/main/java/org/apache/fury/resolver/ClassInfo.java @@ -31,11 +31,11 @@ */ public class ClassInfo { final Class cls; - final EnumStringBytes fullClassNameBytes; - final EnumStringBytes packageNameBytes; - final EnumStringBytes classNameBytes; + final MetaStringBytes fullClassNameBytes; + final MetaStringBytes packageNameBytes; + final MetaStringBytes classNameBytes; final boolean isDynamicGeneratedClass; - final EnumStringBytes typeTagBytes; + final MetaStringBytes typeTagBytes; Serializer serializer; // use primitive to avoid boxing // class id must be less than Integer.MAX_VALUE/2 since we use bit 0 as class id flag. @@ -43,11 +43,11 @@ public class ClassInfo { ClassInfo( Class cls, - EnumStringBytes fullClassNameBytes, - EnumStringBytes packageNameBytes, - EnumStringBytes classNameBytes, + MetaStringBytes fullClassNameBytes, + MetaStringBytes packageNameBytes, + MetaStringBytes classNameBytes, boolean isDynamicGeneratedClass, - EnumStringBytes typeTagBytes, + MetaStringBytes typeTagBytes, Serializer serializer, short classId) { this.cls = cls; @@ -71,9 +71,9 @@ public class ClassInfo { short classId) { this.cls = cls; this.serializer = serializer; - EnumStringResolver enumStringResolver = classResolver.getEnumStringResolver(); + MetaStringResolver metaStringResolver = classResolver.getMetaStringResolver(); if (cls != null && classResolver.getFury().getLanguage() != Language.JAVA) { - this.fullClassNameBytes = enumStringResolver.getOrCreateEnumStringBytes(cls.getName()); + this.fullClassNameBytes = metaStringResolver.getOrCreateMetaStringBytes(cls.getName()); } else { this.fullClassNameBytes = null; } @@ -81,16 +81,16 @@ public class ClassInfo { && (classId == ClassResolver.NO_CLASS_ID || classId == ClassResolver.REPLACE_STUB_ID)) { // REPLACE_STUB_ID for write replace class in `ClassSerializer`. String packageName = ReflectionUtils.getPackage(cls); - this.packageNameBytes = enumStringResolver.getOrCreateEnumStringBytes(packageName); + this.packageNameBytes = metaStringResolver.getOrCreateMetaStringBytes(packageName); this.classNameBytes = - enumStringResolver.getOrCreateEnumStringBytes( + metaStringResolver.getOrCreateMetaStringBytes( ReflectionUtils.getClassNameWithoutPackage(cls)); } else { this.packageNameBytes = null; this.classNameBytes = null; } if (tag != null) { - this.typeTagBytes = enumStringResolver.getOrCreateEnumStringBytes(tag); + this.typeTagBytes = metaStringResolver.getOrCreateMetaStringBytes(tag); } else { this.typeTagBytes = null; } @@ -118,11 +118,11 @@ public short getClassId() { return classId; } - public EnumStringBytes getPackageNameBytes() { + public MetaStringBytes getPackageNameBytes() { return packageNameBytes; } - public EnumStringBytes getClassNameBytes() { + public MetaStringBytes getClassNameBytes() { return classNameBytes; } diff --git a/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java b/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java index 6d2d0743a4..03c9c684dc 100644 --- a/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java +++ b/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java @@ -210,14 +210,14 @@ public class ClassResolver { private final IdentityMap, ClassInfo> classInfoMap = new IdentityMap<>(estimatedNumRegistered, furyMapLoadFactor); private ClassInfo classInfoCache; - private final ObjectMap> classNameBytes2Class = + private final ObjectMap> classNameBytes2Class = new ObjectMap<>(16, furyMapLoadFactor); // Every deserialization for unregistered class will query it, performance is important. private final ObjectMap> compositeClassNameBytes2Class = new ObjectMap<>(16, furyMapLoadFactor); private final HashMap> typeIdToClassXLangMap = new HashMap<>(8, loadFactor); private final HashMap> typeTagToClassXLangMap = new HashMap<>(8, loadFactor); - private final EnumStringResolver enumStringResolver; + private final MetaStringResolver metaStringResolver; private final boolean metaContextShareEnabled; private final Map, ClassDef> classDefMap = new HashMap<>(); private Class currentReadClass; @@ -249,7 +249,7 @@ private static class ExtRegistry { public ClassResolver(Fury fury) { this.fury = fury; - enumStringResolver = fury.getEnumStringResolver(); + metaStringResolver = fury.getMetaStringResolver(); classInfoCache = NIL_CLASS_INFO; metaContextShareEnabled = fury.getConfig().shareMetaContext(); extRegistry = new ExtRegistry(); @@ -1235,9 +1235,9 @@ public void writeClass(MemoryBuffer buffer, ClassInfo classInfo) { } else { // if it's null, it's a bug. assert classInfo.packageNameBytes != null; - enumStringResolver.writeEnumStringBytes(buffer, classInfo.packageNameBytes); + metaStringResolver.writeMetaStringBytes(buffer, classInfo.packageNameBytes); assert classInfo.classNameBytes != null; - enumStringResolver.writeEnumStringBytes(buffer, classInfo.classNameBytes); + metaStringResolver.writeMetaStringBytes(buffer, classInfo.classNameBytes); } } else { // use classId @@ -1434,14 +1434,14 @@ public Expression writeClassExpr( writeUnregistered.add( new Invoke( classResolverRef, - "writeEnumStringBytes", + "writeMetaStringBytes", buffer, - inlineInvoke(classInfo, "getPackageNameBytes", TypeToken.of(EnumStringBytes.class))), + inlineInvoke(classInfo, "getPackageNameBytes", TypeToken.of(MetaStringBytes.class))), new Invoke( classResolverRef, - "writeEnumStringBytes", + "writeMetaStringBytes", buffer, - inlineInvoke(classInfo, "getClassNameBytes", TypeToken.of(EnumStringBytes.class)))); + inlineInvoke(classInfo, "getClassNameBytes", TypeToken.of(MetaStringBytes.class)))); } return new Expression.If( eq(classId, Literal.ofShort(NO_CLASS_ID)), @@ -1461,8 +1461,8 @@ public Expression writeClassExpr(Expression buffer, Expression classId) { } // Invoked by Fury JIT. - public void writeEnumStringBytes(MemoryBuffer buffer, EnumStringBytes byteString) { - enumStringResolver.writeEnumStringBytes(buffer, byteString); + public void writeMetaStringBytes(MemoryBuffer buffer, MetaStringBytes byteString) { + metaStringResolver.writeMetaStringBytes(buffer, byteString); } // Note: Thread safe fot jit thread to call. @@ -1505,8 +1505,8 @@ public Class readClassInternal(MemoryBuffer buffer) { if (metaContextShareEnabled) { return readClassWithMetaShare(buffer); } - EnumStringBytes packageBytes = enumStringResolver.readEnumStringBytes(buffer); - EnumStringBytes simpleClassNameBytes = enumStringResolver.readEnumStringBytes(buffer); + MetaStringBytes packageBytes = metaStringResolver.readMetaStringBytes(buffer); + MetaStringBytes simpleClassNameBytes = metaStringResolver.readMetaStringBytes(buffer); final Class cls = loadBytesToClass(packageBytes, simpleClassNameBytes); currentReadClass = cls; return cls; @@ -1596,14 +1596,14 @@ private ClassInfo readClassInfoFromBytes(MemoryBuffer buffer, ClassInfoHolder cl } private ClassInfo readClassInfoFromBytes(MemoryBuffer buffer, ClassInfo classInfoCache) { - EnumStringBytes simpleClassNameBytesCache = classInfoCache.classNameBytes; + MetaStringBytes simpleClassNameBytesCache = classInfoCache.classNameBytes; if (simpleClassNameBytesCache != null) { - EnumStringBytes packageNameBytesCache = classInfoCache.packageNameBytes; - EnumStringBytes packageBytes = - enumStringResolver.readEnumStringBytes(buffer, packageNameBytesCache); + MetaStringBytes packageNameBytesCache = classInfoCache.packageNameBytes; + MetaStringBytes packageBytes = + metaStringResolver.readMetaStringBytes(buffer, packageNameBytesCache); assert packageNameBytesCache != null; - EnumStringBytes simpleClassNameBytes = - enumStringResolver.readEnumStringBytes(buffer, simpleClassNameBytesCache); + MetaStringBytes simpleClassNameBytes = + metaStringResolver.readMetaStringBytes(buffer, simpleClassNameBytesCache); if (simpleClassNameBytesCache.hashCode == simpleClassNameBytes.hashCode && packageNameBytesCache.hashCode == packageBytes.hashCode) { return classInfoCache; @@ -1612,15 +1612,15 @@ private ClassInfo readClassInfoFromBytes(MemoryBuffer buffer, ClassInfo classInf return getClassInfo(cls); } } else { - EnumStringBytes packageBytes = enumStringResolver.readEnumStringBytes(buffer); - EnumStringBytes simpleClassNameBytes = enumStringResolver.readEnumStringBytes(buffer); + MetaStringBytes packageBytes = metaStringResolver.readMetaStringBytes(buffer); + MetaStringBytes simpleClassNameBytes = metaStringResolver.readMetaStringBytes(buffer); Class cls = loadBytesToClass(packageBytes, simpleClassNameBytes); return getClassInfo(cls); } } private Class loadBytesToClass( - EnumStringBytes packageBytes, EnumStringBytes simpleClassNameBytes) { + MetaStringBytes packageBytes, MetaStringBytes simpleClassNameBytes) { ClassNameBytes classNameBytes = new ClassNameBytes(packageBytes.hashCode, simpleClassNameBytes.hashCode); Class cls = compositeClassNameBytes2Class.get(classNameBytes); @@ -1640,15 +1640,15 @@ private Class loadBytesToClass( } public void xwriteClass(MemoryBuffer buffer, Class cls) { - enumStringResolver.writeEnumStringBytes(buffer, getOrUpdateClassInfo(cls).fullClassNameBytes); + metaStringResolver.writeMetaStringBytes(buffer, getOrUpdateClassInfo(cls).fullClassNameBytes); } public void xwriteTypeTag(MemoryBuffer buffer, Class cls) { - enumStringResolver.writeEnumStringBytes(buffer, getOrUpdateClassInfo(cls).typeTagBytes); + metaStringResolver.writeMetaStringBytes(buffer, getOrUpdateClassInfo(cls).typeTagBytes); } public Class xreadClass(MemoryBuffer buffer) { - EnumStringBytes byteString = enumStringResolver.readEnumStringBytes(buffer); + MetaStringBytes byteString = metaStringResolver.readMetaStringBytes(buffer); Class cls = classNameBytes2Class.get(byteString); if (cls == null) { Preconditions.checkNotNull(byteString); @@ -1661,7 +1661,7 @@ public Class xreadClass(MemoryBuffer buffer) { } public String xreadClassName(MemoryBuffer buffer) { - return enumStringResolver.readEnumString(buffer); + return metaStringResolver.readMetaString(buffer); } public Class getCurrentReadClass() { @@ -1704,7 +1704,7 @@ public Class getClassByTypeId(short typeId) { } public Class readClassByTypeTag(MemoryBuffer buffer) { - String tag = enumStringResolver.readEnumString(buffer); + String tag = metaStringResolver.readMetaString(buffer); return typeTagToClassXLangMap.get(tag); } @@ -1777,8 +1777,8 @@ public boolean isPrimitive(short classId) { return classId >= PRIMITIVE_VOID_CLASS_ID && classId <= PRIMITIVE_DOUBLE_CLASS_ID; } - public EnumStringResolver getEnumStringResolver() { - return enumStringResolver; + public MetaStringResolver getMetaStringResolver() { + return metaStringResolver; } public CodeGenerator getCodeGenerator(ClassLoader... loaders) { diff --git a/java/fury-core/src/main/java/org/apache/fury/resolver/EnumStringBytes.java b/java/fury-core/src/main/java/org/apache/fury/resolver/MetaStringBytes.java similarity index 91% rename from java/fury-core/src/main/java/org/apache/fury/resolver/EnumStringBytes.java rename to java/fury-core/src/main/java/org/apache/fury/resolver/MetaStringBytes.java index c62e827d7d..167bb064db 100644 --- a/java/fury-core/src/main/java/org/apache/fury/resolver/EnumStringBytes.java +++ b/java/fury-core/src/main/java/org/apache/fury/resolver/MetaStringBytes.java @@ -26,7 +26,7 @@ import org.apache.fury.util.Preconditions; @Internal -public final class EnumStringBytes { +final class MetaStringBytes { static final short DEFAULT_DYNAMIC_WRITE_STRING_ID = -1; final byte[] bytes; @@ -34,19 +34,19 @@ public final class EnumStringBytes { short dynamicWriteStringId = DEFAULT_DYNAMIC_WRITE_STRING_ID; /** - * Create a binary EnumString. + * Create a binary MetaString. * * @param bytes String encoded bytes. * @param hashCode String hash code. This should be unique and has no hash collision, and be * deterministic, so we can use cache to reduce hash loop up for read. */ - public EnumStringBytes(byte[] bytes, long hashCode) { + public MetaStringBytes(byte[] bytes, long hashCode) { assert hashCode != 0; this.bytes = bytes; this.hashCode = hashCode; } - public EnumStringBytes(String string) { + public MetaStringBytes(String string) { byte[] classNameBytes = string.getBytes(StandardCharsets.UTF_8); Preconditions.checkArgument(classNameBytes.length <= Short.MAX_VALUE); // Set seed to ensure hash is deterministic. @@ -62,14 +62,14 @@ public EnumStringBytes(String string) { @Override public boolean equals(Object o) { - // EnumStringBytes is used internally, skip unnecessary parameter check. + // MetaStringBytes is used internally, skip unnecessary parameter check. // if (this == o) { // return true; // } // if (o == null || getClass() != o.getClass()) { // return false; // } - EnumStringBytes that = (EnumStringBytes) o; + MetaStringBytes that = (MetaStringBytes) o; // Skip compare data for equality for performance. // Enum string such as classname are very common, compare hashcode only will have better // performance. diff --git a/java/fury-core/src/main/java/org/apache/fury/resolver/EnumStringResolver.java b/java/fury-core/src/main/java/org/apache/fury/resolver/MetaStringResolver.java similarity index 69% rename from java/fury-core/src/main/java/org/apache/fury/resolver/EnumStringResolver.java rename to java/fury-core/src/main/java/org/apache/fury/resolver/MetaStringResolver.java index feb98931c1..baab09fda2 100644 --- a/java/fury-core/src/main/java/org/apache/fury/resolver/EnumStringResolver.java +++ b/java/fury-core/src/main/java/org/apache/fury/resolver/MetaStringResolver.java @@ -30,7 +30,7 @@ * resolver to reduce string cost. TODO add common inner package names and classnames here. TODO * share common immutable datastructure globally across multiple fury. */ -public final class EnumStringResolver { +public final class MetaStringResolver { public static final byte USE_STRING_VALUE = 0; public static final byte USE_STRING_ID = 1; private static final int initialCapacity = 8; @@ -38,60 +38,60 @@ public final class EnumStringResolver { private static final float furyMapLoadFactor = 0.25f; // Every deserialization for unregistered string will query it, performance is important. - private final ObjectMap enumStringBytes2StringMap = + private final ObjectMap metaStringBytes2StringMap = new ObjectMap<>(initialCapacity, furyMapLoadFactor); - private final LongMap hash2EnumStringBytesMap = + private final LongMap hash2MetaStringBytesMap = new LongMap<>(initialCapacity, furyMapLoadFactor); // Every enum bytes should be singleton at every fury, since we keep state in it. - private final ObjectMap enumString2BytesMap = + private final ObjectMap metaString2BytesMap = new ObjectMap<>(initialCapacity, furyMapLoadFactor); - private EnumStringBytes[] dynamicWrittenString = new EnumStringBytes[32]; - private EnumStringBytes[] dynamicReadStringIds = new EnumStringBytes[32]; + private MetaStringBytes[] dynamicWrittenString = new MetaStringBytes[32]; + private MetaStringBytes[] dynamicReadStringIds = new MetaStringBytes[32]; private short dynamicWriteStringId; private short dynamicReadStringId; - public EnumStringResolver() { + public MetaStringResolver() { dynamicWriteStringId = 0; dynamicReadStringId = 0; } - EnumStringBytes getOrCreateEnumStringBytes(String str) { - EnumStringBytes enumStringBytes = enumString2BytesMap.get(str); - if (enumStringBytes == null) { - enumStringBytes = new EnumStringBytes(str); - enumString2BytesMap.put(str, enumStringBytes); + MetaStringBytes getOrCreateMetaStringBytes(String str) { + MetaStringBytes metaStringBytes = metaString2BytesMap.get(str); + if (metaStringBytes == null) { + metaStringBytes = new MetaStringBytes(str); + metaString2BytesMap.put(str, metaStringBytes); } - return enumStringBytes; + return metaStringBytes; } - public void writeEnumString(MemoryBuffer buffer, String str) { - writeEnumStringBytes(buffer, getOrCreateEnumStringBytes(str)); + public void writeMetaString(MemoryBuffer buffer, String str) { + writeMetaStringBytes(buffer, getOrCreateMetaStringBytes(str)); } - public String readEnumString(MemoryBuffer buffer) { - EnumStringBytes byteString = readEnumStringBytes(buffer); - String str = enumStringBytes2StringMap.get(byteString); + public String readMetaString(MemoryBuffer buffer) { + MetaStringBytes byteString = readMetaStringBytes(buffer); + String str = metaStringBytes2StringMap.get(byteString); if (str == null) { // TODO use org.apache.fury.resolver.ObjectMap str = new String(byteString.bytes, StandardCharsets.UTF_8); - enumStringBytes2StringMap.put(byteString, str); + metaStringBytes2StringMap.put(byteString, str); } return str; } - public void writeEnumStringBytes(MemoryBuffer buffer, EnumStringBytes byteString) { + public void writeMetaStringBytes(MemoryBuffer buffer, MetaStringBytes byteString) { short id = byteString.dynamicWriteStringId; int writerIndex = buffer.writerIndex(); - if (id == EnumStringBytes.DEFAULT_DYNAMIC_WRITE_STRING_ID) { + if (id == MetaStringBytes.DEFAULT_DYNAMIC_WRITE_STRING_ID) { id = dynamicWriteStringId++; byteString.dynamicWriteStringId = id; - EnumStringBytes[] dynamicWrittenEnumString = this.dynamicWrittenString; - if (dynamicWrittenEnumString.length <= id) { - EnumStringBytes[] tmp = new EnumStringBytes[id * 2]; - System.arraycopy(dynamicWrittenEnumString, 0, tmp, 0, dynamicWrittenEnumString.length); - dynamicWrittenEnumString = tmp; + MetaStringBytes[] dynamicWrittenMetaString = this.dynamicWrittenString; + if (dynamicWrittenMetaString.length <= id) { + MetaStringBytes[] tmp = new MetaStringBytes[id * 2]; + System.arraycopy(dynamicWrittenMetaString, 0, tmp, 0, dynamicWrittenMetaString.length); + dynamicWrittenMetaString = tmp; this.dynamicWrittenString = tmp; } - dynamicWrittenEnumString[id] = byteString; + dynamicWrittenMetaString[id] = byteString; int bytesLen = byteString.bytes.length; buffer.increaseWriterIndex(11 + bytesLen); buffer._unsafePutByte(writerIndex, USE_STRING_VALUE); @@ -107,10 +107,10 @@ public void writeEnumStringBytes(MemoryBuffer buffer, EnumStringBytes byteString } } - EnumStringBytes readEnumStringBytes(MemoryBuffer buffer) { + MetaStringBytes readMetaStringBytes(MemoryBuffer buffer) { if (buffer.readByte() == USE_STRING_VALUE) { long hashCode = buffer.readInt64(); - EnumStringBytes byteString = trySkipEnumStringBytes(buffer, hashCode); + MetaStringBytes byteString = trySkipMetaStringBytes(buffer, hashCode); updateDynamicString(byteString); return byteString; } else { @@ -118,7 +118,7 @@ EnumStringBytes readEnumStringBytes(MemoryBuffer buffer) { } } - EnumStringBytes readEnumStringBytes(MemoryBuffer buffer, EnumStringBytes cache) { + MetaStringBytes readMetaStringBytes(MemoryBuffer buffer, MetaStringBytes cache) { if (buffer.readByte() == USE_STRING_VALUE) { long hashCode = buffer.readInt64(); if (cache.hashCode == hashCode) { @@ -127,7 +127,7 @@ EnumStringBytes readEnumStringBytes(MemoryBuffer buffer, EnumStringBytes cache) updateDynamicString(cache); return cache; } else { - EnumStringBytes byteString = trySkipEnumStringBytes(buffer, hashCode); + MetaStringBytes byteString = trySkipMetaStringBytes(buffer, hashCode); updateDynamicString(byteString); return byteString; } @@ -136,14 +136,14 @@ EnumStringBytes readEnumStringBytes(MemoryBuffer buffer, EnumStringBytes cache) } } - /** Read enum string by try to reuse previous read {@link EnumStringBytes} object. */ - private EnumStringBytes trySkipEnumStringBytes(MemoryBuffer buffer, long hashCode) { - EnumStringBytes byteString = hash2EnumStringBytesMap.get(hashCode); + /** Read enum string by try to reuse previous read {@link MetaStringBytes} object. */ + private MetaStringBytes trySkipMetaStringBytes(MemoryBuffer buffer, long hashCode) { + MetaStringBytes byteString = hash2MetaStringBytesMap.get(hashCode); if (byteString == null) { int strBytesLength = buffer.readInt16(); byte[] strBytes = buffer.readBytes(strBytesLength); - byteString = new EnumStringBytes(strBytes, hashCode); - hash2EnumStringBytesMap.put(hashCode, byteString); + byteString = new MetaStringBytes(strBytes, hashCode); + hash2MetaStringBytesMap.put(hashCode, byteString); } else { // skip byteString data buffer.increaseReaderIndex(2 + byteString.bytes.length); @@ -151,11 +151,11 @@ private EnumStringBytes trySkipEnumStringBytes(MemoryBuffer buffer, long hashCod return byteString; } - private void updateDynamicString(EnumStringBytes byteString) { + private void updateDynamicString(MetaStringBytes byteString) { short currentDynamicReadId = dynamicReadStringId++; - EnumStringBytes[] dynamicReadStringIds = this.dynamicReadStringIds; + MetaStringBytes[] dynamicReadStringIds = this.dynamicReadStringIds; if (dynamicReadStringIds.length <= currentDynamicReadId) { - EnumStringBytes[] tmp = new EnumStringBytes[currentDynamicReadId * 2]; + MetaStringBytes[] tmp = new MetaStringBytes[currentDynamicReadId * 2]; System.arraycopy(dynamicReadStringIds, 0, tmp, 0, dynamicReadStringIds.length); dynamicReadStringIds = tmp; this.dynamicReadStringIds = tmp; @@ -183,7 +183,7 @@ public void resetWrite() { if (dynamicWriteStringId != 0) { for (int i = 0; i < dynamicWriteStringId; i++) { dynamicWrittenString[i].dynamicWriteStringId = - EnumStringBytes.DEFAULT_DYNAMIC_WRITE_STRING_ID; + MetaStringBytes.DEFAULT_DYNAMIC_WRITE_STRING_ID; dynamicWrittenString[i] = null; } this.dynamicWriteStringId = 0; diff --git a/java/fury-core/src/test/java/org/apache/fury/resolver/EnumStringResolverTest.java b/java/fury-core/src/test/java/org/apache/fury/resolver/MetaStringResolverTest.java similarity index 78% rename from java/fury-core/src/test/java/org/apache/fury/resolver/EnumStringResolverTest.java rename to java/fury-core/src/test/java/org/apache/fury/resolver/MetaStringResolverTest.java index 9b45b38f2d..bc401526b9 100644 --- a/java/fury-core/src/test/java/org/apache/fury/resolver/EnumStringResolverTest.java +++ b/java/fury-core/src/test/java/org/apache/fury/resolver/MetaStringResolverTest.java @@ -27,20 +27,20 @@ import org.apache.fury.util.StringUtils; import org.testng.annotations.Test; -public class EnumStringResolverTest { +public class MetaStringResolverTest { @Test - public void testWriteEnumString() { + public void testWriteMetaString() { MemoryBuffer buffer = MemoryUtils.buffer(32); String str = StringUtils.random(128, 0); - EnumStringResolver stringResolver = new EnumStringResolver(); + MetaStringResolver stringResolver = new MetaStringResolver(); for (int i = 0; i < 128; i++) { - stringResolver.writeEnumString(buffer, str); + stringResolver.writeMetaString(buffer, str); } for (int i = 0; i < 128; i++) { - String enumString = stringResolver.readEnumString(buffer); - assertEquals(enumString.hashCode(), str.hashCode()); - assertEquals(enumString.getBytes(), str.getBytes()); + String metaString = stringResolver.readMetaString(buffer); + assertEquals(metaString.hashCode(), str.hashCode()); + assertEquals(metaString.getBytes(), str.getBytes()); } assertTrue(buffer.writerIndex() < str.getBytes().length + 128 * 4); } diff --git a/python/pyfury/_fury.py b/python/pyfury/_fury.py index 3e60682596..5a06214497 100644 --- a/python/pyfury/_fury.py +++ b/python/pyfury/_fury.py @@ -91,7 +91,7 @@ DEFAULT_DYNAMIC_WRITE_STRING_ID = -1 -class EnumStringBytes: +class MetaStringBytes: __slots__ = ( "data", "length", @@ -106,7 +106,7 @@ def __init__(self, data, hashcode=None): self.dynamic_write_string_id = DEFAULT_DYNAMIC_WRITE_STRING_ID def __eq__(self, other): - return type(other) is EnumStringBytes and other.hashcode == self.hashcode + return type(other) is MetaStringBytes and other.hashcode == self.hashcode def __hash__(self): return self.hashcode @@ -132,9 +132,9 @@ def __init__( self.cls = cls self.class_id = class_id self.serializer = serializer - self.class_name_bytes = EnumStringBytes(class_name_bytes) + self.class_name_bytes = MetaStringBytes(class_name_bytes) self.type_tag_bytes = ( - EnumStringBytes(type_tag_bytes) if type_tag_bytes else None + MetaStringBytes(type_tag_bytes) if type_tag_bytes else None ) def __repr__(self): @@ -299,7 +299,7 @@ def _add_x_lang_serializer(self, cls: type, serializer=None, serializer_cls=None type_tag = serializer.get_xtype_tag() assert type(type_tag) is str assert type_tag not in self._type_tag_to_class_x_lang_map - classinfo.type_tag_bytes = EnumStringBytes(type_tag.encode("utf-8")) + classinfo.type_tag_bytes = MetaStringBytes(type_tag.encode("utf-8")) self._type_tag_to_class_x_lang_map[type_tag] = cls else: self._type_id_to_serializer[type_id] = serializer @@ -498,7 +498,7 @@ def read_classinfo(self, buffer): return classinfo def write_enum_string_bytes( - self, buffer: Buffer, enum_string_bytes: EnumStringBytes + self, buffer: Buffer, enum_string_bytes: MetaStringBytes ): dynamic_write_string_id = enum_string_bytes.dynamic_write_string_id if dynamic_write_string_id == DEFAULT_DYNAMIC_WRITE_STRING_ID: @@ -514,7 +514,7 @@ def write_enum_string_bytes( buffer.write_int8(USE_CLASS_ID) buffer.write_int16(dynamic_write_string_id) - def read_enum_string_bytes(self, buffer: Buffer) -> EnumStringBytes: + def read_enum_string_bytes(self, buffer: Buffer) -> MetaStringBytes: if buffer.read_int8() != USE_CLASSNAME: return self._dynamic_id_to_enum_str_list[buffer.read_int16()] hashcode = buffer.read_int64() @@ -525,7 +525,7 @@ def read_enum_string_bytes(self, buffer: Buffer) -> EnumStringBytes: enum_str = self._hash_to_enum_string.get(hashcode) if enum_str is None: str_bytes = buffer.get_bytes(reader_index, length) - enum_str = EnumStringBytes(str_bytes, hashcode=hashcode) + enum_str = MetaStringBytes(str_bytes, hashcode=hashcode) self._hash_to_enum_string[hashcode] = enum_str self._dynamic_id_to_enum_str_list.append(enum_str) return enum_str diff --git a/python/pyfury/_serialization.pyx b/python/pyfury/_serialization.pyx index da3f6e1325..df435e7086 100644 --- a/python/pyfury/_serialization.pyx +++ b/python/pyfury/_serialization.pyx @@ -304,11 +304,11 @@ cdef class ClassResolver: flat_hash_map[uint64_t, PyObject*] _c_classes_info # hash -> ClassInfo flat_hash_map[int64_t, PyObject*] _c_hash_to_classinfo - # hash -> EnumStringBytes + # hash -> MetaStringBytes flat_hash_map[int64_t, PyObject*] _c_hash_to_enum_string_bytes - # classname EnumStringBytes address -> class + # classname MetaStringBytes address -> class flat_hash_map[uint64_t, PyObject*] _c_str_bytes_to_class - # classname EnumStringBytes address -> str + # classname MetaStringBytes address -> str flat_hash_map[uint64_t, PyObject*] _c_enum_str_to_str int16_t dynamic_write_string_id @@ -449,7 +449,7 @@ cdef class ClassResolver: type_tag = serializer.get_xtype_tag() assert type(type_tag) is str assert type_tag not in self._type_tag_to_class_x_lang_map - classinfo.type_tag_bytes = EnumStringBytes(type_tag.encode("utf-8")) + classinfo.type_tag_bytes = MetaStringBytes(type_tag.encode("utf-8")) self._type_tag_to_class_x_lang_map[type_tag] = cls else: self._type_id_to_serializer[type_id] = serializer @@ -654,7 +654,7 @@ cdef class ClassResolver: return classinfo cdef inline _write_enum_string_bytes( - self, Buffer buffer, EnumStringBytes enum_string_bytes): + self, Buffer buffer, MetaStringBytes enum_string_bytes): cdef int16_t dynamic_class_id = enum_string_bytes.dynamic_write_string_id if dynamic_class_id == DEFAULT_DYNAMIC_WRITE_STRING_ID: dynamic_class_id = self.dynamic_write_string_id @@ -669,9 +669,9 @@ cdef class ClassResolver: buffer.write_int8(USE_CLASS_ID) buffer.write_int16(dynamic_class_id) - cdef inline EnumStringBytes _read_enum_string_bytes(self, Buffer buffer): + cdef inline MetaStringBytes _read_enum_string_bytes(self, Buffer buffer): if buffer.read_int8() != USE_CLASSNAME: - return self._c_dynamic_id_to_enum_string_vec[ + return self._c_dynamic_id_to_enum_string_vec[ buffer.read_int16()] cdef int64_t hashcode = buffer.read_int64() cdef int16_t length = buffer.read_int16() @@ -681,9 +681,9 @@ cdef class ClassResolver: cdef PyObject* enum_str_ptr = self._c_hash_to_enum_string_bytes[hashcode] if enum_str_ptr != NULL: self._c_dynamic_id_to_enum_string_vec.push_back(enum_str_ptr) - return enum_str_ptr + return enum_str_ptr cdef bytes str_bytes = buffer.get_bytes(reader_index, length) - cdef EnumStringBytes enum_str = EnumStringBytes(str_bytes, hashcode=hashcode) + cdef MetaStringBytes enum_str = MetaStringBytes(str_bytes, hashcode=hashcode) self._enum_str_set.add(enum_str) enum_str_ptr = enum_str self._c_hash_to_enum_string_bytes[hashcode] = enum_str_ptr @@ -693,13 +693,13 @@ cdef class ClassResolver: cpdef inline xwrite_class(self, Buffer buffer, cls): cdef PyObject* classinfo_ptr = self._c_classes_info[cls] assert classinfo_ptr != NULL - cdef EnumStringBytes class_name_bytes = (classinfo_ptr).class_name_bytes + cdef MetaStringBytes class_name_bytes = (classinfo_ptr).class_name_bytes self._write_enum_string_bytes(buffer, class_name_bytes) cpdef inline xwrite_type_tag(self, Buffer buffer, cls): cdef PyObject* classinfo_ptr = self._c_classes_info[cls] assert classinfo_ptr != NULL - cdef EnumStringBytes type_tag_bytes = (classinfo_ptr).type_tag_bytes + cdef MetaStringBytes type_tag_bytes = (classinfo_ptr).type_tag_bytes self._write_enum_string_bytes(buffer, type_tag_bytes) cpdef inline read_class_by_type_tag(self, Buffer buffer): @@ -707,7 +707,7 @@ cdef class ClassResolver: return self._type_tag_to_class_x_lang_map[tag] cpdef inline xread_class(self, Buffer buffer): - cdef EnumStringBytes str_bytes = self._read_enum_string_bytes(buffer) + cdef MetaStringBytes str_bytes = self._read_enum_string_bytes(buffer) cdef uint64_t object_id = str_bytes cdef PyObject* cls_ptr = self._c_str_bytes_to_class[object_id] if cls_ptr != NULL: @@ -719,7 +719,7 @@ cdef class ClassResolver: return cls cpdef inline str xread_classname(self, Buffer buffer): - cdef EnumStringBytes str_bytes = self._read_enum_string_bytes(buffer) + cdef MetaStringBytes str_bytes = self._read_enum_string_bytes(buffer) cdef uint64_t object_id = str_bytes cdef PyObject* classname_ptr = self._c_enum_str_to_str[object_id] if classname_ptr != NULL: @@ -744,13 +744,13 @@ cdef class ClassResolver: if self.dynamic_write_string_id != 0: self.dynamic_write_string_id = 0 for ptr in self._c_dynamic_written_enum_string: - (ptr).dynamic_write_string_id = \ + (ptr).dynamic_write_string_id = \ DEFAULT_DYNAMIC_WRITE_STRING_ID self._c_dynamic_written_enum_string.clear() @cython.final -cdef class EnumStringBytes: +cdef class MetaStringBytes: cdef bytes data cdef int16_t length cdef int64_t hashcode @@ -763,7 +763,7 @@ cdef class EnumStringBytes: self.dynamic_write_string_id = DEFAULT_DYNAMIC_WRITE_STRING_ID def __eq__(self, other): - return type(other) is EnumStringBytes and other.hashcode == self.hashcode + return type(other) is MetaStringBytes and other.hashcode == self.hashcode def __hash__(self): return self.hashcode @@ -774,8 +774,8 @@ cdef class ClassInfo: cdef public object cls cdef public int16_t class_id cdef public Serializer serializer - cdef public EnumStringBytes class_name_bytes - cdef public EnumStringBytes type_tag_bytes + cdef public MetaStringBytes class_name_bytes + cdef public MetaStringBytes type_tag_bytes def __init__( self, @@ -788,11 +788,11 @@ cdef class ClassInfo: self.cls = cls self.class_id = class_id self.serializer = serializer - self.class_name_bytes = EnumStringBytes(class_name_bytes) + self.class_name_bytes = MetaStringBytes(class_name_bytes) if type_tag_bytes is None: self.type_tag_bytes = None else: - self.type_tag_bytes = EnumStringBytes(type_tag_bytes) + self.type_tag_bytes = MetaStringBytes(type_tag_bytes) def __repr__(self): return f"ClassInfo(cls={self.cls}, class_id={self.class_id}, " \