PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/dynamic_type/DynamicSerDeTypeSet.java

#
Java | 129 lines | 78 code | 17 blank | 34 comment | 7 complexity | e073af46019f3fefce4a43c0b78193a3 MD5 | raw file
  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
 19package org.apache.hadoop.hive.serde2.dynamic_type;
 20
 21import java.util.Collections;
 22import java.util.HashSet;
 23import java.util.Set;
 24
 25import org.apache.hadoop.hive.serde2.SerDeException;
 26import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
 27import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 28import org.apache.thrift.TException;
 29import org.apache.thrift.protocol.TProtocol;
 30import org.apache.thrift.protocol.TSet;
 31import org.apache.thrift.protocol.TType;
 32
 33/**
 34 * DynamicSerDeTypeSet.
 35 *
 36 */
 37public class DynamicSerDeTypeSet extends DynamicSerDeTypeBase {
 38
 39  // production is: set<FieldType()>
 40
 41  private static final int FD_TYPE = 0;
 42
 43  public DynamicSerDeTypeSet(int i) {
 44    super(i);
 45  }
 46
 47  public DynamicSerDeTypeSet(thrift_grammar p, int i) {
 48    super(p, i);
 49  }
 50
 51  // returns Set<?>
 52  @Override
 53  public Class getRealType() {
 54    try {
 55      Class c = getElementType().getRealType();
 56      Object o = c.newInstance();
 57      Set<?> l = Collections.singleton(o);
 58      return l.getClass();
 59    } catch (Exception e) {
 60      e.printStackTrace();
 61      throw new RuntimeException(e);
 62    }
 63  }
 64
 65  public DynamicSerDeTypeBase getElementType() {
 66    return ((DynamicSerDeFieldType) jjtGetChild(FD_TYPE)).getMyType();
 67  }
 68
 69  @Override
 70  public String toString() {
 71    return "set<" + getElementType().toString() + ">";
 72  }
 73
 74  @Override
 75  public byte getType() {
 76    return TType.SET;
 77  }
 78
 79  /**
 80   * NOTE: Set is not supported by Hive yet.
 81   */
 82  @Override
 83  public Object deserialize(Object reuse, TProtocol iprot)
 84      throws SerDeException, TException, IllegalAccessException {
 85    TSet theset = iprot.readSetBegin();
 86    if (theset == null) {
 87      return null;
 88    }
 89    Set<Object> result;
 90    if (reuse != null) {
 91      result = (Set<Object>) reuse;
 92      result.clear();
 93    } else {
 94      result = new HashSet<Object>();
 95    }
 96    for (int i = 0; i < theset.size; i++) {
 97      Object elem = getElementType().deserialize(null, iprot);
 98      result.add(elem);
 99    }
100    // in theory, the below call isn't needed in non thrift_mode, but let's not
101    // get too crazy
102    iprot.readSetEnd();
103    return result;
104  }
105
106  /**
107   * NOTE: Set is not supported by Hive yet. The code uses ListObjectInspector
108   * right now. We need to change it to SetObjectInspector when that is done.
109   */
110  TSet tset = null;
111
112  @Override
113  public void serialize(Object o, ObjectInspector oi, TProtocol oprot)
114      throws TException, SerDeException, NoSuchFieldException, IllegalAccessException {
115
116    ListObjectInspector loi = (ListObjectInspector) oi;
117
118    Set<Object> set = (Set<Object>) o;
119    DynamicSerDeTypeBase mt = getElementType();
120    tset = new TSet(mt.getType(), set.size());
121    oprot.writeSetBegin(tset);
122    for (Object element : set) {
123      mt.serialize(element, loi.getListElementObjectInspector(), oprot);
124    }
125    // in theory, the below call isn't needed in non thrift_mode, but let's not
126    // get too crazy
127    oprot.writeSetEnd();
128  }
129}