PageRenderTime 23ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/lazy/objectinspector/LazyObjectInspectorFactory.java

#
Java | 141 lines | 99 code | 13 blank | 29 comment | 8 complexity | 406ddeaa99174f79ee110d1a245f5120 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.lazy.objectinspector;
 20
 21import java.util.ArrayList;
 22import java.util.HashMap;
 23import java.util.List;
 24
 25import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 26import org.apache.hadoop.io.Text;
 27
 28/**
 29 * ObjectInspectorFactory is the primary way to create new ObjectInspector
 30 * instances.
 31 * 
 32 * SerDe classes should call the static functions in this library to create an
 33 * ObjectInspector to return to the caller of SerDe2.getObjectInspector().
 34 * 
 35 * The reason of having caches here is that ObjectInspectors do not have an
 36 * internal state - so ObjectInspectors with the same construction parameters
 37 * should result in exactly the same ObjectInspector.
 38 */
 39public final class LazyObjectInspectorFactory {
 40
 41  static HashMap<ArrayList<Object>, LazySimpleStructObjectInspector> cachedLazySimpleStructObjectInspector =
 42      new HashMap<ArrayList<Object>, LazySimpleStructObjectInspector>();
 43
 44  public static LazySimpleStructObjectInspector getLazySimpleStructObjectInspector(
 45      List<String> structFieldNames,
 46      List<ObjectInspector> structFieldObjectInspectors, byte separator,
 47      Text nullSequence, boolean lastColumnTakesRest, boolean escaped,
 48      byte escapeChar) {
 49    ArrayList<Object> signature = new ArrayList<Object>();
 50    signature.add(structFieldNames);
 51    signature.add(structFieldObjectInspectors);
 52    signature.add(Byte.valueOf(separator));
 53    signature.add(nullSequence.toString());
 54    signature.add(Boolean.valueOf(lastColumnTakesRest));
 55    signature.add(Boolean.valueOf(escaped));
 56    signature.add(Byte.valueOf(escapeChar));
 57    LazySimpleStructObjectInspector result = cachedLazySimpleStructObjectInspector
 58        .get(signature);
 59    if (result == null) {
 60      result = new LazySimpleStructObjectInspector(structFieldNames,
 61          structFieldObjectInspectors, separator, nullSequence,
 62          lastColumnTakesRest, escaped, escapeChar);
 63      cachedLazySimpleStructObjectInspector.put(signature, result);
 64    }
 65    return result;
 66  }
 67
 68  static HashMap<ArrayList<Object>, LazyListObjectInspector> cachedLazySimpleListObjectInspector = new HashMap<ArrayList<Object>, LazyListObjectInspector>();
 69
 70  public static LazyListObjectInspector getLazySimpleListObjectInspector(
 71      ObjectInspector listElementObjectInspector, byte separator,
 72      Text nullSequence, boolean escaped, byte escapeChar) {
 73    ArrayList<Object> signature = new ArrayList<Object>();
 74    signature.add(listElementObjectInspector);
 75    signature.add(Byte.valueOf(separator));
 76    signature.add(nullSequence.toString());
 77    signature.add(Boolean.valueOf(escaped));
 78    signature.add(Byte.valueOf(escapeChar));
 79    LazyListObjectInspector result = cachedLazySimpleListObjectInspector
 80        .get(signature);
 81    if (result == null) {
 82      result = new LazyListObjectInspector(listElementObjectInspector,
 83          separator, nullSequence, escaped, escapeChar);
 84      cachedLazySimpleListObjectInspector.put(signature, result);
 85    }
 86    return result;
 87  }
 88
 89  static HashMap<ArrayList<Object>, LazyMapObjectInspector> cachedLazySimpleMapObjectInspector = new HashMap<ArrayList<Object>, LazyMapObjectInspector>();
 90
 91  public static LazyMapObjectInspector getLazySimpleMapObjectInspector(
 92      ObjectInspector mapKeyObjectInspector,
 93      ObjectInspector mapValueObjectInspector, byte itemSeparator,
 94      byte keyValueSeparator, Text nullSequence, boolean escaped,
 95      byte escapeChar) {
 96    ArrayList<Object> signature = new ArrayList<Object>();
 97    signature.add(mapKeyObjectInspector);
 98    signature.add(mapValueObjectInspector);
 99    signature.add(Byte.valueOf(itemSeparator));
100    signature.add(Byte.valueOf(keyValueSeparator));
101    signature.add(nullSequence.toString());
102    signature.add(Boolean.valueOf(escaped));
103    signature.add(Byte.valueOf(escapeChar));
104    LazyMapObjectInspector result = cachedLazySimpleMapObjectInspector
105        .get(signature);
106    if (result == null) {
107      result = new LazyMapObjectInspector(mapKeyObjectInspector,
108          mapValueObjectInspector, itemSeparator, keyValueSeparator,
109          nullSequence, escaped, escapeChar);
110      cachedLazySimpleMapObjectInspector.put(signature, result);
111    }
112    return result;
113  }
114
115  static HashMap<List<Object>, LazyUnionObjectInspector>
116    cachedLazyUnionObjectInspector =
117      new HashMap<List<Object>, LazyUnionObjectInspector>();
118
119  public static LazyUnionObjectInspector getLazyUnionObjectInspector(
120      List<ObjectInspector> ois, byte separator, Text nullSequence,
121      boolean escaped, byte escapeChar) {
122    List<Object> signature = new ArrayList<Object>();
123    signature.add(ois);
124    signature.add(Byte.valueOf(separator));
125    signature.add(nullSequence.toString());
126    signature.add(Boolean.valueOf(escaped));
127    signature.add(Byte.valueOf(escapeChar));
128    LazyUnionObjectInspector result = cachedLazyUnionObjectInspector
129        .get(signature);
130    if (result == null) {
131      result = new LazyUnionObjectInspector(ois, separator,
132          nullSequence, escaped, escapeChar);
133      cachedLazyUnionObjectInspector.put(signature, result);
134    }
135    return result;
136  }
137
138  private LazyObjectInspectorFactory() {
139    // prevent instantiation
140  }
141}