|
|
|
|
@@ -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;
|
|
|
|
|
}
|
|
|
|
|
|