wrapping client.* calls into execute methods.

This commit is contained in:
Michael Nitschinger
2013-04-08 15:43:16 +02:00
parent 67566d99ed
commit a97908e30b
4 changed files with 72 additions and 32 deletions

View File

@@ -1,6 +1,6 @@
package com.couchbase.spring.core;
public interface DbCallback<T> {
public interface BucketCallback<T> {
T doInBucket();
}

View File

@@ -32,6 +32,8 @@ import com.couchbase.client.ObservedModifiedException;
import com.couchbase.client.ObservedTimeoutException;
import com.couchbase.client.vbucket.ConnectionException;
import java.io.IOException;
/**
* Simple {@link PersistenceExceptionTranslator} for Couchbase.
*
@@ -51,12 +53,12 @@ public class CouchbaseExceptionTranslator implements PersistenceExceptionTransla
@Override
public final DataAccessException translateExceptionIfPossible(RuntimeException ex) {
if (ex instanceof ConnectionException) {
return new DataAccessResourceFailureException(ex.getMessage(), ex);
return new DataAccessResourceFailureException(ex.getMessage(), ex);
}
if (ex instanceof ObservedException ||
ex instanceof ObservedTimeoutException ||
ex instanceof ObservedModifiedException) {
if (ex instanceof ObservedException
|| ex instanceof ObservedTimeoutException
|| ex instanceof ObservedModifiedException) {
return new DataIntegrityViolationException(ex.getMessage(), ex);
}

View File

@@ -129,7 +129,7 @@ public interface CouchbaseOperations {
void remove(Collection<? extends Object> batchToRemove);
/**
* Executes a DbCallback translating any exceptions as necessary.
* Executes a BucketCallback translating any exceptions as necessary.
*
* Allows for returning a result object, that is a domain object or a
* collection of domain objects.
@@ -138,5 +138,5 @@ public interface CouchbaseOperations {
* @param <T> the return type.
* @return
*/
<T> T execute(DbCallback<T> action);
<T> T execute(BucketCallback<T> action);
}

View File

