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 * @deprecated Replaced by <a href="https://hadoop.apache.org/avro/">Avro</a>.
028 */
029@Deprecated
030@InterfaceAudience.Public
031@InterfaceStability.Stable
032public class JVector extends JCompType {
033  
034  static private int level = 0;
035  
036  static private String getId(String id) { return id+getLevel(); }
037  
038  static private String getLevel() { return Integer.toString(level); }
039  
040  static private void incrLevel() { level++; }
041  
042  static private void decrLevel() { level--; }
043  
044  private JType type;
045  
046  class JavaVector extends JavaCompType {
047    
048    private JType.JavaType element;
049    
050    JavaVector(JType.JavaType t) {
051      super("java.util.ArrayList<"+t.getWrapperType()+">",
052            "Vector", "java.util.ArrayList<"+t.getWrapperType()+">",
053            "TypeID.RIOType.VECTOR");
054      element = t;
055    }
056    
057    @Override
058    String getTypeIDObjectString() {
059      return "new org.apache.hadoop.record.meta.VectorTypeID(" + 
060      element.getTypeIDObjectString() + ")";
061    }
062
063    @Override
064    void genSetRTIFilter(CodeBuffer cb, Map<String, Integer> nestedStructMap) {
065      element.genSetRTIFilter(cb, nestedStructMap);
066    }
067
068    @Override
069    void genCompareTo(CodeBuffer cb, String fname, String other) {
070      cb.append("{\n");
071      incrLevel();
072      cb.append("int "+getId(Consts.RIO_PREFIX + "len1")+" = "+fname+
073          ".size();\n");
074      cb.append("int "+getId(Consts.RIO_PREFIX + "len2")+" = "+other+
075          ".size();\n");
076      cb.append("for(int "+getId(Consts.RIO_PREFIX + "vidx")+" = 0; "+
077          getId(Consts.RIO_PREFIX + "vidx")+"<"+getId(Consts.RIO_PREFIX + "len1")+
078          " && "+getId(Consts.RIO_PREFIX + "vidx")+"<"+
079          getId(Consts.RIO_PREFIX + "len2")+"; "+
080          getId(Consts.RIO_PREFIX + "vidx")+"++) {\n");
081      cb.append(element.getType()+" "+getId(Consts.RIO_PREFIX + "e1")+
082                " = "+fname+
083                ".get("+getId(Consts.RIO_PREFIX + "vidx")+");\n");
084      cb.append(element.getType()+" "+getId(Consts.RIO_PREFIX + "e2")+
085                " = "+other+
086                ".get("+getId(Consts.RIO_PREFIX + "vidx")+");\n");
087      element.genCompareTo(cb, getId(Consts.RIO_PREFIX + "e1"), 
088          getId(Consts.RIO_PREFIX + "e2"));
089      cb.append("if (" + Consts.RIO_PREFIX + "ret != 0) { return " +
090          Consts.RIO_PREFIX + "ret; }\n");
091      cb.append("}\n");
092      cb.append(Consts.RIO_PREFIX + "ret = ("+getId(Consts.RIO_PREFIX + "len1")+
093          " - "+getId(Consts.RIO_PREFIX + "len2")+");\n");
094      decrLevel();
095      cb.append("}\n");
096    }
097    
098    @Override
099    void genReadMethod(CodeBuffer cb, String fname, String tag, boolean decl) {
100      if (decl) {
101        cb.append(getType()+" "+fname+";\n");
102      }
103      cb.append("{\n");
104      incrLevel();
105      cb.append("org.apache.hadoop.record.Index "+
106          getId(Consts.RIO_PREFIX + "vidx")+" = " + 
107          Consts.RECORD_INPUT + ".startVector(\""+tag+"\");\n");
108      cb.append(fname+"=new "+getType()+"();\n");
109      cb.append("for (; !"+getId(Consts.RIO_PREFIX + "vidx")+".done(); " + 
110          getId(Consts.RIO_PREFIX + "vidx")+".incr()) {\n");
111      element.genReadMethod(cb, getId(Consts.RIO_PREFIX + "e"), 
112          getId(Consts.RIO_PREFIX + "e"), true);
113      cb.append(fname+".add("+getId(Consts.RIO_PREFIX + "e")+");\n");
114      cb.append("}\n");
115      cb.append(Consts.RECORD_INPUT + ".endVector(\""+tag+"\");\n");
116      decrLevel();
117      cb.append("}\n");
118    }
119    
120    @Override
121    void genWriteMethod(CodeBuffer cb, String fname, String tag) {
122      cb.append("{\n");
123      incrLevel();
124      cb.append(Consts.RECORD_OUTPUT + ".startVector("+fname+",\""+tag+"\");\n");
125      cb.append("int "+getId(Consts.RIO_PREFIX + "len")+" = "+fname+".size();\n");
126      cb.append("for(int "+getId(Consts.RIO_PREFIX + "vidx")+" = 0; " + 
127          getId(Consts.RIO_PREFIX + "vidx")+"<"+getId(Consts.RIO_PREFIX + "len")+
128          "; "+getId(Consts.RIO_PREFIX + "vidx")+"++) {\n");
129      cb.append(element.getType()+" "+getId(Consts.RIO_PREFIX + "e")+" = "+
130          fname+".get("+getId(Consts.RIO_PREFIX + "vidx")+");\n");
131      element.genWriteMethod(cb, getId(Consts.RIO_PREFIX + "e"), 
132          getId(Consts.RIO_PREFIX + "e"));
133      cb.append("}\n");
134      cb.append(Consts.RECORD_OUTPUT + ".endVector("+fname+",\""+tag+"\");\n");
135      cb.append("}\n");
136      decrLevel();
137    }
138    
139    @Override
140    void genSlurpBytes(CodeBuffer cb, String b, String s, String l) {
141      cb.append("{\n");
142      incrLevel();
143      cb.append("int "+getId("vi")+
144                " = org.apache.hadoop.record.Utils.readVInt("+b+", "+s+");\n");
145      cb.append("int "+getId("vz")+
146                " = org.apache.hadoop.record.Utils.getVIntSize("+getId("vi")+");\n");
147      cb.append(s+"+="+getId("vz")+"; "+l+"-="+getId("vz")+";\n");
148      cb.append("for (int "+getId("vidx")+" = 0; "+getId("vidx")+
149                " < "+getId("vi")+"; "+getId("vidx")+"++)");
150      element.genSlurpBytes(cb, b, s, l);
151      decrLevel();
152      cb.append("}\n");
153    }
154    
155    @Override
156    void genCompareBytes(CodeBuffer cb) {
157      cb.append("{\n");
158      incrLevel();
159      cb.append("int "+getId("vi1")+
160                " = org.apache.hadoop.record.Utils.readVInt(b1, s1);\n");
161      cb.append("int "+getId("vi2")+
162                " = org.apache.hadoop.record.Utils.readVInt(b2, s2);\n");
163      cb.append("int "+getId("vz1")+
164                " = org.apache.hadoop.record.Utils.getVIntSize("+getId("vi1")+");\n");
165      cb.append("int "+getId("vz2")+
166                " = org.apache.hadoop.record.Utils.getVIntSize("+getId("vi2")+");\n");
167      cb.append("s1+="+getId("vz1")+"; s2+="+getId("vz2")+
168                "; l1-="+getId("vz1")+"; l2-="+getId("vz2")+";\n");
169      cb.append("for (int "+getId("vidx")+" = 0; "+getId("vidx")+
170                " < "+getId("vi1")+" && "+getId("vidx")+" < "+getId("vi2")+
171                "; "+getId("vidx")+"++)");
172      element.genCompareBytes(cb);
173      cb.append("if ("+getId("vi1")+" != "+getId("vi2")+
174                ") { return ("+getId("vi1")+"<"+getId("vi2")+")?-1:0; }\n");
175      decrLevel();
176      cb.append("}\n");
177    }
178  }
179  
180  class CppVector extends CppCompType {
181    
182    private JType.CppType element;
183    
184    CppVector(JType.CppType t) {
185      super("::std::vector< "+t.getType()+" >");
186      element = t;
187    }
188    
189    @Override
190    String getTypeIDObjectString() {
191      return "new ::hadoop::VectorTypeID(" +    
192      element.getTypeIDObjectString() + ")";
193    }
194
195    @Override
196    void genSetRTIFilter(CodeBuffer cb) {
197      element.genSetRTIFilter(cb);
198    }
199
200  }
201  
202  /** Creates a new instance of JVector */
203  public JVector(JType t) {
204    type = t;
205    setJavaType(new JavaVector(t.getJavaType()));
206    setCppType(new CppVector(t.getCppType()));
207    setCType(new CCompType());
208  }
209  
210  @Override
211  String getSignature() {
212    return "[" + type.getSignature() + "]";
213  }
214}