Skip to content

Commit

Permalink
DATACOUCH-246 - Polishing.
Browse files Browse the repository at this point in the history
Removed the dynamic return type obtained from the ResultProcessor held in a query implementation field as this breaks thread safety. We now hand the type to read into the execution methods.

Adapted test cases accordingly and removed a bit of mocking.

Original pull request: #122.
  • Loading branch information
odrotbohm committed Dec 2, 2016
1 parent eb55f2e commit ce5d921
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 120 deletions.
Expand Up @@ -57,7 +57,6 @@ public abstract class AbstractN1qlBasedQuery implements RepositoryQuery {

protected final CouchbaseQueryMethod queryMethod;
private final CouchbaseOperations couchbaseOperations;
private Class<?> returnedType;

protected AbstractN1qlBasedQuery(CouchbaseQueryMethod queryMethod, CouchbaseOperations couchbaseOperations) {
this.queryMethod = queryMethod;
Expand Down Expand Up @@ -87,11 +86,9 @@ public Object execute(Object[] parameters) {

ResultProcessor processor = this.queryMethod.getResultProcessor().withDynamicProjection(accessor);
ReturnedType returnedType = processor.getReturnedType();
this.returnedType = returnedType.getReturnedType();

if (this.returnedType.isInterface()) {
this.returnedType = returnedType.getDomainType();
}

Class<?> typeToRead = returnedType.getTypeToRead();
typeToRead = typeToRead == null ? returnedType.getDomainType() : typeToRead;

Statement statement = getStatement(accessor, parameters, returnedType);
JsonValue queryPlaceholderValues = getPlaceholderValues(accessor);
Expand All @@ -104,8 +101,7 @@ public Object execute(Object[] parameters) {
Statement countStatement = getCount(accessor, parameters);
N1qlQuery countQuery = buildQuery(countStatement, queryPlaceholderValues,
getCouchbaseOperations().getDefaultConsistency().n1qlConsistency());
return processor.processResult(executeDependingOnType(query, countQuery, queryMethod, accessor.getPageable(),
queryMethod.isPageQuery(), queryMethod.isSliceQuery(), queryMethod.isModifyingQuery()));
return processor.processResult(executeDependingOnType(query, countQuery, queryMethod, accessor.getPageable(), typeToRead));
}

protected static N1qlQuery buildQuery(Statement statement, JsonValue queryPlaceholderValues, ScanConsistency scanConsistency) {
Expand All @@ -122,22 +118,23 @@ protected static N1qlQuery buildQuery(Statement statement, JsonValue queryPlaceh
return query;
}

protected Object executeDependingOnType(N1qlQuery query, N1qlQuery countQuery, QueryMethod queryMethod, Pageable pageable,
boolean isPage, boolean isSlice, boolean isModifying) {
if (isModifying) {
protected Object executeDependingOnType(N1qlQuery query, N1qlQuery countQuery, QueryMethod queryMethod,
Pageable pageable, Class<?> typeToRead) {

if (queryMethod.isModifyingQuery()) {
throw new UnsupportedOperationException("Modifying queries not yet supported");
}

if (isPage) {
return executePaged(query, countQuery, pageable);
} else if (isSlice) {
return executeSliced(query, countQuery, pageable);
if (queryMethod.isPageQuery()) {
return executePaged(query, countQuery, pageable, typeToRead);
} else if (queryMethod.isSliceQuery()) {
return executeSliced(query, countQuery, pageable, typeToRead);
} else if (queryMethod.isCollectionQuery()) {
return executeCollection(query);
} else if (queryMethod.isQueryForEntity()) {
return executeEntity(query);
return executeCollection(query, typeToRead);
} else if (queryMethod.isStreamQuery()){
return executeStream(query);
return executeStream(query, typeToRead);
} else if (queryMethod.isQueryForEntity()) {
return executeEntity(query, typeToRead);
} else if (queryMethod.getReturnedObjectType().isPrimitive()
&& useGeneratedCountQuery()) {
//attempt to execute the created COUNT query
Expand All @@ -156,24 +153,24 @@ private void logIfNecessary(N1qlQuery query) {
}
}

protected List<?> executeCollection(N1qlQuery query) {
protected List<?> executeCollection(N1qlQuery query, Class<?> typeToRead) {
logIfNecessary(query);
List<?> result = couchbaseOperations.findByN1QL(query, this.returnedType);
List<?> result = couchbaseOperations.findByN1QL(query, typeToRead);
return result;
}

protected Object executeEntity(N1qlQuery query) {
protected Object executeEntity(N1qlQuery query, Class<?> typeToRead) {
logIfNecessary(query);
List<?> result = executeCollection(query);
List<?> result = executeCollection(query, typeToRead);
return result.isEmpty() ? null : result.get(0);
}

protected Object executeStream(N1qlQuery query) {
protected Object executeStream(N1qlQuery query, Class<?> typeToRead) {
logIfNecessary(query);
return StreamUtils.createStreamFromIterator(executeCollection(query).iterator());
return StreamUtils.createStreamFromIterator(executeCollection(query, typeToRead).iterator());
}

protected Object executePaged(N1qlQuery query, N1qlQuery countQuery, Pageable pageable) {
protected Object executePaged(N1qlQuery query, N1qlQuery countQuery, Pageable pageable, Class<?> typeToRead) {
Assert.notNull(pageable);
long total = 0L;
logIfNecessary(countQuery);
Expand All @@ -183,14 +180,14 @@ protected Object executePaged(N1qlQuery query, N1qlQuery countQuery, Pageable pa
}

logIfNecessary(query);
List<?> result = couchbaseOperations.findByN1QL(query, this.returnedType);
List<?> result = couchbaseOperations.findByN1QL(query, typeToRead);
return new PageImpl(result, pageable, total);
}

protected Object executeSliced(N1qlQuery query, N1qlQuery countQuery, Pageable pageable) {
protected Object executeSliced(N1qlQuery query, N1qlQuery countQuery, Pageable pageable, Class<?> typeToRead) {
Assert.notNull(pageable);
logIfNecessary(query);
List<?> result = couchbaseOperations.findByN1QL(query, this.returnedType);
List<?> result = couchbaseOperations.findByN1QL(query, typeToRead);
int pageSize = pageable.getPageSize();
boolean hasNext = result.size() > pageSize;

Expand Down

0 comments on commit ce5d921

Please sign in to comment.