Skip to content

Instantly share code, notes, and snippets.

@jbrisbin
Created November 12, 2010 14:09
Show Gist options
  • Save jbrisbin/674126 to your computer and use it in GitHub Desktop.
Save jbrisbin/674126 to your computer and use it in GitHub Desktop.
Bucket and Key extraction from a single, composite-key-like object
Object bucket = null;
Object key = null;
if (obj instanceof Map) {
Map m = (Map) obj;
bucket = m.get("bucket");
key = m.get("key");
} else {
// Override from Annotation?
KeyValueStoreMetaData meta = obj.getClass().getAnnotation(KeyValueStoreMetaData.class);
if (null != meta && null != meta.family()) {
bucket = meta.family();
}
String s = obj.toString();
if (s.contains("@")) {
// This is likely the result of Object.toString()
// which returns com.mypackage.MyObject@memaddr
// Convert it using the conversion service if that's the case
s = conversionService.convert(obj, String.class);
}
if (s.contains(":")) {
String[] a = s.split(":");
if (null == bucket) {
bucket = a[0];
}
key = a[1];
} else {
key = s;
}
if (null == bucket) {
// Default to the class name for the bucket
bucket = (obj.getClass() == byte[].class ? "bytes" : obj.getClass().getName());
}
}
return new String[]{(null != bucket ? bucket.toString() : null), (null != key ? key.toString() : null)};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment