Skip to content

Commit

Permalink
Fixes for Custom CB repo
Browse files Browse the repository at this point in the history
Enable upgrade to latest SDK
  • Loading branch information
gkysaad committed Jul 12, 2021
1 parent 41190cc commit c3b2851
Showing 1 changed file with 13 additions and 9 deletions.
Expand Up @@ -31,14 +31,16 @@ import com.couchbase.client.java.search.queries.DocIdQuery;
import com.couchbase.client.java.search.queries.QueryStringQuery;
import com.couchbase.client.java.search.result.SearchResult;
import com.couchbase.client.java.search.result.SearchRow;
import com.couchbase.client.java.search.SearchOptions;
import <%= packageName %>.repository.search.SearchCouchbaseRepository;
<%_ } _%>
import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
import org.springframework.data.couchbase.repository.query.CouchbaseEntityInformation;
// import org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository;
import <%= packageName %>.repository.N1qlCouchbaseRepository;
// import <%= packageName %>.repository.N1qlCouchbaseRepository;
import org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository;
<%_ if (searchEngineCouchbase) { _%>
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
Expand All @@ -56,7 +58,7 @@ import java.util.stream.Collectors;
/**
* A custom implementation of {@code CouchbaseRepository}.
*/
public class CustomN1qlCouchbaseRepository<T, ID extends Serializable> implements N1qlCouchbaseRepository<T, ID><%if (searchEngineCouchbase) { %> , SearchCouchbaseRepository<T, ID><% } %> {
public class CustomN1qlCouchbaseRepository<T, ID extends Serializable> extends SimpleCouchbaseRepository<T,ID> <%if (searchEngineCouchbase) { %> implements SearchCouchbaseRepository<T, ID><% } %> {
private final CouchbasePersistentEntity<?> persistentEntity;
Expand All @@ -68,7 +70,7 @@ public class CustomN1qlCouchbaseRepository<T, ID extends Serializable> implement
*/
public CustomN1qlCouchbaseRepository(CouchbaseEntityInformation<T, String> metadata, CouchbaseOperations couchbaseOperations) {
super(metadata, couchbaseOperations);
persistentEntity = getCouchbaseOperations().getConverter().getMappingContext().getPersistentEntity(getEntityInformation().getJavaType());
persistentEntity = couchbaseOperations.getConverter().getMappingContext().getPersistentEntity(getEntityInformation().getJavaType());
}
@Override
Expand All @@ -78,18 +80,19 @@ public class CustomN1qlCouchbaseRepository<T, ID extends Serializable> implement
<%_ if (searchEngineCouchbase) { _%>
public Page<T> search(String indexName, String request, Pageable pageable) {
SearchQuery searchQuery = new SearchQuery(indexName, queryString(request))
SearchQuery searchQuery = SearchQuery.queryString(request);
SearchOptions searchOptions = SearchOptions.searchOptions()
.limit(pageable.getPageSize())
.skip((int) pageable.getOffset());
SearchResult result = getCouchbaseOperations().getCouchbaseBucket().query(searchQuery);
SearchResult result = couchbaseOperations.getCouchbaseClientFactory().getCluster().searchQuery(indexName, searchQuery, searchOptions); // change to cluster.searchQuery
List<T> pageContent = extractResults(result);
return new PageImpl<>(pageContent, pageable, result.metrics().totalHits());
return new PageImpl<>(pageContent, pageable, result.metaData().metrics().totalRows());
}
@Override
public List<T> search(String indexName, String request) {
SearchQuery searchQuery = new SearchQuery(indexName, queryString(request));
return extractResults(getCouchbaseOperations().getCouchbaseBucket().query(searchQuery));
SearchQuery searchQuery = SearchQuery.queryString(request);
return extractResults(getCouchbaseOperations().getCouchbaseBucket().searchQuery(indexName, searchQuery));
}
static SearchQuery queryString(String request) {
Expand All @@ -113,7 +116,7 @@ public class CustomN1qlCouchbaseRepository<T, ID extends Serializable> implement
@SuppressWarnings("unchecked")
private List<T> extractResults(SearchResult result) {
return result.hits().stream()
return result.rows().stream()
.map(SearchRow::id)
.map(id -> (ID) id)
.map(this::findById)
Expand All @@ -140,4 +143,5 @@ public class CustomN1qlCouchbaseRepository<T, ID extends Serializable> implement
private <S extends T> void setId(S entity, String generatedId) {
persistentEntity.getPropertyAccessor(entity).setProperty(persistentEntity.getIdProperty(), generatedId);
}

}

0 comments on commit c3b2851

Please sign in to comment.