PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.2.0-rc0/src/java/org/apache/hcatalog/data/schema/HCatSchema.java

#
Java | 142 lines | 86 code | 21 blank | 35 comment | 12 complexity | 68d1257ed2d527131baefe9448447e80 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.hcatalog.data.schema;
  19. import java.io.Serializable;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.apache.hcatalog.common.HCatException;
  26. /**
  27. * HCatSchema. This class is NOT thread-safe.
  28. */
  29. public class HCatSchema implements Serializable{
  30. private static final long serialVersionUID = 1L;
  31. private final List<HCatFieldSchema> fieldSchemas;
  32. private final Map<String,Integer> fieldPositionMap;
  33. private final List<String> fieldNames;
  34. /**
  35. *
  36. * @param fieldSchemas is now owned by HCatSchema. Any subsequent modifications
  37. * on fieldSchemas won't get reflected in HCatSchema. Each fieldSchema's name
  38. * in the list must be unique, otherwise throws IllegalArgumentException.
  39. */
  40. public HCatSchema(final List<HCatFieldSchema> fieldSchemas){
  41. this.fieldSchemas = new ArrayList<HCatFieldSchema>(fieldSchemas);
  42. int idx = 0;
  43. fieldPositionMap = new HashMap<String,Integer>();
  44. fieldNames = new ArrayList<String>();
  45. for (HCatFieldSchema field : fieldSchemas){
  46. if(field == null)
  47. throw new IllegalArgumentException("Field cannot be null");
  48. String fieldName = field.getName();
  49. if(fieldPositionMap.containsKey(fieldName))
  50. throw new IllegalArgumentException("Field named " + fieldName +
  51. " already exists");
  52. fieldPositionMap.put(fieldName, idx);
  53. fieldNames.add(fieldName);
  54. idx++;
  55. }
  56. }
  57. public void append(final HCatFieldSchema hfs) throws HCatException{
  58. if(hfs == null)
  59. throw new HCatException("Attempt to append null HCatFieldSchema in HCatSchema.");
  60. String fieldName = hfs.getName();
  61. if(fieldPositionMap.containsKey(fieldName))
  62. throw new HCatException("Attempt to append HCatFieldSchema with already " +
  63. "existing name: " + fieldName + ".");
  64. this.fieldSchemas.add(hfs);
  65. this.fieldNames.add(fieldName);
  66. this.fieldPositionMap.put(fieldName, this.size()-1);
  67. }
  68. /**
  69. * Users are not allowed to modify the list directly, since HCatSchema
  70. * maintains internal state. Use append/remove to modify the schema.
  71. */
  72. public List<HCatFieldSchema> getFields(){
  73. return Collections.unmodifiableList(this.fieldSchemas);
  74. }
  75. /**
  76. * @param fieldName
  77. * @return the index of field named fieldName in Schema. If field is not
  78. * present, returns null.
  79. */
  80. public Integer getPosition(String fieldName) {
  81. return fieldPositionMap.get(fieldName);
  82. }
  83. public HCatFieldSchema get(String fieldName) throws HCatException {
  84. return get(getPosition(fieldName));
  85. }
  86. public List<String> getFieldNames(){
  87. return this.fieldNames;
  88. }
  89. public HCatFieldSchema get(int position) {
  90. return fieldSchemas.get(position);
  91. }
  92. public int size(){
  93. return fieldSchemas.size();
  94. }
  95. public void remove(final HCatFieldSchema hcatFieldSchema) throws HCatException {
  96. if(!fieldSchemas.contains(hcatFieldSchema)){
  97. throw new HCatException("Attempt to delete a non-existent column from HCat Schema: "+ hcatFieldSchema);
  98. }
  99. fieldSchemas.remove(hcatFieldSchema);
  100. fieldPositionMap.remove(hcatFieldSchema);
  101. fieldNames.remove(hcatFieldSchema.getName());
  102. }
  103. @Override
  104. public String toString() {
  105. boolean first = true;
  106. StringBuilder sb = new StringBuilder();
  107. for (HCatFieldSchema hfs : fieldSchemas){
  108. if (!first){
  109. sb.append(",");
  110. }else{
  111. first = false;
  112. }
  113. if (hfs.getName() != null){
  114. sb.append(hfs.getName());
  115. sb.append(":");
  116. }
  117. sb.append(hfs.toString());
  118. }
  119. return sb.toString();
  120. }
  121. }