funky-eyes commented on code in PR #6208:
URL: https://github.com/apache/incubator-seata/pull/6208#discussion_r1442423580
##########
serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java:
##########
@@ -16,52 +16,133 @@
*/
package io.seata.serializer.seata;
-import java.nio.ByteBuffer;
-
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.seata.common.loader.LoadLevel;
+import io.seata.common.loader.Scope;
import io.seata.common.util.BufferUtils;
import io.seata.core.protocol.AbstractMessage;
+import io.seata.core.protocol.ProtocolConstants;
import io.seata.core.serializer.Serializer;
+import java.nio.ByteBuffer;
+
/**
* The Seata codec.
- *
*/
-@LoadLevel(name = "SEATA")
+@LoadLevel(name = "SEATA", scope = Scope.PROTOTYPE)
Review Comment:
为什么它是一个多例?
Why is it a PROTOTYPE case?
##########
serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java:
##########
@@ -16,52 +16,133 @@
*/
package io.seata.serializer.seata;
-import java.nio.ByteBuffer;
-
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.seata.common.loader.LoadLevel;
+import io.seata.common.loader.Scope;
import io.seata.common.util.BufferUtils;
import io.seata.core.protocol.AbstractMessage;
+import io.seata.core.protocol.ProtocolConstants;
import io.seata.core.serializer.Serializer;
+import java.nio.ByteBuffer;
+
/**
* The Seata codec.
- *
*/
-@LoadLevel(name = "SEATA")
+@LoadLevel(name = "SEATA", scope = Scope.PROTOTYPE)
public class SeataSerializer implements Serializer {
+ Serializer versionSeataSerializer;
+
+ Serializer seataSerializerV0 = new SeataSerializerV0();
+ Serializer seataSerializerV1 = new SeataSerializerV1();
+
+ public SeataSerializer(Byte version) {
+ if (version == ProtocolConstants.VERSION_0) {
+ versionSeataSerializer = seataSerializerV0;
+ } else if (version == ProtocolConstants.VERSION_1) {
+ versionSeataSerializer = seataSerializerV1;
+ }
+ if (versionSeataSerializer == null) {
+ throw new UnsupportedOperationException("version is not
supported");
+ }
+ }
+
@Override
public <T> byte[] serialize(T t) {
- if (!(t instanceof AbstractMessage)) {
- throw new IllegalArgumentException("AbstractMessage isn't
available.");
- }
- AbstractMessage abstractMessage = (AbstractMessage)t;
- //typecode
- short typecode = abstractMessage.getTypeCode();
- //msg codec
- MessageSeataCodec messageCodec =
MessageCodecFactory.getMessageCodec(typecode);
- //get empty ByteBuffer
- ByteBuf out = Unpooled.buffer(1024);
- //msg encode
- messageCodec.encode(t, out);
- byte[] body = new byte[out.readableBytes()];
- out.readBytes(body);
-
- //typecode + body
- ByteBuffer byteBuffer = ByteBuffer.allocate(2 + body.length);
- byteBuffer.putShort(typecode);
- byteBuffer.put(body);
-
- BufferUtils.flip(byteBuffer);
- byte[] content = new byte[byteBuffer.limit()];
- byteBuffer.get(content);
- return content;
+ return versionSeataSerializer.serialize(t);
}
@Override
public <T> T deserialize(byte[] bytes) {
+ return versionSeataSerializer.deserialize(bytes);
+ }
+
+
+ static class SeataSerializerV1 implements Serializer {
+
+ private SeataSerializerV1() {
+ }
+
+ @Override
+ public <T> byte[] serialize(T t) {
+ if (!(t instanceof AbstractMessage)) {
+ throw new IllegalArgumentException("AbstractMessage isn't
available.");
+ }
+ AbstractMessage abstractMessage = (AbstractMessage) t;
+ //type code
+ short typecode = abstractMessage.getTypeCode();
+ //msg codec
+ MessageSeataCodec messageCodec =
MessageCodecFactory.getMessageCodec(typecode, ProtocolConstants.VERSION_1);
+ //get empty ByteBuffer
+ ByteBuf out = Unpooled.buffer(1024);
+ //msg encode
+ messageCodec.encode(t, out);
+ byte[] body = new byte[out.readableBytes()];
+ out.readBytes(body);
+
+ ByteBuffer byteBuffer;
+
+ //typecode + body
+ byteBuffer = ByteBuffer.allocate(2 + body.length);
+ byteBuffer.putShort(typecode);
+ byteBuffer.put(body);
+
+ BufferUtils.flip(byteBuffer);
+ byte[] content = new byte[byteBuffer.limit()];
+ byteBuffer.get(content);
+ return content;
+ }
+
+ @Override
+ public <T> T deserialize(byte[] bytes) {
+ return deserializeByVersion(bytes, ProtocolConstants.VERSION_1);
+
+ }
+ }
+ static class SeataSerializerV0 implements Serializer {
Review Comment:
是做成匿名内部类的单例模式,不是将类放到seataserializer中做内部类
It is a singleton mode to make the anonymity inner class, not to put the
class in the seataserializer as the inner class.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]