You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-issues@hadoop.apache.org by "David Parks (JIRA)" <ji...@apache.org> on 2013/01/24 10:35:13 UTC

[jira] [Updated] (HADOOP-9240) Making ArrayWritable typed

     [ https://issues.apache.org/jira/browse/HADOOP-9240?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

David Parks updated HADOOP-9240:
--------------------------------

    Affects Version/s: 1.0.3
    
> Making ArrayWritable typed
> --------------------------
>
>                 Key: HADOOP-9240
>                 URL: https://issues.apache.org/jira/browse/HADOOP-9240
>             Project: Hadoop Common
>          Issue Type: Improvement
>          Components: io
>    Affects Versions: 1.0.3
>            Reporter: David Parks
>            Priority: Minor
>
> ArrayWritable is just painful to use in practice, it would be nice if we had a typed version of ArrayWritable that had all the features of an ArrayList.
> It wasn't hard to write, and it doesn't cost more in terms of storage or CPU than an ArrayWritable. 
> So I wonder why not include a more usable ArrayListWritable class with Hadoop? Code pasted below, unless there's a reason that this is a bad idea I'm happy to provide the unit test for it as well.
> @SuppressWarnings("serial")
> public class ArrayListWritable<E extends Writable> extends ArrayList<E> implements Writable {
> 	Class<E> type = null;
> 	
> 	public ArrayListWritable(Class<E> type){
> 		super();
> 		this.type = type;
> 	}
> 	
> 	public ArrayListWritable(){
> 		super();
> 	}
> 	
> 	public void setArrayClassType(Class<E> clazz){
> 		this.type = clazz;
> 	}
> 	
> 	@Override
> 	public void write(DataOutput out) throws IOException {
> 		if(type==null){ throw new IOException("Cannot write an " + getClass().getName() + " without the class type being set in the constructor or with setArrayClassType(...)"); };
> 		out.writeUTF(type.getCanonicalName());
> 		out.writeInt(size());
> 		for(E writable : this) writable.write(out);
> 	}
> 	
> 	@SuppressWarnings("unchecked")
> 	@Override
> 	public void readFields(DataInput in) throws IOException {
> 		clear();
> 		
> 		//Read the class name
> 		try {
> 			type = (Class<E>)Class.forName(in.readUTF());
> 		} catch (ClassNotFoundException e) {
> 			throw new IOException("Invalid connonical name read from input bytes", e);
> 		}
> 		//Read the size & set capacity of the arraylist
> 		int size = in.readInt();
> 		ensureCapacity(size);
> 		
> 		//Read in individual writables
> 		for (int i = 0; i < size; i++) {
> 			Writable value = WritableFactories.newInstance(type);
> 			value.readFields(in);                       // read a value
> 			add((E)value);								// add it to the ArrayList
> 		}
> 	}
> }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira