Author: cutting Date: Tue Sep 18 11:25:56 2007 New Revision: 577010 URL: http://svn.apache.org/viewvc?rev=577010&view=rev Log: HADOOP-120. In ArrayWritable, prevent creation with null value class and improve documentation. Contributed by Cameron Pope.
Added: lucene/hadoop/trunk/src/test/org/apache/hadoop/io/TestArrayWritable.java Modified: lucene/hadoop/trunk/CHANGES.txt lucene/hadoop/trunk/src/java/org/apache/hadoop/io/ArrayWritable.java Modified: lucene/hadoop/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/CHANGES.txt?rev=577010&r1=577009&r2=577010&view=diff ============================================================================== --- lucene/hadoop/trunk/CHANGES.txt (original) +++ lucene/hadoop/trunk/CHANGES.txt Tue Sep 18 11:25:56 2007 @@ -249,6 +249,9 @@ HADOOP-1878. Add space between priority links on job details page. (Thomas Friol via cutting) + HADOOP-120. In ArrayWritable, prevent creation with null value + class, and improve documentation. (Cameron Pope via cutting) + Release 0.14.1 - 2007-09-04 Modified: lucene/hadoop/trunk/src/java/org/apache/hadoop/io/ArrayWritable.java URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/java/org/apache/hadoop/io/ArrayWritable.java?rev=577010&r1=577009&r2=577010&view=diff ============================================================================== --- lucene/hadoop/trunk/src/java/org/apache/hadoop/io/ArrayWritable.java (original) +++ lucene/hadoop/trunk/src/java/org/apache/hadoop/io/ArrayWritable.java Tue Sep 18 11:25:56 2007 @@ -21,16 +21,29 @@ import java.io.*; import java.lang.reflect.Array; -/** A Writable for arrays containing instances of a class. */ +/** + * A Writable for arrays containing instances of a class. The elements of this + * writable must all be instances of the same class. If this writable will be + * the input for a Reducer, you will need to create a subclass that sets the + * value to be of the proper type. + * + * For example: + * <code> + * public class IntArrayWritable extends ArrayWritable { + * public IntArrayWritable() { + * super(IntWritable.class); + * } + * } + * </code> + */ public class ArrayWritable implements Writable { private Class valueClass; private Writable[] values; - public ArrayWritable() { - this.valueClass = null; - } - public ArrayWritable(Class valueClass) { + if (valueClass == null) { + throw new IllegalArgumentException("null valueClass"); + } this.valueClass = valueClass; } @@ -46,13 +59,6 @@ } } - public void setValueClass(Class valueClass) { - if (valueClass != this.valueClass) { - this.valueClass = valueClass; - this.values = null; - } - } - public Class getValueClass() { return valueClass; } Added: lucene/hadoop/trunk/src/test/org/apache/hadoop/io/TestArrayWritable.java URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/test/org/apache/hadoop/io/TestArrayWritable.java?rev=577010&view=auto ============================================================================== --- lucene/hadoop/trunk/src/test/org/apache/hadoop/io/TestArrayWritable.java (added) +++ lucene/hadoop/trunk/src/test/org/apache/hadoop/io/TestArrayWritable.java Tue Sep 18 11:25:56 2007 @@ -0,0 +1,64 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.io; + +import java.io.*; + +import junit.framework.TestCase; + +/** Unit tests for ArrayWritable */ +public class TestArrayWritable extends TestCase { + + static class TextArrayWritable extends ArrayWritable { + public TextArrayWritable() { + super(Text.class); + } + } + + public TestArrayWritable(String name) { + super(name); + } + + /** + * If valueClass is undefined, readFields should throw an exception indicating + * that the field is null. Otherwise, readFields should succeed. + */ + public void testThrowUndefinedValueException() throws IOException { + // Get a buffer containing a simple text array + Text[] elements = {new Text("zero"), new Text("one"), new Text("two")}; + TextArrayWritable sourceArray = new TextArrayWritable(); + sourceArray.set(elements); + + // Write it to a normal output buffer + DataOutputBuffer out = new DataOutputBuffer(); + DataInputBuffer in = new DataInputBuffer(); + sourceArray.write(out); + + // Read the output buffer with TextReadable. Since the valueClass is defined, + // this should succeed + TextArrayWritable destArray = new TextArrayWritable(); + in.reset(out.getData(), out.getLength()); + destArray.readFields(in); + Writable[] destElements = destArray.get(); + assertTrue(destElements.length == elements.length); + for (int i = 0; i < elements.length; i++) { + assertEquals(destElements[i],elements[i]); + } + } +}