PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/columnar/BytesRefArrayWritable.java

#
Java | 256 lines | 128 code | 25 blank | 103 comment | 26 complexity | aed2b8f7b58a19fd38812b35d7b3b5bd 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.columnar;
  19. import java.io.DataInput;
  20. import java.io.DataOutput;
  21. import java.io.IOException;
  22. import java.util.Arrays;
  23. import org.apache.hadoop.io.Writable;
  24. import org.apache.hadoop.io.WritableFactories;
  25. import org.apache.hadoop.io.WritableFactory;
  26. /**
  27. * <tt>BytesRefArrayWritable</tt> holds an array reference to BytesRefWritable,
  28. * and is able to resize without recreating new array if not necessary.
  29. * <p>
  30. *
  31. * Each <tt>BytesRefArrayWritable holds</tt> instance has a <i>valid</i> field,
  32. * which is the desired valid number of <tt>BytesRefWritable</tt> it holds.
  33. * <tt>resetValid</tt> can reset the valid, but it will not care the underlying
  34. * BytesRefWritable.
  35. */
  36. public class BytesRefArrayWritable implements Writable,
  37. Comparable<BytesRefArrayWritable> {
  38. private BytesRefWritable[] bytesRefWritables = null;
  39. private int valid = 0;
  40. /**
  41. * Constructs an empty array with the specified capacity.
  42. *
  43. * @param capacity
  44. * initial capacity
  45. * @exception IllegalArgumentException
  46. * if the specified initial capacity is negative
  47. */
  48. public BytesRefArrayWritable(int capacity) {
  49. if (capacity < 0) {
  50. throw new IllegalArgumentException("Capacity can not be negative.");
  51. }
  52. bytesRefWritables = new BytesRefWritable[0];
  53. ensureCapacity(capacity);
  54. }
  55. /**
  56. * Constructs an empty array with a capacity of ten.
  57. */
  58. public BytesRefArrayWritable() {
  59. this(10);
  60. }
  61. /**
  62. * Returns the number of valid elements.
  63. *
  64. * @return the number of valid elements
  65. */
  66. public int size() {
  67. return valid;
  68. }
  69. /**
  70. * Gets the BytesRefWritable at the specified position. Make sure the position
  71. * is valid by first call resetValid.
  72. *
  73. * @param index
  74. * the position index, starting from zero
  75. * @throws IndexOutOfBoundsException
  76. */
  77. public BytesRefWritable get(int index) {
  78. if (index >= valid) {
  79. throw new IndexOutOfBoundsException(
  80. "This BytesRefArrayWritable only has " + valid + " valid values.");
  81. }
  82. return bytesRefWritables[index];
  83. }
  84. /**
  85. * Gets the BytesRefWritable at the specified position without checking.
  86. *
  87. * @param index
  88. * the position index, starting from zero
  89. * @throws IndexOutOfBoundsException
  90. */
  91. public BytesRefWritable unCheckedGet(int index) {
  92. return bytesRefWritables[index];
  93. }
  94. /**
  95. * Set the BytesRefWritable at the specified position with the specified
  96. * BytesRefWritable.
  97. *
  98. * @param index
  99. * index position
  100. * @param bytesRefWritable
  101. * the new element
  102. * @throws IllegalArgumentException
  103. * if the specified new element is null
  104. */
  105. public void set(int index, BytesRefWritable bytesRefWritable) {
  106. if (bytesRefWritable == null) {
  107. throw new IllegalArgumentException("Can not assign null.");
  108. }
  109. ensureCapacity(index + 1);
  110. bytesRefWritables[index] = bytesRefWritable;
  111. if (valid <= index) {
  112. valid = index + 1;
  113. }
  114. }
  115. /**
  116. * {@inheritDoc}
  117. */
  118. @Override
  119. public int compareTo(BytesRefArrayWritable other) {
  120. if (other == null) {
  121. throw new IllegalArgumentException("Argument can not be null.");
  122. }
  123. if (this == other) {
  124. return 0;
  125. }
  126. int sizeDiff = valid - other.valid;
  127. if (sizeDiff != 0) {
  128. return sizeDiff;
  129. }
  130. for (int i = 0; i < valid; i++) {
  131. if (other.contains(bytesRefWritables[i])) {
  132. continue;
  133. } else {
  134. return 1;
  135. }
  136. }
  137. return 0;
  138. }
  139. /**
  140. * Returns <tt>true</tt> if this instance contains one or more the specified
  141. * BytesRefWritable.
  142. *
  143. * @param bytesRefWritable
  144. * BytesRefWritable element to be tested
  145. * @return <tt>true</tt> if contains the specified element
  146. * @throws IllegalArgumentException
  147. * if the specified element is null
  148. */
  149. public boolean contains(BytesRefWritable bytesRefWritable) {
  150. if (bytesRefWritable == null) {
  151. throw new IllegalArgumentException("Argument can not be null.");
  152. }
  153. for (int i = 0; i < valid; i++) {
  154. if (bytesRefWritables[i].equals(bytesRefWritable)) {
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. /**
  161. * {@inheritDoc}
  162. */
  163. @Override
  164. public boolean equals(Object o) {
  165. if (o == null || !(o instanceof BytesRefArrayWritable)) {
  166. return false;
  167. }
  168. return compareTo((BytesRefArrayWritable) o) == 0;
  169. }
  170. /**
  171. * Removes all elements.
  172. */
  173. public void clear() {
  174. valid = 0;
  175. }
  176. /**
  177. * enlarge the capacity if necessary, to ensure that it can hold the number of
  178. * elements specified by newValidCapacity argument. It will also narrow the
  179. * valid capacity when needed. Notice: it only enlarge or narrow the valid
  180. * capacity with no care of the already stored invalid BytesRefWritable.
  181. *
  182. * @param newValidCapacity
  183. * the desired capacity
  184. */
  185. public void resetValid(int newValidCapacity) {
  186. ensureCapacity(newValidCapacity);
  187. valid = newValidCapacity;
  188. }
  189. protected void ensureCapacity(int newCapacity) {
  190. int size = bytesRefWritables.length;
  191. if (size < newCapacity) {
  192. bytesRefWritables = Arrays.copyOf(bytesRefWritables, newCapacity);
  193. while (size < newCapacity) {
  194. bytesRefWritables[size] = new BytesRefWritable();
  195. size++;
  196. }
  197. }
  198. }
  199. /**
  200. * {@inheritDoc}
  201. */
  202. @Override
  203. public void readFields(DataInput in) throws IOException {
  204. int count = in.readInt();
  205. ensureCapacity(count);
  206. for (int i = 0; i < count; i++) {
  207. bytesRefWritables[i].readFields(in);
  208. }
  209. valid = count;
  210. }
  211. /**
  212. * {@inheritDoc}
  213. */
  214. @Override
  215. public void write(DataOutput out) throws IOException {
  216. out.writeInt(valid);
  217. for (int i = 0; i < valid; i++) {
  218. BytesRefWritable cu = bytesRefWritables[i];
  219. cu.write(out);
  220. }
  221. }
  222. static {
  223. WritableFactories.setFactory(BytesRefArrayWritable.class,
  224. new WritableFactory() {
  225. @Override
  226. public Writable newInstance() {
  227. return new BytesRefArrayWritable();
  228. }
  229. });
  230. }
  231. }