PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 125 lines | 70 code | 17 blank | 38 comment | 8 complexity | 2b7a59cece1df82d5877abf046b4142e MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.serde2.objectinspector;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. /**
  22. * StandardMapObjectInspector works on map data that is stored as a Java Map
  23. * object. Note: the key object of the map must support equals and hashCode by
  24. * itself.
  25. *
  26. * We also plan to have a GeneralMapObjectInspector which can work on map with
  27. * key objects that does not support equals and hashCode. That will require us
  28. * to store InspectableObject as the key, which will have overridden equals and
  29. * hashCode methods.
  30. *
  31. * Always use the ObjectInspectorFactory to create new ObjectInspector objects,
  32. * instead of directly creating an instance of this class.
  33. */
  34. public class StandardMapObjectInspector implements SettableMapObjectInspector {
  35. ObjectInspector mapKeyObjectInspector;
  36. ObjectInspector mapValueObjectInspector;
  37. /**
  38. * Call ObjectInspectorFactory.getStandardMapObjectInspector instead.
  39. */
  40. protected StandardMapObjectInspector(ObjectInspector mapKeyObjectInspector,
  41. ObjectInspector mapValueObjectInspector) {
  42. this.mapKeyObjectInspector = mapKeyObjectInspector;
  43. this.mapValueObjectInspector = mapValueObjectInspector;
  44. }
  45. // without data
  46. public ObjectInspector getMapKeyObjectInspector() {
  47. return mapKeyObjectInspector;
  48. }
  49. public ObjectInspector getMapValueObjectInspector() {
  50. return mapValueObjectInspector;
  51. }
  52. // with data
  53. // TODO: Now we assume the key Object supports hashCode and equals functions.
  54. public Object getMapValueElement(Object data, Object key) {
  55. if (data == null || key == null) {
  56. return null;
  57. }
  58. Map<?, ?> map = (Map<?, ?>) data;
  59. return map.get(key);
  60. }
  61. public int getMapSize(Object data) {
  62. if (data == null) {
  63. return -1;
  64. }
  65. Map<?, ?> map = (Map<?, ?>) data;
  66. return map.size();
  67. }
  68. public Map<?, ?> getMap(Object data) {
  69. if (data == null) {
  70. return null;
  71. }
  72. Map<?, ?> map = (Map<?, ?>) data;
  73. return map;
  74. }
  75. public final Category getCategory() {
  76. return Category.MAP;
  77. }
  78. public String getTypeName() {
  79. return org.apache.hadoop.hive.serde.Constants.MAP_TYPE_NAME + "<"
  80. + mapKeyObjectInspector.getTypeName() + ","
  81. + mapValueObjectInspector.getTypeName() + ">";
  82. }
  83. // /////////////////////////////
  84. // SettableMapObjectInspector
  85. @Override
  86. public Object create() {
  87. Map<Object, Object> m = new HashMap<Object, Object>();
  88. return m;
  89. }
  90. @Override
  91. public Object clear(Object map) {
  92. Map<Object, Object> m = (HashMap<Object, Object>) map;
  93. m.clear();
  94. return m;
  95. }
  96. @Override
  97. public Object put(Object map, Object key, Object value) {
  98. Map<Object, Object> m = (HashMap<Object, Object>) map;
  99. m.put(key, value);
  100. return m;
  101. }
  102. @Override
  103. public Object remove(Object map, Object key) {
  104. Map<Object, Object> m = (HashMap<Object, Object>) map;
  105. m.remove(key);
  106. return m;
  107. }
  108. }