RepairRunService { static List<Segment> filterSegmentsByNodes( List<Segment> segments, RepairUnit repairUnit, Map<String, List<RingRange>> endpointToRange) { if (repairUnit.getNodes().isEmpty()) { return segments; } else { return segments .stream() .filter( segment -> { RingRange firstRange = segment.getBaseRange(); for (Entry<String, List<RingRange>> entry : endpointToRange.entrySet()) { if (repairUnit.getNodes().contains(entry.getKey())) { for (RingRange range : entry.getValue()) { if (range.encloses(firstRange)) { return true; } } } } return false; }) .collect(Collectors.toList()); } } private  RepairRunService(AppContext context); static RepairRunService create(AppContext context); RepairRun registerRepairRun(
      Cluster cluster,
      RepairUnit repairUnit,
      Optional<String> cause,
      String owner,
      int segments,
      int segmentsPerNode,
      RepairParallelism repairParallelism,
      Double intensity); Set<String> getTableNamesBasedOnParam(
      Cluster cluster,
      String keyspace,
      Optional<String> tableNamesParam); Set<String> getNodesToRepairBasedOnParam(
      Cluster cluster,
      Optional<String> nodesToRepairParam); static Set<String> getDatacentersToRepairBasedOnParam(Cluster cluster, Optional<String> datacenters); static final Splitter COMMA_SEPARATED_LIST_SPLITTER; static final int DEFAULT_SEGMENT_COUNT_PER_NODE; }
@Test public void filterSegmentsByNodesTest() throws ReaperException { Map<List<String>, List<String>> rangeToEndpoint = Maps.newHashMap(); rangeToEndpoint.put(Arrays.asList("1", "2"), Arrays.asList("node1", "node2", "node3")); rangeToEndpoint.put(Arrays.asList("2", "4"), Arrays.asList("node1", "node2", "node3")); rangeToEndpoint.put(Arrays.asList("4", "6"), Arrays.asList("node1")); rangeToEndpoint.put(Arrays.asList("6", "8"), Arrays.asList("node1", "node2")); List<Segment> segments = Arrays.asList( Segment.builder().withTokenRange(new RingRange("1", "2")).build(), Segment.builder().withTokenRange(new RingRange("2", "3")).build(), Segment.builder().withTokenRange(new RingRange("3", "4")).build(), Segment.builder().withTokenRange(new RingRange("4", "5")).build(), Segment.builder().withTokenRange(new RingRange("5", "6")).build(), Segment.builder().withTokenRange(new RingRange("6", "8")).build()); final RepairUnit repairUnit1 = mock(RepairUnit.class); when(repairUnit1.getNodes()).thenReturn(new HashSet<String>(Arrays.asList("node3", "node2"))); final RepairUnit repairUnit2 = mock(RepairUnit.class); when(repairUnit2.getNodes()).thenReturn(new HashSet<String>(Arrays.asList("node1"))); final RepairUnit repairUnit3 = mock(RepairUnit.class); when(repairUnit3.getNodes()).thenReturn(new HashSet<String>(Arrays.asList("node3"))); List<Segment> filtered = RepairRunService.filterSegmentsByNodes( segments, repairUnit1, RepairRunService.buildEndpointToRangeMap(rangeToEndpoint)); assertEquals(filtered.size(), 4); filtered = RepairRunService.filterSegmentsByNodes(segments, repairUnit2, RepairRunService.buildEndpointToRangeMap(rangeToEndpoint)); assertEquals(filtered.size(), 6); filtered = RepairRunService.filterSegmentsByNodes(segments, repairUnit3, RepairRunService.buildEndpointToRangeMap(rangeToEndpoint)); assertEquals(filtered.size(), 3); final RepairUnit repairUnitWithNoNodes = mock(RepairUnit.class); when(repairUnitWithNoNodes.getNodes()).thenReturn(new HashSet<String>()); filtered = RepairRunService.filterSegmentsByNodes(segments, repairUnitWithNoNodes, RepairRunService.buildEndpointToRangeMap(rangeToEndpoint)); assertEquals(filtered.size(), 6); }