Skip to content

Commit ae2217d

Browse files
authored
Implement Stream Queries for non-Reactive repository. (#1172)
Closes #1154. Co-authored-by: mikereiche <[email protected]>
1 parent 0bdecef commit ae2217d

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/main/java/org/springframework/data/couchbase/repository/query/AbstractCouchbaseQuery.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 the original author or authors
2+
* Copyright 2020-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -123,6 +123,8 @@ private CouchbaseQueryExecution getExecutionToWrap(ParameterAccessor accessor,
123123
return (q, t, c) -> operation.matching(q.with(accessor.getPageable())).all(); // s/b tail() instead of all()
124124
} else if (getQueryMethod().isCollectionQuery()) {
125125
return (q, t, c) -> operation.matching(q.with(accessor.getPageable())).all();
126+
} else if (getQueryMethod().isStreamQuery()) {
127+
return (q, t, c) -> operation.matching(q.with(accessor.getPageable())).stream();
126128
} else if (isCountQuery()) {
127129
return (q, t, c) -> operation.matching(q).count();
128130
} else if (isExistsQuery()) {

src/test/java/org/springframework/data/couchbase/domain/UserRepository.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.data.couchbase.domain;
1818

1919
import java.util.List;
20+
import java.util.stream.Stream;
2021

2122
import org.springframework.data.couchbase.repository.CouchbaseRepository;
2223
import org.springframework.data.couchbase.repository.Query;
@@ -36,6 +37,8 @@ public interface UserRepository extends CouchbaseRepository<User, String> {
3637

3738
List<User> findByFirstname(String firstname);
3839

40+
Stream<User> findByLastname(String lastname);
41+
3942
List<User> findByFirstnameIn(String... firstnames);
4043

4144
List<User> findByFirstnameIn(JsonArray firstnames);

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -233,6 +233,21 @@ public void testCas() {
233233
userRepository.delete(user);
234234
}
235235

236+
@Test
237+
public void testStreamQuery() {
238+
User user1 = new User("1", "Dave", "Wilson");
239+
User user2 = new User("2", "Brian", "Wilson");
240+
241+
userRepository.save(user1);
242+
userRepository.save(user2);
243+
List<User> users = userRepository.findByLastname("Wilson").collect(Collectors.toList());
244+
assertEquals(2,users.size());
245+
assertTrue(users.contains(user1));
246+
assertTrue(users.contains(user2));
247+
userRepository.delete(user1);
248+
userRepository.delete(user2);
249+
}
250+
236251
@Test
237252
void count() {
238253
String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };

0 commit comments

Comments
 (0)