Skip to content

Commit 870cbac

Browse files
committed
DATACOUCH-7, DATACOUCH-9 - Implement View support in Template + Repository
This changeset implements basic view support for both the CRUDRepository and the underlying template imeplementation. Note that customization is currently missing, we'll provide a @view annotation that lets you customize behavior.
1 parent 790d8f8 commit 870cbac

File tree

3 files changed

+125
-11
lines changed

3 files changed

+125
-11
lines changed

src/main/java/org/springframework/data/couchbase/core/CouchbaseOperations.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919

2020
import java.util.Collection;
21+
import java.util.List;
2122

23+
import com.couchbase.client.protocol.views.Query;
24+
import com.couchbase.client.protocol.views.ViewResponse;
2225
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
2326

2427
/**
@@ -114,6 +117,42 @@ public interface CouchbaseOperations {
114117
*/
115118
<T> T findById(String id, Class<T> entityClass);
116119

120+
/**
121+
* Query a View for a list of documents of type T.
122+
*
123+
* <p>There is no need to {@link Query#setIncludeDocs(boolean)} explicitely,
124+
* because it will be set to true all the time. It is valid to pass in a
125+
* empty constructed {@link Query} object.</p>
126+
*
127+
* <p>This method does not work with reduced views, because they by design
128+
* do not contain references to original objects. Use the provided
129+
* {@link #queryView} method for more flexibility and direct access.</p>
130+
*
131+
* @param design the name of the design document.
132+
* @param view the name of the view.
133+
* @param query the Query object to customize the view query.
134+
* @param entityClass the entity to map to.
135+
* @return the converted collection
136+
*/
137+
<T> List<T> findByView(String design, String view, Query query, Class<T> entityClass);
138+
139+
140+
/**
141+
* Query a View with direct access to the {@link ViewResponse}.
142+
*
143+
* <p>This method is available to ease the working with views by still wrapping
144+
* exceptions into the Spring infrastructure.</p>
145+
*
146+
* <p>It is especially needed if you want to run reduced view queries, because
147+
* they can't be mapped onto entities directly.</p>
148+
*
149+
* @param design the name of the design document.
150+
* @param view the name of the view.
151+
* @param query the Query object to customize the view query.
152+
* @return
153+
*/
154+
ViewResponse queryView(String design, String view, Query query);
155+
117156
/**
118157
* Checks if the given document exists.
119158
*

src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616

1717
package org.springframework.data.couchbase.core;
1818

19-
import java.util.Collection;
20-
import java.util.Collections;
21-
import java.util.HashSet;
22-
import java.util.Iterator;
23-
import java.util.List;
24-
import java.util.Set;
19+
import java.util.*;
2520

21+
import com.couchbase.client.protocol.views.Query;
22+
import com.couchbase.client.protocol.views.View;
23+
import com.couchbase.client.protocol.views.ViewResponse;
24+
import com.couchbase.client.protocol.views.ViewRow;
2625
import net.spy.memcached.internal.OperationFuture;
2726

2827
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
@@ -158,6 +157,42 @@ public String doInBucket() {
158157
return couchbaseConverter.read(entityClass, converted);
159158
}
160159

160+
161+
@Override
162+
public <T> List<T> findByView(final String designName, final String viewName,
163+
final Query query, final Class<T> entityClass) {
164+
165+
if (!query.willIncludeDocs()) {
166+
query.setIncludeDocs(true);
167+
}
168+
if (query.willReduce()) {
169+
query.setReduce(false);
170+
}
171+
172+
ViewResponse response = queryView(designName, viewName, query);
173+
174+
List<T> result = new ArrayList<T>(response.size());
175+
for (ViewRow row : response) {
176+
ConvertedCouchbaseDocument converted =
177+
new ConvertedCouchbaseDocument(row.getId(), (String) row.getDocument());
178+
result.add(couchbaseConverter.read(entityClass, converted));
179+
}
180+
181+
return result;
182+
}
183+
184+
@Override
185+
public ViewResponse queryView(final String designName, final String viewName,
186+
final Query query) {
187+
return execute(new BucketCallback<ViewResponse>() {
188+
@Override
189+
public ViewResponse doInBucket() {
190+
View view = client.getView(designName, viewName);
191+
return client.query(view, query);
192+
}
193+
});
194+
}
195+
161196
public void remove(final Object objectToRemove) {
162197
ensureNotIterable(objectToRemove);
163198

src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@
1616

1717
package org.springframework.data.couchbase.repository.support;
1818

19+
import com.couchbase.client.protocol.views.ComplexKey;
20+
import com.couchbase.client.protocol.views.Query;
21+
import com.couchbase.client.protocol.views.ViewResponse;
22+
import com.couchbase.client.protocol.views.ViewRow;
1923
import org.springframework.data.couchbase.core.CouchbaseOperations;
2024
import org.springframework.data.couchbase.repository.CouchbaseRepository;
2125
import org.springframework.data.couchbase.repository.query.CouchbaseEntityInformation;
26+
import org.springframework.data.domain.Page;
27+
import org.springframework.data.domain.Pageable;
28+
import org.springframework.data.domain.Sort;
2229
import org.springframework.util.Assert;
2330

2431
import java.io.Serializable;
@@ -98,22 +105,55 @@ public void delete(Iterable<? extends T> entities) {
98105

99106
@Override
100107
public Iterable<T> findAll() {
101-
throw new UnsupportedOperationException("findAll is not supported in the current version!");
108+
String design = entityInformation.getJavaType().getSimpleName().toLowerCase();
109+
String view = "all";
110+
111+
return couchbaseOperations.findByView(design, view, new Query().setReduce(false),
112+
entityInformation.getJavaType());
102113
}
103114

104115
@Override
105-
public Iterable<T> findAll(Iterable<ID> ids) {
106-
throw new UnsupportedOperationException("findAll is not supported in the current version!");
116+
public Iterable<T> findAll(final Iterable<ID> ids) {
117+
String design = entityInformation.getJavaType().getSimpleName().toLowerCase();
118+
String view = "all";
119+
120+
Query query = new Query();
121+
query.setReduce(false);
122+
query.setKeys(ComplexKey.of(ids));
123+
124+
return couchbaseOperations.findByView(design, view, query, entityInformation.getJavaType());
107125
}
108126

109127
@Override
110128
public long count() {
111-
throw new UnsupportedOperationException("count is not supported in the current version!");
129+
String design = entityInformation.getJavaType().getSimpleName().toLowerCase();
130+
String view = "all";
131+
132+
Query query = new Query();
133+
query.setReduce(true);
134+
135+
ViewResponse response = couchbaseOperations.queryView(design, view, query);
136+
137+
long count = 0;
138+
for (ViewRow row : response) {
139+
count += Long.parseLong(row.getValue());
140+
}
141+
142+
return count;
112143
}
113144

114145
@Override
115146
public void deleteAll() {
116-
throw new UnsupportedOperationException("deleteAll is not supported in the current version!");
147+
String design = entityInformation.getJavaType().getSimpleName().toLowerCase();
148+
String view = "all";
149+
150+
Query query = new Query();
151+
query.setReduce(false);
152+
153+
ViewResponse response = couchbaseOperations.queryView(design, view, query);
154+
for (ViewRow row : response) {
155+
couchbaseOperations.remove(row.getId());
156+
}
117157
}
118158

119159
protected CouchbaseOperations getCouchbaseOperations() {

0 commit comments

Comments
 (0)