From af3cdeaf6a09eaf2ff8358e19fadf45f73a4ee0e Mon Sep 17 00:00:00 2001 From: "J. Brisbin" Date: Wed, 29 Dec 2010 14:28:48 -0600 Subject: [PATCH] Switched to using enums rather than String.equals() on method names. --- .../keyvalue/riak/core/AsyncRiakTemplate.java | 4 +- .../keyvalue/riak/groovy/RiakBuilder.java | 388 ++++++++++-------- .../keyvalue/riak/core/RiakBuilderSpec.groovy | 14 +- 3 files changed, 227 insertions(+), 179 deletions(-) diff --git a/spring-data-riak/src/main/java/org/springframework/data/keyvalue/riak/core/AsyncRiakTemplate.java b/spring-data-riak/src/main/java/org/springframework/data/keyvalue/riak/core/AsyncRiakTemplate.java index f8f1f8fe5..1f2e41d9d 100644 --- a/spring-data-riak/src/main/java/org/springframework/data/keyvalue/riak/core/AsyncRiakTemplate.java +++ b/spring-data-riak/src/main/java/org/springframework/data/keyvalue/riak/core/AsyncRiakTemplate.java @@ -220,11 +220,11 @@ public class AsyncRiakTemplate extends AbstractRiakTemplate implements AsyncBuck @SuppressWarnings({"unchecked"}) public Future getWithMetaData(B bucket, K key, Class requiredType, AsyncKeyValueStoreOperation 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) getType(bucketName, key.toString()); } diff --git a/spring-data-riak/src/main/java/org/springframework/data/keyvalue/riak/groovy/RiakBuilder.java b/spring-data-riak/src/main/java/org/springframework/data/keyvalue/riak/groovy/RiakBuilder.java index 5dcf2181a..ddee1f617 100644 --- a/spring-data-riak/src/main/java/org/springframework/data/keyvalue/riak/groovy/RiakBuilder.java +++ b/spring-data-riak/src/main/java/org/springframework/data/keyvalue/riak/groovy/RiakBuilder.java @@ -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 op = new RiakOperation(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 op = new RiakOperation(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 keys = new ArrayList(); - 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 keys = new ArrayList(); + 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(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 qosParams = (Map) 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(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 qosParams = (Map) 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 op = (RiakOperation) getCurrent(); - Object[] args = (Object[]) arg; - Map params; - Closure handler = null; - Closure guard = null; - for (Object o : args) { - if (o instanceof Map) { - params = (Map) o; - if (params.containsKey("when")) { - guard = (Closure) params.get("when"); + if (null != nodeName) { + switch (nodeName) { + case COMPLETED: + case FAILED: + if (getCurrent() instanceof RiakOperation) { + RiakOperation op = (RiakOperation) getCurrent(); + Object[] args = (Object[]) arg; + Map params; + Closure handler = null; + Closure guard = null; + for (Object o : args) { + if (o instanceof Map) { + params = (Map) 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 op = (RiakOperation) node; try { diff --git a/spring-data-riak/src/test/groovy/org/springframework/data/keyvalue/riak/core/RiakBuilderSpec.groovy b/spring-data-riak/src/test/groovy/org/springframework/data/keyvalue/riak/core/RiakBuilderSpec.groovy index 08e882c53..692d49e73 100644 --- a/spring-data-riak/src/test/groovy/org/springframework/data/keyvalue/riak/core/RiakBuilderSpec.groovy +++ b/spring-data-riak/src/test/groovy/org/springframework/data/keyvalue/riak/core/RiakBuilderSpec.groovy @@ -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() } + } } }