Show how to use Limit with repository queries.
Add methods taking a Limit parameter to various samples. Original pull request: #672 Closes #671
This commit is contained in:
committed by
Mark Paluch
parent
e90be0c65b
commit
f98c7b9c93
@@ -18,6 +18,7 @@ package example.springdata.cassandra.basic;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.cassandra.repository.Query;
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
@@ -55,4 +56,13 @@ public interface BasicUserRepository extends CrudRepository<User, Long> {
|
||||
* @return
|
||||
*/
|
||||
List<User> findUsersByLastnameStartsWith(String lastnamePrefix);
|
||||
|
||||
/**
|
||||
* Same as {@link #findUsersByLastnameStartsWith(String)} but reducing the result size to a given {@link Limit}.
|
||||
*
|
||||
* @param lastnamePrefix
|
||||
* @param maxResults the maximum number of results returned.
|
||||
* @return
|
||||
*/
|
||||
List<User> findUsersByLastnameStartsWith(String lastnamePrefix, Limit maxResults);
|
||||
}
|
||||
|
||||
@@ -43,4 +43,10 @@ public class User {
|
||||
public User(Long id) {
|
||||
this.setId(id);
|
||||
}
|
||||
|
||||
public User(Long id, String firstname, String lastname) {
|
||||
this.id = id;
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ package example.springdata.cassandra.basic;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assumptions.*;
|
||||
|
||||
import java.util.stream.LongStream;
|
||||
|
||||
import example.springdata.cassandra.util.CassandraKeyspace;
|
||||
import example.springdata.cassandra.util.CassandraVersion;
|
||||
|
||||
@@ -26,6 +28,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.util.Version;
|
||||
|
||||
import com.datastax.oss.driver.api.core.CqlSession;
|
||||
@@ -120,4 +123,24 @@ class BasicUserRepositoryTests {
|
||||
|
||||
assertThat(repository.findUsersByLastnameStartsWith("last")).contains(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Spring Data Cassandra supports {@code Limit} to reduce the number of returned results.
|
||||
*/
|
||||
@Test
|
||||
void limitResultSize() throws InterruptedException {
|
||||
|
||||
assumeThat(CassandraVersion.getReleaseVersion(session).isGreaterThanOrEqualTo(CASSANDRA_3_4)).isTrue();
|
||||
|
||||
session.execute("CREATE CUSTOM INDEX ON users (lname) USING 'org.apache.cassandra.index.sasi.SASIIndex';");
|
||||
/*
|
||||
Cassandra secondary indexes are created in the background without the possibility to check
|
||||
whether they are available or not. So we are forced to just wait. *sigh*
|
||||
*/
|
||||
Thread.sleep(1000);
|
||||
|
||||
LongStream.range(0, 10).forEach(id -> repository.save(new User(id, user.getFirstname(), user.getLastname())));
|
||||
|
||||
assertThat(repository.findUsersByLastnameStartsWith("last", Limit.of(5))).hasSize(5);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user