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    
019    package org.apache.hadoop.record.compiler;
020    
021    import java.util.Map;
022    
023    import org.apache.hadoop.classification.InterfaceAudience;
024    import org.apache.hadoop.classification.InterfaceStability;
025    
026    
027    /**
028     * @deprecated Replaced by <a href="https://hadoop.apache.org/avro/">Avro</a>.
029     */
030    @Deprecated
031    @InterfaceAudience.Public
032    @InterfaceStability.Stable
033    public class JMap extends JCompType {
034      
035      static private int level = 0;
036      
037      static private String getLevel() { return Integer.toString(level); }
038      
039      static private void incrLevel() { level++; }
040      
041      static private void decrLevel() { level--; }
042      
043      static private String getId(String id) { return id+getLevel(); }
044      
045      private JType keyType;
046      private JType valueType;
047      
048      class JavaMap extends JavaCompType {
049        
050        JType.JavaType key;
051        JType.JavaType value;
052        
053        JavaMap(JType.JavaType key, JType.JavaType value) {
054          super("java.util.TreeMap<"+key.getWrapperType()+","+value.getWrapperType()+">",
055                "Map",
056                "java.util.TreeMap<"+key.getWrapperType()+","+value.getWrapperType()+">",
057                "TypeID.RIOType.MAP");
058          this.key = key;
059          this.value = value;
060        }
061        
062        @Override
063        String getTypeIDObjectString() {
064          return "new org.apache.hadoop.record.meta.MapTypeID(" + 
065            key.getTypeIDObjectString() + ", " + 
066            value.getTypeIDObjectString() + ")";
067        }
068    
069        @Override
070        void genSetRTIFilter(CodeBuffer cb, Map<String, Integer> nestedStructMap) {
071          key.genSetRTIFilter(cb, nestedStructMap);
072          value.genSetRTIFilter(cb, nestedStructMap);
073        }
074    
075        @Override
076        void genCompareTo(CodeBuffer cb, String fname, String other) {
077          String setType = "java.util.Set<"+key.getWrapperType()+"> ";
078          String iterType = "java.util.Iterator<"+key.getWrapperType()+"> ";
079          cb.append("{\n");
080          cb.append(setType+getId(Consts.RIO_PREFIX + "set1")+" = "+
081              fname+".keySet();\n");
082          cb.append(setType+getId(Consts.RIO_PREFIX + "set2")+" = "+
083              other+".keySet();\n");
084          cb.append(iterType+getId(Consts.RIO_PREFIX + "miter1")+" = "+
085                    getId(Consts.RIO_PREFIX + "set1")+".iterator();\n");
086          cb.append(iterType+getId(Consts.RIO_PREFIX + "miter2")+" = "+
087                    getId(Consts.RIO_PREFIX + "set2")+".iterator();\n");
088          cb.append("for(; "+getId(Consts.RIO_PREFIX + "miter1")+".hasNext() && "+
089                    getId(Consts.RIO_PREFIX + "miter2")+".hasNext();) {\n");
090          cb.append(key.getType()+" "+getId(Consts.RIO_PREFIX + "k1")+
091                    " = "+getId(Consts.RIO_PREFIX + "miter1")+".next();\n");
092          cb.append(key.getType()+" "+getId(Consts.RIO_PREFIX + "k2")+
093                    " = "+getId(Consts.RIO_PREFIX + "miter2")+".next();\n");
094          key.genCompareTo(cb, getId(Consts.RIO_PREFIX + "k1"), 
095              getId(Consts.RIO_PREFIX + "k2"));
096          cb.append("if (" + Consts.RIO_PREFIX + "ret != 0) { return " + 
097              Consts.RIO_PREFIX + "ret; }\n");
098          cb.append("}\n");
099          cb.append(Consts.RIO_PREFIX + "ret = ("+getId(Consts.RIO_PREFIX + "set1")+
100              ".size() - "+getId(Consts.RIO_PREFIX + "set2")+".size());\n");
101          cb.append("}\n");
102        }
103        
104        @Override
105        void genReadMethod(CodeBuffer cb, String fname, String tag, boolean decl) {
106          if (decl) {
107            cb.append(getType()+" "+fname+";\n");
108          }
109          cb.append("{\n");
110          incrLevel();
111          cb.append("org.apache.hadoop.record.Index " + 
112              getId(Consts.RIO_PREFIX + "midx")+" = " + 
113              Consts.RECORD_INPUT + ".startMap(\""+tag+"\");\n");
114          cb.append(fname+"=new "+getType()+"();\n");
115          cb.append("for (; !"+getId(Consts.RIO_PREFIX + "midx")+".done(); "+
116              getId(Consts.RIO_PREFIX + "midx")+".incr()) {\n");
117          key.genReadMethod(cb, getId(Consts.RIO_PREFIX + "k"),
118              getId(Consts.RIO_PREFIX + "k"), true);
119          value.genReadMethod(cb, getId(Consts.RIO_PREFIX + "v"), 
120              getId(Consts.RIO_PREFIX + "v"), true);
121          cb.append(fname+".put("+getId(Consts.RIO_PREFIX + "k")+","+
122              getId(Consts.RIO_PREFIX + "v")+");\n");
123          cb.append("}\n");
124          cb.append(Consts.RECORD_INPUT + ".endMap(\""+tag+"\");\n");
125          decrLevel();
126          cb.append("}\n");
127        }
128        
129        @Override
130        void genWriteMethod(CodeBuffer cb, String fname, String tag) {
131          String setType = "java.util.Set<java.util.Map.Entry<"+
132            key.getWrapperType()+","+value.getWrapperType()+">> ";
133          String entryType = "java.util.Map.Entry<"+
134            key.getWrapperType()+","+value.getWrapperType()+"> ";
135          String iterType = "java.util.Iterator<java.util.Map.Entry<"+
136            key.getWrapperType()+","+value.getWrapperType()+">> ";
137          cb.append("{\n");
138          incrLevel();
139          cb.append(Consts.RECORD_OUTPUT + ".startMap("+fname+",\""+tag+"\");\n");
140          cb.append(setType+getId(Consts.RIO_PREFIX + "es")+" = "+
141              fname+".entrySet();\n");
142          cb.append("for("+iterType+getId(Consts.RIO_PREFIX + "midx")+" = "+
143              getId(Consts.RIO_PREFIX + "es")+".iterator(); "+
144              getId(Consts.RIO_PREFIX + "midx")+".hasNext();) {\n");
145          cb.append(entryType+getId(Consts.RIO_PREFIX + "me")+" = "+
146              getId(Consts.RIO_PREFIX + "midx")+".next();\n");
147          cb.append(key.getType()+" "+getId(Consts.RIO_PREFIX + "k")+" = "+
148              getId(Consts.RIO_PREFIX + "me")+".getKey();\n");
149          cb.append(value.getType()+" "+getId(Consts.RIO_PREFIX + "v")+" = "+
150              getId(Consts.RIO_PREFIX + "me")+".getValue();\n");
151          key.genWriteMethod(cb, getId(Consts.RIO_PREFIX + "k"), 
152              getId(Consts.RIO_PREFIX + "k"));
153          value.genWriteMethod(cb, getId(Consts.RIO_PREFIX + "v"), 
154              getId(Consts.RIO_PREFIX + "v"));
155          cb.append("}\n");
156          cb.append(Consts.RECORD_OUTPUT + ".endMap("+fname+",\""+tag+"\");\n");
157          cb.append("}\n");
158          decrLevel();
159        }
160        
161        @Override
162        void genSlurpBytes(CodeBuffer cb, String b, String s, String l) {
163          cb.append("{\n");
164          incrLevel();
165          cb.append("int "+getId("mi")+
166                    " = org.apache.hadoop.record.Utils.readVInt("+b+", "+s+");\n");
167          cb.append("int "+getId("mz")+
168                    " = org.apache.hadoop.record.Utils.getVIntSize("+getId("mi")+");\n");
169          cb.append(s+"+="+getId("mz")+"; "+l+"-="+getId("mz")+";\n");
170          cb.append("for (int "+getId("midx")+" = 0; "+getId("midx")+
171                    " < "+getId("mi")+"; "+getId("midx")+"++) {");
172          key.genSlurpBytes(cb, b, s, l);
173          value.genSlurpBytes(cb, b, s, l);
174          cb.append("}\n");
175          decrLevel();
176          cb.append("}\n");
177        }
178        
179        @Override
180        void genCompareBytes(CodeBuffer cb) {
181          cb.append("{\n");
182          incrLevel();
183          cb.append("int "+getId("mi1")+
184                    " = org.apache.hadoop.record.Utils.readVInt(b1, s1);\n");
185          cb.append("int "+getId("mi2")+
186                    " = org.apache.hadoop.record.Utils.readVInt(b2, s2);\n");
187          cb.append("int "+getId("mz1")+
188                    " = org.apache.hadoop.record.Utils.getVIntSize("+getId("mi1")+");\n");
189          cb.append("int "+getId("mz2")+
190                    " = org.apache.hadoop.record.Utils.getVIntSize("+getId("mi2")+");\n");
191          cb.append("s1+="+getId("mz1")+"; s2+="+getId("mz2")+
192                    "; l1-="+getId("mz1")+"; l2-="+getId("mz2")+";\n");
193          cb.append("for (int "+getId("midx")+" = 0; "+getId("midx")+
194                    " < "+getId("mi1")+" && "+getId("midx")+" < "+getId("mi2")+
195                    "; "+getId("midx")+"++) {");
196          key.genCompareBytes(cb);
197          value.genSlurpBytes(cb, "b1", "s1", "l1");
198          value.genSlurpBytes(cb, "b2", "s2", "l2");
199          cb.append("}\n");
200          cb.append("if ("+getId("mi1")+" != "+getId("mi2")+
201                    ") { return ("+getId("mi1")+"<"+getId("mi2")+")?-1:0; }\n");
202          decrLevel();
203          cb.append("}\n");
204        }
205      }
206      
207      class CppMap extends CppCompType {
208        
209        JType.CppType key;
210        JType.CppType value;
211        
212        CppMap(JType.CppType key, JType.CppType value) {
213          super("::std::map< "+key.getType()+", "+ value.getType()+" >");
214          this.key = key;
215          this.value = value;
216        }
217        
218        @Override
219        String getTypeIDObjectString() {
220          return "new ::hadoop::MapTypeID(" + 
221            key.getTypeIDObjectString() + ", " + 
222            value.getTypeIDObjectString() + ")";
223        }
224    
225        @Override
226        void genSetRTIFilter(CodeBuffer cb) {
227          key.genSetRTIFilter(cb);
228          value.genSetRTIFilter(cb);
229        }
230    
231      }
232      
233      /** Creates a new instance of JMap */
234      public JMap(JType t1, JType t2) {
235        setJavaType(new JavaMap(t1.getJavaType(), t2.getJavaType()));
236        setCppType(new CppMap(t1.getCppType(), t2.getCppType()));
237        setCType(new CType());
238        keyType = t1;
239        valueType = t2;
240      }
241      
242      @Override
243      String getSignature() {
244        return "{" + keyType.getSignature() + valueType.getSignature() +"}";
245      }
246    }