ActiveJ学习心得——serializer(4)

2021SC@SDUSC

一、代码分析内容

本次博客介绍core-serializer包中的impl包。impl包就是serializer模块中对于各种数据结构的序列化定义。下面我们可以通过查看impl包的结构,来得知它是来实现哪一些数据结构的序列化定义的。

二、impl包结构


从图中可以看出,impl包中的类是很多的,因为要定义许多的数据结构。我们本次博客先看一下不是定义数据结构序列化的类,比如AbstractSerializerDefCollection类、AbstractSerializerDefMap类、ForwardingSerializerDef类、SerializerDefWithFixedSize类、SerializerDefWithNullable类、SerializerDefWithVarLength类、SerializerExpressions类。

三、代码解读

我们先来看一下SerializerDefWithFixedSize、SerializerDefWithNullable、SerializerDefWithVarLength这三个接口。
1.SerializerDefWithFixedSize类
SerializerDefWithFixedSize接口继承了SerializerDef 类。SerializerDef 这个类是serializer博客中第一篇提到的。SerializerDef类是一个interface接口,表示特定类到字节数组的序列化程序和反序列化程序,这是对serializer的定义。这个接口中定义了一些尚未实现的方法和接口。
SerializerDefWithFixedSize的意思是具有固定大小的序列化定义,下面定义的ensureFixedSize传入的int参数就是它的固定大小。

public interface SerializerDefWithFixedSize extends SerializerDef {
	SerializerDef ensureFixedSize(int fixedSize);
}

2.SerializerDefWithNullable类
SerializerDefWithNullable也是继承的SerializerDef类,它的作用是定义可为空的序列化器。

public interface SerializerDefWithNullable extends SerializerDef {
	SerializerDef ensureNullable(CompatibilityLevel compatibilityLevel);
}

3.SerializerDefWithVarLength类
SerializerDefWithVarLength接口和上面两个接口类似,也都是继承的SerializerDef类。它的作用是定义的序列化器变量长度。

public interface SerializerDefWithVarLength extends SerializerDef {
	SerializerDef ensureVarLength();
}

4.SerializerExpressions类
SerializerExpressions类提供将原语和字符串写入字节数组的方法,它里面定义着序列化程序表达式。
这个类里面定义了各种数据结构的write和read方法。

	public static Expression writeVarInt(Expression buf, Variable pos, Expression value) {
		return set(pos,
				staticCall(BinaryOutputUtils.class, "writeVarInt", buf, pos, cast(value, int.class)));
	}

	public static Expression writeVarLong(Expression buf, Variable pos, Expression value) {
		return set(pos,
				staticCall(BinaryOutputUtils.class, "writeVarLong", buf, pos, cast(value, long.class)));
	}

	public static Expression writeFloat(Expression buf, Variable pos, Expression value, boolean bigEndian) {
		return writeInt(buf, pos, staticCall(Float.class, "floatToIntBits", cast(value, float.class)), bigEndian);
	}
	public static Expression readShort(Expression in, boolean bigEndian) {
		return JDK_UNSAFE != null ?
				getUnaligned(in, "getShortUnaligned", Short.class, 2, bigEndian) :
				call(in, "readShort" + (bigEndian ? "" : "LE"));
	}

	public static Expression readChar(Expression in, boolean bigEndian) {
		return JDK_UNSAFE != null ?
				getUnaligned(in, "getCharUnaligned", Character.class, 2, bigEndian) :
				call(in, "readChar" + (bigEndian ? "" : "LE"));
	}

	public static Expression readInt(Expression in, boolean bigEndian) {
		return JDK_UNSAFE != null ?
				getUnaligned(in, "getIntUnaligned", Integer.class, 4, bigEndian) :
				call(in, "readInt" + (bigEndian ? "" : "LE"));
	}

5.AbstractSerializerDefCollection类
AbstractSerializerDefCollection类继承了AbstractSerializerDef类和SerializerDefWithNullable接口。它定义的属性和构造方法如下:

	protected final SerializerDef valueSerializer;
	protected final Class<?> encodeType;
	protected final Class<?> decodeType;
	protected final Class<?> elementType;
	protected final boolean nullable;

	protected AbstractSerializerDefCollection(@NotNull SerializerDef valueSerializer, @NotNull Class<?> encodeType, @NotNull Class<?> decodeType, @NotNull Class<?> elementType, boolean nullable) {
		this.valueSerializer = valueSerializer;
		this.encodeType = encodeType;
		this.decodeType = decodeType;
		this.elementType = elementType;
		this.nullable = nullable;
	}

这个类定义了一个序列化器集合的对象,它这里面有序列化器值、编码类别、解码类别、元素类别、为空。
6.AbstractSerializerDefMap类
这个类与上一个比较类似,不过上一个是定义一个集合,这个类是定义一个映射,有一个很明显的差别就是,此类的构造方法中有一个keySerializer。

	protected AbstractSerializerDefMap(@NotNull SerializerDef keySerializer, @NotNull SerializerDef valueSerializer, @NotNull Class<?> encodeType, @NotNull Class<?> decodeType, @NotNull Class<?> keyType, @NotNull Class<?> valueType, boolean nullable) {
		this.keySerializer = keySerializer;
		this.valueSerializer = valueSerializer;
		this.encodeType = encodeType;
		this.decodeType = decodeType;
		this.keyType = keyType;
		this.valueType = valueType;
		this.nullable = nullable;
	}

四、总结

本次分析的代码是impl包中的6个类,它们不是定义数据结构序列化的类,而是比较层次比较高的类,层次要比具体的数据结构要高一些。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值