Fixes for RiakBuilder and templates, update docbook docs.

This commit is contained in:
J. Brisbin
2010-12-28 15:27:56 -06:00
parent 29c601075b
commit afa4c7ada3
7 changed files with 244 additions and 34 deletions

View File

@@ -104,10 +104,6 @@ public abstract class AbstractRiakTemplate extends RestGatewaySupport implements
* Whether or not to use the ETag-based cache.
*/
protected boolean useCache = true;
/**
* {@link java.util.concurrent.ExecutorService} to use for running asynchronous jobs.
*/
protected ExecutorService executorService = Executors.newCachedThreadPool();
/**
* The URI to use inside the RestTemplate.
*/
@@ -124,6 +120,10 @@ public abstract class AbstractRiakTemplate extends RestGatewaySupport implements
* The default QosParameters to use for all operations through this template.
*/
protected QosParameters defaultQosParameters = null;
/**
* {@link java.util.concurrent.ExecutorService} to use for running asynchronous jobs.
*/
protected ExecutorService workerPool = Executors.newCachedThreadPool();
protected Class<?> defaultType = String.class;
protected ClassLoader classLoader = null;
@@ -192,6 +192,15 @@ public abstract class AbstractRiakTemplate extends RestGatewaySupport implements
this.defaultQosParameters = defaultQosParameters;
}
public ExecutorService getWorkerPool() {
return workerPool;
}
public void setWorkerPool(ExecutorService workerPool) {
this.workerPool = workerPool;
}
/**
* Get the default type to use if none can be inferred.
*
@@ -257,14 +266,6 @@ public abstract class AbstractRiakTemplate extends RestGatewaySupport implements
return "/riak";
}
public ExecutorService getExecutorService() {
return executorService;
}
public void setExecutorService(ExecutorService executorService) {
this.executorService = executorService;
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(conversionService,
"Must specify a valid ConversionService.");

View File

@@ -38,16 +38,43 @@ import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* An implementation of {@link AsyncBucketKeyValueStoreOperations} and {@link
* AsyncMapReduceOperations} for the Riak datastore.
* <p/>
* To use the AsyncRiakTemplate, create a singleton in your Spring application-context.xml:
* <pre><code>
* &lt;bean id="riak" class="org.springframework.data.keyvalue.riak.core.AsyncRiakTemplate"
* p:defaultUri="http://localhost:8098/riak/{bucket}/{key}"
* p:mapReduceUri="http://localhost:8098/mapred"/>
* </code></pre>
* To store and retrieve objects in Riak, use the setXXX and getXXX methods (example in
* Groovy):
* <pre><code>
* def callback = [
* completed: { v, meta ->
* ... do something with results ...
* },
* failed: { err ->
* }
* ] as AsyncKeyValueStoreOperation
* def obj = new TestObject(name: "My Name", age: 40)
* def future = riak.set("mybucket", "mykey", obj, callback)
* ... this runs asynchronously, so do other work ...
* def name = future.get().name
* println "Hello $name!"
* </code></pre>
*
* @author J. Brisbin <jon@jbrisbin.com>
*/
public class AsyncRiakTemplate extends AbstractRiakTemplate implements AsyncBucketKeyValueStoreOperations, AsyncMapReduceOperations {
protected final Logger log = LoggerFactory.getLogger(getClass());
protected ExecutorService workerPool = Executors.newCachedThreadPool();
protected AsyncKeyValueStoreOperation<Throwable, Object> defaultErrorHandler = new LoggingErrorHandler();
public AsyncRiakTemplate() {
@@ -58,14 +85,6 @@ public class AsyncRiakTemplate extends AbstractRiakTemplate implements AsyncBuck
super(requestFactory);
}
public ExecutorService getWorkerPool() {
return workerPool;
}
public void setWorkerPool(ExecutorService workerPool) {
this.workerPool = workerPool;
}
public AsyncKeyValueStoreOperation<Throwable, Object> getDefaultErrorHandler() {
return defaultErrorHandler;
}

View File

@@ -129,7 +129,10 @@ public class RiakTemplate extends AbstractRiakTemplate implements BucketKeyValue
KeyValueStoreMetaData origMeta = getMetaData(bucket, keyName);
String vclock = null;
if (null != origMeta) {
vclock = origMeta.getProperties().get(RIAK_VCLOCK).toString();
Object o = origMeta.getProperties().get(RIAK_VCLOCK);
if (null != o) {
vclock = o.toString();
}
}
RestTemplate restTemplate = getRestTemplate();
HttpHeaders headers = new HttpHeaders();
@@ -552,7 +555,7 @@ public class RiakTemplate extends AbstractRiakTemplate implements BucketKeyValue
@SuppressWarnings({"unchecked"})
public <T> Future<List<T>> submit(MapReduceJob job) {
// Run this job asynchronously.
return executorService.submit(job);
return workerPool.submit(job);
}
/*----------------- Link Operations -----------------*/

View File

@@ -37,6 +37,21 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* A Groovy Builder that implements a powerful and syntactically succinct DSL for Riak datastore
* access using SDKV for Riak's {@link AsyncRiakTemplate}.
* <p/>
* The DSL responds to most of the important methods from the <code>AsyncRiakTemplate</code>:
* <ul><li>set</li><li>setAsBytes</li><li>put</li><li>get</li><li>getAsBytes</li>
* <li>getAsType</li><li>containsKey</li><li>delete</li><li>foreach</li></ul>
* <p/>
* An example of DSL usage (to delete all entries in a bucket):
* <pre><code>riak.foreach(bucket: "test") {
* completed { v, meta ->
* delete(bucket: "test", key: meta.key)
* }
* }
* </code></pre>
*
* @author J. Brisbin <jon@jbrisbin.com>
*/
public class RiakBuilder extends BuilderSupport {
@@ -310,7 +325,9 @@ public class RiakBuilder extends BuilderSupport {
@SuppressWarnings({"unchecked"})
@Override
protected void nodeCompleted(Object parent, Object node) {
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;
@@ -339,7 +356,9 @@ public class RiakBuilder extends BuilderSupport {
@SuppressWarnings({"unchecked"})
@Override
protected Object postNodeCompletion(Object parent, Object node) {
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

@@ -84,9 +84,12 @@ public class RiakMapReduceOperation implements Callable {
public Object call() throws Exception {
Future<?> f = riak.execute(job, new AsyncKeyValueStoreOperation<List<?>, Object>() {
public Object completed(KeyValueStoreMetaData meta, List<?> result) {
Object arg = new Object[]{result, meta};
if (null != completed) {
return completed.call(arg);
if (completed.getParameterTypes().length == 2) {
return completed.call(new Object[]{result, meta});
} else {
return completed.call(result);
}
} else {
return new Object[]{result, meta};
}

View File

@@ -264,10 +264,9 @@ class RiakBuilderSpec extends Specification {
given:
def riak = new RiakBuilder(riakTemplate)
def deleted = false
when:
riak {
def deleted = riak {
"test" {
foreach {
completed { v, meta ->