36
36
import java .util .Arrays ;
37
37
import java .util .Collection ;
38
38
import java .util .Collections ;
39
- import java .util .HashMap ;
40
39
import java .util .HashSet ;
40
+ import java .util .LinkedList ;
41
41
import java .util .List ;
42
- import java .util .Map ;
43
42
import java .util .Set ;
43
+ import java .util .concurrent .atomic .AtomicReference ;
44
44
import java .util .function .Predicate ;
45
45
import java .util .stream .Collectors ;
46
46
import java .util .stream .Stream ;
@@ -162,13 +162,15 @@ public List<T> getAll()
162
162
}
163
163
164
164
@ Override
165
+ @ Measure
165
166
public Stream <T > stream ()
166
167
throws IndyDataException
167
168
{
168
169
return stream ( store -> true );
169
170
}
170
171
171
172
@ Override
173
+ @ Measure
172
174
public Stream <T > stream ( Predicate <ArtifactStore > filter )
173
175
throws IndyDataException
174
176
{
@@ -219,6 +221,7 @@ public List<T> getAll( Predicate<ArtifactStore> filter )
219
221
}
220
222
221
223
@ Override
224
+ @ Measure
222
225
public List <T > getAllByDefaultPackageTypes ()
223
226
throws IndyDataException
224
227
{
@@ -233,6 +236,7 @@ public List<T> getAllByDefaultPackageTypes()
233
236
}
234
237
235
238
@ Override
239
+ @ Measure
236
240
public T getByName ( String name )
237
241
throws IndyDataException
238
242
{
@@ -251,9 +255,7 @@ public boolean containsByName( String name )
251
255
public Set <Group > getGroupsContaining ( StoreKey storeKey )
252
256
throws IndyDataException
253
257
{
254
- return new DefaultArtifactStoreQuery <>( dataManager , storeKey .getPackageType (), enabled ,
255
- Group .class ).stream (
256
- store -> ( (Group ) store ).getConstituents ().contains ( storeKey ) ).collect ( Collectors .toSet () );
258
+ return getAllGroups ().stream ().filter ( g -> g .getConstituents ().contains ( storeKey ) ).collect ( Collectors .toSet () );
257
259
}
258
260
259
261
@ Override
@@ -374,21 +376,15 @@ public List<RemoteRepository> getRemoteRepositoryByUrl( String url )
374
376
public List <ArtifactStore > getOrderedConcreteStoresInGroup ( final String groupName )
375
377
throws IndyDataException
376
378
{
377
- Map <StoreKey , ArtifactStore > stores = new HashMap <>();
378
- stream ().forEach ( s -> stores .put ( s .getKey (), s ) );
379
-
380
- return getGroupOrdering ( groupName , stores , false , true );
379
+ return getGroupOrdering ( groupName , false , true );
381
380
}
382
381
383
382
@ Override
384
383
@ Measure
385
384
public List <ArtifactStore > getOrderedStoresInGroup ( final String groupName )
386
385
throws IndyDataException
387
386
{
388
- Map <StoreKey , ArtifactStore > stores = new HashMap <>();
389
- stream ().forEach ( s -> stores .put ( s .getKey (), s ) );
390
-
391
- return getGroupOrdering ( groupName , stores , true , false );
387
+ return getGroupOrdering ( groupName , true , false );
392
388
}
393
389
394
390
@ Override
@@ -417,8 +413,8 @@ public Set<Group> getGroupsAffectedBy( Collection<StoreKey> keys )
417
413
418
414
Set <StoreKey > processed = new HashSet <>();
419
415
420
- Set <Group > all = new DefaultArtifactStoreQuery <>( dataManager , toProcess .get ( 0 ).getPackageType (), null ,
421
- Group .class ).stream ().collect ( Collectors .toSet () );
416
+ Set <StoreKey > all = new DefaultArtifactStoreQuery <>( dataManager , toProcess .get ( 0 ).getPackageType (), null ,
417
+ Group .class ).keyStream ().collect ( Collectors .toSet () );
422
418
423
419
while ( !toProcess .isEmpty () )
424
420
{
@@ -433,8 +429,10 @@ public Set<Group> getGroupsAffectedBy( Collection<StoreKey> keys )
433
429
// use this to avoid reprocessing groups we've already encountered.
434
430
processed .add ( next );
435
431
436
- for ( ArtifactStore store : all )
432
+ for ( StoreKey key : all )
437
433
{
434
+ ArtifactStore store = dataManager .getArtifactStore ( key );
435
+
438
436
if ( !processed .contains ( store .getKey () ) && ( store instanceof Group ) )
439
437
{
440
438
Group g = (Group ) store ;
@@ -452,27 +450,76 @@ public Set<Group> getGroupsAffectedBy( Collection<StoreKey> keys )
452
450
return groups ;
453
451
}
454
452
453
+ public Stream <StoreKey > keyStream ()
454
+ {
455
+ return keyStream ( null );
456
+ }
457
+
458
+ public Stream <StoreKey > keyStream ( Predicate <StoreKey > filterPredicate )
459
+ {
460
+ return dataManager .streamArtifactStoreKeys ().filter (key -> {
461
+ if ( packageType != null && !key .getPackageType ().equals ( packageType ))
462
+ {
463
+ return false ;
464
+ }
465
+
466
+ if ( types != null && !types .isEmpty () && !types .contains ( key .getType () ) )
467
+ {
468
+ return false ;
469
+ }
470
+
471
+ if ( filterPredicate != null && !filterPredicate .test ( key ) )
472
+ {
473
+ return false ;
474
+ }
475
+
476
+ return true ;
477
+ });
478
+ }
479
+
455
480
@ Override
481
+ @ Measure
456
482
public List <RemoteRepository > getAllRemoteRepositories ()
457
483
throws IndyDataException
458
484
{
459
- return new DefaultArtifactStoreQuery <>( dataManager , packageType , enabled ,
460
- RemoteRepository .class ).getAll ();
485
+ return getAllOfType ( RemoteRepository .class );
461
486
}
462
487
463
488
@ Override
489
+ @ Measure
464
490
public List <HostedRepository > getAllHostedRepositories ()
465
491
throws IndyDataException
466
492
{
467
- return new DefaultArtifactStoreQuery <>( dataManager , packageType , enabled ,
468
- HostedRepository .class ).getAll ();
493
+ return getAllOfType ( HostedRepository .class );
469
494
}
470
495
471
496
@ Override
497
+ @ Measure
472
498
public List <Group > getAllGroups ()
473
499
throws IndyDataException
474
500
{
475
- return new DefaultArtifactStoreQuery <>( dataManager , packageType , enabled , Group .class ).getAll ();
501
+ return getAllOfType ( Group .class );
502
+ }
503
+
504
+ private <T extends ArtifactStore > List <T > getAllOfType (Class <T > type )
505
+ throws IndyDataException
506
+ {
507
+ List <StoreKey > keys = new DefaultArtifactStoreQuery <>( dataManager , packageType , enabled , type ).keyStream ()
508
+ .collect (
509
+ Collectors
510
+ .toList () );
511
+
512
+ List <T > stores = new ArrayList <>();
513
+ for ( StoreKey key : keys )
514
+ {
515
+ T store = (T ) dataManager .getArtifactStore ( key );
516
+ if ( store != null && ( enabled == null || store .isDisabled () == !enabled ) )
517
+ {
518
+ stores .add ( store );
519
+ }
520
+ }
521
+
522
+ return stores ;
476
523
}
477
524
478
525
@ Override
@@ -503,7 +550,7 @@ public DefaultArtifactStoreQuery<T> noPackageType()
503
550
return this ;
504
551
}
505
552
506
- private List <ArtifactStore > getGroupOrdering ( final String groupName , final Map < StoreKey , ArtifactStore > stores ,
553
+ private List <ArtifactStore > getGroupOrdering ( final String groupName ,
507
554
final boolean includeGroups , final boolean recurseGroups )
508
555
throws IndyDataException
509
556
{
@@ -512,55 +559,79 @@ private List<ArtifactStore> getGroupOrdering( final String groupName, final Map<
512
559
throw new IndyDataException ( "packageType must be set on the query before calling this method!" );
513
560
}
514
561
515
- final Group master = (Group ) stores . get ( new StoreKey ( packageType , StoreType .group , groupName ) );
562
+ final Group master = (Group ) dataManager . getArtifactStore ( new StoreKey ( packageType , StoreType .group , groupName ) );
516
563
if ( master == null )
517
564
{
518
565
return Collections .emptyList ();
519
566
}
520
567
521
568
final List <ArtifactStore > result = new ArrayList <>();
522
- recurseGroup ( master , stores , result , new HashSet <>(), includeGroups , recurseGroups );
569
+ recurseGroup ( master , result , new HashSet <>(), includeGroups , recurseGroups );
523
570
524
571
return result ;
525
572
}
526
573
527
- private void recurseGroup ( final Group master , final Map < StoreKey , ArtifactStore > stores ,
574
+ private void recurseGroup ( final Group master ,
528
575
final List <ArtifactStore > result , final Set <StoreKey > seen , final boolean includeGroups ,
529
576
final boolean recurseGroups )
577
+ throws IndyDataException
530
578
{
531
- if ( master == null || master .isDisabled () && Boolean .TRUE .equals ( enabled ) )
532
- {
533
- return ;
534
- }
579
+ AtomicReference <IndyDataException > errorRef = new AtomicReference <>();
580
+ LinkedList <Group > toCheck = new LinkedList <>();
581
+ toCheck .add ( master );
535
582
536
- List <StoreKey > members = new ArrayList <>( master .getConstituents () );
537
- if ( includeGroups )
583
+ while ( !toCheck .isEmpty () )
538
584
{
539
- result .add ( master );
540
- }
585
+ Group next = toCheck .removeFirst ();
541
586
542
- members .forEach ( ( key ) ->
543
- {
544
- if ( !seen .contains ( key ) )
587
+ if ( next == null || next .isDisabled () && Boolean .TRUE .equals ( enabled ) )
588
+ {
589
+ return ;
590
+ }
591
+
592
+ List <StoreKey > members = new ArrayList <>( next .getConstituents () );
593
+ if ( includeGroups )
594
+ {
595
+ result .add ( next );
596
+ }
597
+
598
+ // TODO: Need to refactor away from actual recursion.
599
+ members .forEach ( ( key ) ->
545
600
{
546
- seen .add ( key );
547
- final StoreType type = key .getType ();
548
- if ( recurseGroups && type == StoreType .group )
549
- {
550
- // if we're here, we're definitely recursing groups...
551
- recurseGroup ( (Group ) stores .get ( key ), stores , result , seen , includeGroups ,
552
- true );
553
- }
554
- else
601
+ if ( !seen .contains ( key ) )
555
602
{
556
- final ArtifactStore store = stores .get ( key );
557
- if ( store != null && !( store .isDisabled () && Boolean .TRUE .equals ( enabled ) ) )
603
+ seen .add ( key );
604
+ final StoreType type = key .getType ();
605
+ try
606
+ {
607
+ if ( recurseGroups && type == StoreType .group )
608
+ {
609
+ // if we're here, we're definitely recursing groups...
610
+ Group group = (Group ) dataManager .getArtifactStore ( key );
611
+ toCheck .addFirst ( group );
612
+ }
613
+ else
614
+ {
615
+ final ArtifactStore store = dataManager .getArtifactStore ( key );
616
+ if ( store != null && !( store .isDisabled () && Boolean .TRUE .equals ( enabled ) ) )
617
+ {
618
+ result .add ( store );
619
+ }
620
+ }
621
+ }
622
+ catch ( IndyDataException e )
558
623
{
559
- result . add ( store );
624
+ errorRef . set ( e );
560
625
}
561
626
}
562
- }
563
- } );
627
+ } );
628
+
629
+ IndyDataException error = errorRef .get ();
630
+ if ( error != null )
631
+ {
632
+ throw error ;
633
+ }
634
+ }
564
635
}
565
636
566
637
}
0 commit comments