Switched to using enums rather than String.equals() on method names.

This commit is contained in:
J. Brisbin
2010-12-29 14:28:48 -06:00
parent efc566036a
commit af3cdeaf6a
3 changed files with 227 additions and 179 deletions

View File

@@ -220,11 +220,11 @@ public class AsyncRiakTemplate extends AbstractRiakTemplate implements AsyncBuck
@SuppressWarnings({"unchecked"})
public <B, K, T, R> Future<?> getWithMetaData(B bucket, K key, Class<T> requiredType,
AsyncKeyValueStoreOperation<T, R> callback) {
String bucketName = (null != bucket ? bucket.toString() : requiredType.getName());
// Get a key name that may or may not include the QOS parameters.
Assert.notNull(key, "Cannot use a null key.");
Assert.notNull(callback, "Callback cannot be null");
String bucketName = (null != bucket ? bucket.toString() : requiredType.getName());
if (null == requiredType) {
requiredType = (Class<T>) getType(bucketName, key.toString());
}

View File

@@ -56,6 +56,10 @@ import java.util.concurrent.Executors;
*/
public class RiakBuilder extends BuilderSupport {
private static enum NodeName {
CALL, FOREACH, MAPREDUCE, QUERY, MAP, REDUCE, INPUTS, LANGUAGE, SOURCE, KEEP, ARG, COMPLETED, FAILED
}
protected final Logger log = LoggerFactory.getLogger(getClass());
@Autowired(required = false)
protected AsyncRiakTemplate riak;
@@ -105,30 +109,42 @@ public class RiakBuilder extends BuilderSupport {
@Override
protected void setParent(Object parent, Object child) {
log.debug("setParent/2 " + parent + " " + child);
// log.debug("setParent/2 " + parent + " " + child);
}
@SuppressWarnings({"unchecked"})
@Override
protected Object createNode(Object name) {
log.debug("createNode/1 " + name);
if ("call".equals(name)) {
// log.debug("createNode/1 " + name);
NodeName nodeName = null;
try {
nodeName = NodeName.valueOf(name.toString().toUpperCase());
} catch (IllegalArgumentException e) {
// IGNORED
} else if ("foreach".equals(name)) {
RiakOperation<Object> op = new RiakOperation<Object>(riak, RiakOperation.Type.FOREACH);
op.setBucket(defaultBucketName);
return op;
} else if ("mapreduce".equals(name)) {
return createMapReduceJob();
} else if ("query".equals(name)) {
QueryPhase p = new QueryPhase();
p.job = ((RiakMapReduceOperation) getCurrent()).getJob();
return getCurrent();
} else if ("map".equals(name) || "reduce".equals(name)) {
QueryPhase p = new QueryPhase();
p.job = ((RiakMapReduceOperation) getCurrent()).getJob();
p.phase = name.toString();
return p;
}
if (null != nodeName) {
QueryPhase p;
switch (nodeName) {
case CALL:
break;
case FOREACH:
RiakOperation<Object> op = new RiakOperation<Object>(riak,
RiakOperation.Type.FOREACH);
op.setBucket(defaultBucketName);
return op;
case MAPREDUCE:
return createMapReduceJob();
case QUERY:
p = new QueryPhase();
p.job = ((RiakMapReduceOperation) getCurrent()).getJob();
return getCurrent();
case REDUCE:
case MAP:
p = new QueryPhase();
p.job = ((RiakMapReduceOperation) getCurrent()).getJob();
p.phase = name.toString();
return p;
}
} else {
defaultBucketName = name.toString();
}
@@ -139,187 +155,217 @@ public class RiakBuilder extends BuilderSupport {
@SuppressWarnings({"unchecked"})
@Override
protected Object createNode(Object name, Object value) {
log.debug("createNode/2 " + name + " " + value);
if ("inputs".equals(name)) {
AsyncRiakMapReduceJob job = ((RiakMapReduceOperation) getCurrent()).getJob();
if (null != value && value instanceof String) {
List<String> keys = new ArrayList<String>();
keys.add(value.toString());
job.addInputs(keys);
} else if (value instanceof List) {
job.addInputs((List) value);
}
return job;
} else if ("language".equals(name)) {
QueryPhase p = (QueryPhase) getCurrent();
p.language = value.toString();
return p;
} else if ("source".equals(name)) {
QueryPhase p = (QueryPhase) getCurrent();
p.source = value.toString();
return p;
} else if ("keep".equals(name)) {
QueryPhase p = (QueryPhase) getCurrent();
p.keep = (value instanceof Boolean ? (Boolean) value : new Boolean(value.toString()));
return p;
} else if ("arg".equals(name)) {
QueryPhase p = (QueryPhase) getCurrent();
p.arg = value;
return p;
// log.debug("createNode/2 " + name + " " + value);
NodeName nodeName = null;
try {
nodeName = NodeName.valueOf(name.toString().toUpperCase());
} catch (IllegalArgumentException e) {
// IGNORED
}
return null; //To change body of implemented methods use File | Settings | File Templates.
if (null != nodeName) {
QueryPhase p;
switch (nodeName) {
case INPUTS:
AsyncRiakMapReduceJob job = ((RiakMapReduceOperation) getCurrent()).getJob();
if (null != value && value instanceof String) {
List<String> keys = new ArrayList<String>();
keys.add(value.toString());
job.addInputs(keys);
} else if (value instanceof List) {
job.addInputs((List) value);
}
return job;
case LANGUAGE:
p = (QueryPhase) getCurrent();
p.language = value.toString();
return p;
case SOURCE:
p = (QueryPhase) getCurrent();
p.source = value.toString();
return p;
case KEEP:
p = (QueryPhase) getCurrent();
p.keep = (value instanceof Boolean ? (Boolean) value : new Boolean(value.toString()));
return p;
case ARG:
p = (QueryPhase) getCurrent();
p.arg = value;
return p;
}
}
return null;
}
@SuppressWarnings({"unchecked"})
@Override
protected Object createNode(Object name, Map attributes) {
log.debug("createNode/2 (Map) " + name + " " + attributes);
// log.debug("createNode/2 (Map) " + name + " " + attributes);
NodeName nodeName = null;
try {
nodeName = NodeName.valueOf(name.toString().toUpperCase());
} catch (IllegalArgumentException e) {
// IGNORED
}
if (null != nodeName) {
switch (nodeName) {
case MAPREDUCE:
RiakMapReduceOperation oper = createMapReduceJob();
// Set timeout
Object o = attributes.get("wait");
if (null != o) {
if (o instanceof Long) {
oper.setTimeout((Long) o);
} else if (o instanceof String) {
oper.setTimeout(new Long(o.toString()));
} else if (o instanceof Integer) {
oper.setTimeout(new Long((Integer) o));
} else {
throw new IllegalArgumentException(
"Timeout should be an Integer, a Long, or a String denoting milliseconds");
}
}
return oper;
case MAP:
case REDUCE:
QueryPhase p = new QueryPhase();
p.job = ((RiakMapReduceOperation) getCurrent()).getJob();
p.phase = name.toString();
// Set arg
p.arg = attributes.get("arg");
return p;
}
}
if ("mapreduce".equals(name)) {
RiakMapReduceOperation oper = createMapReduceJob();
RiakOperation.Type type = null;
try {
type = RiakOperation.Type.valueOf(name.toString().toUpperCase());
} catch (IllegalArgumentException ignored) {
// IGNORED
}
if (null != type) {
RiakOperation op = new RiakOperation<Object>(riak, type);
// Set a bucket name
Object o = attributes.get("bucket");
if (null == o && null != defaultBucketName) {
op.setBucket(defaultBucketName);
} else {
op.setBucket((null != o ? o.toString() : null));
}
// Set the object's key
o = attributes.get("key");
op.setKey((null != o ? o.toString() : null));
// Set the value
o = attributes.get("value");
op.setValue(o);
// Set the type of object (for getAsType)
o = attributes.get("type");
if (null != o) {
if (o instanceof Class) {
op.setRequiredType((Class<?>) o);
} else if (o instanceof String) {
try {
op.setRequiredType(Class.forName((String) o));
} catch (ClassNotFoundException e) {
throw new DataStoreOperationException(e.getMessage(), e);
}
} else {
op.setRequiredType(o.getClass());
}
}
// Set QOS parameters
o = attributes.get("qos");
if (null != o) {
RiakQosParameters qos = new RiakQosParameters();
Map<String, Object> qosParams = (Map<String, Object>) o;
if (qosParams.containsKey("dw")) {
qos.setDurableWriteThreshold(qosParams.get("dw"));
}
if (qosParams.containsKey("w")) {
qos.setWriteThreshold(qosParams.get("w"));
}
if (qosParams.containsKey("r")) {
qos.setReadThreshold(qosParams.get("r"));
}
op.setQosParameters(qos);
}
// Set timeout
Object o = attributes.get("wait");
o = attributes.get("wait");
if (null != o) {
if (o instanceof Long) {
oper.setTimeout((Long) o);
op.setTimeout((Long) o);
} else if (o instanceof String) {
oper.setTimeout(new Long(o.toString()));
op.setTimeout(new Long(o.toString()));
} else if (o instanceof Integer) {
oper.setTimeout(new Long((Integer) o));
op.setTimeout(new Long((Integer) o));
} else {
throw new IllegalArgumentException(
"Timeout should be an Integer, a Long, or a String denoting milliseconds");
}
}
return oper;
} else if ("map".equals(name) || "reduce".equals(name)) {
QueryPhase p = new QueryPhase();
p.job = ((RiakMapReduceOperation) getCurrent()).getJob();
p.phase = name.toString();
// Set arg
p.arg = attributes.get("arg");
return p;
} else {
RiakOperation.Type type = RiakOperation.Type.valueOf(name.toString().toUpperCase());
if (null != type) {
RiakOperation op = new RiakOperation<Object>(riak, type);
// Set a bucket name
Object o = attributes.get("bucket");
if (null == o && null != defaultBucketName) {
op.setBucket(defaultBucketName);
} else {
op.setBucket((null != o ? o.toString() : null));
}
// Set the object's key
o = attributes.get("key");
op.setKey((null != o ? o.toString() : null));
// Set the value
o = attributes.get("value");
op.setValue(o);
// Set the type of object (for getAsType)
o = attributes.get("type");
if (null != o) {
if (o instanceof Class) {
op.setRequiredType((Class<?>) o);
} else if (o instanceof String) {
try {
op.setRequiredType(Class.forName((String) o));
} catch (ClassNotFoundException e) {
throw new DataStoreOperationException(e.getMessage(), e);
}
} else {
op.setRequiredType(o.getClass());
}
}
// Set QOS parameters
o = attributes.get("qos");
if (null != o) {
RiakQosParameters qos = new RiakQosParameters();
Map<String, Object> qosParams = (Map<String, Object>) o;
if (qosParams.containsKey("dw")) {
qos.setDurableWriteThreshold(qosParams.get("dw"));
}
if (qosParams.containsKey("w")) {
qos.setWriteThreshold(qosParams.get("w"));
}
if (qosParams.containsKey("r")) {
qos.setReadThreshold(qosParams.get("r"));
}
op.setQosParameters(qos);
}
// Set timeout
o = attributes.get("wait");
if (null != o) {
if (o instanceof Long) {
op.setTimeout((Long) o);
} else if (o instanceof String) {
op.setTimeout(new Long(o.toString()));
} else if (o instanceof Integer) {
op.setTimeout(new Long((Integer) o));
} else {
throw new IllegalArgumentException(
"Timeout should be an Integer, a Long, or a String denoting milliseconds");
}
}
return op;
}
return op;
}
return null;
}
@Override
protected Object createNode(Object name, Map attributes, Object value) {
log.debug("createNode/3");
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public Object invokeMethod(String methodName) {
log.debug("invokeMethod/1 " + methodName);
return super.invokeMethod(
methodName); //To change body of overridden methods use File | Settings | File Templates.
// log.debug("createNode/3");
return null;
}
@SuppressWarnings({"unchecked"})
@Override
public Object invokeMethod(String methodName, Object arg) {
if (log.isDebugEnabled()) {
log.debug("invokeMethod/2: " + methodName + " " + arg);
// if (log.isDebugEnabled()) {
// log.debug("invokeMethod/2: " + methodName + " " + arg);
// }
NodeName nodeName = null;
try {
nodeName = NodeName.valueOf(methodName.toString().toUpperCase());
} catch (IllegalArgumentException e) {
// IGNORED
}
if ("completed".equals(methodName) || "failed".equals(methodName)) {
if (getCurrent() instanceof RiakOperation) {
RiakOperation<Object> op = (RiakOperation<Object>) getCurrent();
Object[] args = (Object[]) arg;
Map<String, Object> params;
Closure handler = null;
Closure guard = null;
for (Object o : args) {
if (o instanceof Map) {
params = (Map<String, Object>) o;
if (params.containsKey("when")) {
guard = (Closure) params.get("when");
if (null != nodeName) {
switch (nodeName) {
case COMPLETED:
case FAILED:
if (getCurrent() instanceof RiakOperation) {
RiakOperation<Object> op = (RiakOperation<Object>) getCurrent();
Object[] args = (Object[]) arg;
Map<String, Object> params;
Closure handler = null;
Closure guard = null;
for (Object o : args) {
if (o instanceof Map) {
params = (Map<String, Object>) o;
if (params.containsKey("when")) {
guard = (Closure) params.get("when");
}
} else if (o instanceof Closure) {
handler = (Closure) o;
}
}
} else if (o instanceof Closure) {
handler = (Closure) o;
op.addHandler(methodName, handler, guard);
return op;
} else if (getCurrent() instanceof RiakMapReduceOperation) {
RiakMapReduceOperation oper = (RiakMapReduceOperation) getCurrent();
Object[] args = (Object[]) arg;
if ("completed".equals(methodName)) {
oper.setCompleted((Closure) args[0]);
} else if ("failed".equals(methodName)) {
oper.setFailed((Closure) args[0]);
}
return oper;
}
}
op.addHandler(methodName, handler, guard);
return op;
} else if (getCurrent() instanceof RiakMapReduceOperation) {
RiakMapReduceOperation oper = (RiakMapReduceOperation) getCurrent();
Object[] args = (Object[]) arg;
if ("completed".equals(methodName)) {
oper.setCompleted((Closure) args[0]);
} else if ("failed".equals(methodName)) {
oper.setFailed((Closure) args[0]);
}
return oper;
case CALL:
results.clear();
defaultBucketName = null;
}
} else if ("call".equals(methodName)) {
results.clear();
defaultBucketName = null;
}
// By default
return super.invokeMethod(methodName, arg);
}
@@ -327,9 +373,9 @@ public class RiakBuilder extends BuilderSupport {
@SuppressWarnings({"unchecked"})
@Override
protected void nodeCompleted(Object parent, Object node) {
if (log.isDebugEnabled()) {
log.debug("nodeCompleted: parent=" + parent + ", node=" + node);
}
// if (log.isDebugEnabled()) {
// log.debug("nodeCompleted: parent=" + parent + ", node=" + node);
// }
if (parent instanceof RiakMapReduceOperation && node instanceof QueryPhase) {
QueryPhase p = (QueryPhase) node;
MapReduceOperation oper = null;
@@ -358,9 +404,9 @@ public class RiakBuilder extends BuilderSupport {
@SuppressWarnings({"unchecked"})
@Override
protected Object postNodeCompletion(Object parent, Object node) {
if (log.isDebugEnabled()) {
log.debug("postNodeCompletion: " + parent + " " + node);
}
// if (log.isDebugEnabled()) {
// log.debug("postNodeCompletion: " + parent + " " + node);
// }
if (node instanceof RiakOperation) {
RiakOperation<Object> op = (RiakOperation<Object>) node;
try {

View File

@@ -190,13 +190,15 @@ class RiakBuilderSpec extends Specification {
when:
riak {
put(bucket: "test", value: [test: "value 1"])
put(bucket: "test", value: [test: "value 2"])
put(bucket: "test", value: [test: "value 3"])
test {
put(value: [test: "value 1"])
put(value: [test: "value 2"])
put(value: [test: "value 3"])
foreach(bucket: "test") {
completed { v, meta -> ids << meta.key }
failed { it.printStackTrace() }
foreach {
completed { v, meta -> ids << meta.key }
failed { it.printStackTrace() }
}
}
}