Add project(fields) to findByQuery similar to same method on findById. (#1212)

Closes #1208, #1213.
This commit is contained in:
Michael Reiche
2021-09-12 17:17:47 -07:00
committed by GitHub
parent b3c2f005ed
commit 4bd82ea23c
17 changed files with 369 additions and 111 deletions

View File

@@ -27,10 +27,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import com.couchbase.client.core.error.CouchbaseException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.dao.DataIntegrityViolationException;
@@ -38,18 +41,23 @@ import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.couchbase.core.ExecutableRemoveByIdOperation.ExecutableRemoveById;
import org.springframework.data.couchbase.core.ExecutableReplaceByIdOperation.ExecutableReplaceById;
import org.springframework.data.couchbase.core.support.OneAndAllEntity;
import org.springframework.data.couchbase.domain.Address;
import org.springframework.data.couchbase.domain.Course;
import org.springframework.data.couchbase.domain.NaiveAuditorAware;
import org.springframework.data.couchbase.domain.PersonValue;
import org.springframework.data.couchbase.domain.Submission;
import org.springframework.data.couchbase.domain.User;
import org.springframework.data.couchbase.domain.UserAnnotated;
import org.springframework.data.couchbase.domain.UserAnnotated2;
import org.springframework.data.couchbase.domain.UserAnnotated3;
import org.springframework.data.couchbase.domain.UserSubmission;
import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.IgnoreWhen;
import org.springframework.data.couchbase.util.JavaIntegrationTests;
import com.couchbase.client.java.kv.PersistTo;
import com.couchbase.client.java.kv.ReplicateTo;
import com.couchbase.client.java.query.QueryOptions;
import com.couchbase.client.java.query.QueryScanConsistency;
;
@@ -86,8 +94,8 @@ class CouchbaseTemplateKeyValueIntegrationTests extends JavaIntegrationTests {
modifying.setVersion(user.getVersion());
modified = couchbaseTemplate.replaceById(User.class).one(modifying);
assertEquals(modifying, modified);
if(user == modified){
throw new RuntimeException ( " user == modified ");
if (user == modified) {
throw new RuntimeException(" user == modified ");
}
assertNotEquals(user, modified);
assertEquals(NaiveAuditorAware.AUDITOR, modified.getCreatedBy());
@@ -107,6 +115,52 @@ class CouchbaseTemplateKeyValueIntegrationTests extends JavaIntegrationTests {
couchbaseTemplate.removeById().one(user.getId());
}
@Test
void findProjected() {
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
couchbaseTemplate.insertById(User.class).one(user);
User found = couchbaseTemplate.findById(User.class).project(new String[] { "firstname" }).one(user.getId());
System.err.println(found);
couchbaseTemplate.removeById(User.class).one(user.getId());
}
@Test
void findProjecting() {
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
couchbaseTemplate.insertById(User.class).one(user);
List<User> found = couchbaseTemplate.findByQuery(User.class).project(new String[] { "firstname" })
.withOptions(QueryOptions.queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS)).all();
assertEquals(1, found.size());
assertNotEquals(user, found.get(0), "should have found this document");
assertEquals(user.getFirstname(), found.get(0).getFirstname(), "firstname should match");
assertNull(found.get(0).getLastname(), "lastname should be null");
couchbaseTemplate.removeById(User.class).one(user.getId());
}
@Test
void findProjectingPath() {
UserSubmission user = new UserSubmission();
user.setId(UUID.randomUUID().toString());
user.setUsername("dave");
user.setRoles(Arrays.asList("role1", "role2"));
Address address = new Address();
address.setStreet("1234 Olcott Street");
address.setCity("Santa Clara");
user.setAddress(address);
user.setSubmissions(
Arrays.asList(new Submission(UUID.randomUUID().toString(), user.getId(), "tid", "status", 123)));
couchbaseTemplate.upsertById(UserSubmission.class).one(user);
assertThrows(CouchbaseException.class, () -> couchbaseTemplate.findByQuery(UserSubmission.class).project(new String[] { "address.street" })
.withConsistency(QueryScanConsistency.REQUEST_PLUS).all());
List<UserSubmission> found = couchbaseTemplate.findByQuery(UserSubmission.class).project(new String[] { "address" })
.withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
assertEquals(found.size(), 1);
assertEquals(found.get(0).getAddress(), address);
assertNull(found.get(0).getUsername(), "username should have been null");
couchbaseTemplate.removeById(User.class).one(user.getId());
}
@Test
void withDurability()
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2017-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.domain;
import java.util.Objects;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.couchbase.core.mapping.Document;
/**
* AirportMini entity
*
* @author Michael Reiche
*/
@Document
public class AirportMini extends ComparableEntity {
@Id private String id;
private String iata;
private Address address;
@PersistenceConstructor
public AirportMini(final String id, final String iata) {
this.id = id;
this.iata = iata;
}
public String getId() {
return id;
}
public String getIata() {
return iata;
}
public void setIata(String iata) {
this.iata = iata;
}
@Override
public int hashCode() {
return Objects.hash(id, iata);
}
}

View File

@@ -68,6 +68,9 @@ public interface AirportRepository extends CouchbaseRepository<Airport, String>,
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<Airport> findAllByIata(String iata);
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<AirportMini> getByIata(String iata);
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
@ComposedMetaAnnotation(collection = "_default", timeoutMs = 1000)
Airport findByIata(String iata);

View File

@@ -56,6 +56,7 @@ import org.springframework.data.couchbase.core.query.N1QLExpression;
import org.springframework.data.couchbase.core.query.QueryCriteria;
import org.springframework.data.couchbase.domain.Address;
import org.springframework.data.couchbase.domain.Airport;
import org.springframework.data.couchbase.domain.AirportMini;
import org.springframework.data.couchbase.domain.AirportRepository;
import org.springframework.data.couchbase.domain.NaiveAuditorAware;
import org.springframework.data.couchbase.domain.Person;
@@ -239,6 +240,20 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
}
}
@Test
void findBySimplePropertyReturnType() {
Airport vie = null;
try {
vie = new Airport("airports::vie", "vie", "low6");
vie = airportRepository.save(vie);
List<AirportMini> airports = airportRepository.getByIata("vie");
assertEquals(1, airports.size());
System.out.println(airports.get(0));
} finally {
airportRepository.delete(vie);
}
}
@Test
void findByTypeAlias() {
Airport vie = null;