Upgrading dependencies and more tests for templates.
This commit is contained in:
@@ -22,10 +22,15 @@
|
||||
|
||||
package com.couchbase.spring.core;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface CouchbaseOperations {
|
||||
|
||||
/**
|
||||
* Insert the object into the connected bucket.
|
||||
* Save the given object.
|
||||
*
|
||||
* When the document already exists (specified by its unique id),
|
||||
* then it will be overriden. Otherwise it will be created.
|
||||
*
|
||||
* <p>
|
||||
* The object is converted to a JSON representation using an instance of
|
||||
@@ -34,5 +39,78 @@ public interface CouchbaseOperations {
|
||||
*
|
||||
* @param objectToSave the object to store in the bucket.
|
||||
*/
|
||||
void save(Object objectToSave);
|
||||
|
||||
/**
|
||||
* Save a list of objects.
|
||||
*
|
||||
* When one of the documents already exists (specified by its unique id),
|
||||
* then it will be overriden. Otherwise it will be created.
|
||||
*
|
||||
* @param batchToSave the list of objects to store in the bucket.
|
||||
*/
|
||||
void save(Collection<? extends Object> batchToSave);
|
||||
|
||||
/**
|
||||
* Insert the given object.
|
||||
*
|
||||
* When the document already exists (specified by its unique id),
|
||||
* then it will not be overriden. Use the {@link CouchbaseOperations#save}
|
||||
* method for this.
|
||||
*
|
||||
* <p>
|
||||
* The object is converted to a JSON representation using an instance of
|
||||
* {@link CouchbaseConverter}.
|
||||
* </p>
|
||||
*
|
||||
* @param objectToSave the object to add to the bucket.
|
||||
*/
|
||||
void insert(Object objectToSave);
|
||||
|
||||
/**
|
||||
* Insert a list of objects.
|
||||
*
|
||||
* When one of the documents already exists (specified by its unique id),
|
||||
* then it will not be overriden. Use the {@link CouchbaseOperations#save}
|
||||
* method for this.
|
||||
*
|
||||
* @param batchToSave the list of objects to add to the bucket.
|
||||
*/
|
||||
void insert(Collection<? extends Object> batchToSave);
|
||||
|
||||
/**
|
||||
* Update the given object.
|
||||
*
|
||||
* When the document does not exists (specified by its unique id),
|
||||
* then it will not be created. Use the {@link CouchbaseOperations#save}
|
||||
* method for this.
|
||||
*
|
||||
* <p>
|
||||
* The object is converted to a JSON representation using an instance of
|
||||
* {@link CouchbaseConverter}.
|
||||
* </p>
|
||||
*
|
||||
* @param objectToSave the object to add to the bucket.
|
||||
*/
|
||||
void update(Object objectToSave);
|
||||
|
||||
/**
|
||||
* Insert a list of objects.
|
||||
*
|
||||
* When one of the documents does not exists (specified by its unique id),
|
||||
* then it will not be created. Use the {@link CouchbaseOperations#save}
|
||||
* method for this.
|
||||
*
|
||||
* @param batchToSave the list of objects to add to the bucket.
|
||||
*/
|
||||
void update(Collection<? extends Object> batchToSave);
|
||||
|
||||
/**
|
||||
* Find an object by its given Id and map it to the corresponding entity.
|
||||
*
|
||||
* @param id the unique ID of the document.
|
||||
* @param entityClass the entity to map to.
|
||||
* @return returns the found object or null otherwise.
|
||||
*/
|
||||
public <T> T findById(String id, Class<T> entityClass);
|
||||
}
|
||||
|
||||
@@ -22,15 +22,37 @@
|
||||
|
||||
package com.couchbase.spring.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.spring.core.convert.CouchbaseConverter;
|
||||
import com.couchbase.spring.core.convert.MappingCouchbaseConverter;
|
||||
import com.couchbase.spring.core.mapping.ConvertedCouchbaseDocument;
|
||||
import com.couchbase.spring.core.mapping.CouchbasePersistentEntity;
|
||||
import com.couchbase.spring.core.mapping.CouchbasePersistentProperty;
|
||||
|
||||
public class CouchbaseTemplate implements CouchbaseOperations {
|
||||
|
||||
private CouchbaseClient client;
|
||||
private CouchbaseConverter couchbaseConverter;
|
||||
protected final MappingContext<? extends CouchbasePersistentEntity<?>,
|
||||
CouchbasePersistentProperty> mappingContext;
|
||||
private static final Collection<String> ITERABLE_CLASSES;
|
||||
|
||||
static {
|
||||
Set<String> iterableClasses = new HashSet<String>();
|
||||
iterableClasses.add(List.class.getName());
|
||||
iterableClasses.add(Collection.class.getName());
|
||||
iterableClasses.add(Iterator.class.getName());
|
||||
ITERABLE_CLASSES = Collections.unmodifiableCollection(iterableClasses);
|
||||
}
|
||||
|
||||
public CouchbaseTemplate(CouchbaseClient client) {
|
||||
this(client, null);
|
||||
@@ -39,6 +61,7 @@ public class CouchbaseTemplate implements CouchbaseOperations {
|
||||
public CouchbaseTemplate(CouchbaseClient client, CouchbaseConverter converter) {
|
||||
this.client = client;
|
||||
this.couchbaseConverter = converter == null ? getDefaultConverter(client) : converter;
|
||||
this.mappingContext = this.couchbaseConverter.getMappingContext();
|
||||
}
|
||||
|
||||
private CouchbaseConverter getDefaultConverter(CouchbaseClient client) {
|
||||
@@ -48,12 +71,73 @@ public class CouchbaseTemplate implements CouchbaseOperations {
|
||||
return converter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insert(Object objectToSave) {
|
||||
ensureNotIterable(objectToSave);
|
||||
|
||||
ConvertedCouchbaseDocument converted = new ConvertedCouchbaseDocument();
|
||||
couchbaseConverter.write(objectToSave, converted);
|
||||
client.add(converted.getId(), converted.getExpiry(), converted.getValue());
|
||||
}
|
||||
|
||||
public void insert(Collection<? extends Object> batchToSave) {
|
||||
Iterator<? extends Object> iter = batchToSave.iterator();
|
||||
while(iter.hasNext()) {
|
||||
insert(iter.next());
|
||||
}
|
||||
}
|
||||
|
||||
public void save(Object objectToSave) {
|
||||
ensureNotIterable(objectToSave);
|
||||
|
||||
ConvertedCouchbaseDocument converted = new ConvertedCouchbaseDocument();
|
||||
couchbaseConverter.write(objectToSave, converted);
|
||||
client.set(converted.getId(), converted.getExpiry(), converted.getValue());
|
||||
}
|
||||
|
||||
public void save(Collection<? extends Object> batchToSave) {
|
||||
Iterator<? extends Object> iter = batchToSave.iterator();
|
||||
while(iter.hasNext()) {
|
||||
save(iter.next());
|
||||
}
|
||||
}
|
||||
|
||||
public void update(Object objectToSave) {
|
||||
ensureNotIterable(objectToSave);
|
||||
|
||||
ConvertedCouchbaseDocument converted = new ConvertedCouchbaseDocument();
|
||||
couchbaseConverter.write(objectToSave, converted);
|
||||
client.replace(converted.getId(), converted.getExpiry(), converted.getValue());
|
||||
}
|
||||
|
||||
public void update(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);
|
||||
if(result == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ConvertedCouchbaseDocument converted = new ConvertedCouchbaseDocument(id, result);
|
||||
return couchbaseConverter.read(entityClass, converted);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure the given object is not a iterable.
|
||||
*
|
||||
* @param o the object to verify.
|
||||
*/
|
||||
protected void ensureNotIterable(Object o) {
|
||||
if (null != o) {
|
||||
if (o.getClass().isArray() || ITERABLE_CLASSES.contains(o.getClass().getName())) {
|
||||
throw new IllegalArgumentException("Cannot use a collection here.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import java.io.OutputStream;
|
||||
import org.codehaus.jackson.JsonEncoding;
|
||||
import org.codehaus.jackson.JsonFactory;
|
||||
import org.codehaus.jackson.JsonGenerator;
|
||||
import org.codehaus.jackson.JsonParser;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
@@ -64,8 +65,14 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> R read(Class<R> type, ConvertedCouchbaseDocument s) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
public <R> R read(Class<R> type, ConvertedCouchbaseDocument doc) {
|
||||
JsonFactory jsonFactory = new JsonFactory();
|
||||
try {
|
||||
JsonParser parser = jsonFactory.createJsonParser(doc.getValue());
|
||||
} catch(Exception e) {
|
||||
throw new MappingException("Could not read from JSON while converting "
|
||||
+ doc.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user