DATACOUCH-168 - Support and document all reduce functions
Detection whether or not reduce should be activated is now centralized. Triggers are: - using the count prefix instead of find in method name - setting reduce = true in the View annotation explicitly. The reduce can thus be returning any kind of value (added an integration example with _stats), which is now properly documented.
This commit is contained in:
@@ -56,9 +56,11 @@ public class CouchbaseRepositoryViewListener extends DependencyInjectionTestExec
|
||||
private void createAndWaitForDesignDocs(final Bucket client) {
|
||||
String mapFunction = "function (doc, meta) { if(doc._class == \"org.springframework.data.couchbase.repository.User\") { emit(null, null); } }";
|
||||
String mapFunctionName = "function (doc, meta) { if(doc._class == \"org.springframework.data.couchbase.repository.User\") { emit(doc.username, null); } }";
|
||||
String mapFunctionAge = "function (doc, meta) { if(doc._class == \"org.springframework.data.couchbase.repository.User\") { emit(doc.age, doc.age); } }";
|
||||
View view = DefaultView.create("customFindAllView", mapFunction, "_count");
|
||||
View customFindByNameView = DefaultView.create("customFindByNameView", mapFunctionName, "_count");
|
||||
List<View> views = Arrays.asList(view, customFindByNameView);
|
||||
View customFindByAgeStatsView = DefaultView.create("customFindByAgeStatsView", mapFunctionAge, "_stats");
|
||||
List<View> views = Arrays.asList(view, customFindByNameView, customFindByAgeStatsView);
|
||||
DesignDocument designDoc = DesignDocument.create("user", views);
|
||||
client.bucketManager().upsertDesignDocument(designDoc);
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.document.json.JsonObject;
|
||||
import com.couchbase.client.java.view.Stale;
|
||||
import com.couchbase.client.java.view.ViewQuery;
|
||||
import com.couchbase.client.java.view.ViewResult;
|
||||
@@ -111,6 +112,17 @@ public class CouchbaseRepositoryViewTests {
|
||||
assertEquals(12, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeriveViewParametersAndReduceNonNumerical() {
|
||||
JsonObject reduceResult = repository.findByAgeLessThan(50);
|
||||
|
||||
assertNotNull(reduceResult);
|
||||
assertEquals(51, (long) reduceResult.getLong("count"));
|
||||
assertEquals(50, (long) reduceResult.getLong("max"));
|
||||
assertEquals(0, (long) reduceResult.getLong("min"));
|
||||
assertEquals(1275, (long) reduceResult.getLong("sum"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeriveViewParameters() {
|
||||
String lowKey = "uname-1";
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.data.couchbase.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.couchbase.client.java.document.json.JsonObject;
|
||||
|
||||
import org.springframework.data.couchbase.core.view.View;
|
||||
|
||||
/**
|
||||
@@ -72,4 +74,7 @@ public interface CustomUserRepository extends CouchbaseRepository<User, String>
|
||||
|
||||
@View
|
||||
long countCustomFindInvalid();
|
||||
|
||||
@View(viewName = "customFindByAgeStatsView", reduce = true)
|
||||
JsonObject findByAgeLessThan(int maxAge);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user