@@ -29,6 +29,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Set;
import net.spy.memcached.internal.OperationFuture;
import org.springframework.data.mapping.context.MappingContext;
import com.couchbase.client.CouchbaseClient;
@@ -56,70 +57,101 @@ public class CouchbaseTemplate implements CouchbaseOperations {
ITERABLE_CLASSES = Collections.unmodifiableCollection(iterableClasses);
}
public CouchbaseTemplate(CouchbaseClient client) {
public CouchbaseTemplate(final CouchbaseClient client) {
this(client, null);
}
public CouchbaseTemplate(CouchbaseClient client, CouchbaseConverter converter) {
public CouchbaseTemplate(final CouchbaseClient client,
final CouchbaseConverter converter) {
this.client = client;
this.couchbaseConverter = converter == null ? getDefaultConverter(client) : converter;
this.mappingContext = this.couchbaseConverter.getMappingContext();
}
private CouchbaseConverter getDefaultConverter(CouchbaseClient client) {
private CouchbaseConverter getDefaultConverter(final CouchbaseClient client) {
MappingCouchbaseConverter converter = new MappingCouchbaseConverter(
new CouchbaseMappingContext());
converter.afterPropertiesSet();
return converter;
}
public void insert(Object objectToSave) {
public final void insert(final Object objectToSave) {
ensureNotIterable(objectToSave);
ConvertedCouchbaseDocument converted = new ConvertedCouchbaseDocument();
final ConvertedCouchbaseDocument converted =
new ConvertedCouchbaseDocument();
couchbaseConverter.write(objectToSave, converted);
client.add(converted.getId(), converted.getExpiry(), converted.getRawValue());
execute(new BucketCallback<OperationFuture<Boolean>>() {
@Override
public OperationFuture<Boolean> doInBucket() {
return client.add(
converted.getId(), converted.getExpiry(), converted.getRawValue());
}
});
}
public void insert(Collection<? extends Object> batchToSave) {
public final void insert(final Collection<? extends Object> batchToSave) {
Iterator<? extends Object> iter = batchToSave.iterator();
while(iter.hasNext()) {
insert(iter.next());
}
}
public void save(Object objectToSave) {
public void save(final Object objectToSave) {
ensureNotIterable(objectToSave);
ConvertedCouchbaseDocument converted = new ConvertedCouchbaseDocument();
final ConvertedCouchbaseDocument converted =
new ConvertedCouchbaseDocument();
couchbaseConverter.write(objectToSave, converted);
client.set(converted.getId(), converted.getExpiry(), converted.getRawValue());
execute(new BucketCallback<OperationFuture<Boolean>>() {
@Override
public OperationFuture<Boolean> doInBucket() {
return client.set(
converted.getId(), converted.getExpiry(), converted.getRawValue());
}
});
}
public void save(Collection<? extends Object> batchToSave) {
public void save(final Collection<? extends Object> batchToSave) {
Iterator<? extends Object> iter = batchToSave.iterator();
while(iter.hasNext()) {
while (iter.hasNext()) {
save(iter.next());
}
}
public void update(Object objectToSave) {
public void update(final Object objectToSave) {
ensureNotIterable(objectToSave);
ConvertedCouchbaseDocument converted = new ConvertedCouchbaseDocument();
final ConvertedCouchbaseDocument converted =
new ConvertedCouchbaseDocument();
couchbaseConverter.write(objectToSave, converted);
client.replace(converted.getId(), converted.getExpiry(), converted.getRawValue());
execute(new BucketCallback<OperationFuture<Boolean>>() {
@Override
public OperationFuture<Boolean> doInBucket() {
return client.replace(
converted.getId(), converted.getExpiry(), converted.getRawValue());
}
});
}
public void update(Collection<? extends Object> batchToSave) {
public void update(final Collection<? extends Object> batchToSave) {
Iterator<? extends Object> iter = batchToSave.iterator();
while (iter.hasNext()) {
save(iter.next());
}
}
public <T> T findById(String id, Class<T> entityClass) {
String result = (String) client.get(id);
public final <T> T findById(final String id, final Class<T> entityClass) {
String result = execute(new BucketCallback<String>() {
@Override
public String doInBucket() {
return (String) client.get(id);
}
});
if (result == null) {
return null;
}
@@ -128,22 +160,28 @@ public class CouchbaseTemplate implements CouchbaseOperations {
return couchbaseConverter.read(entityClass, converted);
}
public void remove(Object objectToRemove) {
public void remove(final Object objectToRemove) {
ensureNotIterable(objectToRemove);
ConvertedCouchbaseDocument converted = new ConvertedCouchbaseDocument();
final ConvertedCouchbaseDocument converted = new ConvertedCouchbaseDocument();
couchbaseConverter.write(objectToRemove, converted);
client.delete(converted.getId());
execute(new BucketCallback<OperationFuture<Boolean>>() {
@Override
public OperationFuture<Boolean> doInBucket() {
return client.delete(converted.getId());
}
});
}
public void remove(Collection<? extends Object> batchToRemove) {
public void remove(final Collection<? extends Object> batchToRemove) {
Iterator<? extends Object> iter = batchToRemove.iterator();
while (iter.hasNext()) {
remove(iter.next());
}
}
public <T> T execute(final DbCallback<T> action) {
public <T> T execute(final BucketCallback<T> action) {
try {
return action.doInBucket();
} catch (RuntimeException e) {
@@ -163,8 +201,8 @@ public class CouchbaseTemplate implements CouchbaseOperations {
}
}
}
private RuntimeException potentiallyConvertRuntimeException(RuntimeException ex) {
private RuntimeException potentiallyConvertRuntimeException(final RuntimeException ex) {
RuntimeException resolved = this.exceptionTranslator.translateExceptionIfPossible(ex);
return resolved == null ? ex : resolved;
}