@@ -93,8 +93,14 @@ public class EbeanAspectDao implements AspectDao, AspectMigrationsDao {
93
93
*/
94
94
private final LoadingCache <String , Lock > locks ;
95
95
96
+ private final String batchGetMethod ;
97
+
96
98
public EbeanAspectDao (@ Nonnull final Database server , EbeanConfiguration ebeanConfiguration ) {
97
99
_server = server ;
100
+ this .batchGetMethod =
101
+ ebeanConfiguration .getBatchGetMethod () != null
102
+ ? ebeanConfiguration .getBatchGetMethod ()
103
+ : "IN" ;
98
104
if (ebeanConfiguration .getLocking ().isEnabled ()) {
99
105
this .locks =
100
106
CacheBuilder .newBuilder ()
@@ -371,23 +377,37 @@ private List<EbeanAspectV2> batchGet(
371
377
372
378
final int totalPageCount = QueryUtils .getTotalPageCount (keys .size (), keysCount );
373
379
final List <EbeanAspectV2 > finalResult =
374
- batchGetUnion (new ArrayList <>(keys ), keysCount , position , forUpdate );
380
+ batchGetSelectString (new ArrayList <>(keys ), keysCount , position , forUpdate );
375
381
376
382
while (QueryUtils .hasMore (position , keysCount , totalPageCount )) {
377
383
position += keysCount ;
378
384
final List <EbeanAspectV2 > oneStatementResult =
379
- batchGetUnion (new ArrayList <>(keys ), keysCount , position , forUpdate );
385
+ batchGetSelectString (new ArrayList <>(keys ), keysCount , position , forUpdate );
380
386
finalResult .addAll (oneStatementResult );
381
387
}
382
388
383
389
return finalResult ;
384
390
}
385
391
392
+ @ Nonnull
393
+ private List <EbeanAspectV2 > batchGetSelectString (
394
+ @ Nonnull final List <EbeanAspectV2 .PrimaryKey > keys ,
395
+ final int keysCount ,
396
+ final int position ,
397
+ boolean forUpdate ) {
398
+
399
+ if (batchGetMethod .equals ("IN" )) {
400
+ return batchGetIn (keys , keysCount , position , forUpdate );
401
+ }
402
+
403
+ return batchGetUnion (keys , keysCount , position , forUpdate );
404
+ }
405
+
386
406
/**
387
407
* Builds a single SELECT statement for batch get, which selects one entity, and then can be
388
408
* UNION'd with other SELECT statements.
389
409
*/
390
- private String batchGetSelect (
410
+ private String batchGetSelectString (
391
411
final int selectId ,
392
412
@ Nonnull final String urn ,
393
413
@ Nonnull final String aspect ,
@@ -434,7 +454,7 @@ private List<EbeanAspectV2> batchGetUnion(
434
454
final Map <String , Object > params = new HashMap <>();
435
455
for (int index = position ; index < end ; index ++) {
436
456
sb .append (
437
- batchGetSelect (
457
+ batchGetSelectString (
438
458
index - position ,
439
459
keys .get (index ).getUrn (),
440
460
keys .get (index ).getAspect (),
@@ -467,6 +487,65 @@ private List<EbeanAspectV2> batchGetUnion(
467
487
return query .findList ();
468
488
}
469
489
490
+ @ Nonnull
491
+ private List <EbeanAspectV2 > batchGetIn (
492
+ @ Nonnull final List <EbeanAspectV2 .PrimaryKey > keys ,
493
+ final int keysCount ,
494
+ final int position ,
495
+ boolean forUpdate ) {
496
+ validateConnection ();
497
+
498
+ // Build a single SELECT with IN clause using composite key comparison
499
+ // Query will look like:
500
+ // SELECT * FROM metadata_aspect WHERE (urn, aspect, version) IN
501
+ // (('urn0', 'aspect0', 0), ('urn1', 'aspect1', 1))
502
+ final StringBuilder sb = new StringBuilder ();
503
+ sb .append (
504
+ "SELECT urn, aspect, version, metadata, systemMetadata, createdOn, createdBy, createdFor " );
505
+ sb .append ("FROM metadata_aspect_v2 WHERE (urn, aspect, version) IN (" );
506
+
507
+ final int end = Math .min (keys .size (), position + keysCount );
508
+ final Map <String , Object > params = new HashMap <>();
509
+
510
+ for (int index = position ; index < end ; index ++) {
511
+ int paramIndex = index - position ;
512
+ String urnParam = "urn" + paramIndex ;
513
+ String aspectParam = "aspect" + paramIndex ;
514
+ String versionParam = "version" + paramIndex ;
515
+
516
+ params .put (urnParam , keys .get (index ).getUrn ());
517
+ params .put (aspectParam , keys .get (index ).getAspect ());
518
+ params .put (versionParam , keys .get (index ).getVersion ());
519
+
520
+ sb .append ("(:" + urnParam + ", :" + aspectParam + ", :" + versionParam + ")" );
521
+
522
+ if (index != end - 1 ) {
523
+ sb .append ("," );
524
+ }
525
+ }
526
+
527
+ sb .append (")" );
528
+
529
+ if (forUpdate ) {
530
+ sb .append (" FOR UPDATE" );
531
+ }
532
+
533
+ final RawSql rawSql =
534
+ RawSqlBuilder .parse (sb .toString ())
535
+ .columnMapping (EbeanAspectV2 .URN_COLUMN , "key.urn" )
536
+ .columnMapping (EbeanAspectV2 .ASPECT_COLUMN , "key.aspect" )
537
+ .columnMapping (EbeanAspectV2 .VERSION_COLUMN , "key.version" )
538
+ .create ();
539
+
540
+ final Query <EbeanAspectV2 > query = _server .find (EbeanAspectV2 .class ).setRawSql (rawSql );
541
+
542
+ for (Map .Entry <String , Object > param : params .entrySet ()) {
543
+ query .setParameter (param .getKey (), param .getValue ());
544
+ }
545
+
546
+ return query .findList ();
547
+ }
548
+
470
549
@ Override
471
550
@ Nonnull
472
551
public ListResult <String > listUrns (
0 commit comments