[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.record.compiler; |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * Code generator for "buffer" type. |
---|
| 24 | */ |
---|
| 25 | public class JBuffer extends JCompType { |
---|
| 26 | |
---|
| 27 | class JavaBuffer extends JavaCompType { |
---|
| 28 | |
---|
| 29 | JavaBuffer() { |
---|
| 30 | super("org.apache.hadoop.record.Buffer", "Buffer", |
---|
| 31 | "org.apache.hadoop.record.Buffer", "TypeID.RIOType.BUFFER"); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | String getTypeIDObjectString() { |
---|
| 35 | return "org.apache.hadoop.record.meta.TypeID.BufferTypeID"; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | void genCompareTo(CodeBuffer cb, String fname, String other) { |
---|
| 39 | cb.append(Consts.RIO_PREFIX + "ret = "+fname+".compareTo("+other+");\n"); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | void genEquals(CodeBuffer cb, String fname, String peer) { |
---|
| 43 | cb.append(Consts.RIO_PREFIX + "ret = "+fname+".equals("+peer+");\n"); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | void genHashCode(CodeBuffer cb, String fname) { |
---|
| 47 | cb.append(Consts.RIO_PREFIX + "ret = "+fname+".hashCode();\n"); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | void genSlurpBytes(CodeBuffer cb, String b, String s, String l) { |
---|
| 51 | cb.append("{\n"); |
---|
| 52 | cb.append("int i = org.apache.hadoop.record.Utils.readVInt("+ |
---|
| 53 | b+", "+s+");\n"); |
---|
| 54 | cb.append("int z = org.apache.hadoop.record.Utils.getVIntSize(i);\n"); |
---|
| 55 | cb.append(s+" += z+i; "+l+" -= (z+i);\n"); |
---|
| 56 | cb.append("}\n"); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | void genCompareBytes(CodeBuffer cb) { |
---|
| 60 | cb.append("{\n"); |
---|
| 61 | cb.append("int i1 = org.apache.hadoop.record.Utils.readVInt(b1, s1);\n"); |
---|
| 62 | cb.append("int i2 = org.apache.hadoop.record.Utils.readVInt(b2, s2);\n"); |
---|
| 63 | cb.append("int z1 = org.apache.hadoop.record.Utils.getVIntSize(i1);\n"); |
---|
| 64 | cb.append("int z2 = org.apache.hadoop.record.Utils.getVIntSize(i2);\n"); |
---|
| 65 | cb.append("s1+=z1; s2+=z2; l1-=z1; l2-=z2;\n"); |
---|
| 66 | cb.append("int r1 = org.apache.hadoop.record.Utils.compareBytes(b1,s1,i1,b2,s2,i2);\n"); |
---|
| 67 | cb.append("if (r1 != 0) { return (r1<0)?-1:0; }\n"); |
---|
| 68 | cb.append("s1+=i1; s2+=i2; l1-=i1; l1-=i2;\n"); |
---|
| 69 | cb.append("}\n"); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | class CppBuffer extends CppCompType { |
---|
| 74 | |
---|
| 75 | CppBuffer() { |
---|
| 76 | super(" ::std::string"); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void genGetSet(CodeBuffer cb, String fname) { |
---|
| 80 | cb.append("virtual const "+getType()+"& get"+toCamelCase(fname)+"() const {\n"); |
---|
| 81 | cb.append("return "+fname+";\n"); |
---|
| 82 | cb.append("}\n"); |
---|
| 83 | cb.append("virtual "+getType()+"& get"+toCamelCase(fname)+"() {\n"); |
---|
| 84 | cb.append("return "+fname+";\n"); |
---|
| 85 | cb.append("}\n"); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | String getTypeIDObjectString() { |
---|
| 89 | return "new ::hadoop::TypeID(::hadoop::RIOTYPE_BUFFER)"; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | } |
---|
| 93 | /** Creates a new instance of JBuffer */ |
---|
| 94 | public JBuffer() { |
---|
| 95 | setJavaType(new JavaBuffer()); |
---|
| 96 | setCppType(new CppBuffer()); |
---|
| 97 | setCType(new CCompType()); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | String getSignature() { |
---|
| 101 | return "B"; |
---|
| 102 | } |
---|
| 103 | } |
---|