简体   繁体   中英

How does Stream.count() work for Spring repository queries?

I am working in Spring framework and use a repository to load data as a stream. Below is code that is being used:

@Query("select j from User j ")
@QueryHints({ @QueryHint(name = HINT_FETCH_SIZE, value = Constants.JPA_QUERYHINT_FETCH_SIZE) })
Stream<User> streamAll();

When we call service.getStream().count() method, does it load the complete set of data in memory or does it have some meta information about the size of stream?

Here my concern is memory consumption and performance.

Try it yourself.

If this returns true then it is possible to get the count without iterating.

If it returns false then you will have to iterate.

myRepo.streamAll().spliterator().hasCharacteristics(Spliterator.SIZED)

It's a bit fiddly to check the implementation, since Spring creates a dynamic proxy for repos .

I suppose there is nothing to say that even if this optimization isn't in place now, that it may not be available in the future. Probably depends on DB vendor too, as not all of them have an O(1) way to get a row count (usually accomplished by storing it as table meta-data).

Streams are lazy

That means that the results are not fetched until a terminal operation is called.

However, count() is a terminal operation, so the pipeline is executed.

Streams are also short-circuiting

That means that if enough elements are traversed for the operation to succeed, no more elements are traversed. Such is the case with findFirst() , for example.

However, the count() operation suggests that all elements are to be counted, requiring all elements to be traversed to get the result.

Now the good news is that some information can directly be queried from the stream source. One such example is the number of elements, when the flag SIZED is set on the stream. The stream is then smart enough to directly get the information from the stream source, instead of traversing all elements. This optimization was erroneously not implemented in Java 8, and has been implemented in Java 9 .

Stream provider

The characteristics are set by the provider of the stream. And whether count() is short-circuiting, in the end, is up to the stream provider.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM