ZooKeeper源码阅读(六):JUTE
JUTE是跨语言序列化/反序列化工具,类似Thrift
用于产生java/c版本的消息类
比如:
zkDataBase.addCommittedProposal:
QuorumPacket pp = newQuorumPacket(Leader.PROPOSAL, request.zxid,
baos.toByteArray(), null);
Proposal p = newProposal();
p.packet = pp;
p.request = request;
这里就用到了jute生成的java版QuorumPacket。
rcc.jj --javacc--> org.apache.jute.compiler
zookeeper.jute --org.apache.jute.compiler.generated.RCC--> java/c的消息类实现
//JUTE由javacc编译src\java\main\org\apache\jute\compiler\generated\rcc.jj产生
Jute is a partial implementation of Hadoop's record serialization format.
http://www.java2s.com/Open-Source/Java-Open-Source-Library/Development/zookeeper/org.apache.jute.htm
//javacc : a lexer andparser generator for LL(k) grammars
http://cs.lmu.edu/~ray/notes/javacc/
//org.apache.jute.compiler 包含C、C++、C#、java的生成器
//Generate record code in given language. Language should be alllowercase.
public void genCode(String language, File outputDirectory)
//org.apache.jute.compiler
CppGenerator andJavaGenerator are the main entry points from the parser.
//入口在JFile.genCode
JFile又在rcc.jj中被引用
generate_jute_parser:
<mkdir dir="${jute_javacc.dir}${jute_javacc.packagedir}"/>
创建${build.dir}/jute_compiler/org/apache/jute/compiler/generated目录
<javacc
target="${java.src.dir}${jute_javacc.packagedir}/rcc.jj"
outputdirectory="${jute_javacc.dir}${jute_javacc.packagedir}/"
javacchome="${ivy.lib}"
/>
javacc rcc.jj
javacc编译.jj语法文件,生成一个工具Rcc。Rcc调用JFile的genCode
compile_jute:
<java classname="org.apache.jute.compiler.generated.Rcc"fork="true"dir="${src_generated.dir}">
<arg value="-l" />
<arg value="java" />
<arg value="../../zookeeper.jute" />
<java classname="org.apache.jute.compiler.generated.Rcc"fork="true"dir="${csrc_generated.dir}">
<arg value="-l" />
<arg value="c" />
<arg value="../../zookeeper.jute" />
生成:实体类java代码。(在文件夹src/java/generated目录下)
org.apache.zookeeper.data
…
生成:对应的C代码
rcc –l C++ …
org.apache.jute包中其余的类:
OutputArchive 序列化需要实现的接口
+BinaryInputArchive
+CsvInputArchive
+XmlInputArchive
InputArchive 反序列化需要实现的接口
+BinaryInputArchive
+CsvInputArchive
+XmlInputArchive
Index 表现为反序列化map的一个迭代器
+BinaryIndex
+CsvIndex
+XmlIndex
*Index idx = startVector(...);
* while(!idx.done()) {
* .... // read element of a vector
* idx.incr();
* }
Record : 所有产生的类都必须实现的接口
public interface Record{
public voidserialize(OutputArchive archive, String tag) throws IOException;
public voiddeserialize(InputArchive archive, String tag) throws IOException;
}
RecordReader、RecordWriter是所有InputArchive/OutputArchive的工厂类