001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.record.compiler; 020 021import java.util.Map; 022 023import org.apache.hadoop.classification.InterfaceAudience; 024import org.apache.hadoop.classification.InterfaceStability; 025 026 027/** 028 * Abstract Base class for all types supported by Hadoop Record I/O. 029 * 030 * @deprecated Replaced by <a href="https://hadoop.apache.org/avro/">Avro</a>. 031 */ 032@Deprecated 033@InterfaceAudience.Public 034@InterfaceStability.Stable 035abstract public class JType { 036 037 static String toCamelCase(String name) { 038 char firstChar = name.charAt(0); 039 if (Character.isLowerCase(firstChar)) { 040 return ""+Character.toUpperCase(firstChar) + name.substring(1); 041 } 042 return name; 043 } 044 045 JavaType javaType; 046 CppType cppType; 047 CType cType; 048 049 abstract class JavaType { 050 private String name; 051 private String methodSuffix; 052 private String wrapper; 053 private String typeIDByteString; // points to TypeID.RIOType 054 055 JavaType(String javaname, 056 String suffix, 057 String wrapper, 058 String typeIDByteString) { 059 this.name = javaname; 060 this.methodSuffix = suffix; 061 this.wrapper = wrapper; 062 this.typeIDByteString = typeIDByteString; 063 } 064 065 void genDecl(CodeBuffer cb, String fname) { 066 cb.append("private "+name+" "+fname+";\n"); 067 } 068 069 void genStaticTypeInfo(CodeBuffer cb, String fname) { 070 cb.append(Consts.RTI_VAR + ".addField(\"" + fname + "\", " + 071 getTypeIDObjectString() + ");\n"); 072 } 073 074 abstract String getTypeIDObjectString(); 075 076 void genSetRTIFilter(CodeBuffer cb, Map<String, Integer> nestedStructMap) { 077 // do nothing by default 078 return; 079 } 080 081 /*void genRtiFieldCondition(CodeBuffer cb, String fname, int ct) { 082 cb.append("if ((tInfo.fieldID.equals(\"" + fname + "\")) && (typeVal ==" + 083 " org.apache.hadoop.record.meta." + getTypeIDByteString() + ")) {\n"); 084 cb.append("rtiFilterFields[i] = " + ct + ";\n"); 085 cb.append("}\n"); 086 } 087 088 void genRtiNestedFieldCondition(CodeBuffer cb, String varName, int ct) { 089 cb.append("if (" + varName + ".getElementTypeID().getTypeVal() == " + 090 "org.apache.hadoop.record.meta." + getTypeIDByteString() + 091 ") {\n"); 092 cb.append("rtiFilterFields[i] = " + ct + ";\n"); 093 cb.append("}\n"); 094 }*/ 095 096 void genConstructorParam(CodeBuffer cb, String fname) { 097 cb.append("final "+name+" "+fname); 098 } 099 100 void genGetSet(CodeBuffer cb, String fname) { 101 cb.append("public "+name+" get"+toCamelCase(fname)+"() {\n"); 102 cb.append("return "+fname+";\n"); 103 cb.append("}\n"); 104 cb.append("public void set"+toCamelCase(fname)+"(final "+name+" "+fname+") {\n"); 105 cb.append("this."+fname+"="+fname+";\n"); 106 cb.append("}\n"); 107 } 108 109 String getType() { 110 return name; 111 } 112 113 String getWrapperType() { 114 return wrapper; 115 } 116 117 String getMethodSuffix() { 118 return methodSuffix; 119 } 120 121 String getTypeIDByteString() { 122 return typeIDByteString; 123 } 124 125 void genWriteMethod(CodeBuffer cb, String fname, String tag) { 126 cb.append(Consts.RECORD_OUTPUT + ".write"+methodSuffix + 127 "("+fname+",\""+tag+"\");\n"); 128 } 129 130 void genReadMethod(CodeBuffer cb, String fname, String tag, boolean decl) { 131 if (decl) { 132 cb.append(name+" "+fname+";\n"); 133 } 134 cb.append(fname+"=" + Consts.RECORD_INPUT + ".read" + 135 methodSuffix+"(\""+tag+"\");\n"); 136 } 137 138 void genCompareTo(CodeBuffer cb, String fname, String other) { 139 cb.append(Consts.RIO_PREFIX + "ret = ("+fname+" == "+other+")? 0 :(("+ 140 fname+"<"+other+")?-1:1);\n"); 141 } 142 143 abstract void genCompareBytes(CodeBuffer cb); 144 145 abstract void genSlurpBytes(CodeBuffer cb, String b, String s, String l); 146 147 void genEquals(CodeBuffer cb, String fname, String peer) { 148 cb.append(Consts.RIO_PREFIX + "ret = ("+fname+"=="+peer+");\n"); 149 } 150 151 void genHashCode(CodeBuffer cb, String fname) { 152 cb.append(Consts.RIO_PREFIX + "ret = (int)"+fname+";\n"); 153 } 154 155 void genConstructorSet(CodeBuffer cb, String fname) { 156 cb.append("this."+fname+" = "+fname+";\n"); 157 } 158 159 void genClone(CodeBuffer cb, String fname) { 160 cb.append(Consts.RIO_PREFIX + "other."+fname+" = this."+fname+";\n"); 161 } 162 } 163 164 abstract class CppType { 165 private String name; 166 167 CppType(String cppname) { 168 name = cppname; 169 } 170 171 void genDecl(CodeBuffer cb, String fname) { 172 cb.append(name+" "+fname+";\n"); 173 } 174 175 void genStaticTypeInfo(CodeBuffer cb, String fname) { 176 cb.append("p->addField(new ::std::string(\"" + 177 fname + "\"), " + getTypeIDObjectString() + ");\n"); 178 } 179 180 void genGetSet(CodeBuffer cb, String fname) { 181 cb.append("virtual "+name+" get"+toCamelCase(fname)+"() const {\n"); 182 cb.append("return "+fname+";\n"); 183 cb.append("}\n"); 184 cb.append("virtual void set"+toCamelCase(fname)+"("+name+" m_) {\n"); 185 cb.append(fname+"=m_;\n"); 186 cb.append("}\n"); 187 } 188 189 abstract String getTypeIDObjectString(); 190 191 void genSetRTIFilter(CodeBuffer cb) { 192 // do nothing by default 193 return; 194 } 195 196 String getType() { 197 return name; 198 } 199 } 200 201 class CType { 202 203 } 204 205 abstract String getSignature(); 206 207 void setJavaType(JavaType jType) { 208 this.javaType = jType; 209 } 210 211 JavaType getJavaType() { 212 return javaType; 213 } 214 215 void setCppType(CppType cppType) { 216 this.cppType = cppType; 217 } 218 219 CppType getCppType() { 220 return cppType; 221 } 222 223 void setCType(CType cType) { 224 this.cType = cType; 225 } 226 227 CType getCType() { 228 return cType; 229 } 230}