SweepableCellFilter { public BatchOfCellsToSweep getCellsToSweep(List<CandidateCellForSweeping> candidates) { Map<Long, Long> startToCommitTs = commitTsCache.loadBatch(getAllValidTimestamps(candidates)); ImmutableBatchOfCellsToSweep.Builder builder = ImmutableBatchOfCellsToSweep.builder(); long numCellTsPairsExamined = 0; for (CandidateCellForSweeping candidate : candidates) { getCellToSweep(candidate, startToCommitTs).ifPresent(builder::addCells); numCellTsPairsExamined += candidate.sortedTimestamps().size(); } return builder .numCellTsPairsExamined(numCellTsPairsExamined) .lastCellExamined(candidates.get(candidates.size() - 1).cell()) .build(); } SweepableCellFilter(CommitTsCache commitTsCache, Sweeper sweeper, long sweepTs); BatchOfCellsToSweep getCellsToSweep(List<CandidateCellForSweeping> candidates);  }
@Test public void conservative_getTimestampsToSweep_twoEntriesBelowSweepTimestamp_returnsLowerOne() { long sweepTimestampHigherThanCommitTimestamp = HIGH_COMMIT_TS + 1; List<CandidateCellForSweeping> candidates = twoCommittedTimestampsForSingleCell(); SweepableCellFilter filter = new SweepableCellFilter( commitTsCache, Sweeper.CONSERVATIVE, sweepTimestampHigherThanCommitTimestamp); List<CellToSweep> cells = filter.getCellsToSweep(candidates).cells(); assertThat(cells.size()).isEqualTo(1); assertThat(Iterables.getOnlyElement(cells).sortedTimestamps()).containsExactly(LOW_START_TS); }
@Test public void conservative_getTimestampsToSweep_oneEntryBelowTimestamp_oneAbove_returnsNone() { long sweepTimestampLowerThanCommitTimestamp = HIGH_COMMIT_TS - 1; List<CandidateCellForSweeping> candidates = twoCommittedTimestampsForSingleCell(); SweepableCellFilter filter = new SweepableCellFilter( commitTsCache, Sweeper.CONSERVATIVE, sweepTimestampLowerThanCommitTimestamp); List<CellToSweep> cells = filter.getCellsToSweep(candidates).cells(); assertThat(cells).isEmpty(); }
@Test public void conservativeGetTimestampToSweepAddsSentinels() { long sweepTimestampHigherThanCommitTimestamp = HIGH_COMMIT_TS + 1; List<CandidateCellForSweeping> candidates = twoCommittedTimestampsForSingleCell(); SweepableCellFilter filter = new SweepableCellFilter( commitTsCache, Sweeper.CONSERVATIVE, sweepTimestampHigherThanCommitTimestamp); List<CellToSweep> cells = filter.getCellsToSweep(candidates).cells(); assertThat(Iterables.getOnlyElement(cells).needsSentinel()).isTrue(); }
@Test public void thoroughGetTimestampToSweepDoesNotAddSentinels() { long sweepTimestampHigherThanCommitTimestamp = HIGH_COMMIT_TS + 1; List<CandidateCellForSweeping> candidates = twoCommittedTimestampsForSingleCell(); SweepableCellFilter filter = new SweepableCellFilter( commitTsCache, Sweeper.THOROUGH, sweepTimestampHigherThanCommitTimestamp); List<CellToSweep> cells = filter.getCellsToSweep(candidates).cells(); assertThat(Iterables.getOnlyElement(cells).needsSentinel()).isFalse(); }
@Test public void getTimestampsToSweep_onlyTransactionUncommitted_returnsIt() { List<CandidateCellForSweeping> candidate = ImmutableList.of( ImmutableCandidateCellForSweeping.builder() .cell(SINGLE_CELL) .sortedTimestamps(ImmutableList.of(LOW_START_TS)) .isLatestValueEmpty(false) .build()); when(mockTransactionService.get(anyCollection())) .thenReturn(ImmutableMap.of(LOW_START_TS, TransactionConstants.FAILED_COMMIT_TS)); SweepableCellFilter filter = new SweepableCellFilter( commitTsCache, Sweeper.CONSERVATIVE, HIGH_START_TS); List<CellToSweep> cells = filter.getCellsToSweep(candidate).cells(); assertThat(cells.size()).isEqualTo(1); assertThat(Iterables.getOnlyElement(cells).sortedTimestamps()).containsExactly(LOW_START_TS); }
@Test public void thorough_getTimestampsToSweep_oneTransaction_emptyValue_returnsIt() { List<CandidateCellForSweeping> candidate = ImmutableList.of( ImmutableCandidateCellForSweeping.builder() .cell(SINGLE_CELL) .sortedTimestamps(ImmutableList.of(LOW_START_TS)) .isLatestValueEmpty(true) .build()); when(mockTransactionService.get(anyCollection())) .thenReturn(ImmutableMap.of(LOW_START_TS, LOW_COMMIT_TS)); SweepableCellFilter filter = new SweepableCellFilter( commitTsCache, Sweeper.THOROUGH, HIGH_START_TS); List<CellToSweep> cells = filter.getCellsToSweep(candidate).cells(); assertThat(cells.size()).isEqualTo(1); assertThat(Iterables.getOnlyElement(cells).sortedTimestamps()).containsExactly(LOW_START_TS); }
@Test public void thorough_getTimestampsToSweep_oneSentinel_returnsIt() { List<CandidateCellForSweeping> candidate = ImmutableList.of( ImmutableCandidateCellForSweeping.builder() .cell(SINGLE_CELL) .sortedTimestamps(ImmutableList.of(Value.INVALID_VALUE_TIMESTAMP)) .isLatestValueEmpty(true) .build()); when(mockTransactionService.get(anyCollection())) .thenReturn(ImmutableMap.of()); SweepableCellFilter filter = new SweepableCellFilter( commitTsCache, Sweeper.THOROUGH, HIGH_START_TS); List<CellToSweep> cells = filter.getCellsToSweep(candidate).cells(); assertThat(cells.size()).isEqualTo(1); assertThat(Iterables.getOnlyElement(cells).sortedTimestamps()).containsExactly(Value.INVALID_VALUE_TIMESTAMP); }
@Test public void testNoCandidates() { List<CandidateCellForSweeping> candidate = ImmutableList.of( ImmutableCandidateCellForSweeping.builder() .cell(SINGLE_CELL) .sortedTimestamps(ImmutableList.of()) .isLatestValueEmpty(true) .build()); SweepableCellFilter filter = new SweepableCellFilter( commitTsCache, Sweeper.CONSERVATIVE, 100L); BatchOfCellsToSweep result = filter.getCellsToSweep(candidate); assertThat(result.lastCellExamined()).isEqualTo(SINGLE_CELL); assertThat(result.cells()).isEmpty(); assertThat(result.numCellTsPairsExamined()).isEqualTo(0L); }
@Test public void testTwoCandidates() { long sweepTimestampHigherThanCommitTimestamp = HIGH_COMMIT_TS + 1; CandidateCellForSweeping snd = ImmutableCandidateCellForSweeping.builder() .cell(ANOTHER_CELL) .sortedTimestamps(ImmutableList.of(HIGH_START_TS)) .isLatestValueEmpty(true) .build(); List<CandidateCellForSweeping> candidates = ImmutableList.of(twoCommittedTimestampsForSingleCell().get(0), snd); SweepableCellFilter filter = new SweepableCellFilter( commitTsCache, Sweeper.THOROUGH, sweepTimestampHigherThanCommitTimestamp); BatchOfCellsToSweep result = filter.getCellsToSweep(candidates); assertThat(result.lastCellExamined()).isEqualTo(ANOTHER_CELL); assertThat(result.cells().size()).isEqualTo(2); assertThat(result.numCellTsPairsExamined()).isEqualTo(3L); assertThat(result.cells().get(0).sortedTimestamps()).containsExactly(LOW_START_TS); assertThat(result.cells().get(1).sortedTimestamps()).containsExactly(HIGH_START_TS); }