1
1
package com .redis .om .spring .repository .support ;
2
2
3
+ import com .google .common .collect .Iterables ;
3
4
import com .google .common .collect .Lists ;
4
5
import com .google .gson .Gson ;
5
6
import com .google .gson .GsonBuilder ;
17
18
import com .redis .om .spring .repository .RedisDocumentRepository ;
18
19
import com .redis .om .spring .search .stream .EntityStream ;
19
20
import com .redis .om .spring .search .stream .EntityStreamImpl ;
20
- import com .redis .om .spring .search .stream .FluentQueryByExample ;
21
+ import com .redis .om .spring .search .stream .RedisFluentQueryByExample ;
22
+ import com .redis .om .spring .search .stream .SearchStream ;
21
23
import com .redis .om .spring .serialization .gson .GsonListOfType ;
22
24
import com .redis .om .spring .util .ObjectUtils ;
23
25
import com .redis .om .spring .vectorize .FeatureExtractor ;
26
28
import org .springframework .beans .PropertyAccessor ;
27
29
import org .springframework .beans .PropertyAccessorFactory ;
28
30
import org .springframework .beans .factory .annotation .Qualifier ;
31
+ import org .springframework .dao .IncorrectResultSizeDataAccessException ;
29
32
import org .springframework .dao .OptimisticLockingFailureException ;
30
33
import org .springframework .data .annotation .Reference ;
31
34
import org .springframework .data .annotation .Version ;
@@ -89,7 +92,9 @@ public SimpleRedisDocumentRepository( //
89
92
KeyValueOperations operations , //
90
93
@ Qualifier ("redisModulesOperations" ) RedisModulesOperations <?> rmo , //
91
94
RediSearchIndexer indexer , //
92
- RedisMappingContext mappingContext , GsonBuilder gsonBuilder , FeatureExtractor featureExtractor , //
95
+ RedisMappingContext mappingContext , //
96
+ GsonBuilder gsonBuilder , //
97
+ FeatureExtractor featureExtractor , //
93
98
RedisOMProperties properties ) {
94
99
super (metadata , operations );
95
100
this .modulesOperations = (RedisModulesOperations <String >) rmo ;
@@ -373,7 +378,13 @@ private Number getEntityVersion(String key, String versionProperty) {
373
378
374
379
@ Override
375
380
public <S extends T > Optional <S > findOne (Example <S > example ) {
376
- return entityStream .of (example .getProbeType ()).filter (example ).findFirst ();
381
+ Iterable <S > result = findAll (example );
382
+ var size = Iterables .size (result );
383
+ if (size > 1 ) {
384
+ throw new IncorrectResultSizeDataAccessException ("Query returned non unique result" , 1 );
385
+ }
386
+
387
+ return StreamSupport .stream (result .spliterator (), false ).findFirst ();
377
388
}
378
389
379
390
@ Override
@@ -388,7 +399,13 @@ public <S extends T> Iterable<S> findAll(Example<S> example, Sort sort) {
388
399
389
400
@ Override
390
401
public <S extends T > Page <S > findAll (Example <S > example , Pageable pageable ) {
391
- return pageFromSlice (entityStream .of (example .getProbeType ()).filter (example ).getSlice (pageable ));
402
+ SearchStream <S > stream = entityStream .of (example .getProbeType ());
403
+ var offset = pageable .getOffset () * pageable .getPageSize ();
404
+ var limit = pageable .getPageSize ();
405
+ Slice <S > slice = stream .filter (example ).loadAll ().limit (limit , Math .toIntExact (offset ))
406
+ .toList (pageable , stream .getEntityClass ());
407
+
408
+ return pageFromSlice (slice );
392
409
}
393
410
394
411
/* (non-Javadoc)
@@ -467,7 +484,8 @@ public <S extends T, R> R findBy(Example<S> example, Function<FetchableFluentQue
467
484
Assert .notNull (queryFunction , "Query function must not be null" );
468
485
469
486
return queryFunction .apply (
470
- new FluentQueryByExample <>(example , example .getProbeType (), entityStream , getSearchOps ()));
487
+ new RedisFluentQueryByExample <>(example , example .getProbeType (), entityStream , getSearchOps (),
488
+ mappingConverter .getMappingContext ()));
471
489
}
472
490
473
491
private SearchOperations <String > getSearchOps () {
0 commit comments