private List < TaskLockPosse > findLockPossesOverlapsInterval ( final String dataSource , final Interval interval ) { giant . lock ( ) ; try { final NavigableMap < DateTime , SortedMap < Interval , List < TaskLockPosse > > > dsRunning = running . get ( dataSource ) ; if ( dsRunning == null ) { // No locks at all return Collections . emptyList ( ) ; } else { // Tasks are indexed by locked interval, which are sorted by interval start. Intervals are non-overlapping, so: final NavigableSet < DateTime > dsLockbox = dsRunning . navigableKeySet ( ) ; final Iterable < DateTime > searchStartTimes = Iterables . concat ( // Single interval that starts at or before ours Collections . singletonList ( dsLockbox . floor ( interval . getStart ( ) ) ) , // All intervals that start somewhere between our start instant (exclusive) and end instant (exclusive) dsLockbox . subSet ( interval . getStart ( ) , false , interval . getEnd ( ) , false ) ) ; return StreamSupport . stream ( searchStartTimes . spliterator ( ) , false ) . filter ( java . util . Objects :: nonNull ) . map ( dsRunning :: get ) . filter ( java . util . Objects :: nonNull ) . flatMap ( sortedMap -> sortedMap . entrySet ( ) . stream ( ) ) . filter ( entry -> entry . getKey ( ) . overlaps ( interval ) ) . flatMap ( entry -> entry . getValue ( ) . stream ( ) ) . collect ( Collectors . toList ( ) ) ; } } finally { giant . unlock ( ) ; } }
Return all locks that overlap some search interval .