1
1
package tech .ydb .yoj .repository .db ;
2
2
3
- import com .google .common .collect .Sets ;
4
3
import lombok .NonNull ;
5
4
import tech .ydb .yoj .databind .expression .FilterExpression ;
6
5
import tech .ydb .yoj .databind .expression .OrderExpression ;
14
13
import javax .annotation .CheckForNull ;
15
14
import javax .annotation .Nullable ;
16
15
import java .util .Collection ;
17
- import java .util .HashMap ;
18
- import java .util .HashSet ;
19
16
import java .util .List ;
20
17
import java .util .Optional ;
21
18
import java .util .Set ;
25
22
import java .util .stream .Stream ;
26
23
27
24
import static java .util .stream .Collectors .toList ;
28
- import static java .util .stream .Collectors .toSet ;
29
25
import static java .util .stream .Stream .concat ;
30
26
31
27
public interface Table <T extends Entity <T >> {
@@ -42,6 +38,8 @@ public interface Table<T extends Entity<T>> {
42
38
43
39
<V extends View > V find (Class <V > viewType , Entity .Id <T > id );
44
40
41
+ <ID extends Entity .Id <T >> List <T > find (Set <ID > ids );
42
+
45
43
<ID extends Entity .Id <T >> List <T > find (Range <ID > range );
46
44
47
45
<ID extends Entity .Id <T >> List <ID > findIds (Range <ID > range );
@@ -265,54 +263,6 @@ default <V extends Table.View> ViewListResult<T, V> list(Class<V> viewType, List
265
263
return ViewListResult .forPage (request , viewType , nextPage );
266
264
}
267
265
268
- default <ID extends Entity .Id <T >> List <T > find (Set <ID > ids ) {
269
- if (ids .isEmpty ()) {
270
- return List .of ();
271
- }
272
-
273
- var orderBy = EntityExpressions .defaultOrder (getType ());
274
- var cache = Tx .Current .get ().getRepositoryTransaction ().getTransactionLocal ().firstLevelCache ();
275
- var isPartialIdMode = ids .iterator ().next ().isPartial ();
276
-
277
- var foundInCache = ids .stream ()
278
- .filter (cache ::containsKey )
279
- .map (cache ::peek )
280
- .flatMap (Optional ::stream )
281
- .collect (Collectors .toMap (Entity ::getId , Function .identity ()));
282
- var remainingIds = Sets .difference (ids , foundInCache .keySet ());
283
- var foundInDb = findUncached (remainingIds , null , orderBy , null );
284
-
285
- var merged = new HashMap <Entity .Id <T >, T >();
286
-
287
- // some entries found in db with partial id query may already be in cache (after update/delete),
288
- // so we must return actual entries from cache
289
- for (var entry : foundInDb ) {
290
- var id = entry .getId ();
291
- if (cache .containsKey (id )) {
292
- var cached = cache .peek (id );
293
- cached .ifPresent (t -> merged .put (id , t ));
294
- // not present means marked as deleted in cache
295
- } else {
296
- merged .put (id , this .postLoad (entry ));
297
- }
298
- }
299
-
300
- // add entries found in cache and not fetched from db
301
- for (var pair : foundInCache .entrySet ()) {
302
- var id = pair .getKey ();
303
- var entry = pair .getValue ();
304
- merged .put (id , entry );
305
- }
306
-
307
- if (!isPartialIdMode ) {
308
- Set <Entity .Id <T >> foundInDbIds = foundInDb .stream ().map (Entity ::getId ).collect (toSet ());
309
- Set <Entity .Id <T >> foundInCacheIds = new HashSet <>(foundInCache .keySet ());
310
- Sets .difference (Sets .difference (ids , foundInDbIds ), foundInCacheIds ).forEach (cache ::putEmpty );
311
- }
312
-
313
- return merged .values ().stream ().sorted (EntityIdSchema .SORT_ENTITY_BY_ID ).collect (Collectors .toList ());
314
- }
315
-
316
266
default void bulkUpsert (List <T > input , BulkParams params ) {
317
267
throw new UnsupportedOperationException ();
318
268
}
0 commit comments