Skip to content

Instantly share code, notes, and snippets.

@Stivo
Created June 14, 2012 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stivo/2930414 to your computer and use it in GitHub Desktop.
Save Stivo/2930414 to your computer and use it in GitHub Desktop.
Performance fixes for crunch's join
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
// No static typeinfo on Tuples
public void readFields(DataInput in) throws IOException {
int card = WritableUtils.readVInt(in);
if (values == null || values.length != card) {
values = new Writable[card];
}
written = WritableUtils.readVLong(in);
Class<? extends Writable>[] cls = new Class[card];
try {
for (int i = 0; i < card; ++i) {
if (has(i)) {
cls[i] = lookupClass(Text.readString(in));
}
}
for (int i = 0; i < card; ++i) {
if (has(i)) {
if (!cls[i].isInstance(values[i])) {
values[i] = cls[i].newInstance();
}
values[i].readFields(in);
} else {
values[i] = null;
}
}
} catch (ClassNotFoundException e) {
throw (IOException) new IOException("Failed tuple init").initCause(e);
} catch (IllegalAccessException e) {
throw (IOException) new IOException("Failed tuple init").initCause(e);
} catch (InstantiationException e) {
throw (IOException) new IOException("Failed tuple init").initCause(e);
}
}
private Map<String, Class<? extends Writable>> writableClassCache = new HashMap<String, Class<? extends Writable>>();
private Class<? extends Writable> lookupClass(String readString) throws ClassNotFoundException {
if (!writableClassCache.containsKey(readString)) {
writableClassCache.put(readString,
Class.forName(readString).asSubclass(Writable.class));
}
return writableClassCache.get(readString);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment