⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jrecord.java

📁 hadoop:Nutch集群平台
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** * Copyright 2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.hadoop.record.compiler;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;/** * * @author Milind Bhandarkar */public class JRecord extends JCompType {    private String mFQName;    private String mName;    private String mModule;    private ArrayList mFields;        /**     * Creates a new instance of JRecord     */    public JRecord(String name, ArrayList flist) {        super(name.replaceAll("\\.","::"), name, "Record", name);        mFQName = name;        int idx = name.lastIndexOf('.');        mName = name.substring(idx+1);        mModule = name.substring(0, idx);        mFields = flist;    }        public String getName() {        return mName;    }        public String getJavaFQName() {        return mFQName;    }        public String getCppFQName() {        return mFQName.replaceAll("\\.", "::");    }        public String getJavaPackage() {        return mModule;    }        public String getCppNameSpace() {        return mModule.replaceAll("\\.", "::");    }        public ArrayList getFields() {        return mFields;    }        public String getSignature() {        StringBuffer sb = new StringBuffer();        sb.append("L").append(mName).append("(");        for (Iterator i = mFields.iterator(); i.hasNext();) {            String s = ((JField) i.next()).getSignature();            sb.append(s);        }        sb.append(")");        return sb.toString();    }        public String genCppDecl(String fname) {        return "  "+mName+" "+fname+";\n";    }        public String genJavaReadMethod(String fname, String tag) {        return genJavaReadWrapper(fname, tag, false);    }        public String genJavaReadWrapper(String fname, String tag, boolean decl) {        StringBuffer ret = new StringBuffer("");        if (decl) {            ret.append("    "+getJavaFQName()+" "+fname+";\n");        }        ret.append("    "+fname+"= new "+getJavaFQName()+"();\n");        ret.append("    a_.readRecord("+fname+",\""+tag+"\");\n");        return ret.toString();    }        public String genJavaWriteWrapper(String fname, String tag) {        return "    a_.writeRecord("+fname+",\""+tag+"\");\n";    }        public void genCppCode(FileWriter hh, FileWriter cc)        throws IOException {        String[] ns = getCppNameSpace().split("::");        for (int i = 0; i < ns.length; i++) {            hh.write("namespace "+ns[i]+" {\n");        }                hh.write("class "+getName()+" : public ::hadoop::Record {\n");        hh.write("private:\n");                for (Iterator i = mFields.iterator(); i.hasNext();) {            JField jf = (JField) i.next();            hh.write(jf.genCppDecl());        }        hh.write("  mutable std::bitset<"+mFields.size()+"> bs_;\n");        hh.write("public:\n");        hh.write("  virtual void serialize(::hadoop::OArchive& a_, const char* tag) const;\n");        hh.write("  virtual void deserialize(::hadoop::IArchive& a_, const char* tag);\n");        hh.write("  virtual const ::std::string& type() const;\n");        hh.write("  virtual const ::std::string& signature() const;\n");        hh.write("  virtual bool validate() const;\n");        hh.write("  virtual bool operator<(const "+getName()+"& peer_) const;\n");        hh.write("  virtual bool operator==(const "+getName()+"& peer_) const;\n");        hh.write("  virtual ~"+getName()+"() {};\n");        int fIdx = 0;        for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) {            JField jf = (JField) i.next();            hh.write(jf.genCppGetSet(fIdx));        }        hh.write("}; // end record "+getName()+"\n");        for (int i=ns.length-1; i>=0; i--) {            hh.write("} // end namespace "+ns[i]+"\n");        }        cc.write("void "+getCppFQName()+"::serialize(::hadoop::OArchive& a_, const char* tag) const {\n");        cc.write("  if (!validate()) throw new ::hadoop::IOException(\"All fields not set.\");\n");        cc.write("  a_.startRecord(*this,tag);\n");        fIdx = 0;        for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) {            JField jf = (JField) i.next();            String name = jf.getName();            if (jf.getType() instanceof JBuffer) {                cc.write("  a_.serialize("+name+","+name+".length(),\""+jf.getTag()+"\");\n");            } else {                cc.write("  a_.serialize("+name+",\""+jf.getTag()+"\");\n");            }            cc.write("  bs_.reset("+fIdx+");\n");        }        cc.write("  a_.endRecord(*this,tag);\n");        cc.write("  return;\n");        cc.write("}\n");                cc.write("void "+getCppFQName()+"::deserialize(::hadoop::IArchive& a_, const char* tag) {\n");        cc.write("  a_.startRecord(*this,tag);\n");        fIdx = 0;        for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) {            JField jf = (JField) i.next();            String name = jf.getName();            if (jf.getType() instanceof JBuffer) {                cc.write("  { size_t len=0; a_.deserialize("+name+",len,\""+jf.getTag()+"\");}\n");            } else {                cc.write("  a_.deserialize("+name+",\""+jf.getTag()+"\");\n");            }            cc.write("  bs_.set("+fIdx+");\n");        }        cc.write("  a_.endRecord(*this,tag);\n");        cc.write("  return;\n");        cc.write("}\n");                cc.write("bool "+getCppFQName()+"::validate() const {\n");        cc.write("  if (bs_.size() != bs_.count()) return false;\n");        for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) {            JField jf = (JField) i.next();            JType type = jf.getType();            if (type instanceof JRecord) {                cc.write("  if (!"+jf.getName()+".validate()) return false;\n");            }        }        cc.write("  return true;\n");        cc.write("}\n");                cc.write("bool "+getCppFQName()+"::operator< (const "+getCppFQName()+"& peer_) const {\n");        cc.write("  return (1\n");        for (Iterator i = mFields.iterator(); i.hasNext();) {            JField jf = (JField) i.next();            String name = jf.getName();            cc.write("    && ("+name+" < peer_."+name+")\n");        }        cc.write("  );\n");        cc.write("}\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -