Skip to content

DATACOUCH-28. #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ target/
.classpath
.project
.settings/*

.idea
*.iml
out
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface CouchbaseOperations {
* @param objectToSave the object to store in the bucket.
*/
void save(Object objectToSave);

/**
* Save a list of objects.
*
Expand All @@ -49,8 +49,8 @@ public interface CouchbaseOperations {
*
* @param batchToSave the list of objects to store in the bucket.
*/
void save(Collection<? extends Object> batchToSave);
void save(Collection<? extends Object> batchToSave);

/**
* Insert the given object.
*
Expand All @@ -60,7 +60,7 @@ public interface CouchbaseOperations {
* @param objectToSave the object to add to the bucket.
*/
void insert(Object objectToSave);

/**
* Insert a list of objects.
*
Expand All @@ -80,7 +80,7 @@ public interface CouchbaseOperations {
* @param objectToSave the object to add to the bucket.
*/
void update(Object objectToSave);

/**
* Insert a list of objects.
*
Expand All @@ -90,7 +90,7 @@ public interface CouchbaseOperations {
* @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.
*
Expand All @@ -103,8 +103,9 @@ public interface CouchbaseOperations {
/**
* Query a View for a list of documents of type T.
*
* <p>There is no need to {@link Query#setIncludeDocs(boolean)} explicitely, because it will be set to true all the
* time. It is valid to pass in a empty constructed {@link Query} object.</p>
* <p>{@link Query#setIncludeDocs(boolean)} defaults to false. If you require the query to include the entire
* document, you will need to set it to true before calling this method. It is valid to pass in a empty
* constructed {@link Query} object.</p>
*
* <p>This method does not work with reduced views, because they by design do not contain references to original
* objects. Use the provided {@link #queryView} method for more flexibility and direct access.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ public class CouchbaseTemplate implements CouchbaseOperations {
protected final MappingContext<? extends CouchbasePersistentEntity<?>,
CouchbasePersistentProperty> mappingContext;
private static final Collection<String> ITERABLE_CLASSES;
private final CouchbaseExceptionTranslator exceptionTranslator =
new CouchbaseExceptionTranslator();
private final CouchbaseExceptionTranslator exceptionTranslator =
new CouchbaseExceptionTranslator();
private final TranslationService<Object> translationService;
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);
}

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(final CouchbaseClient client) {
this(client, null);
Expand Down Expand Up @@ -84,7 +84,7 @@ private CouchbaseStorable translateDecode(final String source, final CouchbaseSt
}

public final void insert(final Object objectToSave) {
ensureNotIterable(objectToSave);
ensureNotIterable(objectToSave);

final CouchbaseDocument converted = new CouchbaseDocument();
couchbaseConverter.write(objectToSave, converted);
Expand All @@ -96,16 +96,16 @@ public Boolean doInBucket() throws InterruptedException, ExecutionException {
}
});
}

public final void insert(final Collection<? extends Object> batchToSave) {
Iterator<? extends Object> iter = batchToSave.iterator();
while (iter.hasNext()) {
insert(iter.next());
}
Iterator<? extends Object> iter = batchToSave.iterator();
while (iter.hasNext()) {
insert(iter.next());
}
}

public void save(final Object objectToSave) {
ensureNotIterable(objectToSave);
ensureNotIterable(objectToSave);

final CouchbaseDocument converted = new CouchbaseDocument();
couchbaseConverter.write(objectToSave, converted);
Expand All @@ -117,7 +117,7 @@ public Boolean doInBucket() throws InterruptedException, ExecutionException {
}
});
}

public void save(final Collection<? extends Object> batchToSave) {
Iterator<? extends Object> iter = batchToSave.iterator();
while (iter.hasNext()) {
Expand All @@ -126,7 +126,7 @@ public void save(final Collection<? extends Object> batchToSave) {
}

public void update(final Object objectToSave) {
ensureNotIterable(objectToSave);
ensureNotIterable(objectToSave);

final CouchbaseDocument converted = new CouchbaseDocument();
couchbaseConverter.write(objectToSave, converted);
Expand All @@ -139,14 +139,14 @@ public Boolean doInBucket() throws InterruptedException, ExecutionException {
});

}

public void update(final Collection<? extends Object> batchToSave) {
Iterator<? extends Object> iter = batchToSave.iterator();
while (iter.hasNext()) {
save(iter.next());
}
Iterator<? extends Object> iter = batchToSave.iterator();
while (iter.hasNext()) {
save(iter.next());
}
}

public final <T> T findById(final String id, final Class<T> entityClass) {
String result = execute(new BucketCallback<String>() {
@Override
Expand All @@ -155,22 +155,23 @@ public String doInBucket() {
}
});

if (result == null) {
return null;
}
if (result == null) {
return null;
}

CouchbaseDocument converted = new CouchbaseDocument(id);
return couchbaseConverter.read(entityClass, (CouchbaseDocument) translateDecode(result, converted));
return couchbaseConverter.read(entityClass, (CouchbaseDocument) translateDecode(result, converted));
}


@Override
public <T> List<T> findByView(final String designName, final String viewName,
final Query query, final Class<T> entityClass) {

if (!query.willIncludeDocs()) {
query.setIncludeDocs(true);
if(query.willIncludeDocs()) {
query.setIncludeDocs(true);
}

if (query.willReduce()) {
query.setReduce(false);
}
Expand Down Expand Up @@ -261,18 +262,18 @@ public String doInBucket() {
*
* @param o the object to verify.
*/
protected final 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.");
}
}
}

private RuntimeException potentiallyConvertRuntimeException(final RuntimeException ex) {
RuntimeException resolved = exceptionTranslator.translateExceptionIfPossible(ex);
return resolved == null ? ex : resolved;
}
protected final 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.");
}
}
}

private RuntimeException potentiallyConvertRuntimeException(final RuntimeException ex) {
RuntimeException resolved = exceptionTranslator.translateExceptionIfPossible(ex);
return resolved == null ? ex : resolved;
}

@Override
public CouchbaseConverter getConverter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Iterable<T> findAll() {
String design = entityInformation.getJavaType().getSimpleName().toLowerCase();
String view = "all";

return couchbaseOperations.findByView(design, view, new Query().setReduce(false),
return couchbaseOperations.findByView(design, view, new Query().setReduce(false).setIncludeDocs(true),
entityInformation.getJavaType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public void saveSimpleEntityCorrectly() throws Exception {

@Test
public void saveDocumentWithExpiry() throws Exception {
String id = "simple-doc-with-expiry";
DocumentWithExpiry doc = new DocumentWithExpiry(id);
template.save(doc);
assertNotNull(client.get(id));
Thread.sleep(3000);
assertNull(client.get(id));
String id = "simple-doc-with-expiry";
DocumentWithExpiry doc = new DocumentWithExpiry(id);
template.save(doc);
assertNotNull(client.get(id));
Thread.sleep(3000);
assertNull(client.get(id));
}

@Test
Expand All @@ -96,10 +96,10 @@ public void insertDoesNotOverride() {

@Test
public void updateDoesNotInsert() {
String id ="update-does-not-insert";
SimplePerson doc = new SimplePerson(id, "Nice Guy");
template.update(doc);
assertNull(client.get(id));
String id ="update-does-not-insert";
SimplePerson doc = new SimplePerson(id, "Nice Guy");
template.update(doc);
assertNull(client.get(id));
}


Expand Down Expand Up @@ -168,6 +168,7 @@ public void validFindById() {
@Test
public void shouldLoadAndMapViewDocs() {
Query query = new Query();
query.setIncludeDocs(true);
query.setStale(Stale.FALSE);

final List<Beer> beers = template.findByView("test_beers", "by_name", query, Beer.class);
Expand All @@ -179,7 +180,7 @@ public void shouldLoadAndMapViewDocs() {
assertNotNull(beer.getActive());
}
}

/**
* A sample document with just an id and property.
*/
Expand All @@ -191,21 +192,21 @@ static class SimplePerson {
private final String name;

public SimplePerson(String id, String name) {
this.id = id;
this.name = name;
}
this.id = id;
this.name = name;
}
}

/**
* A sample document that expires in 2 seconds.
*/
@Document(expiry=2)
static class DocumentWithExpiry {
@Id
private final String id;

public DocumentWithExpiry(String id) {
this.id = id;
this.id = id;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void simpleCrud() {
@Test
public void shouldFindAll() {
// do a non-stale query to populate data for testing.
client.query(client.getView("user", "all"), new Query().setStale(Stale.FALSE));
client.query(client.getView("user", "all"), new Query().setIncludeDocs(true).setStale(Stale.FALSE));

Iterable<User> allUsers = repository.findAll();
int size = 0;
Expand Down