DATACOUCH-172 - Fix PagingAndSorting consistency

Default consistency from template wasn't used in PagingAndSorting findAll methods.
This commit is contained in:
Simon Baslé
2015-11-13 15:45:02 +01:00
parent 40d131aab0
commit 95ea97244a
2 changed files with 15 additions and 8 deletions

View File

@@ -16,8 +16,7 @@
package org.springframework.data.couchbase.repository;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
@@ -69,6 +68,7 @@ public class N1qlCouchbaseRepositoryTests {
assertTrue(party.getAttendees() <= previousAttendance);
previousAttendance = party.getAttendees();
}
assertFalse("Expected to find several parties", previousAttendance == Long.MAX_VALUE);
}
@Test
@@ -81,6 +81,7 @@ public class N1qlCouchbaseRepositoryTests {
}
previousDesc = party.getDescription();
}
assertNotNull("Expected to find several parties", previousDesc);
}
@Test
@@ -88,7 +89,7 @@ public class N1qlCouchbaseRepositoryTests {
Pageable pageable = new PageRequest(0, 8);
Page<Party> page1 = repository.findAll(pageable);
assertEquals(14, page1.getTotalElements()); //13 generated parties + 1 specifically crafted party
assertEquals(13, page1.getTotalElements()); //12 generated parties + 1 specifically crafted party
assertEquals(8, page1.getNumberOfElements());
}
}

View File

@@ -19,8 +19,11 @@ package org.springframework.data.couchbase.repository.support;
import java.io.Serializable;
import java.util.List;
import com.couchbase.client.java.query.N1qlParams;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.SimpleN1qlQuery;
import com.couchbase.client.java.query.Statement;
import com.couchbase.client.java.query.consistency.ScanConsistency;
import com.couchbase.client.java.query.dsl.Expression;
import com.couchbase.client.java.query.dsl.path.WherePath;
@@ -68,21 +71,24 @@ public class N1qlCouchbaseRepository<T, ID extends Serializable>
Statement st = selectFrom.where(whereCriteria).orderBy(orderings);
//fire the query
N1qlQuery query = N1qlQuery.simple(st);
ScanConsistency consistency = getCouchbaseOperations().getDefaultConsistency().n1qlConsistency();
N1qlQuery query = N1qlQuery.simple(st, N1qlParams.build().consistency(consistency));
return getCouchbaseOperations().findByN1QL(query, getEntityInformation().getJavaType());
}
@Override
public Page<T> findAll(Pageable pageable) {
Assert.notNull(pageable);
ScanConsistency consistency = getCouchbaseOperations().getDefaultConsistency().n1qlConsistency();
//prepare the count total query
Statement countStatement = N1qlUtils.createCountQueryForEntity(getCouchbaseOperations().getCouchbaseBucket().name(),
getCouchbaseOperations().getConverter(), getEntityInformation());
SimpleN1qlQuery countQuery = N1qlQuery.simple(countStatement, N1qlParams.build().consistency(consistency));
//TODO how to avoid to do that more than once?
//fire the count query and get total count
List<CountFragment> countResult = getCouchbaseOperations().findByN1QLProjection(N1qlQuery.simple(countStatement), CountFragment.class);
//TODO how to avoid to do that more than once?
//fire the count query and get total count
List < CountFragment > countResult = getCouchbaseOperations().findByN1QLProjection(countQuery, CountFragment.class);
long totalCount = countResult == null || countResult.isEmpty() ? 0 : countResult.get(0).count;
//prepare elements of the data query
@@ -94,7 +100,7 @@ public class N1qlCouchbaseRepository<T, ID extends Serializable>
Statement pageStatement = selectFrom.where(whereCriteria).limit(pageable.getPageSize()).offset(pageable.getOffset());
//fire the query
N1qlQuery query = N1qlQuery.simple(pageStatement);
N1qlQuery query = N1qlQuery.simple(pageStatement, N1qlParams.build().consistency(consistency));
List<T> pageContent = getCouchbaseOperations().findByN1QL(query, getEntityInformation().getJavaType());
//return the list as a Page