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